Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report edits to user with Behavior::on_edit #29

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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