When the slint scale factor is provided at compile time, propagate it also as initial value to the slint::Window created

This commit is contained in:
Simon Hausmann 2024-08-06 16:21:16 +02:00 committed by Simon Hausmann
parent 360f192b91
commit fb6775b386
5 changed files with 47 additions and 24 deletions

View file

@ -8,6 +8,8 @@
use std::fmt::Write;
use lyon_path::geom::euclid::approxeq::ApproxEq;
/// The configuration for the C++ code generator
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Config {
@ -810,20 +812,31 @@ pub fn generate(
}),
));
let mut window_creation_code = vec![
format!("auto self = const_cast<SharedGlobals *>(this);"),
"if (!self->m_window.has_value()) {".into(),
" auto &window = self->m_window.emplace(slint::private_api::WindowAdapterRc());".into(),
];
if !compiler_config.const_scale_factor.approx_eq(&1.0) {
window_creation_code.push(format!(
"window.dispatch_scale_factor_change_event({});",
compiler_config.const_scale_factor
));
}
window_creation_code.extend([
" window.window_handle().set_component(self->root_weak);".into(),
"}".into(),
"return *self->m_window;".into(),
]);
globals_struct.members.push((
Access::Public,
Declaration::Function(Function {
name: "window".into(),
signature: "() const -> slint::Window&".into(),
statements: Some(vec![
format!("auto self = const_cast<SharedGlobals *>(this);"),
"if (!self->m_window.has_value()) {".into(),
" auto &window = self->m_window.emplace(slint::private_api::WindowAdapterRc());"
.into(),
" window.window_handle().set_component(self->root_weak);".into(),
"}".into(),
"return *self->m_window;".into(),
]),
statements: Some(window_creation_code),
..Default::default()
}),
));

View file

@ -22,6 +22,7 @@ use crate::llr::{
use crate::object_tree::Document;
use crate::CompilerConfiguration;
use itertools::Either;
use lyon_path::geom::euclid::approxeq::ApproxEq;
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use std::collections::{BTreeMap, BTreeSet};
@ -186,7 +187,7 @@ pub fn generate(doc: &Document, compiler_config: &CompilerConfiguration) -> Toke
.iter()
.filter(|glob| glob.must_generate())
.map(|glob| generate_global(glob, &llr));
let shared_globals = generate_shared_globals(&llr);
let shared_globals = generate_shared_globals(&llr, &compiler_config);
let globals_ids = llr.globals.iter().filter(|glob| glob.exported).flat_map(|glob| {
std::iter::once(ident(&glob.name)).chain(glob.aliases.iter().map(|x| ident(x)))
});
@ -312,7 +313,10 @@ fn generate_public_component(
)
}
fn generate_shared_globals(llr: &llr::CompilationUnit) -> TokenStream {
fn generate_shared_globals(
llr: &llr::CompilationUnit,
compiler_config: &CompilerConfiguration,
) -> TokenStream {
let global_names = llr
.globals
.iter()
@ -326,6 +330,15 @@ fn generate_shared_globals(llr: &llr::CompilationUnit) -> TokenStream {
.map(global_inner_name)
.collect::<Vec<_>>();
let apply_constant_scale_factor = if !compiler_config.const_scale_factor.approx_eq(&1.0) {
let factor = compiler_config.const_scale_factor as f32;
Some(
quote!(adapter.window().dispatch_event(slint::platform::WindowEvent::ScaleFactorChanged{ scale_factor: #factor });),
)
} else {
None
};
quote! {
struct SharedGlobals {
#(#global_names : ::core::pin::Pin<sp::Rc<#global_types>>,)*
@ -355,6 +368,7 @@ fn generate_shared_globals(llr: &llr::CompilationUnit) -> TokenStream {
let adapter = slint::private_unstable_api::create_window_adapter()?;
let root_rc = self.root_item_tree_weak.upgrade().unwrap();
sp::WindowInner::from_pub(adapter.window()).set_component(&root_rc);
#apply_constant_scale_factor
core::result::Result::Ok(adapter)
})
}