Skip to content

Commit

Permalink
OS independent Zoom factor & serialization thereof (#1448)
Browse files Browse the repository at this point in the history
* Add Zoom in/out/reset to command palette
* Use screen independent zoom factor and serialize it with app state
  • Loading branch information
Wumpf authored Feb 28, 2023
1 parent d49d497 commit 867d0f0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
23 changes: 23 additions & 0 deletions crates/re_ui/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ pub enum Command {

#[cfg(not(target_arch = "wasm32"))]
ToggleFullscreen,
#[cfg(not(target_arch = "wasm32"))]
ZoomIn,
#[cfg(not(target_arch = "wasm32"))]
ZoomOut,
#[cfg(not(target_arch = "wasm32"))]
ZoomReset,

SelectionPrevious,
SelectionNext,
Expand Down Expand Up @@ -91,6 +97,15 @@ impl Command {
"Toggle fullscreen",
"Toggle between windowed and fullscreen viewer",
),
#[cfg(not(target_arch = "wasm32"))]
Command::ZoomIn => ("Zoom In", "Increases the ui scaling factor"),
#[cfg(not(target_arch = "wasm32"))]
Command::ZoomOut => ("Zoom Out", "Decreases the ui scaling factor"),
#[cfg(not(target_arch = "wasm32"))]
Command::ZoomReset => (
"Reset Zoom",
"Resets ui scaling factor to the OS provided default",
),

Command::SelectionPrevious => ("Previous selection", "Go to previous selection"),
Command::SelectionNext => ("Next selection", "Go to next selection"),
Expand Down Expand Up @@ -151,8 +166,16 @@ impl Command {
Command::ToggleBlueprintPanel => Some(ctrl_shift(Key::B)),
Command::ToggleSelectionPanel => Some(ctrl_shift(Key::S)),
Command::ToggleTimePanel => Some(ctrl_shift(Key::T)),

#[cfg(not(target_arch = "wasm32"))]
Command::ToggleFullscreen => Some(key(Key::F11)),
#[cfg(not(target_arch = "wasm32"))]
Command::ZoomIn => Some(egui::gui_zoom::kb_shortcuts::ZOOM_IN),
#[cfg(not(target_arch = "wasm32"))]
Command::ZoomOut => Some(egui::gui_zoom::kb_shortcuts::ZOOM_OUT),
#[cfg(not(target_arch = "wasm32"))]
Command::ZoomReset => Some(egui::gui_zoom::kb_shortcuts::ZOOM_RESET),

Command::SelectionPrevious => Some(ctrl_shift(Key::ArrowLeft)),
Command::SelectionNext => Some(ctrl_shift(Key::ArrowRight)),
Command::ToggleCommandPalette => Some(cmd(Key::P)),
Expand Down
53 changes: 43 additions & 10 deletions crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ pub struct StartupOptions {

// ----------------------------------------------------------------------------

#[cfg(not(target_arch = "wasm32"))]
const MIN_ZOOM_FACTOR: f32 = 0.2;
#[cfg(not(target_arch = "wasm32"))]
const MAX_ZOOM_FACTOR: f32 = 4.0;

/// The Rerun viewer as an [`eframe`] application.
pub struct App {
build_info: re_build_info::BuildInfo,
Expand Down Expand Up @@ -215,17 +220,10 @@ impl App {
self.pending_promises.contains_key(name.as_ref())
}

fn check_keyboard_shortcuts(&mut self, egui_ctx: &egui::Context, frame: &mut eframe::Frame) {
fn check_keyboard_shortcuts(&mut self, egui_ctx: &egui::Context) {
if let Some(cmd) = Command::listen_for_kb_shortcut(egui_ctx) {
self.pending_commands.push(cmd);
}

if !frame.is_web() {
egui::gui_zoom::zoom_with_keyboard_shortcuts(
egui_ctx,
frame.info().native_pixels_per_point,
);
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -294,6 +292,18 @@ impl App {
Command::ToggleFullscreen => {
_frame.set_fullscreen(!_frame.info().window_info.fullscreen);
}
#[cfg(not(target_arch = "wasm32"))]
Command::ZoomIn => {
self.state.app_options.zoom_factor += 0.1;
}
#[cfg(not(target_arch = "wasm32"))]
Command::ZoomOut => {
self.state.app_options.zoom_factor -= 0.1;
}
#[cfg(not(target_arch = "wasm32"))]
Command::ZoomReset => {
self.state.app_options.zoom_factor = 1.0;
}

Command::SelectionPrevious => {
let state = &mut self.state;
Expand Down Expand Up @@ -409,6 +419,22 @@ impl eframe::App for App {
return;
}

#[cfg(not(target_arch = "wasm32"))]
{
// Ensure zoom factor is sane and in 10% steps at all times before applying it.
{
let mut zoom_factor = self.state.app_options.zoom_factor;
zoom_factor = zoom_factor.clamp(MIN_ZOOM_FACTOR, MAX_ZOOM_FACTOR);
zoom_factor = (zoom_factor * 10.).round() / 10.;
self.state.app_options.zoom_factor = zoom_factor;
}

// Apply zoom factor on top of natively reported pixel per point.
let pixels_per_point = frame.info().native_pixels_per_point.unwrap_or(1.0)
* self.state.app_options.zoom_factor;
egui_ctx.set_pixels_per_point(pixels_per_point);
}

let gpu_resource_stats = {
// TODO(andreas): store the re_renderer somewhere else.
let egui_renderer = {
Expand All @@ -427,7 +453,7 @@ impl eframe::App for App {

self.memory_panel.update(&gpu_resource_stats, &store_stats); // do first, before doing too many allocations

self.check_keyboard_shortcuts(egui_ctx, frame);
self.check_keyboard_shortcuts(egui_ctx);

self.purge_memory_if_needed();

Expand Down Expand Up @@ -1042,7 +1068,14 @@ fn rerun_menu_button_ui(ui: &mut egui::Ui, _frame: &mut eframe::Frame, app: &mut
ui.add_space(spacing);

// On the web the browser controls the zoom
egui::gui_zoom::zoom_menu_buttons(ui, _frame.info().native_pixels_per_point);
let zoom_factor = app.state.app_options.zoom_factor;
ui.weak(format!("Zoom {:.0}%", zoom_factor * 100.0))
.on_hover_text("The zoom factor applied on top of the OS scaling factor.");
Command::ZoomIn.menu_button_ui(ui, &mut app.pending_commands);
Command::ZoomOut.menu_button_ui(ui, &mut app.pending_commands);
ui.add_enabled_ui(zoom_factor != 1.0, |ui| {
Command::ZoomReset.menu_button_ui(ui, &mut app.pending_commands)
});

Command::ToggleFullscreen.menu_button_ui(ui, &mut app.pending_commands);

Expand Down
12 changes: 12 additions & 0 deletions crates/re_viewer/src/misc/app_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ pub struct AppOptions {

/// Show milliseconds, RAM usage, etc.
pub show_metrics: bool,

/// Zoom factor, independent of OS points_per_pixel setting.
///
/// At every frame we check the OS reported scaling (i.e. points_per_pixel)
/// and apply this zoom factor to determine the actual points_per_pixel.
/// This way, the zooming stays constant when switching between differently scaled screens.
/// (Since this is serialized, even between sessions!)
#[cfg(not(target_arch = "wasm32"))]
pub zoom_factor: f32,
}

impl Default for AppOptions {
Expand All @@ -20,6 +29,9 @@ impl Default for AppOptions {
warn_latency: 0.200,

show_metrics: false,

#[cfg(not(target_arch = "wasm32"))]
zoom_factor: 1.0,
}
}
}

0 comments on commit 867d0f0

Please sign in to comment.