From b4f05d4644f51a264d45840b08ce997ccb101fe9 Mon Sep 17 00:00:00 2001 From: Marc Espin Date: Sat, 28 Sep 2024 11:08:57 +0200 Subject: [PATCH] feat: Custom scale factor shortcuts (#931) * feat: Custom scale factor shortcuts * add feature to disable the shortcuts --- Cargo.toml | 1 + crates/freya/Cargo.toml | 1 + crates/renderer/Cargo.toml | 1 + crates/renderer/src/app.rs | 3 ++- crates/renderer/src/renderer.rs | 41 ++++++++++++++++++++++++++++++--- 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 91ce951f0..c05a7f57c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ hot-reload = ["freya/hot-reload"] custom-tokio-rt = ["freya/custom-tokio-rt"] performance-overlay = ["freya/performance-overlay"] fade-cached-incremental-areas = ["freya/fade-cached-incremental-areas"] +disable-zoom-shortcuts = ["freya/disable-zoom-shortcuts"] [patch.crates-io] # dioxus = { git = "https://github.com/DioxusLabs/dioxus", rev = "7beacdf9c76ae5412d3c2bcd55f7c5d87f486a0f" } diff --git a/crates/freya/Cargo.toml b/crates/freya/Cargo.toml index a8f3dfdc8..8797c7eda 100644 --- a/crates/freya/Cargo.toml +++ b/crates/freya/Cargo.toml @@ -26,6 +26,7 @@ mocked-engine-development = ["freya-engine/mocked-engine"] # This is just for th default = ["skia"] performance-overlay = [] fade-cached-incremental-areas = ["freya-core/fade-cached-incremental-areas"] +disable-zoom-shortcuts = ["freya-renderer/disable-zoom-shortcuts"] [dependencies] freya-devtools = { workspace = true, optional = true } diff --git a/crates/renderer/Cargo.toml b/crates/renderer/Cargo.toml index ec243f9fe..f4c11b6ec 100644 --- a/crates/renderer/Cargo.toml +++ b/crates/renderer/Cargo.toml @@ -17,6 +17,7 @@ features = ["freya-engine/mocked-engine"] [features] hot-reload = [] skia-engine = ["freya-engine/skia-engine"] +disable-zoom-shortcuts = [] [dependencies] freya-node-state = { workspace = true } diff --git a/crates/renderer/src/app.rs b/crates/renderer/src/app.rs index 26c1ffdd7..2f6899bde 100644 --- a/crates/renderer/src/app.rs +++ b/crates/renderer/src/app.rs @@ -287,6 +287,7 @@ impl Application { surface: &mut Surface, dirty_surface: &mut Surface, window: &Window, + scale_factor: f64, ) { self.plugins.send( PluginEvent::BeforeRender { @@ -303,7 +304,7 @@ impl Application { surface, dirty_surface, window.inner_size(), - window.scale_factor() as f32, + scale_factor as f32, ); self.plugins.send( diff --git a/crates/renderer/src/renderer.rs b/crates/renderer/src/renderer.rs index 6eda7db36..49fd15fa3 100644 --- a/crates/renderer/src/renderer.rs +++ b/crates/renderer/src/renderer.rs @@ -63,6 +63,7 @@ pub struct DesktopRenderer<'a, State: Clone + 'static> { pub(crate) mouse_state: ElementState, pub(crate) modifiers_state: ModifiersState, pub(crate) dropped_file_path: Option, + pub(crate) custom_scale_factor: f64, } impl<'a, State: Clone + 'static> DesktopRenderer<'a, State> { @@ -120,6 +121,7 @@ impl<'a, State: Clone + 'static> DesktopRenderer<'a, State> { mouse_state: ElementState::Released, modifiers_state: ModifiersState::default(), dropped_file_path: None, + custom_scale_factor: 0., } } @@ -135,7 +137,9 @@ impl<'a, State: Clone + 'static> DesktopRenderer<'a, State> { /// Get the current scale factor of the Window fn scale_factor(&self) -> f64 { match &self.state { - WindowState::Created(CreatedState { window, .. }) => window.scale_factor(), + WindowState::Created(CreatedState { window, .. }) => { + window.scale_factor() + self.custom_scale_factor + } _ => 0.0, } } @@ -193,8 +197,7 @@ impl<'a, State: Clone> ApplicationHandler for DesktopRenderer<'a, } EventMessage::InvalidateArea(mut area) => { let fdom = app.sdom.get(); - let sf = window.scale_factor() as f32; - area.size *= sf; + area.size *= scale_factor as f32; let mut compositor_dirty_area = fdom.compositor_dirty_area(); compositor_dirty_area.unite_or_insert(&area) } @@ -296,6 +299,7 @@ impl<'a, State: Clone> ApplicationHandler for DesktopRenderer<'a, surface, dirty_surface, window, + scale_factor, ); app.event_loop_tick(); @@ -359,6 +363,37 @@ impl<'a, State: Clone> ApplicationHandler for DesktopRenderer<'a, return; } + #[cfg(not(feature = "disable-zoom-shortcuts"))] + { + let is_control_pressed = { + if cfg!(target_os = "macos") { + self.modifiers_state.super_key() + } else { + self.modifiers_state.control_key() + } + }; + + if is_control_pressed && state == ElementState::Pressed { + let ch = logical_key.to_text(); + let render_with_new_scale_factor = if ch == Some("+") { + self.custom_scale_factor = + (self.custom_scale_factor + 0.10).clamp(-1.0, 5.0); + true + } else if ch == Some("-") { + self.custom_scale_factor = + (self.custom_scale_factor - 0.10).clamp(-1.0, 5.0); + true + } else { + false + }; + + if render_with_new_scale_factor { + app.resize(window); + window.request_redraw(); + } + } + } + let name = match state { ElementState::Pressed => EventName::KeyDown, ElementState::Released => EventName::KeyUp,