From f9e5cdc18c083646aa49091bc67a85a4a077eff5 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 25 Jan 2024 11:28:29 +0100 Subject: [PATCH] Register `LabelSelectionState` like a plugin --- crates/egui/src/context.rs | 3 +-- .../egui/src/text_selection/label_text_selection.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 51ad7113a62..9c35edcecab 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -599,6 +599,7 @@ impl Default for Context { // Register built-in plugins: crate::debug_text::register(&ctx); + crate::text_selection::LabelSelectionState::register(&ctx); ctx } @@ -665,7 +666,6 @@ impl Context { /// ``` pub fn begin_frame(&self, new_input: RawInput) { crate::profile_function!(); - crate::text_selection::LabelSelectionState::begin_frame(self); self.read(|ctx| ctx.plugins.clone()).on_begin_frame(self); self.write(|ctx| ctx.begin_frame_mut(new_input)); } @@ -1680,7 +1680,6 @@ impl Context { crate::gui_zoom::zoom_with_keyboard(self); } - crate::text_selection::LabelSelectionState::end_frame(self); self.read(|ctx| ctx.plugins.clone()).on_end_frame(self); self.write(|ctx| ctx.end_frame()) diff --git a/crates/egui/src/text_selection/label_text_selection.rs b/crates/egui/src/text_selection/label_text_selection.rs index 45730375ba7..5685bd8b7fe 100644 --- a/crates/egui/src/text_selection/label_text_selection.rs +++ b/crates/egui/src/text_selection/label_text_selection.rs @@ -145,6 +145,14 @@ impl Default for LabelSelectionState { } impl LabelSelectionState { + pub(crate) fn register(ctx: &Context) { + ctx.on_begin_frame( + "LabelSelectionState", + std::sync::Arc::new(Self::begin_frame), + ); + ctx.on_end_frame("LabelSelectionState", std::sync::Arc::new(Self::end_frame)); + } + pub fn load(ctx: &Context) -> Self { ctx.data(|data| data.get_temp::(Id::NULL)) .unwrap_or_default() @@ -156,7 +164,7 @@ impl LabelSelectionState { }); } - pub fn begin_frame(ctx: &Context) { + fn begin_frame(ctx: &Context) { let mut state = Self::load(ctx); if ctx.input(|i| i.pointer.any_pressed() && !i.modifiers.shift) { @@ -177,7 +185,7 @@ impl LabelSelectionState { state.store(ctx); } - pub fn end_frame(ctx: &Context) { + fn end_frame(ctx: &Context) { let mut state = Self::load(ctx); if state.is_dragging {