Skip to content

Commit

Permalink
implement panels (Adanos020#164) against 0.12 with sensible drag/drop…
Browse files Browse the repository at this point in the history
… behavior
  • Loading branch information
iostat committed Mar 18, 2024
1 parent d835b47 commit 3f23c8c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/widgets/dock_area/drag_and_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ enum LockState {
/// Lock remains locked, but can be unlocked.
SoftLock,

/// Lock is locked forever.
/// Lock is locked forever.
HardLock,
}

Expand All @@ -162,6 +162,7 @@ impl DragDropState {
ui: &Ui,
style: &Style,
allowed_splits: AllowedSplits,
insert_allowed: bool,
windows_allowed: bool,
window_bounds: Rect,
) -> Option<TabDestination> {
Expand All @@ -188,7 +189,7 @@ impl DragDropState {
let center = rect.center();
let rect = Rect::from_center_size(center, Vec2::splat(shortest_side));

if button_ui(rect, ui, &mut hovering_buttons, pointer, style, None) {
if insert_allowed && button_ui(rect, ui, &mut hovering_buttons, pointer, style, None) {
match self.hover.dst {
TreeComponent::Node(surface, node) => {
destination = Some(TabDestination::Node(surface, node, TabInsert::Append))
Expand Down Expand Up @@ -243,6 +244,7 @@ impl DragDropState {
ui: &Ui,
style: &Style,
allowed_splits: AllowedSplits,
insert_allowed: bool,
windows_allowed: bool,
window_bounds: Rect,
) -> Option<TabDestination> {
Expand Down Expand Up @@ -287,10 +289,10 @@ impl DragDropState {
);

// Find out what kind of tab insertion (if any) should be used to move this widget.
if center_drop_rect.contains(a_pos) {
if insert_allowed && center_drop_rect.contains(a_pos) {
(Some(TabInsert::Append), Rect::EVERYTHING)
} else if window_drop_rect.contains(a_pos) {
match windows_allowed {
match windows_allowed || !insert_allowed {
true => (None, Rect::NOTHING),
false => (Some(TabInsert::Append), Rect::EVERYTHING),
}
Expand Down
21 changes: 14 additions & 7 deletions src/widgets/dock_area/show/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
fade_style: Option<(&Style, f32)>,
) {
assert!(self.dock_state[surface_index][node_index].is_leaf());
let hide_tab_bar = self.dock_state[surface_index][node_index]
.iter_tabs()
.all(|tab| tab_viewer.is_fixed_panel(tab));

let rect = self.dock_state[surface_index][node_index]
.rect()
Expand All @@ -40,13 +43,17 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
ui.spacing_mut().item_spacing = Vec2::ZERO;
ui.set_clip_rect(rect);

let tabbar_rect = self.tab_bar(
ui,
state,
(surface_index, node_index),
tab_viewer,
fade_style.map(|(style, _)| style),
);
let tabbar_rect = if hide_tab_bar {
Rect::ZERO
} else {
self.tab_bar(
ui,
state,
(surface_index, node_index),
tab_viewer,
fade_style.map(|(style, _)| style),
)
};
self.tab_body(
ui,
state,
Expand Down
10 changes: 10 additions & 0 deletions src/widgets/dock_area/show/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
let drag_state = state.dnd.as_mut().unwrap();
let style = self.style.as_ref().unwrap();

let dst_is_fixed_panel = match drag_state.hover.dst.node_address() {
(dst_surf, Some(dst_node)) => self.dock_state[dst_surf][dst_node]
.iter_tabs()
.all(|tab| tab_viewer.is_fixed_panel(tab)),
_ => false,
};
let insert_allowed = !dst_is_fixed_panel;

let deserted_node = {
match (
drag_state.drag.src.node_address(),
Expand Down Expand Up @@ -231,6 +239,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
ui,
style,
allowed_splits,
insert_allowed,
allowed_in_window,
window_bounds,
)
Expand All @@ -239,6 +248,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
ui,
style,
allowed_splits,
insert_allowed,
allowed_in_window,
window_bounds,
)
Expand Down
8 changes: 8 additions & 0 deletions src/widgets/tab_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,12 @@ pub trait TabViewer {
fn scroll_bars(&self, _tab: &Self::Tab) -> [bool; 2] {
[true, true]
}

/// Returns `true` if the tab can be considered a "fixed panel"
///
/// If there is only a single fixed panel in a docking area, no tab bar
/// will be shown, and the panel thus can't be dragged/moved.
fn is_fixed_panel(&self, _tab: &Self::Tab) -> bool {
false
}
}

0 comments on commit 3f23c8c

Please sign in to comment.