Skip to content
Draft
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
10 changes: 9 additions & 1 deletion src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use egui::{
vec2,
};

use super::{ResizeState, SimplificationOptions, Tile, TileId, Tiles, UiResponse};
use super::{PreviewOptions, ResizeState, SimplificationOptions, Tile, TileId, Tiles, UiResponse};

/// The kind of edit that triggered the call to [`Behavior::on_edit`].
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -270,6 +270,14 @@ pub trait Behavior<Pane> {
32.0
}

/// Options controlling the animated drag preview.
///
/// Set [`PreviewOptions::enabled`] to `false` to disable the animated
/// preview and show only a simple highlighted drop zone.
fn preview_options(&self) -> PreviewOptions {
PreviewOptions::default()
}

/// Show we preview panes that are being dragged,
/// i.e. show their ui in the region where they will end up?
fn preview_dragged_panes(&self) -> bool {
Expand Down
18 changes: 10 additions & 8 deletions src/container/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,22 @@ impl Grid {
);
}

self.resize_columns(&tree.tiles, behavior, ui, tile_id);
self.resize_rows(&tree.tiles, behavior, ui, tile_id);
if !tree.is_previewing() {
self.resize_columns(tree, behavior, ui, tile_id);
self.resize_rows(tree, behavior, ui, tile_id);
}
}

fn resize_columns<Pane>(
&mut self,
tiles: &Tiles<Pane>,
tree: &Tree<Pane>,
behavior: &mut dyn Behavior<Pane>,
ui: &egui::Ui,
parent_id: TileId,
) {
let resizable = behavior.is_container_resizable(tiles, parent_id);
let resizable = behavior.is_container_resizable(&tree.tiles, parent_id);

let parent_rect = tiles.rect_or_die(parent_id);
let parent_rect = tree.display_rect_or_die(parent_id);
for (i, (left, right)) in self.col_ranges.iter().copied().tuple_windows().enumerate() {
let resize_id = ui.id().with((parent_id, "resize_col", i));

Expand Down Expand Up @@ -328,14 +330,14 @@ impl Grid {

fn resize_rows<Pane>(
&mut self,
tiles: &Tiles<Pane>,
tree: &Tree<Pane>,
behavior: &mut dyn Behavior<Pane>,
ui: &egui::Ui,
parent_id: TileId,
) {
let resizable = behavior.is_container_resizable(tiles, parent_id);
let resizable = behavior.is_container_resizable(&tree.tiles, parent_id);

let parent_rect = tiles.rect_or_die(parent_id);
let parent_rect = tree.display_rect_or_die(parent_id);
for (i, (top, bottom)) in self.row_ranges.iter().copied().tuple_windows().enumerate() {
let resize_id = ui.id().with((parent_id, "resize_row", i));

Expand Down
20 changes: 14 additions & 6 deletions src/container/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,18 @@ impl Linear {
// ------------------------
// resizing:

if tree.is_previewing() {
return;
}

let resizable = behavior.is_container_resizable(&tree.tiles, parent_id);

let parent_rect = tree.tiles.rect_or_die(parent_id);
let parent_rect = tree.display_rect_or_die(parent_id);
for (i, (left, right)) in visible_children.iter().copied().tuple_windows().enumerate() {
let resize_id = ui.id().with((parent_id, "resize", i));

let left_rect = tree.tiles.rect_or_die(left);
let right_rect = tree.tiles.rect_or_die(right);
let left_rect = tree.display_rect_or_die(left);
let right_rect = tree.display_rect_or_die(right);
let x = egui::lerp(left_rect.right()..=right_rect.left(), 0.5);

if !resizable {
Expand Down Expand Up @@ -328,14 +332,18 @@ impl Linear {
// ------------------------
// resizing:

if tree.is_previewing() {
return;
}

let resizable = behavior.is_container_resizable(&tree.tiles, parent_id);

let parent_rect = tree.tiles.rect_or_die(parent_id);
let parent_rect = tree.display_rect_or_die(parent_id);
for (i, (top, bottom)) in visible_children.iter().copied().tuple_windows().enumerate() {
let resize_id = ui.id().with((parent_id, "resize", i));

let top_rect = tree.tiles.rect_or_die(top);
let bottom_rect = tree.tiles.rect_or_die(bottom);
let top_rect = tree.display_rect_or_die(top);
let bottom_rect = tree.display_rect_or_die(bottom);
let y = egui::lerp(top_rect.bottom()..=bottom_rect.top(), 0.5);

if !resizable {
Expand Down
26 changes: 21 additions & 5 deletions src/container/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,22 @@ impl Tabs {

ui.spacing_mut().item_spacing.x = 0.0; // Tabs have spacing built-in

for (i, &child_id) in self.children.iter().enumerate() {
let tab_children = drop_context
.dragged_tile_id
.and_then(|_| tree.preview_tab_children(tile_id).cloned())
.unwrap_or_else(|| self.children.clone());

for (i, &child_id) in tab_children.iter().enumerate() {
if !tree.is_visible(child_id) {
continue;
}

let is_being_dragged = is_being_dragged(ui, tree.id, child_id);

let selected = self.is_active(child_id);
let selected = drop_context
.dragged_tile_id
.and_then(|_| tree.is_preview_active_tab(tile_id, child_id))
.unwrap_or_else(|| self.is_active(child_id));
let id = child_id.egui_id(tree.id);
let tab_state = TabState {
active: selected,
Expand Down Expand Up @@ -334,11 +342,19 @@ impl Tabs {
// -----------
// Drop zones:

let drop_children = drop_context
.dragged_tile_id
.and_then(|_| tree.preview_tab_children(tile_id).cloned())
.unwrap_or_else(|| self.children.clone());

let preview_thickness = 6.0;
let after_rect = |rect: Rect| {
let dragged_size = if let Some(dragged_index) = dragged_index {
// We actually know the size of this thing
button_rects[&self.children[dragged_index]].size()
drop_children
.get(dragged_index)
.and_then(|id| button_rects.get(id))
.map(|r| r.size())
.unwrap_or_else(|| rect.size())
} else {
rect.size() // guess that the size is the same as the last button
};
Expand All @@ -349,7 +365,7 @@ impl Tabs {
};
super::linear::drop_zones(
preview_thickness,
&self.children,
&drop_children,
dragged_index,
super::LinearDir::Horizontal,
|tile_id| button_rects.get(&tile_id).copied(),
Expand Down
28 changes: 25 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub use behavior::{Behavior, EditAction, TabState};
pub use container::{Container, ContainerKind, Grid, GridLayout, Linear, LinearDir, Shares, Tabs};
pub use tile::{Tile, TileId};
pub use tiles::Tiles;
pub use tree::Tree;
pub use tree::{PreviewOptions, Tree};

// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -255,7 +255,7 @@ impl ContainerInsertion {
}

/// Where in the tree to insert a tile.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct InsertionPoint {
pub parent_id: TileId,

Expand Down Expand Up @@ -298,7 +298,8 @@ fn cover_tile_if_dragged<Pane>(
tile_id: TileId,
) {
if is_being_dragged(ui, tree.id, tile_id)
&& let Some(child_rect) = tree.tiles.rect(tile_id)
&& !tree.is_previewing()
&& let Some(child_rect) = tree.display_rect(tile_id)
{
let overlay_color = behavior.dragged_overlay_color(ui.visuals());
ui.painter().rect_filled(child_rect, 0.0, overlay_color);
Expand Down Expand Up @@ -378,3 +379,24 @@ impl DropContext {
}
}
}

pub(crate) struct MoveJournal {
/// Tiles displaced by wrapping operations (`original_id`, `displaced_to_id`).
displaced_tiles: Vec<(TileId, TileId)>,
}

impl MoveJournal {
fn new() -> Self {
Self {
displaced_tiles: Vec::new(),
}
}

fn record_displaced_tile(&mut self, original_id: TileId, displaced_to_id: TileId) {
self.displaced_tiles.push((original_id, displaced_to_id));
}

pub(crate) fn displaced_tiles(&self) -> &[(TileId, TileId)] {
&self.displaced_tiles
}
}
32 changes: 27 additions & 5 deletions src/tiles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use egui::{Pos2, Rect};

use crate::MoveJournal;

use super::{
Behavior, Container, ContainerInsertion, ContainerKind, GcAction, Grid, InsertionPoint, Linear,
LinearDir, SimplificationOptions, SimplifyAction, Tabs, Tile, TileId,
Expand Down Expand Up @@ -261,7 +263,20 @@ impl<Pane> Tiles<Pane> {
self.parent_of(tile_id).is_none()
}

pub(super) fn insert_at(&mut self, insertion_point: InsertionPoint, inserted_id: TileId) {
pub(super) fn next_tile_id(&self) -> u64 {
self.next_tile_id
}

pub(super) fn set_next_tile_id(&mut self, id: u64) {
self.next_tile_id = id;
}

pub(super) fn insert_at(
&mut self,
insertion_point: InsertionPoint,
inserted_id: TileId,
journal: &mut MoveJournal,
) {
let InsertionPoint {
parent_id,
insertion,
Expand All @@ -281,6 +296,7 @@ impl<Pane> Tiles<Pane> {
self.tiles.insert(parent_id, parent_tile);
} else {
let new_tile_id = self.insert_new(parent_tile);
journal.record_displaced_tile(parent_id, new_tile_id);
let mut tabs = Tabs::new(vec![new_tile_id]);
tabs.children.insert(index.min(1), inserted_id);
tabs.set_active(inserted_id);
Expand All @@ -291,15 +307,17 @@ impl<Pane> Tiles<Pane> {
ContainerInsertion::Horizontal(index) => {
if let Tile::Container(Container::Linear(Linear {
dir: LinearDir::Horizontal,
children,
ref mut children,
..
})) = &mut parent_tile
})) = parent_tile
{
let index = index.min(children.len());
children.insert(index, inserted_id);
self.tiles.insert(parent_id, parent_tile);
} else {
let new_tile_id = self.insert_new(parent_tile);
journal.record_displaced_tile(parent_id, new_tile_id);

let mut linear = Linear::new(LinearDir::Horizontal, vec![new_tile_id]);
linear.children.insert(index.min(1), inserted_id);
self.tiles
Expand All @@ -309,15 +327,17 @@ impl<Pane> Tiles<Pane> {
ContainerInsertion::Vertical(index) => {
if let Tile::Container(Container::Linear(Linear {
dir: LinearDir::Vertical,
children,
ref mut children,
..
})) = &mut parent_tile
})) = parent_tile
{
let index = index.min(children.len());
children.insert(index, inserted_id);
self.tiles.insert(parent_id, parent_tile);
} else {
let new_tile_id = self.insert_new(parent_tile);
journal.record_displaced_tile(parent_id, new_tile_id);

let mut linear = Linear::new(LinearDir::Vertical, vec![new_tile_id]);
linear.children.insert(index.min(1), inserted_id);
self.tiles
Expand All @@ -330,6 +350,8 @@ impl<Pane> Tiles<Pane> {
self.tiles.insert(parent_id, parent_tile);
} else {
let new_tile_id = self.insert_new(parent_tile);
journal.record_displaced_tile(parent_id, new_tile_id);

let grid = Grid::new(vec![new_tile_id, inserted_id]);
self.tiles
.insert(parent_id, Tile::Container(Container::Grid(grid)));
Expand Down
Loading
Loading