From 2500a600623fe2f527ba17a6371f0fcc79679a8b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 30 Jul 2022 18:39:38 +0200 Subject: [PATCH] Code cleanup and improved docs --- eframe/src/epi.rs | 3 ++ egui/src/data/output.rs | 8 ++++++ egui/src/menu.rs | 62 ++++++++++++++++++----------------------- egui/src/response.rs | 1 + egui/src/ui.rs | 7 +++++ sh/build_demo_web.sh | 1 + sh/check.sh | 2 +- 7 files changed, 48 insertions(+), 36 deletions(-) diff --git a/eframe/src/epi.rs b/eframe/src/epi.rs index 70c77c5e..963a8dc7 100644 --- a/eframe/src/epi.rs +++ b/eframe/src/epi.rs @@ -195,6 +195,9 @@ pub struct NativeOptions { pub drag_and_drop_support: bool, /// The application icon, e.g. in the Windows task bar etc. + /// + /// This doesn't work on Mac and on Wayland. + /// See for more. pub icon_data: Option, /// The initial (inner) position of the native window in points (logical pixels). diff --git a/egui/src/data/output.rs b/egui/src/data/output.rs index 385cd673..81349b86 100644 --- a/egui/src/data/output.rs +++ b/egui/src/data/output.rs @@ -66,6 +66,14 @@ pub struct PlatformOutput { /// If set, put this text in the system clipboard. Ignore if empty. /// /// This is often a response to [`crate::Event::Copy`] or [`crate::Event::Cut`]. + /// + /// ``` + /// # egui::__run_test_ui(|ui| { + /// if ui.button("📋").clicked() { + /// ui.output().copied_text = "some_text".to_string(); + /// } + /// # }); + /// ``` pub copied_text: String, /// Events that may be useful to e.g. a screen reader. diff --git a/egui/src/menu.rs b/egui/src/menu.rs index 54f329a5..c4beb1fb 100644 --- a/egui/src/menu.rs +++ b/egui/src/menu.rs @@ -20,7 +20,7 @@ use super::{ Sense, TextStyle, Ui, Vec2, }; use crate::{widgets::*, *}; -use epaint::{mutex::RwLock, Stroke}; +use epaint::mutex::RwLock; use std::sync::Arc; /// What is saved between frames. @@ -61,20 +61,20 @@ impl std::ops::DerefMut for BarState { } } +fn set_menu_style(style: &mut Style) { + style.spacing.button_padding = vec2(2.0, 0.0); + style.visuals.widgets.active.bg_stroke = Stroke::none(); + style.visuals.widgets.hovered.bg_stroke = Stroke::none(); + style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT; + style.visuals.widgets.inactive.bg_stroke = Stroke::none(); +} + /// The menu bar goes well in a [`TopBottomPanel::top`], /// but can also be placed in a [`Window`]. /// In the latter case you may want to wrap it in [`Frame`]. pub fn bar(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse { ui.horizontal(|ui| { - let mut style = (**ui.style()).clone(); - style.spacing.button_padding = vec2(2.0, 0.0); - // style.visuals.widgets.active.bg_fill = Color32::TRANSPARENT; - style.visuals.widgets.active.bg_stroke = Stroke::none(); - // style.visuals.widgets.hovered.bg_fill = Color32::TRANSPARENT; - style.visuals.widgets.hovered.bg_stroke = Stroke::none(); - style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT; - style.visuals.widgets.inactive.bg_stroke = Stroke::none(); - ui.set_style(style); + set_menu_style(ui.style_mut()); // Take full width and fixed height: let height = ui.spacing().interact_size.y; @@ -130,27 +130,17 @@ pub(crate) fn menu_ui<'c, R>( .interactable(true) .drag_bounds(Rect::EVERYTHING); let inner_response = area.show(ctx, |ui| { - ui.scope(|ui| { - let style = ui.style_mut(); - style.spacing.item_spacing = Vec2::ZERO; - style.spacing.button_padding = crate::vec2(2.0, 0.0); + set_menu_style(ui.style_mut()); - style.visuals.widgets.active.bg_stroke = Stroke::none(); - style.visuals.widgets.hovered.bg_stroke = Stroke::none(); - style.visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT; - style.visuals.widgets.inactive.bg_stroke = Stroke::none(); - - Frame::menu(style) - .show(ui, |ui| { - const DEFAULT_MENU_WIDTH: f32 = 150.0; // TODO(emilk): add to ui.spacing - ui.set_max_width(DEFAULT_MENU_WIDTH); - ui.set_menu_state(Some(menu_state_arc.clone())); - ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents) - .inner - }) - .inner - }) - .inner + Frame::menu(ui.style()) + .show(ui, |ui| { + const DEFAULT_MENU_WIDTH: f32 = 150.0; // TODO(emilk): add to ui.spacing + ui.set_max_width(DEFAULT_MENU_WIDTH); + ui.set_menu_state(Some(menu_state_arc.clone())); + ui.with_layout(Layout::top_down_justified(Align::LEFT), add_contents) + .inner + }) + .inner }); menu_state_arc.write().rect = inner_response.response.rect; inner_response @@ -437,11 +427,13 @@ impl SubMenuButton { .align_size_within_rect(icon_galley.size(), rect.shrink2(button_padding)) .min; - ui.painter().rect_filled( - rect.expand(visuals.expansion), - visuals.rounding, - visuals.bg_fill, - ); + if ui.visuals().button_frame { + ui.painter().rect_filled( + rect.expand(visuals.expansion), + visuals.rounding, + visuals.bg_fill, + ); + } let text_color = visuals.text_color(); text_galley.paint_with_fallback_color(ui.painter(), text_pos, text_color); diff --git a/egui/src/response.rs b/egui/src/response.rs index 591adbd0..f1c72bd8 100644 --- a/egui/src/response.rs +++ b/egui/src/response.rs @@ -463,6 +463,7 @@ impl Response { /// if response.clicked() { /* … */ } /// # }); /// ``` + #[must_use] pub fn interact(&self, sense: Sense) -> Self { self.ctx.interact_with_hovered( self.layer_id, diff --git a/egui/src/ui.rs b/egui/src/ui.rs index cb2397ab..413677c5 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -370,6 +370,13 @@ impl Ui { /// The [`PlatformOutput`] of the [`Context`] associated with this ui. /// Equivalent to `.ctx().output()`. + /// + /// ``` + /// # egui::__run_test_ui(|ui| { + /// if ui.button("📋").clicked() { + /// ui.output().copied_text = "some_text".to_string(); + /// } + /// # }); #[inline] pub fn output(&self) -> RwLockWriteGuard<'_, PlatformOutput> { self.ctx().output() diff --git a/sh/build_demo_web.sh b/sh/build_demo_web.sh index a0b22de6..2a1068ab 100755 --- a/sh/build_demo_web.sh +++ b/sh/build_demo_web.sh @@ -6,6 +6,7 @@ cd "$script_path/.." ./sh/setup_web.sh CRATE_NAME="egui_demo_app" + # NOTE: persistence use up about 400kB (10%) of the WASM! FEATURES="glow,http,persistence,screen_reader" OPEN=false diff --git a/sh/check.sh b/sh/check.sh index 7741bda3..c4c38735 100755 --- a/sh/check.sh +++ b/sh/check.sh @@ -58,7 +58,7 @@ cargo deny check # # For finding bloat: -# cargo bloat --release --bin demo_glium -n 200 | rg egui +# cargo bloat --release --bin egui_demo_app -n 200 | rg egui # Also try https://github.com/google/bloaty # what compiles slowly?