-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper module
egui::gui_zoom
for zooming an app
- Loading branch information
Showing
5 changed files
with
115 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//! Helpers for zooming the whole GUI (changing [`Context::pixels_per_point`]. | ||
use crate::*; | ||
|
||
/// The suggested keyboard shortcuts for global gui zooming. | ||
pub mod kb_shortcuts { | ||
use super::*; | ||
|
||
// Using winit on Mac the key with the Plus sign on it is reported as the Equals key | ||
// (with both English and Swedish keyboard). | ||
pub const ZOOM_IN: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Equals); | ||
pub const ZOOM_OUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Minus); | ||
pub const ZOOM_RESET: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::Num0); | ||
} | ||
|
||
/// Let the user scale the GUI (change `Context::pixels_per_point`) by pressing | ||
/// Cmd+Plus, Cmd+Minus or Cmd+0, just like in a browser. | ||
/// | ||
/// When using [`eframe`](https://github.com/emilk/egui/tree/master/crates/eframe), you want to call this as: | ||
/// ```ignore | ||
/// // On web, the browser controls `pixels_per_point`. | ||
/// if !frame.is_web() { | ||
/// egui::gui_zoom::scale_ui_with_keyboard_shortcuts( | ||
/// ctx, | ||
/// frame.info().native_pixels_per_point, | ||
/// ); | ||
/// } | ||
/// ``` | ||
pub fn scale_ui_with_keyboard_shortcuts(ctx: &Context, native_pixels_per_point: Option<f32>) { | ||
if ctx.input_mut().consume_shortcut(&kb_shortcuts::ZOOM_RESET) { | ||
if let Some(native_pixels_per_point) = native_pixels_per_point { | ||
ctx.set_pixels_per_point(native_pixels_per_point); | ||
} | ||
} else { | ||
if ctx.input_mut().consume_shortcut(&kb_shortcuts::ZOOM_IN) { | ||
zoom_in(ctx); | ||
} | ||
if ctx.input_mut().consume_shortcut(&kb_shortcuts::ZOOM_OUT) { | ||
zoom_out(ctx); | ||
} | ||
} | ||
} | ||
|
||
const MIN_PIXELS_PER_POINT: f32 = 0.2; | ||
const MAX_PIXELS_PER_POINT: f32 = 4.0; | ||
|
||
/// Make everything larger. | ||
pub fn zoom_in(ctx: &Context) { | ||
let mut pixels_per_point = ctx.pixels_per_point(); | ||
pixels_per_point += 0.1; | ||
pixels_per_point = pixels_per_point.clamp(MIN_PIXELS_PER_POINT, MAX_PIXELS_PER_POINT); | ||
pixels_per_point = (pixels_per_point * 10.).round() / 10.; | ||
ctx.set_pixels_per_point(pixels_per_point); | ||
} | ||
|
||
/// Make everything smaller. | ||
pub fn zoom_out(ctx: &Context) { | ||
let mut pixels_per_point = ctx.pixels_per_point(); | ||
pixels_per_point -= 0.1; | ||
pixels_per_point = pixels_per_point.clamp(MIN_PIXELS_PER_POINT, MAX_PIXELS_PER_POINT); | ||
pixels_per_point = (pixels_per_point * 10.).round() / 10.; | ||
ctx.set_pixels_per_point(pixels_per_point); | ||
} | ||
|
||
/// Show buttons for zooming the ui. | ||
pub fn zoom_menu_buttons(ui: &mut Ui, native_pixels_per_point: Option<f32>) { | ||
if ui | ||
.add_enabled( | ||
ui.ctx().pixels_per_point() < MAX_PIXELS_PER_POINT, | ||
Button::new("Zoom In").shortcut_text(ui.ctx().format_shortcut(&kb_shortcuts::ZOOM_IN)), | ||
) | ||
.clicked() | ||
{ | ||
zoom_in(ui.ctx()); | ||
ui.close_menu(); | ||
} | ||
|
||
if ui | ||
.add_enabled( | ||
ui.ctx().pixels_per_point() > MIN_PIXELS_PER_POINT, | ||
Button::new("Zoom Out") | ||
.shortcut_text(ui.ctx().format_shortcut(&kb_shortcuts::ZOOM_OUT)), | ||
) | ||
.clicked() | ||
{ | ||
zoom_out(ui.ctx()); | ||
ui.close_menu(); | ||
} | ||
|
||
if let Some(native_pixels_per_point) = native_pixels_per_point { | ||
if ui | ||
.add_enabled( | ||
ui.ctx().pixels_per_point() != native_pixels_per_point, | ||
Button::new("Reset Zoom") | ||
.shortcut_text(ui.ctx().format_shortcut(&kb_shortcuts::ZOOM_RESET)), | ||
) | ||
.clicked() | ||
{ | ||
ui.ctx().set_pixels_per_point(native_pixels_per_point); | ||
ui.close_menu(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters