Skip to content

Commit

Permalink
feat: Custom scale factor shortcuts (#931)
Browse files Browse the repository at this point in the history
* feat: Custom scale factor shortcuts

* add feature to disable the shortcuts
  • Loading branch information
marc2332 authored Sep 28, 2024
1 parent 315a056 commit b4f05d4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions crates/freya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions crates/renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion crates/renderer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ impl Application {
surface: &mut Surface,
dirty_surface: &mut Surface,
window: &Window,
scale_factor: f64,
) {
self.plugins.send(
PluginEvent::BeforeRender {
Expand All @@ -303,7 +304,7 @@ impl Application {
surface,
dirty_surface,
window.inner_size(),
window.scale_factor() as f32,
scale_factor as f32,
);

self.plugins.send(
Expand Down
41 changes: 38 additions & 3 deletions crates/renderer/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
pub(crate) custom_scale_factor: f64,
}

impl<'a, State: Clone + 'static> DesktopRenderer<'a, State> {
Expand Down Expand Up @@ -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.,
}
}

Expand All @@ -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,
}
}
Expand Down Expand Up @@ -193,8 +197,7 @@ impl<'a, State: Clone> ApplicationHandler<EventMessage> 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)
}
Expand Down Expand Up @@ -296,6 +299,7 @@ impl<'a, State: Clone> ApplicationHandler<EventMessage> for DesktopRenderer<'a,
surface,
dirty_surface,
window,
scale_factor,
);

app.event_loop_tick();
Expand Down Expand Up @@ -359,6 +363,37 @@ impl<'a, State: Clone> ApplicationHandler<EventMessage> 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,
Expand Down

0 comments on commit b4f05d4

Please sign in to comment.