Browse Source

Add ui.data(), ctx.data(), ctx.options() and ctx.tessellation_options() (#1175)

Helpful access deeper into Memory
pull/1178/head
Emil Ernerfeldt 3 years ago
committed by GitHub
parent
commit
b618636425
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      CHANGELOG.md
  2. 2
      egui-winit/src/lib.rs
  3. 4
      egui/src/containers/collapsing_header.rs
  4. 4
      egui/src/containers/panel.rs
  5. 4
      egui/src/containers/popup.rs
  6. 4
      egui/src/containers/resize.rs
  7. 4
      egui/src/containers/scroll_area.rs
  8. 70
      egui/src/context.rs
  9. 4
      egui/src/grid.rs
  10. 2
      egui/src/memory.rs
  11. 7
      egui/src/menu.rs
  12. 12
      egui/src/ui.rs
  13. 2
      egui/src/widgets/color_picker.rs
  14. 4
      egui/src/widgets/plot/mod.rs
  15. 4
      egui/src/widgets/text_edit/state.rs
  16. 4
      egui_demo_lib/src/apps/demo/password.rs
  17. 4
      egui_demo_lib/src/backend_panel.rs
  18. 19
      egui_demo_lib/src/syntax_highlighting.rs
  19. 2
      egui_web/src/backend.rs

7
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`

2
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());
}

4
egui/src/containers/collapsing_header.rs

@ -15,11 +15,11 @@ pub(crate) struct State {
impl State {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
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 {

4
egui/src/containers/panel.rs

@ -25,11 +25,11 @@ struct PanelState {
impl PanelState {
fn load(ctx: &Context, bar_id: Id) -> Option<Self> {
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);
}
}

4
egui/src/containers/popup.rs

@ -13,11 +13,11 @@ pub(crate) struct MonoState {
impl MonoState {
fn load(ctx: &Context) -> Option<Self> {
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<Vec2> {

4
egui/src/containers/resize.rs

@ -18,11 +18,11 @@ pub(crate) struct State {
impl State {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
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);
}
}

4
egui/src/containers/scroll_area.rs

@ -43,11 +43,11 @@ impl Default for State {
impl State {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
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);
}
}

70
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<Fonts>> {
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<Style> {
self.memory().options.style.clone()
self.options().style.clone()
}
/// The [`Style`] used by all new windows, panels etc.
@ -548,7 +573,7 @@ impl Context {
/// ctx.set_style(style);
/// ```
pub fn set_style(&self, style: impl Into<Arc<Style>>) {
self.memory().options.style = style.into();
self.options().style = style.into();
}
/// The [`Visuals`] used by all subsequent windows, panels etc.
@ -561,7 +586,7 @@ impl Context {
/// ctx.set_visuals(egui::Visuals::light()); // Switch to light mode
/// ```
pub fn set_visuals(&self, visuals: crate::Visuals) {
std::sync::Arc::make_mut(&mut self.memory().options.style).visuals = visuals;
std::sync::Arc::make_mut(&mut self.options().style).visuals = visuals;
}
/// The number of physical pixels for each logical point.
@ -747,7 +772,7 @@ impl Context {
// shapes are the same, but just comparing the shapes takes about 50% of the time
// it takes to tessellate them, so it is not a worth optimization.
let mut tessellation_options = self.memory().options.tessellation_options;
let mut tessellation_options = *self.tessellation_options();
tessellation_options.pixels_per_point = self.pixels_per_point();
tessellation_options.aa_size = 1.0 / self.pixels_per_point();
let paint_stats = PaintStats::from_shapes(&shapes);
@ -877,12 +902,12 @@ impl Context {
/// Wether or not to debug widget layout on hover.
pub fn debug_on_hover(&self) -> bool {
self.memory().options.style.debug.debug_on_hover
self.options().style.debug.debug_on_hover
}
/// Turn on/off wether or not to debug widget layout on hover.
pub fn set_debug_on_hover(&self, debug_on_hover: bool) {
let mut style = (*self.memory().options.style).clone();
let mut style = (*self.options().style).clone();
style.debug.debug_on_hover = debug_on_hover;
self.set_style(style);
}
@ -956,10 +981,10 @@ impl Context {
CollapsingHeader::new("✒ Painting")
.default_open(true)
.show(ui, |ui| {
let mut tessellation_options = self.memory().options.tessellation_options;
let mut tessellation_options = self.options().tessellation_options;
tessellation_options.ui(ui);
ui.vertical_centered(|ui| reset_button(ui, &mut tessellation_options));
self.memory().options.tessellation_options = tessellation_options;
*self.tessellation_options() = tessellation_options;
});
}
@ -1104,8 +1129,8 @@ impl Context {
*self.memory() = Default::default();
}
let num_state = self.memory().data.len();
let num_serialized = self.memory().data.count_serialized();
let num_state = self.data().len();
let num_serialized = self.data().count_serialized();
ui.label(format!(
"{} widget states stored (of which {} are serialized).",
num_state, num_serialized
@ -1149,13 +1174,10 @@ impl Context {
ui.horizontal(|ui| {
ui.label(format!(
"{} collapsing headers",
self.memory()
.data
.count::<containers::collapsing_header::State>()
self.data().count::<containers::collapsing_header::State>()
));
if ui.button("Reset").clicked() {
self.memory()
.data
self.data()
.remove_by_type::<containers::collapsing_header::State>();
}
});
@ -1163,30 +1185,30 @@ impl Context {
ui.horizontal(|ui| {
ui.label(format!(
"{} menu bars",
self.memory().data.count::<menu::BarState>()
self.data().count::<menu::BarState>()
));
if ui.button("Reset").clicked() {
self.memory().data.remove_by_type::<menu::BarState>();
self.data().remove_by_type::<menu::BarState>();
}
});
ui.horizontal(|ui| {
ui.label(format!(
"{} scroll areas",
self.memory().data.count::<scroll_area::State>()
self.data().count::<scroll_area::State>()
));
if ui.button("Reset").clicked() {
self.memory().data.remove_by_type::<scroll_area::State>();
self.data().remove_by_type::<scroll_area::State>();
}
});
ui.horizontal(|ui| {
ui.label(format!(
"{} resize areas",
self.memory().data.count::<resize::State>()
self.data().count::<resize::State>()
));
if ui.button("Reset").clicked() {
self.memory().data.remove_by_type::<resize::State>();
self.data().remove_by_type::<resize::State>();
}
});

4
egui/src/grid.rs

@ -9,11 +9,11 @@ pub(crate) struct State {
impl State {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
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);
}
fn set_min_col_width(&mut self, col: usize, width: f32) {

2
egui/src/memory.rs

@ -423,7 +423,7 @@ impl Memory {
/// Popups are things like combo-boxes, color pickers, menus etc.
/// Only one can be be open at a time.
impl Memory {
pub fn is_popup_open(&mut self, popup_id: Id) -> bool {
pub fn is_popup_open(&self, popup_id: Id) -> bool {
self.popup == Some(popup_id) || self.everything_is_visible()
}

7
egui/src/menu.rs

@ -30,14 +30,11 @@ pub(crate) struct BarState {
impl BarState {
fn load(ctx: &Context, bar_id: Id) -> Self {
ctx.memory()
.data
.get_temp::<Self>(bar_id)
.unwrap_or_default()
ctx.data().get_temp::<Self>(bar_id).unwrap_or_default()
}
fn store(self, ctx: &Context, bar_id: Id) {
ctx.memory().data.insert_temp(bar_id, self);
ctx.data().insert_temp(bar_id, self);
}
/// Show a menu at pointer if primary-clicked response.

12
egui/src/ui.rs

@ -338,21 +338,27 @@ impl Ui {
self.ctx().input()
}
/// The `Memory` of the `Context` associated with the `Ui`.
/// The [`Memory`] of the [`Context`] associated with this ui.
/// Equivalent to `.ctx().memory()`.
#[inline]
pub fn memory(&self) -> RwLockWriteGuard<'_, Memory> {
self.ctx().memory()
}
/// The `Output` of the `Context` associated with the `Ui`.
/// Stores superficial widget state.
#[inline]
pub fn data(&self) -> RwLockWriteGuard<'_, crate::util::IdTypeMap> {
self.ctx().data()
}
/// The [`Output`] of the [`Context`] associated with this ui.
/// Equivalent to `.ctx().output()`.
#[inline]
pub fn output(&self) -> RwLockWriteGuard<'_, Output> {
self.ctx().output()
}
/// The `Fonts` of the `Context` associated with the `Ui`.
/// The [`Fonts`] of the [`Context`] associated with this ui.
/// Equivalent to `.ctx().fonts()`.
#[inline]
pub fn fonts(&self) -> RwLockReadGuard<'_, Fonts> {

2
egui/src/widgets/color_picker.rs

@ -429,5 +429,5 @@ fn color_cache_set(ctx: &Context, rgba: impl Into<Rgba>, hsva: Hsva) {
// To ensure we keep hue slider when `srgba` is gray we store the full `Hsva` in a cache:
fn use_color_cache<R>(ctx: &Context, f: impl FnOnce(&mut FixedCache<Rgba, Hsva>) -> R) -> R {
f(ctx.memory().data.get_temp_mut_or_default(Id::null()))
f(ctx.data().get_temp_mut_or_default(Id::null()))
}

4
egui/src/widgets/plot/mod.rs

@ -39,11 +39,11 @@ struct PlotMemory {
impl PlotMemory {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
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);
}
}

4
egui/src/widgets/text_edit/state.rs

@ -34,11 +34,11 @@ pub struct TextEditState {
impl TextEditState {
pub fn load(ctx: &Context, id: Id) -> Option<Self> {
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);
}
/// The the currently selected range of characters.

4
egui_demo_lib/src/apps/demo/password.rs

@ -19,7 +19,7 @@ pub fn password_ui(ui: &mut egui::Ui, password: &mut String) -> egui::Response {
// Get state for this widget.
// You should get state by value, not by reference to avoid borrowing of `Memory`.
let mut show_plaintext = ui.memory().data.get_temp::<bool>(state_id).unwrap_or(false);
let mut show_plaintext = ui.data().get_temp::<bool>(state_id).unwrap_or(false);
// Process ui, change a local copy of the state
// We want TextEdit to fill entire space, and have button after that, so in that case we can
@ -42,7 +42,7 @@ pub fn password_ui(ui: &mut egui::Ui, password: &mut String) -> egui::Response {
});
// Store the (possibly changed) state:
ui.memory().data.insert_temp(state_id, show_plaintext);
ui.data().insert_temp(state_id, show_plaintext);
// All done! Return the interaction response so the user can check what happened
// (hovered, clicked, …) and maybe show a tooltip:

4
egui_demo_lib/src/backend_panel.rs

@ -127,9 +127,9 @@ impl BackendPanel {
ui.separator();
{
let mut screen_reader = ui.ctx().memory().options.screen_reader;
let mut screen_reader = ui.ctx().options().screen_reader;
ui.checkbox(&mut screen_reader, "🔈 Screen reader").on_hover_text("Experimental feature: checking this will turn on the screen reader on supported platforms");
ui.ctx().memory().options.screen_reader = screen_reader;
ui.ctx().options().screen_reader = screen_reader;
}
if !frame.is_web() {

19
egui_demo_lib/src/syntax_highlighting.rs

@ -146,13 +146,11 @@ impl CodeTheme {
pub fn from_memory(ctx: &egui::Context) -> Self {
if ctx.style().visuals.dark_mode {
ctx.memory()
.data
ctx.data()
.get_persisted(egui::Id::new("dark"))
.unwrap_or_else(CodeTheme::dark)
} else {
ctx.memory()
.data
ctx.data()
.get_persisted(egui::Id::new("light"))
.unwrap_or_else(CodeTheme::light)
}
@ -160,13 +158,9 @@ impl CodeTheme {
pub fn store_in_memory(self, ctx: &egui::Context) {
if self.dark_mode {
ctx.memory()
.data
.insert_persisted(egui::Id::new("dark"), self);
ctx.data().insert_persisted(egui::Id::new("dark"), self);
} else {
ctx.memory()
.data
.insert_persisted(egui::Id::new("light"), self);
ctx.data().insert_persisted(egui::Id::new("light"), self);
}
}
}
@ -237,8 +231,7 @@ impl CodeTheme {
ui.horizontal_top(|ui| {
let selected_id = egui::Id::null();
let mut selected_tt: TokenType = *ui
.memory()
.data
.data()
.get_persisted_mut_or(selected_id, TokenType::Comment);
ui.vertical(|ui| {
@ -281,7 +274,7 @@ impl CodeTheme {
ui.add_space(16.0);
ui.memory().data.insert_persisted(selected_id, selected_tt);
ui.data().insert_persisted(selected_id, selected_tt);
egui::Frame::group(ui.style())
.margin(egui::Vec2::splat(2.0))

2
egui_web/src/backend.rs

@ -239,7 +239,7 @@ impl AppRunner {
}
fn handle_egui_output(&mut self, output: egui::Output) -> egui::TexturesDelta {
if self.egui_ctx.memory().options.screen_reader {
if self.egui_ctx.options().screen_reader {
self.screen_reader.speak(&output.events_description());
}

Loading…
Cancel
Save