Skip to content

Commit

Permalink
Report edits to user with Behavior::on_edit (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored Sep 28, 2023
1 parent 6daa444 commit b34432b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ pub trait Behavior<Pane> {
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?
Expand Down
4 changes: 4 additions & 0 deletions src/container/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,16 @@ fn resize_interaction<Pane>(
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(
Expand Down
4 changes: 4 additions & 0 deletions src/container/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,16 @@ fn resize_interaction<Pane>(
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(
Expand Down
3 changes: 3 additions & 0 deletions src/container/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Expand All @@ -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);
}

Expand All @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ impl<Pane> Tree<Pane> {
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);
Expand Down

0 comments on commit b34432b

Please sign in to comment.