Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Keep zoom between 50% and 200% (#908)

Sanket Dofe30ebeb8a24a5
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 03f6f926..a64510e4 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -622,6 +622,8 @@ impl CraftApp {
pub fn setup_context(ctx: &egui::Context) {
theme::install_fonts(ctx);
egui_extras::install_image_loaders(ctx);
+ // The zoom keys are ours (`shortcuts`), so zoom stays inside ZOOM_RANGE.
+ ctx.options_mut(|o| o.zoom_with_keyboard = false);
}
/// Gives the macOS app its menu bar (nothing elsewhere). Call once the window is open.
@@ -1878,6 +1880,24 @@ impl CraftApp {
if palette {
palette::toggle(self);
}
+ // Cmd/Ctrl + / − / 0. A zoom saved from outside the range comes back inside it.
+ let zoom = ctx.zoom_factor();
+ let asked = ctx.input_mut(|i| {
+ use egui::gui_zoom::kb_shortcuts::{ZOOM_IN, ZOOM_IN_SECONDARY, ZOOM_OUT, ZOOM_RESET};
+ if i.consume_shortcut(&ZOOM_RESET) {
+ 1.0
+ } else if i.consume_shortcut(&ZOOM_IN) || i.consume_shortcut(&ZOOM_IN_SECONDARY) {
+ zoom + 0.1
+ } else if i.consume_shortcut(&ZOOM_OUT) {
+ zoom - 0.1
+ } else {
+ zoom
+ }
+ });
+ let next = bounded_zoom(asked);
+ if next != zoom {
+ ctx.set_zoom_factor(next);
+ }
if esc {
if self.state.palette.open {
self.state.palette.open = false;
@@ -1893,6 +1913,18 @@ impl CraftApp {
}
}
+/// How far the app zooms (Cmd/Ctrl + / −). Below half size its text is unreadable; above double,
+/// the smallest window has room for almost nothing. egui's own limits are 20% and 500%.
+pub const ZOOM_RANGE: std::ops::RangeInclusive<f32> = 0.5..=2.0;
+
+/// `zoom` on a 10% step, inside [`ZOOM_RANGE`].
+pub fn bounded_zoom(zoom: f32) -> f32 {
+ if !zoom.is_finite() {
+ return 1.0;
+ }
+ ((zoom * 10.0).round() / 10.0).clamp(*ZOOM_RANGE.start(), *ZOOM_RANGE.end())
+}
+
/// The 256 px app icon.
pub static APP_ICON_PNG: &[u8] = include_bytes!("../../../assets/app-icon/hicolor/256x256/apps/ai.storyteller.craft-anything.png");
/// The app icon's master SVG, drawn at the exact pixel size wherever the app shows its icon.
diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs
index 038f428a..cc9e485d 100644
--- a/crates/ui-egui/tests/smoke.rs
+++ b/crates/ui-egui/tests/smoke.rs
@@ -1406,6 +1406,30 @@ fn claude_codes_account_folder_is_set_in_settings() {
assert!(wait(&mut h, |a| a.backends.iter().any(|b| b.name == "Claude Code 2" && b.model == "claude-code")));
}
+/// Zoom keys stop at 50% and 200%, Cmd/Ctrl 0 goes back to 100%, and a zoom saved outside the
+/// range comes back inside it.
+#[test]
+fn zoom_stays_between_half_and_double() {
+ fn press(h: &mut Harness<'static, CraftApp>, key: egui::Key, times: usize) {
+ for _ in 0..times {
+ h.key_press_modifiers(egui::Modifiers::COMMAND, key);
+ h.run_steps(2);
+ }
+ }
+ let mut h = harness(|| demo::app(demo::engine(Mode::Experienced, true)));
+ h.run_steps(2);
+ press(&mut h, egui::Key::Minus, 12);
+ assert_eq!(h.ctx.zoom_factor(), 0.5);
+ press(&mut h, egui::Key::Equals, 20);
+ assert_eq!(h.ctx.zoom_factor(), 2.0);
+ press(&mut h, egui::Key::Num0, 1);
+ assert_eq!(h.ctx.zoom_factor(), 1.0);
+ h.ctx.set_zoom_factor(0.2);
+ h.run_steps(3);
+ assert_eq!(h.ctx.zoom_factor(), 0.5);
+ assert_eq!(craft_anything_ui_egui::bounded_zoom(f32::NAN), 1.0);
+}
+
#[test]
fn settings_window_tabs_draw() {
let mut h = harness(|| {
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index ab95581e..262c9196 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -713,6 +713,9 @@ Gaps found while using the app for real projects.
itself once the process it serves is gone: the Windows loop checks that process every 30 s,
and macOS uses `caffeinate -w`. Live on Windows: a CLI build killed outright, its helper
ended 30 s later. Not run on a Mac
+- [x] Zoom (Cmd/Ctrl + / − / 0) between 50% and 200%: egui's own limits are 20% and 500%, where
+ text is unreadable or the window has room for almost nothing
+ — claimed: sanketdofe, branch feature/r2.9-zoom-bounds
- [ ] Find why a site capture lost its browser ("the WebSocket is closed", 419 ms into
`web_site_capture` for a second page, seen once; the same capture passed 41 s earlier)
- [x] Delete a private browser's temporary profile when it closes or fails to start (#810) — PR #822