|
| 1 | +//! Helpers for zooming the whole GUI (changing [`Context::pixels_per_point`]. |
| 2 | +use crate::*; |
| 3 | + |
| 4 | +/// The suggested keyboard shortcuts for global gui zooming. |
| 5 | +pub mod kb_shortcuts { |
| 6 | + use super::*; |
| 7 | + |
| 8 | + // Using winit on Mac the key with the Plus sign on it is reported as the Equals key |
| 9 | + // (with both English and Swedish keyboard). |
| 10 | + pub const ZOOM_IN: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Equals); |
| 11 | + pub const ZOOM_OUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Minus); |
| 12 | + pub const ZOOM_RESET: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Num0); |
| 13 | +} |
| 14 | + |
| 15 | +/// Let the user scale the GUI (change `Context::pixels_per_point`) by pressing |
| 16 | +/// Cmd+Plus, Cmd+Minus or Cmd+0, just like in a browser. |
| 17 | +/// |
| 18 | +/// When using [`eframe`](https://github.com/emilk/egui/tree/master/crates/eframe), you want to call this as: |
| 19 | +/// ```ignore |
| 20 | +/// // On web, the browser controls `pixels_per_point`. |
| 21 | +/// if !frame.is_web() { |
| 22 | +/// egui::gui_zoom::scale_ui_with_keyboard_shortcuts( |
| 23 | +/// ctx, |
| 24 | +/// frame.info().native_pixels_per_point, |
| 25 | +/// ); |
| 26 | +/// } |
| 27 | +/// ``` |
| 28 | +pub fn scale_ui_with_keyboard_shortcuts(ctx: &Context, native_pixels_per_point: Option<f32>) { |
| 29 | + if ctx.input_mut().consume_shortcut(&kb_shortcuts::ZOOM_RESET) { |
| 30 | + if let Some(native_pixels_per_point) = native_pixels_per_point { |
| 31 | + ctx.set_pixels_per_point(native_pixels_per_point); |
| 32 | + } |
| 33 | + } else { |
| 34 | + if ctx.input_mut().consume_shortcut(&kb_shortcuts::ZOOM_IN) { |
| 35 | + zoom_in(ctx); |
| 36 | + } |
| 37 | + if ctx.input_mut().consume_shortcut(&kb_shortcuts::ZOOM_OUT) { |
| 38 | + zoom_out(ctx); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +const MIN_PIXELS_PER_POINT: f32 = 0.2; |
| 44 | +const MAX_PIXELS_PER_POINT: f32 = 4.0; |
| 45 | + |
| 46 | +/// Make everything larger. |
| 47 | +pub fn zoom_in(ctx: &Context) { |
| 48 | + let mut pixels_per_point = ctx.pixels_per_point(); |
| 49 | + pixels_per_point += 0.1; |
| 50 | + pixels_per_point = pixels_per_point.clamp(MIN_PIXELS_PER_POINT, MAX_PIXELS_PER_POINT); |
| 51 | + pixels_per_point = (pixels_per_point * 10.).round() / 10.; |
| 52 | + ctx.set_pixels_per_point(pixels_per_point); |
| 53 | +} |
| 54 | + |
| 55 | +/// Make everything smaller. |
| 56 | +pub fn zoom_out(ctx: &Context) { |
| 57 | + let mut pixels_per_point = ctx.pixels_per_point(); |
| 58 | + pixels_per_point -= 0.1; |
| 59 | + pixels_per_point = pixels_per_point.clamp(MIN_PIXELS_PER_POINT, MAX_PIXELS_PER_POINT); |
| 60 | + pixels_per_point = (pixels_per_point * 10.).round() / 10.; |
| 61 | + ctx.set_pixels_per_point(pixels_per_point); |
| 62 | +} |
| 63 | + |
| 64 | +/// Show buttons for zooming the ui. |
| 65 | +pub fn zoom_menu_buttons(ui: &mut Ui, native_pixels_per_point: Option<f32>) { |
| 66 | + if ui |
| 67 | + .add_enabled( |
| 68 | + ui.ctx().pixels_per_point() < MAX_PIXELS_PER_POINT, |
| 69 | + Button::new("Zoom In").shortcut_text(ui.ctx().format_shortcut(&kb_shortcuts::ZOOM_IN)), |
| 70 | + ) |
| 71 | + .clicked() |
| 72 | + { |
| 73 | + zoom_in(ui.ctx()); |
| 74 | + ui.close_menu(); |
| 75 | + } |
| 76 | + |
| 77 | + if ui |
| 78 | + .add_enabled( |
| 79 | + ui.ctx().pixels_per_point() > MIN_PIXELS_PER_POINT, |
| 80 | + Button::new("Zoom Out") |
| 81 | + .shortcut_text(ui.ctx().format_shortcut(&kb_shortcuts::ZOOM_OUT)), |
| 82 | + ) |
| 83 | + .clicked() |
| 84 | + { |
| 85 | + zoom_out(ui.ctx()); |
| 86 | + ui.close_menu(); |
| 87 | + } |
| 88 | + |
| 89 | + if let Some(native_pixels_per_point) = native_pixels_per_point { |
| 90 | + if ui |
| 91 | + .add_enabled( |
| 92 | + ui.ctx().pixels_per_point() != native_pixels_per_point, |
| 93 | + Button::new("Reset Zoom") |
| 94 | + .shortcut_text(ui.ctx().format_shortcut(&kb_shortcuts::ZOOM_RESET)), |
| 95 | + ) |
| 96 | + .clicked() |
| 97 | + { |
| 98 | + ui.ctx().set_pixels_per_point(native_pixels_per_point); |
| 99 | + ui.close_menu(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments