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)
})
}

View file

@ -117,7 +117,8 @@ pub struct CompilerConfiguration {
pub inline_all_elements: bool,
/// Compile time scale factor to apply to embedded resources such as images and glyphs.
pub scale_factor: f64,
/// If != 1.0 then the scale factor will be set on the `slint::Window`.
pub const_scale_factor: f64,
/// expose the accessible role and properties
pub accessibility: bool,
@ -176,7 +177,7 @@ impl CompilerConfiguration {
Err(_) => output_format == OutputFormat::Interpreter,
};
let scale_factor = std::env::var("SLINT_SCALE_FACTOR")
let const_scale_factor = std::env::var("SLINT_SCALE_FACTOR")
.ok()
.and_then(|x| x.parse::<f64>().ok())
.filter(|f| *f > 0.)
@ -206,7 +207,7 @@ impl CompilerConfiguration {
open_import_fallback: None,
resource_url_mapper: None,
inline_all_elements,
scale_factor,
const_scale_factor,
accessibility: true,
enable_experimental,
translation_domain: None,

View file

@ -219,7 +219,7 @@ pub async fn run_passes(
embed_images::embed_images(
doc,
type_loader.compiler_config.embed_resources,
type_loader.compiler_config.scale_factor,
type_loader.compiler_config.const_scale_factor,
&type_loader.compiler_config.resource_url_mapper,
diag,
)
@ -232,11 +232,11 @@ pub async fn run_passes(
// Include at least the default font sizes used in the MCU backend
let mut font_pixel_sizes =
vec![(12. * type_loader.compiler_config.scale_factor) as i16];
vec![(12. * type_loader.compiler_config.const_scale_factor) as i16];
doc.visit_all_used_components(|component| {
embed_glyphs::collect_font_sizes_used(
component,
type_loader.compiler_config.scale_factor,
type_loader.compiler_config.const_scale_factor,
&mut font_pixel_sizes,
);
embed_glyphs::scan_string_literals(component, &mut characters_seen);
@ -245,7 +245,7 @@ pub async fn run_passes(
embed_glyphs::embed_glyphs(
doc,
&type_loader.compiler_config,
type_loader.compiler_config.scale_factor,
type_loader.compiler_config.const_scale_factor,
font_pixel_sizes,
characters_seen,
std::iter::once(&*doc).chain(type_loader.all_documents()),