From eccf90701faace2800d41e055bee27ed81d849c9 Mon Sep 17 00:00:00 2001 From: Konkitoman Date: Sun, 13 Aug 2023 16:20:02 +0300 Subject: [PATCH] add: `Context::get_viewport_id_by_name`, `Context::get_viewport_parent_id_by_name`, `Context::input_for` and `Context::input_mut_for` --- crates/egui/src/context.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 85cba1acb..187b99430 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -580,12 +580,24 @@ impl Context { }) } + /// This will create a `InputState::default()` if there is no input state for that viewport + #[inline] + pub fn input_for(&self, id: ViewportId, reader: impl FnOnce(&InputState) -> R) -> R { + self.read(move |ctx| reader(ctx.input.get(&id).unwrap_or(&Default::default()))) + } + /// Read-write access to [`InputState`]. #[inline] pub fn input_mut(&self, writer: impl FnOnce(&mut InputState) -> R) -> R { self.write(move |ctx| writer(ctx.input.entry(ctx.get_viewport_id()).or_default())) } + /// This will create a `InputState::default()` if there is no input state for that viewport + #[inline] + pub fn input_mut_for(&self, id: ViewportId, writer: impl FnOnce(&mut InputState) -> R) -> R { + self.write(move |ctx| writer(ctx.input.entry(id).or_default())) + } + /// Read-only access to [`Memory`]. #[inline] pub fn memory(&self, reader: impl FnOnce(&Memory) -> R) -> R { @@ -1576,7 +1588,7 @@ impl Context { // --------------------------------------------------------------------- - /// Position and size of the egui area. + /// Position and size of the current viewport. /// min is the position, max is the size pub fn screen_rect(&self) -> Rect { self.input(|i| i.screen_rect()) @@ -2167,6 +2179,14 @@ impl Context { self.read(|ctx| ctx.get_parent_viewport_id()) } + pub fn get_viewport_id_by_name(&self, name: &str) -> Option { + self.read(|ctx| ctx.viewports.get(name).map(|v| v.1)) + } + + pub fn get_viewport_parent_id_by_name(&self, name: &str) -> Option { + self.read(|ctx| ctx.viewports.get(name).map(|v| v.1)) + } + /// This should only be used by the backend! /// /// When a viewport sync is created will be rendered by this function