Skip to content

Commit 18e4f88

Browse files
authored
feat: Expose scale factor (#607)
* feat: Reactive scale factor * feat: Expose scale factor
1 parent d0b777f commit 18e4f88

File tree

10 files changed

+16
-7
lines changed

10 files changed

+16
-7
lines changed

crates/components/src/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn Graph(props: GraphProps) -> Element {
4343
}));
4444

4545
let canvas = use_canvas(&props, |state| {
46-
Box::new(move |canvas, font_collection, region| {
46+
Box::new(move |canvas, font_collection, region, _| {
4747
canvas.translate((region.min_x(), region.min_y()));
4848

4949
let mut paragraph_style = ParagraphStyle::default();

crates/core/src/platform_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub struct NativePlatformState {
99
pub preferred_theme: PreferredTheme,
1010
pub navigation_mode: NavigationMode,
1111
pub information: PlatformInformation,
12+
pub scale_factor: f32,
1213
}
1314

1415
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]

crates/hooks/src/use_canvas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl UseCanvas {
3535
/// # use freya::prelude::*;
3636
/// fn app() -> Element {
3737
/// let canvas = use_canvas((), |_| {
38-
/// Box::new(|canvas, font_collection, area| {
38+
/// Box::new(|canvas, font_collection, area, scale_factor| {
3939
/// // Draw using the canvas !
4040
/// })
4141
/// });

crates/renderer/src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl Application {
8181
preferred_theme: window.theme().map(|theme| theme.into()).unwrap_or_default(),
8282
navigation_mode: NavigationMode::default(),
8383
information: PlatformInformation::from_winit(window),
84+
scale_factor: window.scale_factor() as f32,
8485
});
8586

8687
plugins.send(PluginEvent::WindowCreated(window));

crates/renderer/src/elements/rect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,6 @@ pub fn render_rect(
180180
let references = node_ref.get::<References>().unwrap();
181181

182182
if let Some(canvas_ref) = &references.canvas_ref {
183-
(canvas_ref.runner)(canvas, font_collection, area);
183+
(canvas_ref.runner)(canvas, font_collection, area, scale_factor);
184184
}
185185
}

crates/renderer/src/renderer.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ impl<'a, State: Clone> ApplicationHandler<EventMessage> for DesktopRenderer<'a,
397397
});
398398
}
399399
WindowEvent::RedrawRequested => {
400+
app.platform_sender.send_if_modified(|state| {
401+
let scale_factor_is_different = state.scale_factor == scale_factor;
402+
state.scale_factor = scale_factor;
403+
scale_factor_is_different
404+
});
405+
400406
if app.measure_layout_on_next_render {
401407
app.process_layout(window.inner_size(), scale_factor);
402408
app.process_accessibility(window);

crates/state/src/custom_attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Display for NodeReference {
4747
}
4848
}
4949

50-
pub type CanvasRunner = dyn Fn(&Canvas, &mut FontCollection, Area) + Sync + Send + 'static;
50+
pub type CanvasRunner = dyn Fn(&Canvas, &mut FontCollection, Area, f32) + Sync + Send + 'static;
5151

5252
/// Canvas Reference
5353
#[derive(Clone)]

crates/testing/src/launch.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use tokio::sync::mpsc::unbounded_channel;
1010
use tokio::sync::{broadcast, watch};
1111
use winit::window::CursorIcon;
1212

13-
use crate::config::TestingConfig;
1413
use crate::test_handler::TestingHandler;
1514
use crate::test_utils::TestUtils;
15+
use crate::{config::TestingConfig, SCALE_FACTOR};
1616

1717
/// Run a Component in a headless testing environment.
1818
///
@@ -34,6 +34,7 @@ pub fn launch_test_with_config(root: AppComponent, config: TestingConfig) -> Tes
3434
preferred_theme: PreferredTheme::default(),
3535
navigation_mode: NavigationMode::default(),
3636
information: PlatformInformation::new(config.size, false, false, false),
37+
scale_factor: SCALE_FACTOR as f32,
3738
});
3839
let mut font_collection = FontCollection::new();
3940
let font_mgr = FontMgr::default();

examples/render_canvas.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn app() -> Element {
1919
});
2020

2121
let canvas = use_canvas(&*state.read(), |state| {
22-
Box::new(move |canvas, font_collection, region| {
22+
Box::new(move |canvas, font_collection, region, _| {
2323
canvas.translate((region.min_x(), region.min_y()));
2424

2525
let mut text_paint = Paint::default();

examples/shader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn app() -> Element {
5151
let shader_wrapper = Arc::new(Mutex::new(ShaderWrapper(shader)));
5252
let instant = Instant::now();
5353

54-
Box::new(move |canvas, _, region| {
54+
Box::new(move |canvas, _, region, _| {
5555
let shader = shader_wrapper.lock().unwrap();
5656

5757
let mut builder = UniformsBuilder::default();

0 commit comments

Comments
 (0)