mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 19:14:26 +00:00
Dock UI: icon groups register the drop spots of expanded groups; groups in a floating window take no side drops
This commit is contained in:
@@ -18,7 +18,7 @@ use crate::runtime::env::Label;
|
||||
use crate::runtime::focus::{self, FocusTarget};
|
||||
use crate::style::{ComponentStyle, Corners, StyleCx, SurfaceKind};
|
||||
use crate::tabs::{Tab, TabKind, TabStrip, TornOff};
|
||||
use craft_behavior::{IconId, Metrics, PartKey, PressPhase, Sense, keyed};
|
||||
use craft_behavior::{DockSide, IconId, Metrics, PartKey, PressPhase, Sense, keyed};
|
||||
use craft_theme::{ControlSize, Tokens};
|
||||
use egui::{Id, Pos2, Rect, Response, Ui, Vec2};
|
||||
use std::borrow::Cow;
|
||||
@@ -205,8 +205,9 @@ struct Reported {
|
||||
/// A group of panels: a header of tab strip and panel menu button, dragged by its empty part, over the active panel's
|
||||
/// body. Show it with `show_at(group_rect)` in the body of a `SplitArea`, with an `IdSource::Id` made from the
|
||||
/// group's anchor so its state survives a re-dock. Under a `dock` id it registers the spots a drop lands on: the
|
||||
/// strip, and (while expanded) the sides and the middle of the body. The group is an instance of the `dockGroup`
|
||||
/// command scope: it publishes the panel command states it is given and reports the commands that reach it.
|
||||
/// strip, and (while expanded) the sides and the middle of the body; in a floating window
|
||||
/// ([`in_window`](DockGroup::in_window)) the top, the bottom and the middle only. The group is an instance of the
|
||||
/// `dockGroup` command scope: it publishes the panel command states it is given and reports the commands that reach it.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(container, overlay, probe(DockGroup<'static, usize>))]
|
||||
pub struct DockGroup<'a, P: Clone + Hash + Send + Sync + 'static> {
|
||||
@@ -235,6 +236,10 @@ pub struct DockGroup<'a, P: Clone + Hash + Send + Sync + 'static> {
|
||||
/// How the body's side zones are sized.
|
||||
#[prop]
|
||||
zones: ZoneRule,
|
||||
/// The group is in a floating window, which is one column with nothing beside it: the body's start and end
|
||||
/// sides take no drop, and its top, bottom and middle zones reach across them.
|
||||
#[prop(flag)]
|
||||
in_window: bool,
|
||||
/// The rows of the group menu: the panel menu button and a context request on a tab open them.
|
||||
#[prop]
|
||||
menu: Vec<PanelRow>,
|
||||
@@ -271,6 +276,7 @@ impl<'a, P: Clone + Hash + Send + Sync + 'static> DockGroup<'a, P> {
|
||||
wheel_selects_tabs: false,
|
||||
dock: None,
|
||||
zones: ZoneRule::Fraction,
|
||||
in_window: false,
|
||||
menu: Vec::new(),
|
||||
panel_menu_shown: true,
|
||||
command_states: Vec::new(),
|
||||
@@ -394,6 +400,11 @@ impl<P: Clone + Hash + Send + Sync + 'static> Component for DockGroup<'_, P> {
|
||||
}
|
||||
let body = Rect::from_min_max(Pos2::new(rect.left(), strip.bottom()), rect.max);
|
||||
for (side, zone) in self.zones.zones(body, &cx.style.sizes, rtl) {
|
||||
let zone = match side {
|
||||
DockSide::Start | DockSide::End if self.in_window => continue,
|
||||
_ if self.in_window => Rect::from_x_y_ranges(body.x_range(), zone.y_range()),
|
||||
_ => zone,
|
||||
};
|
||||
DockTargets::<P>::register_in(&cx.core.ctx, dock, DockSpot::group(self.anchor.clone(), side), zone, rect);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,9 +150,10 @@ pub struct IconicColumnStyle {
|
||||
/// outside close the flyout; with `auto_collapse` the focus leaving it does too.
|
||||
///
|
||||
/// The open flyout is the bound `Option<P>` of [`new`](IconicColumn::new), owned by the host like the layout: a click
|
||||
/// on an icon, Escape and auto-collapse write it, and the host opens a panel (the Window menu) by writing it. The column registers the drop bands of its edges and of the halves of
|
||||
/// each icon group with the dock `dock`. The flyout shows its group through the `view` of
|
||||
/// [`show_flyouts`](IconicColumn::show_flyouts), with the opened panel active, as tall as the view measures the
|
||||
/// on an icon, Escape and auto-collapse write it, and the host opens a panel (the Window menu) by writing it. The
|
||||
/// column registers its drop spots with the dock `dock` as an expanded column does: its edges, the seams between its
|
||||
/// icon groups, the top and bottom of the stack and each icon group's cells, which join its tabs. The flyout shows
|
||||
/// its group through the `view` of [`show_flyouts`](IconicColumn::show_flyouts), with the opened panel active, as tall as the view measures the
|
||||
/// group within the room and at least the height of its panels' default heights; a group the user sized keeps that
|
||||
/// size. The flyout's top is level with its group's icons, shifted by the group's `flyout_offset`, inside `within`.
|
||||
/// The user resizes it by its top and bottom edges and the edge away from the column; each step comes back in
|
||||
@@ -619,24 +620,41 @@ impl<P: Value + Hash + 'static> Component for IconicColumn<'_, P> {
|
||||
}
|
||||
|
||||
impl<P: Value + Hash + 'static> IconicColumn<'_, P> {
|
||||
/// Registers the drop bands of a dock column's edges, and the upper and lower half of every icon group: a drop on
|
||||
/// the upper half stacks above the group, one on the lower half below it.
|
||||
/// Registers the drop spots of the column the way an expanded column registers them: the bands of a dock
|
||||
/// column's two edges (a new column beside it), the seam on the separator between two icon groups (a new group
|
||||
/// between them), the top of the first group and the bottom of the last down to the column's foot (a new group
|
||||
/// above or below), and the cells of each icon group (join its tabs). A floating column registers no edge bands:
|
||||
/// nothing goes beside it.
|
||||
fn register(&self, ctx: &egui::Context, rect: Rect, rtl: bool, plan: &Plan, sizes: &IconicSizes) {
|
||||
let Some(first) = self.column.groups.first().and_then(|group| group.panels.first()) else { return };
|
||||
let anchor = |group: usize| self.column.groups.get(group).and_then(|group| group.panels.first());
|
||||
let Some(first) = anchor(0) else { return };
|
||||
if self.bar == ColumnBar::Own {
|
||||
register_edges(ctx, self.dock, first, rect, rtl, &sizes.column);
|
||||
}
|
||||
let half = sizes.separator * 0.5;
|
||||
let last = plan.groups.len().saturating_sub(1);
|
||||
let half = sizes.column.seam_band * 0.5;
|
||||
// How far the top and bottom bands reach into a group's cells: a quarter of a short group leaves its middle
|
||||
// to the join.
|
||||
let reach = |group: &Rect| half.min(group.height() * 0.25);
|
||||
let mut above: Option<(&P, Rect)> = None;
|
||||
for (at, group) in &plan.groups {
|
||||
let Some(anchor) = self.column.groups.get(*at).and_then(|group| group.panels.first()) else { continue };
|
||||
let middle = group.center().y;
|
||||
let upper_top = if *at == 0 { plan.body.top() } else { group.top() - half };
|
||||
let lower_bottom = if *at == last { plan.body.bottom() } else { group.bottom() + half };
|
||||
let upper = Rect::from_x_y_ranges(group.x_range(), upper_top..=middle);
|
||||
let lower = Rect::from_x_y_ranges(group.x_range(), middle..=lower_bottom);
|
||||
DockTargets::register(ctx, self.dock, DockSpot::group(anchor.clone(), DockSide::Top), upper);
|
||||
DockTargets::register(ctx, self.dock, DockSpot::group(anchor.clone(), DockSide::Bottom), lower);
|
||||
let Some(panel) = anchor(*at) else { continue };
|
||||
DockTargets::register(ctx, self.dock, DockSpot::group(panel.clone(), DockSide::Center), *group);
|
||||
if let Some((before, over)) = above {
|
||||
let middle = (over.bottom() + group.top()) * 0.5;
|
||||
let seam = Rect::from_x_y_ranges(group.x_range(), (middle - half)..=(middle + half));
|
||||
DockTargets::register(ctx, self.dock, DockSpot::seam(before.clone(), panel.clone()), seam);
|
||||
}
|
||||
above = Some((panel, *group));
|
||||
}
|
||||
if let Some((_, top)) = plan.groups.first() {
|
||||
let band = Rect::from_x_y_ranges(top.x_range(), plan.body.top()..=(top.top() + reach(top)));
|
||||
DockTargets::register_in(ctx, self.dock, DockSpot::group(first.clone(), DockSide::Top), band, *top);
|
||||
}
|
||||
if let Some((at, bottom)) = plan.groups.last()
|
||||
&& let Some(last) = anchor(*at)
|
||||
{
|
||||
let band = Rect::from_x_y_ranges(bottom.x_range(), (bottom.bottom() - reach(bottom))..=plan.body.bottom().max(bottom.bottom()));
|
||||
DockTargets::register_in(ctx, self.dock, DockSpot::group(last.clone(), DockSide::Bottom), band, *bottom);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
#![allow(clippy::panic, clippy::expect_used, reason = "trials assert by panicking")]
|
||||
|
||||
use crate::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use crate::docking::{DockHeights, DockSizes, DragUnit, FlyoutResize, GroupAt, GroupGrip, IconicColumn, PanelKey, PanelSpec};
|
||||
use crate::docking::{DockHeights, DockSizes, DockSpot, DockTargets, DragUnit, FlyoutResize, GroupAt, GroupGrip, IconicColumn, PanelKey, PanelSpec};
|
||||
use crate::sections::Panel;
|
||||
use crate::style::StyleCx;
|
||||
use crate::testing::memory::global;
|
||||
use crate::testing::{Fixture, trial};
|
||||
use craft_behavior::DockSide;
|
||||
use craft_behavior::dock::{Action, Column, DockEdge, Group, Layout, Node};
|
||||
use egui::{Pos2, Rect, Vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
@@ -336,3 +337,40 @@ fn a_flyout_with_an_offset_in_a_short_viewport_stays_inside_and_resizes_its_widt
|
||||
assert!(bounds.contains_rect(after), "{height}: {after:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The icon groups take drops as an expanded column's groups do: the middle of a group joins its tabs, the separator
|
||||
/// between two groups is the seam between them and wins there, the top of the first group stacks above it, and the
|
||||
/// bottom of the last group, down through the column's empty foot, stacks below it.
|
||||
#[trial]
|
||||
fn icon_groups_take_the_drops_of_an_expanded_columns_groups() {
|
||||
let dock = global("iconic column", "spots");
|
||||
let probes: Rc<RefCell<Vec<Pos2>>> = Rc::default();
|
||||
let found: Rc<RefCell<Vec<Option<DockSpot<u8>>>>> = Rc::default();
|
||||
let (asked, seen) = (Rc::clone(&probes), Rc::clone(&found));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut column = Column::new(vec![Group::new(vec![1, 2], 0), Group::new(vec![3], 0)]);
|
||||
column.iconic = true;
|
||||
let place = Rect::from_min_size(Pos2::ZERO, Vec2::new(RAIL, SCREEN.y));
|
||||
IconicColumn::new("Panels", dock, &column, Bind::kept(), &named).within(screen()).show_flyouts_at(ui, place, |_, _, _, _| None);
|
||||
// The registry a drop reads is the previous pass's.
|
||||
*seen.borrow_mut() = asked.borrow().iter().map(|at| DockTargets::<u8>::resolve(ui.ctx(), dock, *at).map(|(spot, _)| spot)).collect();
|
||||
});
|
||||
fixture.room(SCREEN).settle();
|
||||
let icon = |label: &str| fixture.by_label(label).expect("the panel's icon").rect;
|
||||
let (brushes, layers, swatches) = (icon("Brushes"), icon("Layers"), icon("Swatches"));
|
||||
let x = brushes.center().x;
|
||||
*probes.borrow_mut() = vec![
|
||||
Pos2::new(x, brushes.bottom()),
|
||||
Pos2::new(x, (layers.bottom() + swatches.top()) / 2.0),
|
||||
Pos2::new(x, brushes.top() + 1.0),
|
||||
Pos2::new(x, swatches.center().y),
|
||||
Pos2::new(x, swatches.bottom() + 2.0 * swatches.height()),
|
||||
];
|
||||
fixture.idle(2);
|
||||
let found = found.borrow().clone();
|
||||
assert_eq!(found[0], Some(DockSpot::group(1, DockSide::Center)), "the middle of a group joins its tabs");
|
||||
assert_eq!(found[1], Some(DockSpot::seam(1, 3)), "the separator is the seam between the groups");
|
||||
assert_eq!(found[2], Some(DockSpot::group(1, DockSide::Top)), "the first group's top stacks above it");
|
||||
assert_eq!(found[3], Some(DockSpot::group(3, DockSide::Center)), "a one-icon group keeps its middle for the join");
|
||||
assert_eq!(found[4], Some(DockSpot::group(3, DockSide::Bottom)), "the foot under the icons stacks below the last group");
|
||||
}
|
||||
|
||||
@@ -87,6 +87,13 @@ fn two_panels(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
DockGroup::new(LAYERS, &panels, panels[0].key).show(ui, |_, _| ()).output.into()
|
||||
}
|
||||
|
||||
/// A group in a floating window: nothing goes beside it, so a drop on its body joins it or stacks above or below.
|
||||
#[story(DockGroup<'static, usize>, covers = [Default])]
|
||||
fn a_group_in_a_window(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
let panels = titles(&[LAYERS, CHANNELS]);
|
||||
DockGroup::new(LAYERS, &panels, panels[0].key).in_window().show(ui, |_, _| ()).output.into()
|
||||
}
|
||||
|
||||
/// A click on the panel menu button opens the group's panel menu.
|
||||
fn open_panel_menu(fixture: &mut Fixture) {
|
||||
if let Some(button) = fixture.root_id().and_then(|id| fixture.part(id, MENU)) {
|
||||
|
||||
@@ -267,11 +267,13 @@ struct Passed<P> {
|
||||
files: Vec<drag::OsFile>,
|
||||
}
|
||||
|
||||
/// How a group is shown: minimized, and whether it is an accordion section (with its open state).
|
||||
/// How a group is shown: minimized, whether it is an accordion section (with its open state), and whether it is in
|
||||
/// a floating window, where nothing goes beside it.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Look {
|
||||
minimized: bool,
|
||||
section: Option<bool>,
|
||||
in_window: bool,
|
||||
}
|
||||
|
||||
/// A dock to show: its window edge, its columns and its rect.
|
||||
@@ -662,7 +664,7 @@ impl<P: Clone + Eq + Hash + Debug + Send + Sync + 'static> Pass<'_, P> {
|
||||
let scene = self.scene;
|
||||
match node {
|
||||
Node::Tabs { panels: members, active, .. } => {
|
||||
self.group(ui, body, members, *active, Look { minimized: false, section: None }, GroupAt::Show(rect));
|
||||
self.group(ui, body, members, *active, Look { minimized: false, section: None, in_window: false }, GroupAt::Show(rect));
|
||||
}
|
||||
Node::Stack { entries, .. } => {
|
||||
let sections: Vec<StackSection> = entries
|
||||
@@ -678,7 +680,7 @@ impl<P: Clone + Eq + Hash + Debug + Send + Sync + 'static> Pass<'_, P> {
|
||||
for (entry, height) in entries.iter().zip(&heights) {
|
||||
let section = Rect::from_min_size(Pos2::new(rect.left(), top), Vec2::new(rect.width(), *height));
|
||||
top += height;
|
||||
let look = Look { minimized: !entry.open, section: Some(entry.open) };
|
||||
let look = Look { minimized: !entry.open, section: Some(entry.open), in_window: false };
|
||||
self.group(ui, inner, std::slice::from_ref(&entry.panel), 0, look, GroupAt::Show(section));
|
||||
}
|
||||
});
|
||||
@@ -813,6 +815,9 @@ impl<P: Clone + Eq + Hash + Debug + Send + Sync + 'static> Pass<'_, P> {
|
||||
.menu(menu.clone())
|
||||
.panel_menu_shown(hosted)
|
||||
.command_states(states.clone());
|
||||
if look.in_window {
|
||||
group = group.in_window();
|
||||
}
|
||||
if minimized {
|
||||
group = group.minimized();
|
||||
} else if !document(&scene.spec(&members[at])) {
|
||||
@@ -887,8 +892,9 @@ impl<P: Clone + Eq + Hash + Debug + Send + Sync + 'static> Pass<'_, P> {
|
||||
None
|
||||
}
|
||||
|
||||
fn group_of(&mut self, ui: &mut Ui, body: &mut BodyCx<'_>, group: &Group<P>, at: GroupAt) -> Option<f32> {
|
||||
let look = Look { minimized: group.minimized, section: None };
|
||||
/// A group of a column, docked or in a floating window (`in_window`).
|
||||
fn group_of(&mut self, ui: &mut Ui, body: &mut BodyCx<'_>, group: &Group<P>, at: GroupAt, in_window: bool) -> Option<f32> {
|
||||
let look = Look { minimized: group.minimized, section: None, in_window };
|
||||
self.group(ui, body, &group.panels, group.active, look, at)
|
||||
}
|
||||
|
||||
@@ -919,13 +925,14 @@ impl<P: Clone + Eq + Hash + Debug + Send + Sync + 'static> Pass<'_, P> {
|
||||
let name = Label::from(body.words::<DockWords>().panels.as_str().to_owned());
|
||||
let dock = self.dock;
|
||||
let ColumnAt { cell, edge, bar, within, open } = at;
|
||||
let in_window = bar == ColumnBar::Window;
|
||||
let work = if column.iconic {
|
||||
let output = IconicColumn::new(name, dock, column, Bind::given(open), &spec)
|
||||
.edge(edge)
|
||||
.bar(bar)
|
||||
.within(within)
|
||||
.auto_collapse(scene.auto_collapse)
|
||||
.show_flyouts_at(ui, cell, |group, at, ui, body| self.group_of(ui, body, group, at));
|
||||
.show_flyouts_at(ui, cell, |group, at, ui, body| self.group_of(ui, body, group, at, in_window));
|
||||
self.report.flyout.extend(output.edit.events);
|
||||
ColumnWork { iconic: output.iconic, picked: output.picked, resized: Vec::new(), width: output.width, flyout: output.flyout }
|
||||
} else {
|
||||
@@ -935,7 +942,7 @@ impl<P: Clone + Eq + Hash + Debug + Send + Sync + 'static> Pass<'_, P> {
|
||||
.within(within)
|
||||
.auto_collapse(scene.auto_collapse)
|
||||
.show_groups_at(ui, cell, |group, rect, ui, body| {
|
||||
self.group_of(ui, body, group, GroupAt::Show(rect));
|
||||
self.group_of(ui, body, group, GroupAt::Show(rect), in_window);
|
||||
});
|
||||
ColumnWork { iconic: output.iconic, picked: output.picked, resized: output.resized, width: output.width, flyout: None }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user