From 89d19860b820a577f159a61aba7acfb483ff4c9c Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 19 Feb 2022 20:51:51 +0100 Subject: [PATCH] Misc code cleanup, docs fixes, etc --- docs/index.html | 8 +++---- eframe/examples/hello_world.rs | 10 ++++----- egui/Cargo.toml | 2 +- egui/src/containers/panel.rs | 2 ++ egui/src/data/input.rs | 12 +++++++++- egui/src/introspection.rs | 2 +- egui/src/layers.rs | 1 + egui/src/response.rs | 2 +- egui/src/ui.rs | 8 +++---- egui/src/widgets/plot/items/mod.rs | 2 ++ egui_web/README.md | 4 ++-- epaint/src/shape.rs | 36 ++++++++++++++++++++---------- epaint/src/text/fonts.rs | 6 ++--- epi/src/lib.rs | 12 +++++----- sh/check.sh | 1 + 15 files changed, 67 insertions(+), 41 deletions(-) diff --git a/docs/index.html b/docs/index.html index 6d3a5588..bf9dfb5f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -128,20 +128,20 @@ .catch(on_wasm_error); function on_wasm_loaded() { - console.debug("wasm loaded. starting egui app…"); + console.debug("wasm loaded. starting app…"); // This call installs a bunch of callbacks and then returns: wasm_bindgen.start("the_canvas_id"); - console.debug("egui app started."); + console.debug("app started."); document.getElementById("center_text").remove(); } function on_wasm_error(error) { - console.error("Failed to start egui: " + error); + console.error("Failed to start: " + error); document.getElementById("center_text").innerHTML = `

- An error occurred loading egui + An error occurred during loading:

${error} diff --git a/eframe/examples/hello_world.rs b/eframe/examples/hello_world.rs index 7ec61fba..77f1479b 100644 --- a/eframe/examples/hello_world.rs +++ b/eframe/examples/hello_world.rs @@ -22,19 +22,17 @@ impl epi::App for MyApp { } fn update(&mut self, ctx: &egui::Context, frame: &epi::Frame) { - let Self { name, age } = self; - egui::CentralPanel::default().show(ctx, |ui| { ui.heading("My egui Application"); ui.horizontal(|ui| { ui.label("Your name: "); - ui.text_edit_singleline(name); + ui.text_edit_singleline(&mut self.name); }); - ui.add(egui::Slider::new(age, 0..=120).text("age")); + ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age")); if ui.button("Click each year").clicked() { - *age += 1; + self.age += 1; } - ui.label(format!("Hello '{}', age {}", name, age)); + ui.label(format!("Hello '{}', age {}", self.name, self.age)); }); // Resize the native window to be just the size we need it to be: diff --git a/egui/Cargo.toml b/egui/Cargo.toml index 67591cf2..663bce13 100644 --- a/egui/Cargo.toml +++ b/egui/Cargo.toml @@ -2,7 +2,7 @@ name = "egui" version = "0.16.1" authors = ["Emil Ernerfeldt "] -description = "Simple, portable immediate mode GUI library for Rust" +description = "An easy-to-use immediate mode GUI that runs on both web and native" edition = "2021" rust-version = "1.56" homepage = "https://github.com/emilk/egui" diff --git a/egui/src/containers/panel.rs b/egui/src/containers/panel.rs index 9fc6aae8..ea17aa17 100644 --- a/egui/src/containers/panel.rs +++ b/egui/src/containers/panel.rs @@ -9,6 +9,8 @@ //! The order in which you add panels matter! //! The first panel you add will always be the outermost, and the last you add will always be the innermost. //! +//! You must never open one top-level panel from within another panel. Add one panel, then the next. +//! //! Always add any [`CentralPanel`] last. //! //! Add your [`Window`]:s after any top-level panels. diff --git a/egui/src/data/input.rs b/egui/src/data/input.rs index 29a3458c..704c31fe 100644 --- a/egui/src/data/input.rs +++ b/egui/src/data/input.rs @@ -165,18 +165,27 @@ pub enum Event { /// /// When the user presses enter/return, do not send a `Text` (just [`Key::Enter`]). Text(String), + /// A key was pressed or released. Key { key: Key, + /// Was it pressed or released? pressed: bool, + /// The state of the modifier keys at the time of the event. modifiers: Modifiers, }, + /// The mouse or touch moved to a new place. PointerMoved(Pos2), + + /// A mouse button was pressed or released (or a touch started or stopped). PointerButton { + /// Where is the pointer? pos: Pos2, + /// What mouse button? For touches, use [`PointerButton::Primary`]. button: PointerButton, + /// Was it the button/touch pressed this frame, or released? pressed: bool, - /// The state of the modifier keys at the time of the event + /// The state of the modifier keys at the time of the event. modifiers: Modifiers, }, /// The mouse left the screen, or the last/primary touch input disappeared. @@ -217,6 +226,7 @@ pub enum Event { /// Unique identifier of a finger/pen. Value is stable from touch down /// to lift-up id: TouchId, + /// One of: start move end cancel. phase: TouchPhase, /// Position of the touch (or where the touch was last detected) pos: Pos2, diff --git a/egui/src/introspection.rs b/egui/src/introspection.rs index 3726b808..e5f814cc 100644 --- a/egui/src/introspection.rs +++ b/egui/src/introspection.rs @@ -146,7 +146,7 @@ impl Widget for &mut epaint::TessellationOptions { epsilon: _, } = self; ui.checkbox(anti_alias, "Antialias") - .on_hover_text("Turn off for small performance gain."); + .on_hover_text("Apply feathering to smooth out the edges of shapes. Turn off for small performance gain."); ui.add( crate::widgets::Slider::new(bezier_tolerance, 0.0001..=10.0) .logarithmic(true) diff --git a/egui/src/layers.rs b/egui/src/layers.rs index 578d505a..76c8dfe3 100644 --- a/egui/src/layers.rs +++ b/egui/src/layers.rs @@ -23,6 +23,7 @@ pub enum Order { /// Debug layer, always painted last / on top Debug, } + impl Order { const COUNT: usize = 6; const ALL: [Order; Self::COUNT] = [ diff --git a/egui/src/response.rs b/egui/src/response.rs index 29bb5626..e8a94330 100644 --- a/egui/src/response.rs +++ b/egui/src/response.rs @@ -447,7 +447,7 @@ impl Response { /// /// If `align` is `None`, it'll scroll enough to bring the UI into view. /// - /// See also: [`Ui::scroll_to_cursor`], [`Ui::scroll_to`]. + /// See also: [`Ui::scroll_to_cursor`], [`Ui::scroll_to_rect`]. /// /// ``` /// # egui::__run_test_ui(|ui| { diff --git a/egui/src/ui.rs b/egui/src/ui.rs index ae0d26cb..5774f6c5 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -908,7 +908,7 @@ impl Ui { /// /// If `align` is `None`, it'll scroll enough to bring the cursor into view. /// - /// See also: [`Response::scroll_to_me`], [`Ui::scroll_to`]. + /// See also: [`Response::scroll_to_me`], [`Ui::scroll_to_rect`]. /// /// ``` /// # use egui::Align; @@ -933,7 +933,7 @@ impl Ui { /// /// If `align` is not provided, it'll scroll enough to bring the cursor into view. /// - /// See also: [`Response::scroll_to_me`], [`Ui::scroll_to`]. + /// See also: [`Response::scroll_to_me`], [`Ui::scroll_to_rect`]. /// /// ``` /// # use egui::Align; @@ -1014,7 +1014,7 @@ impl Ui { .inner } - /// Add a single[`Widget`] that is possibly disabled, i.e. greyed out and non-interactive. + /// Add a single [`Widget`] that is possibly disabled, i.e. greyed out and non-interactive. /// /// If you call `add_enabled` from within an already disabled `Ui`, /// the widget will always be disabled, even if the `enabled` argument is true. @@ -1069,7 +1069,7 @@ impl Ui { }) } - /// Add a single[`Widget`] that is possibly invisible. + /// Add a single [`Widget`] that is possibly invisible. /// /// An invisible widget still takes up the same space as if it were visible. /// diff --git a/egui/src/widgets/plot/items/mod.rs b/egui/src/widgets/plot/items/mod.rs index eeb8180a..55ae512e 100644 --- a/egui/src/widgets/plot/items/mod.rs +++ b/egui/src/widgets/plot/items/mod.rs @@ -613,6 +613,7 @@ impl PlotItem for Polygon { } /// Text inside the plot. +#[derive(Clone)] pub struct Text { pub(super) text: WidgetText, pub(super) position: Value, @@ -1079,6 +1080,7 @@ impl PlotItem for Arrows { } /// An image in the plot. +#[derive(Clone)] pub struct PlotImage { pub(super) position: Value, pub(super) texture_id: TextureId, diff --git a/egui_web/README.md b/egui_web/README.md index ac4f194d..045fd905 100644 --- a/egui_web/README.md +++ b/egui_web/README.md @@ -24,6 +24,6 @@ Check out [eframe_template](https://github.com/emilk/eframe_template) for an exa * No integration with browser settings for colors and fonts. * On Linux and Mac, Firefox will copy the WebGL render target from GPU, to CPU and then back again (https://bugzilla.mozilla.org/show_bug.cgi?id=1010527#c0), slowing down egui. -The suggested use for `egui_web` is for experiments, personal projects and web games. Using egui for a serious web page is probably a bad idea. - In many ways, `egui_web` is trying to make the browser do something it wasn't designed to do (though there are many things browser vendors could do to improve how well libraries like egui work). + +The suggested use for `egui_web` are for web apps where performance and responsiveness are more important than accessability and mobile text editing. diff --git a/epaint/src/shape.rs b/epaint/src/shape.rs index 6d745eee..6e2ab5df 100644 --- a/epaint/src/shape.rs +++ b/epaint/src/shape.rs @@ -31,6 +31,20 @@ pub enum Shape { CubicBezier(CubicBezierShape), } +impl From> for Shape { + #[inline(always)] + fn from(shapes: Vec) -> Self { + Self::Vec(shapes) + } +} + +impl From for Shape { + #[inline(always)] + fn from(mesh: Mesh) -> Self { + Self::Mesh(mesh) + } +} + /// ## Constructors impl Shape { /// A line between two points. @@ -59,25 +73,25 @@ impl Shape { /// Turn a line into equally spaced dots. pub fn dotted_line( - points: &[Pos2], + path: &[Pos2], color: impl Into, spacing: f32, radius: f32, ) -> Vec { let mut shapes = Vec::new(); - points_from_line(points, spacing, radius, color.into(), &mut shapes); + points_from_line(path, spacing, radius, color.into(), &mut shapes); shapes } /// Turn a line into dashes. pub fn dashed_line( - points: &[Pos2], + path: &[Pos2], stroke: impl Into, dash_length: f32, gap_length: f32, ) -> Vec { let mut shapes = Vec::new(); - dashes_from_line(points, stroke.into(), dash_length, gap_length, &mut shapes); + dashes_from_line(path, stroke.into(), dash_length, gap_length, &mut shapes); shapes } @@ -495,16 +509,15 @@ impl From for Shape { /// Creates equally spaced filled circles from a line. fn points_from_line( - line: &[Pos2], + path: &[Pos2], spacing: f32, radius: f32, color: Color32, shapes: &mut Vec, ) { let mut position_on_segment = 0.0; - line.windows(2).for_each(|window| { - let start = window[0]; - let end = window[1]; + path.windows(2).for_each(|window| { + let (start, end) = (window[0], window[1]); let vector = end - start; let segment_length = vector.length(); while position_on_segment < segment_length { @@ -518,7 +531,7 @@ fn points_from_line( /// Creates dashes from a line. fn dashes_from_line( - line: &[Pos2], + path: &[Pos2], stroke: Stroke, dash_length: f32, gap_length: f32, @@ -526,9 +539,8 @@ fn dashes_from_line( ) { let mut position_on_segment = 0.0; let mut drawing_dash = false; - line.windows(2).for_each(|window| { - let start = window[0]; - let end = window[1]; + path.windows(2).for_each(|window| { + let (start, end) = (window[0], window[1]); let vector = end - start; let segment_length = vector.length(); diff --git a/epaint/src/text/fonts.rs b/epaint/src/text/fonts.rs index 790db06b..3f4dc131 100644 --- a/epaint/src/text/fonts.rs +++ b/epaint/src/text/fonts.rs @@ -160,15 +160,15 @@ pub struct FontTweak { /// Shift font downwards by this fraction of the font size (in points). /// - /// A positive value shifts the text upwards. - /// A negative value shifts it downwards. + /// A positive value shifts the text downwards. + /// A negative value shifts it upwards. /// /// Example value: `-0.2`. pub y_offset_factor: f32, /// Shift font downwards by this amount of logical points. /// - /// Example value: `-1.0`. + /// Example value: `2.0`. pub y_offset: f32, } diff --git a/epi/src/lib.rs b/epi/src/lib.rs index b0dda591..4481cd1c 100644 --- a/epi/src/lib.rs +++ b/epi/src/lib.rs @@ -372,27 +372,27 @@ pub struct WebInfo { pub struct Location { /// The full URL (`location.href`) without the hash. /// - /// Example: "http://www.example.com:80/index.html?foo=bar". + /// Example: `"http://www.example.com:80/index.html?foo=bar"`. pub url: String, /// `location.protocol` /// - /// Example: "http:". + /// Example: `"http:"`. pub protocol: String, /// `location.host` /// - /// Example: "example.com:80". + /// Example: `"example.com:80"`. pub host: String, /// `location.hostname` /// - /// Example: "example.com". + /// Example: `"example.com"`. pub hostname: String, /// `location.port` /// - /// Example: "80". + /// Example: `"80"`. pub port: String, /// The "#fragment" part of "www.example.com/index.html?query#fragment". @@ -415,7 +415,7 @@ pub struct Location { /// `location.origin` /// - /// Example: "http://www.example.com:80" + /// Example: `"http://www.example.com:80"`. pub origin: String, } diff --git a/sh/check.sh b/sh/check.sh index afcc1db2..72fb1c64 100755 --- a/sh/check.sh +++ b/sh/check.sh @@ -52,6 +52,7 @@ cargo doc --document-private-items --no-deps --all-features # For finding bloat: # cargo bloat --release --bin demo_glium -n 200 | rg egui +# Also try https://github.com/google/bloaty # what compiles slowly? # https://fasterthanli.me/articles/why-is-my-rust-build-so-slow