From 7183f6b3708c4df579b4dd1991f767c7b8b802b2 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 29 Jan 2022 17:35:47 +0100 Subject: [PATCH] Add ui.data(), ctx.data(), ctx.options() and ctx.tessellation_options() Helpful access deeper into Memory --- CHANGELOG.md | 7 ++- egui-winit/src/lib.rs | 2 +- egui/src/containers/collapsing_header.rs | 4 +- egui/src/containers/panel.rs | 4 +- egui/src/containers/popup.rs | 4 +- egui/src/containers/resize.rs | 4 +- egui/src/containers/scroll_area.rs | 4 +- egui/src/context.rs | 70 ++++++++++++++++-------- egui/src/grid.rs | 4 +- egui/src/memory.rs | 2 +- egui/src/menu.rs | 7 +-- egui/src/ui.rs | 12 +++- egui/src/widgets/color_picker.rs | 2 +- egui/src/widgets/plot/mod.rs | 4 +- egui/src/widgets/text_edit/state.rs | 4 +- egui_demo_lib/src/apps/demo/password.rs | 4 +- egui_demo_lib/src/backend_panel.rs | 4 +- egui_demo_lib/src/syntax_highlighting.rs | 19 ++----- egui_web/src/backend.rs | 2 +- 19 files changed, 91 insertions(+), 72 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32562e08dac..e6454b4aa85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,8 +15,9 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w * Added `TextStyle::resolve`. * `Context::load_texture` to convert an image into a texture which can be displayed using e.g. `ui.image(texture, size)` ([#1110](https://github.com/emilk/egui/pull/1110)). * Added `Ui::add_visible` and `Ui::add_visible_ui`. -* Added `CollapsingHeader::icon` to override the default open/close icon using a custom function. ([1147](https://github.com/emilk/egui/pull/1147)) -* Added `Plot::x_axis_formatter` and `Plot::y_axis_formatter` for custom axis labels ([#1130](https://github.com/emilk/egui/pull/1130)) +* Added `CollapsingHeader::icon` to override the default open/close icon using a custom function. ([1147](https://github.com/emilk/egui/pull/1147)). +* Added `Plot::x_axis_formatter` and `Plot::y_axis_formatter` for custom axis labels ([#1130](https://github.com/emilk/egui/pull/1130)). +* Added `ui.data()`, `ctx.data()`, `ctx.options()` and `ctx.tessellation_options()` ([#1175](https://github.com/emilk/egui/pull/1175)). ### Changed 🔧 * ⚠️ `Context::input` and `Ui::input` now locks a mutex. This can lead to a dead-lock is used in an `if let` binding! @@ -42,9 +43,9 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w * Fixed `enable_drag` for Windows ([#1108](https://github.com/emilk/egui/pull/1108)). ### Contributors 🙏 +* [AlexxxRu](https://github.com/alexxxru): [#1108](https://github.com/emilk/egui/pull/1108). * [danielkeller](https://github.com/danielkeller): [#1050](https://github.com/emilk/egui/pull/1050). * [juancampa](https://github.com/juancampa): [#1147](https://github.com/emilk/egui/pull/1147). -* [AlexxxRu](https://github.com/alexxxru): [#1108](https://github.com/emilk/egui/pull/1108). ## 0.16.1 - 2021-12-31 - Add back `CtxRef::begin_frame,end_frame` diff --git a/egui-winit/src/lib.rs b/egui-winit/src/lib.rs index a3a98150614..4923601207a 100644 --- a/egui-winit/src/lib.rs +++ b/egui-winit/src/lib.rs @@ -520,7 +520,7 @@ impl State { egui_ctx: &egui::Context, output: egui::Output, ) -> egui::TexturesDelta { - if egui_ctx.memory().options.screen_reader { + if egui_ctx.options().screen_reader { self.screen_reader.speak(&output.events_description()); } diff --git a/egui/src/containers/collapsing_header.rs b/egui/src/containers/collapsing_header.rs index 652d9956a28..f922cf8d205 100644 --- a/egui/src/containers/collapsing_header.rs +++ b/egui/src/containers/collapsing_header.rs @@ -15,11 +15,11 @@ pub(crate) struct State { impl State { pub fn load(ctx: &Context, id: Id) -> Option { - ctx.memory().data.get_persisted(id) + ctx.data().get_persisted(id) } pub fn store(self, ctx: &Context, id: Id) { - ctx.memory().data.insert_persisted(id, self); + ctx.data().insert_persisted(id, self); } pub fn from_memory_with_default_open(ctx: &Context, id: Id, default_open: bool) -> Self { diff --git a/egui/src/containers/panel.rs b/egui/src/containers/panel.rs index 487049d8989..9fc6aae86c6 100644 --- a/egui/src/containers/panel.rs +++ b/egui/src/containers/panel.rs @@ -25,11 +25,11 @@ struct PanelState { impl PanelState { fn load(ctx: &Context, bar_id: Id) -> Option { - ctx.memory().data.get_persisted(bar_id) + ctx.data().get_persisted(bar_id) } fn store(self, ctx: &Context, bar_id: Id) { - ctx.memory().data.insert_persisted(bar_id, self); + ctx.data().insert_persisted(bar_id, self); } } diff --git a/egui/src/containers/popup.rs b/egui/src/containers/popup.rs index 76627ba6438..ffe366721e3 100644 --- a/egui/src/containers/popup.rs +++ b/egui/src/containers/popup.rs @@ -13,11 +13,11 @@ pub(crate) struct MonoState { impl MonoState { fn load(ctx: &Context) -> Option { - ctx.memory().data.get_temp(Id::null()) + ctx.data().get_temp(Id::null()) } fn store(self, ctx: &Context) { - ctx.memory().data.insert_temp(Id::null(), self); + ctx.data().insert_temp(Id::null(), self); } fn tooltip_size(&self, id: Id, index: usize) -> Option { diff --git a/egui/src/containers/resize.rs b/egui/src/containers/resize.rs index 903d669a9c1..5dd46dbe4bc 100644 --- a/egui/src/containers/resize.rs +++ b/egui/src/containers/resize.rs @@ -18,11 +18,11 @@ pub(crate) struct State { impl State { pub fn load(ctx: &Context, id: Id) -> Option { - ctx.memory().data.get_persisted(id) + ctx.data().get_persisted(id) } pub fn store(self, ctx: &Context, id: Id) { - ctx.memory().data.insert_persisted(id, self); + ctx.data().insert_persisted(id, self); } } diff --git a/egui/src/containers/scroll_area.rs b/egui/src/containers/scroll_area.rs index 78d6156bf14..fc2eaea26d7 100644 --- a/egui/src/containers/scroll_area.rs +++ b/egui/src/containers/scroll_area.rs @@ -43,11 +43,11 @@ impl Default for State { impl State { pub fn load(ctx: &Context, id: Id) -> Option { - ctx.memory().data.get_persisted(id) + ctx.data().get_persisted(id) } pub fn store(self, ctx: &Context, id: Id) { - ctx.memory().data.insert_persisted(id, self); + ctx.data().insert_persisted(id, self); } } diff --git a/egui/src/context.rs b/egui/src/context.rs index c997ea7dc0f..37e99cdaee6 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -2,9 +2,9 @@ use crate::{ animation_manager::AnimationManager, data::output::Output, frame_state::FrameState, - input_state::*, layers::GraphicLayers, TextureHandle, *, + input_state::*, layers::GraphicLayers, memory::Options, TextureHandle, *, }; -use epaint::{mutex::*, stats::*, text::Fonts, *}; +use epaint::{mutex::*, stats::*, text::Fonts, TessellationOptions, *}; // ---------------------------------------------------------------------------- @@ -444,20 +444,31 @@ impl Context { /// ## Borrows parts of [`Context`] impl Context { /// Stores all the egui state. + /// /// If you want to store/restore egui, serialize this. + #[inline] pub fn memory(&self) -> RwLockWriteGuard<'_, Memory> { RwLockWriteGuard::map(self.write(), |c| &mut c.memory) } + /// Stores superficial widget state. + #[inline] + pub fn data(&self) -> RwLockWriteGuard<'_, crate::util::IdTypeMap> { + RwLockWriteGuard::map(self.write(), |c| &mut c.memory.data) + } + + #[inline] pub(crate) fn graphics(&self) -> RwLockWriteGuard<'_, GraphicLayers> { RwLockWriteGuard::map(self.write(), |c| &mut c.graphics) } /// What egui outputs each frame. + #[inline] pub fn output(&self) -> RwLockWriteGuard<'_, Output> { RwLockWriteGuard::map(self.write(), |c| &mut c.output) } + #[inline] pub(crate) fn frame_state(&self) -> RwLockWriteGuard<'_, FrameState> { RwLockWriteGuard::map(self.write(), |c| &mut c.frame_state) } @@ -481,17 +492,19 @@ impl Context { /// // This is fine! /// } /// ``` - #[inline(always)] + #[inline] pub fn input(&self) -> RwLockReadGuard<'_, InputState> { RwLockReadGuard::map(self.read(), |c| &c.input) } + #[inline] pub fn input_mut(&self) -> RwLockWriteGuard<'_, InputState> { RwLockWriteGuard::map(self.write(), |c| &mut c.input) } /// Not valid until first call to [`Context::run()`]. /// That's because since we don't know the proper `pixels_per_point` until then. + #[inline] pub fn fonts(&self) -> RwLockReadGuard<'_, Fonts> { RwLockReadGuard::map(self.read(), |c| { c.fonts @@ -500,9 +513,21 @@ impl Context { }) } + #[inline] fn fonts_mut(&self) -> RwLockWriteGuard<'_, Option> { RwLockWriteGuard::map(self.write(), |c| &mut c.fonts) } + + #[inline] + pub fn options(&self) -> RwLockWriteGuard<'_, Options> { + RwLockWriteGuard::map(self.write(), |c| &mut c.memory.options) + } + + /// Change the options used by the tessellator. + #[inline] + pub fn tessellation_options(&self) -> RwLockWriteGuard<'_, TessellationOptions> { + RwLockWriteGuard::map(self.write(), |c| &mut c.memory.options.tessellation_options) + } } impl Context { @@ -533,7 +558,7 @@ impl Context { /// The [`Style`] used by all subsequent windows, panels etc. pub fn style(&self) -> Arc