egui/
gui_zoom.rs

1//! Helpers for zooming the whole GUI of an app (changing [`Context::pixels_per_point`].
2//!
3use crate::{Button, Context, Key, KeyboardShortcut, Modifiers, Ui};
4
5/// The suggested keyboard shortcuts for global gui zooming.
6pub mod kb_shortcuts {
7    use super::{Key, KeyboardShortcut, Modifiers};
8
9    /// Primary keyboard shortcut for zooming in (`Cmd` + `+`).
10    pub const ZOOM_IN: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Plus);
11
12    /// Secondary keyboard shortcut for zooming in (`Cmd` + `=`).
13    ///
14    /// On an English keyboard `+` is `Shift` + `=`,
15    /// but it is annoying to have to press shift.
16    /// So most browsers also allow `Cmd` + `=` for zooming in.
17    /// We do the same.
18    pub const ZOOM_IN_SECONDARY: KeyboardShortcut =
19        KeyboardShortcut::new(Modifiers::COMMAND, Key::Equals);
20
21    /// Keyboard shortcut for zooming in (`Cmd` + `-`).
22    pub const ZOOM_OUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Minus);
23
24    /// Keyboard shortcut for resetting zoom in (`Cmd` + `0`).
25    pub const ZOOM_RESET: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Num0);
26}
27
28/// Let the user scale the GUI (change [`Context::zoom_factor`]) by pressing
29/// Cmd+Plus, Cmd+Minus or Cmd+0, just like in a browser.
30///
31/// By default, [`crate::Context`] calls this function at the end of each frame,
32/// controllable by [`crate::Options::zoom_with_keyboard`].
33pub(crate) fn zoom_with_keyboard(ctx: &Context) {
34    if ctx.input_mut(|i| i.consume_shortcut(&kb_shortcuts::ZOOM_RESET)) {
35        ctx.set_zoom_factor(1.0);
36    } else {
37        if ctx.input_mut(|i| i.consume_shortcut(&kb_shortcuts::ZOOM_IN))
38            || ctx.input_mut(|i| i.consume_shortcut(&kb_shortcuts::ZOOM_IN_SECONDARY))
39        {
40            zoom_in(ctx);
41        }
42        if ctx.input_mut(|i| i.consume_shortcut(&kb_shortcuts::ZOOM_OUT)) {
43            zoom_out(ctx);
44        }
45    }
46}
47
48const MIN_ZOOM_FACTOR: f32 = 0.2;
49const MAX_ZOOM_FACTOR: f32 = 5.0;
50
51/// Make everything larger by increasing [`Context::zoom_factor`].
52pub fn zoom_in(ctx: &Context) {
53    let mut zoom_factor = ctx.zoom_factor();
54    zoom_factor += 0.1;
55    zoom_factor = zoom_factor.clamp(MIN_ZOOM_FACTOR, MAX_ZOOM_FACTOR);
56    zoom_factor = (zoom_factor * 10.).round() / 10.;
57    ctx.set_zoom_factor(zoom_factor);
58}
59
60/// Make everything smaller by decreasing [`Context::zoom_factor`].
61pub fn zoom_out(ctx: &Context) {
62    let mut zoom_factor = ctx.zoom_factor();
63    zoom_factor -= 0.1;
64    zoom_factor = zoom_factor.clamp(MIN_ZOOM_FACTOR, MAX_ZOOM_FACTOR);
65    zoom_factor = (zoom_factor * 10.).round() / 10.;
66    ctx.set_zoom_factor(zoom_factor);
67}
68
69/// Show buttons for zooming the ui.
70///
71/// This is meant to be called from within a menu (See [`Ui::menu_button`]).
72pub fn zoom_menu_buttons(ui: &mut Ui) {
73    fn button(ctx: &Context, text: &str, shortcut: &KeyboardShortcut) -> Button<'static> {
74        let btn = Button::new(text);
75        let zoom_with_keyboard = ctx.options(|o| o.zoom_with_keyboard);
76        if zoom_with_keyboard {
77            btn.shortcut_text(ctx.format_shortcut(shortcut))
78        } else {
79            btn
80        }
81    }
82
83    if ui
84        .add_enabled(
85            ui.ctx().zoom_factor() < MAX_ZOOM_FACTOR,
86            button(ui.ctx(), "Zoom In", &kb_shortcuts::ZOOM_IN),
87        )
88        .clicked()
89    {
90        zoom_in(ui.ctx());
91        ui.close_menu();
92    }
93
94    if ui
95        .add_enabled(
96            ui.ctx().zoom_factor() > MIN_ZOOM_FACTOR,
97            button(ui.ctx(), "Zoom Out", &kb_shortcuts::ZOOM_OUT),
98        )
99        .clicked()
100    {
101        zoom_out(ui.ctx());
102        ui.close_menu();
103    }
104
105    if ui
106        .add_enabled(
107            ui.ctx().zoom_factor() != 1.0,
108            button(ui.ctx(), "Reset Zoom", &kb_shortcuts::ZOOM_RESET),
109        )
110        .clicked()
111    {
112        ui.ctx().set_zoom_factor(1.0);
113        ui.close_menu();
114    }
115}