Skip to content

Commit a0b3f11

Browse files
authored
Add helpers for zooming an app using Ctrl+Plus and Ctrl+Minus (#2239)
* Using tracing-subscriber in hello_world example * Add Key::Plus/Minus/Equals * Warn if failing to guess OS from User-Agent * Remove jitter when using Context::set_pixels_per_point * Demo app: zoom in/out using ⌘+ and ⌘- * Demo app: make backend panel GUI scale slider better * Optimize debug builds a bit * typo * Update changelog * Add helper module `egui::gui_zoom` for zooming an app * Better names, and update changelog * Combine Plus and Equals keys * Last fix * Fix docs
1 parent 25718f2 commit a0b3f11

File tree

15 files changed

+295
-117
lines changed

15 files changed

+295
-117
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG
1313
* Added `Button::shortcut_text` for showing keyboard shortcuts in menu buttons ([#2202](https://github.com/emilk/egui/pull/2202)).
1414
* Added `egui::KeyboardShortcut` for showing keyboard shortcuts in menu buttons ([#2202](https://github.com/emilk/egui/pull/2202)).
1515
* Texture loading now takes a `TexureOptions` with minification and magnification filters ([#2224](https://github.com/emilk/egui/pull/2224)).
16+
* Added `Key::Minus` and `Key::Equals` ([#2239](https://github.com/emilk/egui/pull/2239)).
17+
* Added `egui::gui_zoom` module with helpers for scaling the whole GUI of an app ([#2239](https://github.com/emilk/egui/pull/2239)).
1618

1719
### Fixed 🐛
1820
* ⚠️ BREAKING: Fix text being too small ([#2069](https://github.com/emilk/egui/pull/2069)).
1921
* Improved text rendering ([#2071](https://github.com/emilk/egui/pull/2071)).
22+
* Less jitter when calling `Context::set_pixels_per_point` ([#2239](https://github.com/emilk/egui/pull/2239)).
2023

2124

2225
## 0.19.0 - 2022-08-20

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ members = [
1515
"examples/*",
1616
]
1717

18-
[profile.dev]
19-
split-debuginfo = "unpacked" # faster debug builds on mac
20-
2118
[profile.release]
2219
# lto = true # VERY slightly smaller wasm
2320
# opt-level = 's' # 10-20% smaller wasm compared to `opt-level = 3`
@@ -26,3 +23,12 @@ opt-level = 2 # fast and small wasm, basically same as `opt-level = 's'`
2623
# opt-level = 3 # unecessarily large wasm for no performance gain
2724

2825
# debug = true # include debug symbols, useful when profiling wasm
26+
27+
28+
[profile.dev]
29+
split-debuginfo = "unpacked" # faster debug builds on mac
30+
opt-level = 1 # Make debug builds run faster
31+
32+
# Optimize all dependencies even in debug builds (does not affect workspace packages):
33+
[profile.dev.package."*"]
34+
opt-level = 2

crates/eframe/src/web/input.rs

+81-76
Original file line numberDiff line numberDiff line change
@@ -113,83 +113,88 @@ pub fn should_ignore_key(key: &str) -> bool {
113113
/// Web sends all all keys as strings, so it is up to us to figure out if it is
114114
/// a real text input or the name of a key.
115115
pub fn translate_key(key: &str) -> Option<egui::Key> {
116+
use egui::Key;
117+
116118
match key {
117-
"ArrowDown" => Some(egui::Key::ArrowDown),
118-
"ArrowLeft" => Some(egui::Key::ArrowLeft),
119-
"ArrowRight" => Some(egui::Key::ArrowRight),
120-
"ArrowUp" => Some(egui::Key::ArrowUp),
121-
122-
"Esc" | "Escape" => Some(egui::Key::Escape),
123-
"Tab" => Some(egui::Key::Tab),
124-
"Backspace" => Some(egui::Key::Backspace),
125-
"Enter" => Some(egui::Key::Enter),
126-
"Space" | " " => Some(egui::Key::Space),
127-
128-
"Help" | "Insert" => Some(egui::Key::Insert),
129-
"Delete" => Some(egui::Key::Delete),
130-
"Home" => Some(egui::Key::Home),
131-
"End" => Some(egui::Key::End),
132-
"PageUp" => Some(egui::Key::PageUp),
133-
"PageDown" => Some(egui::Key::PageDown),
134-
135-
"0" => Some(egui::Key::Num0),
136-
"1" => Some(egui::Key::Num1),
137-
"2" => Some(egui::Key::Num2),
138-
"3" => Some(egui::Key::Num3),
139-
"4" => Some(egui::Key::Num4),
140-
"5" => Some(egui::Key::Num5),
141-
"6" => Some(egui::Key::Num6),
142-
"7" => Some(egui::Key::Num7),
143-
"8" => Some(egui::Key::Num8),
144-
"9" => Some(egui::Key::Num9),
145-
146-
"a" | "A" => Some(egui::Key::A),
147-
"b" | "B" => Some(egui::Key::B),
148-
"c" | "C" => Some(egui::Key::C),
149-
"d" | "D" => Some(egui::Key::D),
150-
"e" | "E" => Some(egui::Key::E),
151-
"f" | "F" => Some(egui::Key::F),
152-
"g" | "G" => Some(egui::Key::G),
153-
"h" | "H" => Some(egui::Key::H),
154-
"i" | "I" => Some(egui::Key::I),
155-
"j" | "J" => Some(egui::Key::J),
156-
"k" | "K" => Some(egui::Key::K),
157-
"l" | "L" => Some(egui::Key::L),
158-
"m" | "M" => Some(egui::Key::M),
159-
"n" | "N" => Some(egui::Key::N),
160-
"o" | "O" => Some(egui::Key::O),
161-
"p" | "P" => Some(egui::Key::P),
162-
"q" | "Q" => Some(egui::Key::Q),
163-
"r" | "R" => Some(egui::Key::R),
164-
"s" | "S" => Some(egui::Key::S),
165-
"t" | "T" => Some(egui::Key::T),
166-
"u" | "U" => Some(egui::Key::U),
167-
"v" | "V" => Some(egui::Key::V),
168-
"w" | "W" => Some(egui::Key::W),
169-
"x" | "X" => Some(egui::Key::X),
170-
"y" | "Y" => Some(egui::Key::Y),
171-
"z" | "Z" => Some(egui::Key::Z),
172-
173-
"F1" => Some(egui::Key::F1),
174-
"F2" => Some(egui::Key::F2),
175-
"F3" => Some(egui::Key::F3),
176-
"F4" => Some(egui::Key::F4),
177-
"F5" => Some(egui::Key::F5),
178-
"F6" => Some(egui::Key::F6),
179-
"F7" => Some(egui::Key::F7),
180-
"F8" => Some(egui::Key::F8),
181-
"F9" => Some(egui::Key::F9),
182-
"F10" => Some(egui::Key::F10),
183-
"F11" => Some(egui::Key::F11),
184-
"F12" => Some(egui::Key::F12),
185-
"F13" => Some(egui::Key::F13),
186-
"F14" => Some(egui::Key::F14),
187-
"F15" => Some(egui::Key::F15),
188-
"F16" => Some(egui::Key::F16),
189-
"F17" => Some(egui::Key::F17),
190-
"F18" => Some(egui::Key::F18),
191-
"F19" => Some(egui::Key::F19),
192-
"F20" => Some(egui::Key::F20),
119+
"ArrowDown" => Some(Key::ArrowDown),
120+
"ArrowLeft" => Some(Key::ArrowLeft),
121+
"ArrowRight" => Some(Key::ArrowRight),
122+
"ArrowUp" => Some(Key::ArrowUp),
123+
124+
"Esc" | "Escape" => Some(Key::Escape),
125+
"Tab" => Some(Key::Tab),
126+
"Backspace" => Some(Key::Backspace),
127+
"Enter" => Some(Key::Enter),
128+
"Space" | " " => Some(Key::Space),
129+
130+
"Help" | "Insert" => Some(Key::Insert),
131+
"Delete" => Some(Key::Delete),
132+
"Home" => Some(Key::Home),
133+
"End" => Some(Key::End),
134+
"PageUp" => Some(Key::PageUp),
135+
"PageDown" => Some(Key::PageDown),
136+
137+
"-" => Some(Key::Minus),
138+
"+" | "=" => Some(Key::PlusEquals),
139+
140+
"0" => Some(Key::Num0),
141+
"1" => Some(Key::Num1),
142+
"2" => Some(Key::Num2),
143+
"3" => Some(Key::Num3),
144+
"4" => Some(Key::Num4),
145+
"5" => Some(Key::Num5),
146+
"6" => Some(Key::Num6),
147+
"7" => Some(Key::Num7),
148+
"8" => Some(Key::Num8),
149+
"9" => Some(Key::Num9),
150+
151+
"a" | "A" => Some(Key::A),
152+
"b" | "B" => Some(Key::B),
153+
"c" | "C" => Some(Key::C),
154+
"d" | "D" => Some(Key::D),
155+
"e" | "E" => Some(Key::E),
156+
"f" | "F" => Some(Key::F),
157+
"g" | "G" => Some(Key::G),
158+
"h" | "H" => Some(Key::H),
159+
"i" | "I" => Some(Key::I),
160+
"j" | "J" => Some(Key::J),
161+
"k" | "K" => Some(Key::K),
162+
"l" | "L" => Some(Key::L),
163+
"m" | "M" => Some(Key::M),
164+
"n" | "N" => Some(Key::N),
165+
"o" | "O" => Some(Key::O),
166+
"p" | "P" => Some(Key::P),
167+
"q" | "Q" => Some(Key::Q),
168+
"r" | "R" => Some(Key::R),
169+
"s" | "S" => Some(Key::S),
170+
"t" | "T" => Some(Key::T),
171+
"u" | "U" => Some(Key::U),
172+
"v" | "V" => Some(Key::V),
173+
"w" | "W" => Some(Key::W),
174+
"x" | "X" => Some(Key::X),
175+
"y" | "Y" => Some(Key::Y),
176+
"z" | "Z" => Some(Key::Z),
177+
178+
"F1" => Some(Key::F1),
179+
"F2" => Some(Key::F2),
180+
"F3" => Some(Key::F3),
181+
"F4" => Some(Key::F4),
182+
"F5" => Some(Key::F5),
183+
"F6" => Some(Key::F6),
184+
"F7" => Some(Key::F7),
185+
"F8" => Some(Key::F8),
186+
"F9" => Some(Key::F9),
187+
"F10" => Some(Key::F10),
188+
"F11" => Some(Key::F11),
189+
"F12" => Some(Key::F12),
190+
"F13" => Some(Key::F13),
191+
"F14" => Some(Key::F14),
192+
"F15" => Some(Key::F15),
193+
"F16" => Some(Key::F16),
194+
"F17" => Some(Key::F17),
195+
"F18" => Some(Key::F18),
196+
"F19" => Some(Key::F19),
197+
"F20" => Some(Key::F20),
193198

194199
_ => None,
195200
}

crates/egui-winit/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,11 @@ fn translate_virtual_key_code(key: winit::event::VirtualKeyCode) -> Option<egui:
710710
VirtualKeyCode::PageUp => Key::PageUp,
711711
VirtualKeyCode::PageDown => Key::PageDown,
712712

713+
VirtualKeyCode::Minus => Key::Minus,
714+
// Using Mac the key with the Plus sign on it is reported as the Equals key
715+
// (with both English and Swedish keyboard).
716+
VirtualKeyCode::Equals => Key::PlusEquals,
717+
713718
VirtualKeyCode::Key0 | VirtualKeyCode::Numpad0 => Key::Num0,
714719
VirtualKeyCode::Key1 | VirtualKeyCode::Numpad1 => Key::Num1,
715720
VirtualKeyCode::Key2 | VirtualKeyCode::Numpad2 => Key::Num2,

crates/egui/src/context.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,25 @@ struct ContextImpl {
6565
}
6666

6767
impl ContextImpl {
68-
fn begin_frame_mut(&mut self, new_raw_input: RawInput) {
68+
fn begin_frame_mut(&mut self, mut new_raw_input: RawInput) {
6969
self.has_requested_repaint_this_frame = false; // allow new calls during the frame
7070

71+
if let Some(new_pixels_per_point) = self.memory.new_pixels_per_point.take() {
72+
new_raw_input.pixels_per_point = Some(new_pixels_per_point);
73+
74+
// This is a bit hacky, but is required to avoid jitter:
75+
let ratio = self.input.pixels_per_point / new_pixels_per_point;
76+
let mut rect = self.input.screen_rect;
77+
rect.min = (ratio * rect.min.to_vec2()).to_pos2();
78+
rect.max = (ratio * rect.max.to_vec2()).to_pos2();
79+
new_raw_input.screen_rect = Some(rect);
80+
}
81+
7182
self.memory.begin_frame(&self.input, &new_raw_input);
7283

7384
self.input = std::mem::take(&mut self.input)
7485
.begin_frame(new_raw_input, self.requested_repaint_last_frame);
7586

76-
if let Some(new_pixels_per_point) = self.memory.new_pixels_per_point.take() {
77-
self.input.pixels_per_point = new_pixels_per_point;
78-
}
79-
8087
self.frame_state.begin_frame(&self.input);
8188

8289
self.update_fonts_mut();

crates/egui/src/data/input.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use crate::emath::*;
1313
#[derive(Clone, Debug, PartialEq)]
1414
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
1515
pub struct RawInput {
16-
/// Position and size of the area that egui should use.
16+
/// Position and size of the area that egui should use, in points.
1717
/// Usually you would set this to
1818
///
19-
/// `Some(Rect::from_pos_size(Default::default(), screen_size))`.
19+
/// `Some(Rect::from_pos_size(Default::default(), screen_size_in_points))`.
2020
///
2121
/// but you could also constrain egui to some smaller portion of your window if you like.
2222
///
@@ -516,13 +516,13 @@ impl ModifierNames<'static> {
516516
concat: "",
517517
};
518518

519-
/// Alt, Ctrl, Shift, Command
519+
/// Alt, Ctrl, Shift, Cmd
520520
pub const NAMES: Self = Self {
521521
is_short: false,
522522
alt: "Alt",
523523
ctrl: "Ctrl",
524524
shift: "Shift",
525-
mac_cmd: "Command",
525+
mac_cmd: "Cmd",
526526
concat: "+",
527527
};
528528
}
@@ -585,6 +585,11 @@ pub enum Key {
585585
PageUp,
586586
PageDown,
587587

588+
/// The virtual keycode for the Minus key.
589+
Minus,
590+
/// The virtual keycode for the Plus/Equals key.
591+
PlusEquals,
592+
588593
/// Either from the main row or from the numpad.
589594
Num0,
590595
/// Either from the main row or from the numpad.
@@ -667,6 +672,8 @@ impl Key {
667672
Key::ArrowLeft => "⏴",
668673
Key::ArrowRight => "⏵",
669674
Key::ArrowUp => "⏶",
675+
Key::Minus => "-",
676+
Key::PlusEquals => "+",
670677
_ => self.name(),
671678
}
672679
}
@@ -689,6 +696,8 @@ impl Key {
689696
Key::End => "End",
690697
Key::PageUp => "PageUp",
691698
Key::PageDown => "PageDown",
699+
Key::Minus => "Minus",
700+
Key::PlusEquals => "Plus",
692701
Key::Num0 => "0",
693702
Key::Num1 => "1",
694703
Key::Num2 => "2",

0 commit comments

Comments
 (0)