Skip to content

Commit

Permalink
Register LabelSelectionState like a plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 25, 2024
1 parent ca82fcf commit f9e5cdc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 1 addition & 2 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ impl Default for Context {

// Register built-in plugins:
crate::debug_text::register(&ctx);
crate::text_selection::LabelSelectionState::register(&ctx);

ctx
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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())
Expand Down
12 changes: 10 additions & 2 deletions crates/egui/src/text_selection/label_text_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>(Id::NULL))
.unwrap_or_default()
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit f9e5cdc

Please sign in to comment.