From 59f1fba035a39168d0504ce31725354eb7fd5921 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 28 Sep 2023 18:41:55 +0200 Subject: [PATCH] Report edits to user with Behavior::on_edit --- src/behavior.rs | 6 ++++++ src/container/grid.rs | 4 ++++ src/container/linear.rs | 4 ++++ src/container/tabs.rs | 3 +++ src/tree.rs | 1 + 5 files changed, 18 insertions(+) diff --git a/src/behavior.rs b/src/behavior.rs index a1eb8d8..89f5994 100644 --- a/src/behavior.rs +++ b/src/behavior.rs @@ -287,6 +287,12 @@ pub trait Behavior { fn ideal_tile_aspect_ratio(&self) -> f32 { 4.0 / 3.0 } + + // Callbacks: + + /// Called if the user edits the tree somehow, e.g. changes the size of some container, + /// clicks a tab, or drags a tile. + fn on_edit(&mut self) {} } /// How many columns should we use to fit `n` children in a grid? diff --git a/src/container/grid.rs b/src/container/grid.rs index 72c7f00..0204209 100644 --- a/src/container/grid.rs +++ b/src/container/grid.rs @@ -397,12 +397,16 @@ fn resize_interaction( let right = i + 1; if splitter_response.double_clicked() { + behavior.on_edit(); + // double-click to center the split between left and right: let mean = 0.5 * (shares[left] + shares[right]); shares[left] = mean; shares[right] = mean; ResizeState::Hovering } else if splitter_response.dragged() { + behavior.on_edit(); + if dx < 0.0 { // Expand right, shrink stuff to the left: shares[right] += shrink_shares( diff --git a/src/container/linear.rs b/src/container/linear.rs index c1bae92..8792ff2 100644 --- a/src/container/linear.rs +++ b/src/container/linear.rs @@ -374,12 +374,16 @@ fn resize_interaction( tile_width: impl Fn(TileId) -> f32, ) -> ResizeState { if splitter_response.double_clicked() { + behavior.on_edit(); + // double-click to center the split between left and right: let mean = 0.5 * (shares[left] + shares[right]); shares[left] = mean; shares[right] = mean; ResizeState::Hovering } else if splitter_response.dragged() { + behavior.on_edit(); + if dx < 0.0 { // Expand right, shrink stuff to the left: shares[right] += shrink_shares( diff --git a/src/container/tabs.rs b/src/container/tabs.rs index b97f5c1..72bdd8f 100644 --- a/src/container/tabs.rs +++ b/src/container/tabs.rs @@ -126,6 +126,7 @@ impl Tabs { .on_hover_cursor(egui::CursorIcon::Grab) .drag_started() { + behavior.on_edit(); ui.memory_mut(|mem| mem.set_dragged_id(tile_id.egui_id())); } } @@ -144,6 +145,7 @@ impl Tabs { behavior.tab_ui(&tree.tiles, ui, id, child_id, selected, is_being_dragged); let response = response.on_hover_cursor(egui::CursorIcon::Grab); if response.clicked() { + behavior.on_edit(); next_active = Some(child_id); } @@ -152,6 +154,7 @@ impl Tabs { && response.rect.contains(mouse_pos) { // Expand this tab - maybe the user wants to drop something into it! + behavior.on_edit(); next_active = Some(child_id); } } diff --git a/src/tree.rs b/src/tree.rs index 577be4d..92605d9 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -304,6 +304,7 @@ impl Tree { if ui.input(|i| i.pointer.any_released()) { ui.memory_mut(|mem| mem.stop_dragging()); if let Some(insertion_point) = drop_context.best_insertion { + behavior.on_edit(); self.move_tile(dragged_tile_id, insertion_point); } clear_smooth_preview_rect(ui.ctx(), dragged_tile_id);