Skip to content

Commit

Permalink
Add ui.data(), ctx.data(), ctx.options() and ctx.tessellation_options()
Browse files Browse the repository at this point in the history
Helpful access deeper into Memory
  • Loading branch information
emilk committed Jan 29, 2022
1 parent 3333d63 commit ef057ce
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
* 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 `ui.data()`, `ctx.data()`, `ctx.options()` and `ctx.tessellation_options()`.

### Changed 🔧
* ⚠️ `Context::input` and `Ui::input` now locks a mutex. This can lead to a dead-lock is used in an `if let` binding!
Expand Down
2 changes: 1 addition & 1 deletion egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
4 changes: 2 additions & 2 deletions egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
4 changes: 2 additions & 2 deletions egui/src/containers/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
46 changes: 41 additions & 5 deletions egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, *};

// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -500,9 +513,32 @@ impl Context {
})
}

#[inline]
fn fonts_mut(&self) -> RwLockWriteGuard<'_, Option<Fonts>> {
RwLockWriteGuard::map(self.write(), |c| &mut c.fonts)
}

#[inline]
pub fn options(&self) -> RwLockReadGuard<'_, Options> {
RwLockReadGuard::map(self.read(), |c| &c.memory.options)
}

#[inline]
pub fn options_mut(&self) -> RwLockWriteGuard<'_, Options> {
RwLockWriteGuard::map(self.write(), |c| &mut c.memory.options)
}

/// Read the options used by the tessellator.
#[inline]
pub fn tessellation_options(&self) -> RwLockReadGuard<'_, TessellationOptions> {
RwLockReadGuard::map(self.read(), |c| &c.memory.options.tessellation_options)
}

/// Change the options used by the tessellator.
#[inline]
pub fn tessellation_options_mut(&self) -> RwLockWriteGuard<'_, TessellationOptions> {
RwLockWriteGuard::map(self.write(), |c| &mut c.memory.options.tessellation_options)
}
}

impl Context {
Expand Down Expand Up @@ -877,7 +913,7 @@ 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.
Expand Down Expand Up @@ -959,7 +995,7 @@ impl Context {
let mut tessellation_options = self.memory().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_mut() = tessellation_options;
});
}

Expand Down
4 changes: 2 additions & 2 deletions egui/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion egui/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl BarState {
}

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.
Expand Down
12 changes: 9 additions & 3 deletions egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion egui/src/widgets/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 2 deletions egui/src/widgets/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions egui/src/widgets/text_edit/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions egui_demo_lib/src/apps/demo/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion egui_demo_lib/src/backend_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl BackendPanel {
{
let mut screen_reader = ui.ctx().memory().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_mut().screen_reader = screen_reader;
}

if !frame.is_web() {
Expand Down
2 changes: 1 addition & 1 deletion egui_demo_lib/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,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))
Expand Down
2 changes: 1 addition & 1 deletion egui_web/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down

0 comments on commit ef057ce

Please sign in to comment.