1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10:03 -04:00

[app] Add demo app slider to change scale of all of Egui

This commit is contained in:
Emil Ernerfeldt
2020-10-17 23:54:46 +02:00
parent 251cde60f0
commit a4e19d7207
9 changed files with 149 additions and 85 deletions

View File

@@ -24,12 +24,14 @@ pub trait App {
fn on_exit(&mut self, _storage: &mut dyn Storage) {}
}
#[derive(Clone, Debug)]
pub struct WebInfo {
/// e.g. "#fragment" part of "www.example.com/index.html#fragment"
pub web_location_hash: String,
}
/// Information about the backend passed to the use app each frame.
#[derive(Clone, Debug)]
pub struct BackendInfo {
/// If the app is running in a Web context, this returns information about the environment.
pub web_info: Option<WebInfo>,
@@ -41,14 +43,20 @@ pub struct BackendInfo {
/// Local time. Used for the clock in the demo app.
/// Set to `None` if you don't know.
pub seconds_since_midnight: Option<f64>,
/// The OS native pixels-per-point
pub native_pixels_per_point: Option<f32>,
}
/// Action that can be taken by the user app.
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AppOutput {
/// Set to `true` to stop the app.
/// This does nothing for web apps.
pub quit: bool,
/// If the app sets this, change the `pixels_per_point` of Egui to this next frame.
pub pixels_per_point: Option<f32>,
}
pub trait TextureAllocator {

View File

@@ -188,6 +188,9 @@ pub struct DemoApp {
demo_window: DemoWindow,
fractal_clock: FractalClock,
/// current slider value for current gui scale (backend demo only)
pixels_per_point: Option<f32>,
#[cfg_attr(feature = "serde", serde(skip))]
frame_history: FrameHistory,
@@ -320,8 +323,6 @@ impl DemoApp {
let is_web = info.web_info.is_some();
let mut options = app::AppOutput::default();
if is_web {
ui.label("Egui is an immediate mode GUI written in Rust, compiled to WebAssembly, rendered with WebGL.");
ui.label(
@@ -332,23 +333,17 @@ impl DemoApp {
ui.label("Project home page:");
ui.hyperlink("https://github.com/emilk/egui");
});
} else {
ui.heading("Egui");
options.quit |= ui.button("Quit").clicked;
ui.separator();
}
ui.separator();
self.run_mode_ui(ui);
if self.run_mode == RunMode::Continuous {
ui.label(format!(
"Repainting the UI each frame. FPS: {:.1}",
self.frame_history.fps()
));
} else {
ui.label("Only running UI code when there are animations or input");
}
ui.separator();
let mut output = app::AppOutput::default();
output.pixels_per_point = self.pixels_per_point_ui(ui, info);
ui.separator();
ui.separator();
self.frame_history.ui(ui);
@@ -359,7 +354,41 @@ impl DemoApp {
"Show color blend test (debug backend painter)",
);
options
if !is_web {
output.quit |= ui.button("Quit").clicked;
}
output
}
fn pixels_per_point_ui(&mut self, ui: &mut Ui, info: &app::BackendInfo) -> Option<f32> {
self.pixels_per_point = self
.pixels_per_point
.or(info.native_pixels_per_point)
.or_else(|| Some(ui.ctx().pixels_per_point()));
if let Some(pixels_per_point) = &mut self.pixels_per_point {
ui.add(
Slider::f32(pixels_per_point, 0.5..=5.0)
.logarithmic(true)
.text("Scale (physical pixels per point)"),
);
if let Some(native_pixels_per_point) = info.native_pixels_per_point {
if ui
.button(format!(
"Reset scale to native value ({:.1})",
native_pixels_per_point
))
.clicked
{
*pixels_per_point = native_pixels_per_point;
}
}
if !ui.ctx().is_using_mouse() {
// We wait until mouse release to activate:
return Some(*pixels_per_point);
}
}
None
}
fn run_mode_ui(&mut self, ui: &mut Ui) {
@@ -371,6 +400,15 @@ impl DemoApp {
ui.radio_value(run_mode, RunMode::Reactive, "Reactive")
.on_hover_text("Repaint when there are animations or input (e.g. mouse movement)");
});
if self.run_mode == RunMode::Continuous {
ui.label(format!(
"Repainting the UI each frame. FPS: {:.1}",
self.frame_history.fps()
));
} else {
ui.label("Only running UI code when there are animations or input");
}
}
}

View File

@@ -388,7 +388,7 @@ impl InputState {
ui.label(format!("scroll_delta: {:?} points", scroll_delta));
ui.label(format!("screen_size: {:?} points", screen_size));
ui.label(format!(
"{:?} points for each physical pixel (HDPI factor)",
"{:?} physical pixels for each logical point",
pixels_per_point
));
ui.label(format!("time: {:.3} s", time));