1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 07:03:14 -04:00

add: Context::get_viewport_id_by_name, Context::get_viewport_parent_id_by_name, Context::input_for and Context::input_mut_for

This commit is contained in:
Konkitoman
2023-08-13 16:20:02 +03:00
parent 4bbdab1788
commit eccf90701f

View File

@@ -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<R>(&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<R>(&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<R>(&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<R>(&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<ViewportId> {
self.read(|ctx| ctx.viewports.get(name).map(|v| v.1))
}
pub fn get_viewport_parent_id_by_name(&self, name: &str) -> Option<ViewportId> {
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