diff --git a/src/event_loop/app_data.rs b/src/event_loop/app_data.rs index 9fb3a01..051b5f7 100644 --- a/src/event_loop/app_data.rs +++ b/src/event_loop/app_data.rs @@ -447,6 +447,7 @@ impl AppData // current buffer scale before handing the dimensions to the app. let sf = self.main.scale_factor.max( 1 ) as u32; crate::types::set_viewport_size( w * sf, h * sf ); + crate::types::set_density( sf as f32 ); self.app.on_scale_changed( sf ); self.app.on_resize( w * sf, h * sf ); // `on_resize` may flip app-state that the view depends on (apps that diff --git a/src/event_loop/handlers.rs b/src/event_loop/handlers.rs index 67d77e3..9761471 100644 --- a/src/event_loop/handlers.rs +++ b/src/event_loop/handlers.rs @@ -130,6 +130,7 @@ impl CompositorHandler for AppData if matches!( focus, super::SurfaceFocus::Main ) { crate::types::set_viewport_size( pw, ph ); + crate::types::set_density( new_factor as f32 ); self.app.on_scale_changed( new_factor as u32 ); self.app.on_resize( pw, ph ); self.dirty_caches(); diff --git a/src/event_loop/overlays_reconcile.rs b/src/event_loop/overlays_reconcile.rs index 86f717d..76bcdaa 100644 --- a/src/event_loop/overlays_reconcile.rs +++ b/src/event_loop/overlays_reconcile.rs @@ -114,9 +114,10 @@ pub( super ) fn reconcile_overlays( data: &mut AppData ) let main_vp = ( data.main.physical_width() as f32, data.main.physical_height() as f32 ); let resolve_size = move | s: &( Length, Length ) | -> ( u32, u32 ) { + let density = crate::types::density(); ( - s.0.resolve( main_vp, Length::EM_BASE_DEFAULT ).max( 0.0 ).round() as u32, - s.1.resolve( main_vp, Length::EM_BASE_DEFAULT ).max( 0.0 ).round() as u32, + s.0.resolve_physical( main_vp, Length::EM_BASE_DEFAULT, density, parent_scale ).max( 0.0 ).round() as u32, + s.1.resolve_physical( main_vp, Length::EM_BASE_DEFAULT, density, parent_scale ).max( 0.0 ).round() as u32, ) }; // `OverlaySpec::size` is physical pixels (the app's layout space); the diff --git a/src/render/mod.rs b/src/render/mod.rs index 4810fb4..6c28d46 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -318,7 +318,7 @@ impl Canvas /// a `Length::dp` override follows the canvas the widget draws on. pub fn resolve_geom( &self, l: Length ) -> f32 { - l.resolve_with_density( self.viewport_layout(), Length::EM_BASE_DEFAULT, self.density() ) + l.resolve_physical( self.viewport_layout(), Length::EM_BASE_DEFAULT, self.density(), self.dpi_scale() ) } /// Resolve an explicit [`Length`] in **font** space: against @@ -960,6 +960,23 @@ mod viewport_tests set_widget_scaling( WidgetScaling::Fluid ); } + #[ test ] + fn geom_px_fluid_cap_is_logical_on_hidpi() + { + use crate::types::{ set_widget_scaling, WidgetScaling }; + let _g = crate::TEST_GLOBALS_LOCK.lock().unwrap_or_else( |e| e.into_inner() ); + set_widget_scaling( WidgetScaling::Fluid ); + + // 1080 × 2460 phone at buffer scale 2: geometry and fonts must share + // the same surface factor (2.62 physical px per design px). + let mut c = Canvas::new( 1080, 2460 ); + c.set_dpi_scale( 2.0 ); + let geom = c.geom_px( 48.0 ); + let font = c.font_px( 48.0 ) * 2.0; + assert!( ( geom - 48.0 / 412.0 * 1080.0 ).abs() < 1e-2 ); + assert!( ( geom - font ).abs() < 1e-2 ); + } + #[ test ] fn canvas_density_overrides_process_density() { diff --git a/src/types.rs b/src/types.rs index 03fcb49..ec76a60 100644 --- a/src/types.rs +++ b/src/types.rs @@ -639,10 +639,22 @@ impl Length /// what [`crate::Canvas`]-routed resolution calls with the canvas' /// own density. pub fn resolve_with_density( &self, viewport: ( f32, f32 ), em_base: f32, density: f32 ) -> f32 + { + self.resolve_physical( viewport, em_base, density, 1.0 ) + } + + /// [`Self::resolve_with_density`] against a **physical** viewport + /// (`dpi_scale` physical px per logical px). The bounds are logical + /// px, so they are scaled to physical before they apply; otherwise a + /// `fluid` value hits its cap on any HiDPI surface whose physical + /// short side exceeds the cap, while fonts (resolved in logical space) + /// keep growing. + pub fn resolve_physical( &self, viewport: ( f32, f32 ), em_base: f32, density: f32, dpi_scale: f32 ) -> f32 { let raw = self.base.resolve( viewport, em_base, density ); - let lo = self.min_px; - let hi = self.max_px; + let k = if dpi_scale > 0.0 { dpi_scale } else { 1.0 }; + let lo = self.min_px.map( |v| v * k ); + let hi = self.max_px.map( |v| v * k ); // If both bounds present, normalise their order so swapped args // don't produce NaN out of f32::clamp. let ( lo, hi ) = match ( lo, hi ) @@ -732,16 +744,17 @@ pub fn text_scale() -> f32 static DENSITY_BITS: AtomicU32 = AtomicU32::new( 1.0_f32.to_bits() ); /// Set the process-wide pixel density used by [`Length::dp`] (the -/// [`WidgetScaling::Physical`] mode). Typically derived from the output's -/// physical DPI so `dp` sizes stay physically constant across displays. -/// Default: `1.0`. +/// [`WidgetScaling::Physical`] mode). The runtime sets it to the main +/// surface's buffer scale right before [`crate::App::on_scale_changed`], +/// so `dp( n )` is `n` logical px by default; an app that wants a +/// DPI-derived density overrides it from that callback. Default: `1.0`. pub fn set_density( d: f32 ) { DENSITY_BITS.store( d.max( 0.0 ).to_bits(), Ordering::Relaxed ); } /// Current pixel density — the factor [`Length::dp`] multiplies its design -/// pixels by. `1.0` until [`set_density`] is called. +/// pixels by. `1.0` until the runtime or [`set_density`] sets it. pub fn density() -> f32 { f32::from_bits( DENSITY_BITS.load( Ordering::Relaxed ) ) @@ -946,6 +959,20 @@ mod length_tests assert_eq!( p, l ); } + #[ test ] + fn fluid_bounds_scale_with_dpi_on_a_physical_viewport() + { + // 1080 × 2460 at scale 2: the physical short side is 2.62 × the + // reference, the cap is 1.5 × 48 logical = 144 physical, so the + // unclamped 125.8 stands. + let v = Length::fluid( 48.0 ).resolve_physical( ( 1080.0, 2460.0 ), 16.0, 1.0, 2.0 ); + assert!( ( v - 48.0 / 412.0 * 1080.0 ).abs() < 1e-3 ); + // Same surface treated as scale 1 hits the cap in physical px. + assert_eq!( Length::fluid( 48.0 ).resolve_physical( ( 1080.0, 2460.0 ), 16.0, 1.0, 1.0 ), 72.0 ); + // A zero scale falls back to unscaled bounds instead of collapsing to 0. + assert_eq!( Length::fluid( 48.0 ).resolve_physical( ( 1080.0, 2460.0 ), 16.0, 1.0, 0.0 ), 72.0 ); + } + #[ test ] fn dp_is_identity_at_default_density() {