mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
Dock: closable panel tabs stay panel tabs
A group with a closable panel made its strip TabKind::Document, so its tabs overflowed whole instead of shrinking to their first letters. TabKind::Panel shows a close button on the tabs the host makes closable and closes them on a middle click (TabKind::closes_by_default: only document tabs close unless the host says so), and a dock group's strip is always a panel strip. - A middle press picks no row in a collection that senses MIDDLE_CLICK: there the middle click is the row's own action, so closing a panel tab does not show it first. - Tab's least width holds the tail, icon and close button beside the first letters, as `place` lays them out, so a shrunk tab keeps its letters.
This commit is contained in:
@@ -84,10 +84,12 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
_ => None,
|
||||
};
|
||||
// Under `AutoScroll` a middle press on the rows starts or ends the latch (the items step's `middle_scroll`) and
|
||||
// picks no row.
|
||||
// picks no row. In a collection that senses `MIDDLE_CLICK` the middle click is the row's own action
|
||||
// (`CollectionEvent::MiddleClick`: a tab strip closes the tab), so the press picks no row there either.
|
||||
let middle_clicks = cx.core.root_sense.contains(craft_behavior::Sense::MIDDLE_CLICK);
|
||||
if let PressPhase::Down { start, .. } = cx.core.press.phase
|
||||
&& local(start.origin).y >= 0.0
|
||||
&& !(start.button == PointerButton::Middle && props.middle_button == MiddleButton::AutoScroll)
|
||||
&& !(start.button == PointerButton::Middle && (props.middle_button == MiddleButton::AutoScroll || middle_clicks))
|
||||
{
|
||||
// A second press, slow or not, ends the wait of the first.
|
||||
self.drags.slow = None;
|
||||
|
||||
@@ -625,8 +625,9 @@ impl<P: Clone + PartialEq + Debug + Hash + Send + Sync + 'static> Component for
|
||||
cx.core.memory.shown::<MenuAsked>(sub(cx.core.id, MENU_ASKED)).0 = Some(request.at);
|
||||
}
|
||||
let grip = cx.part_if_declared(GRIP).map(|grip| (grip.rect, crate::runtime::access::sub_node_id(cx.core.id, GRIP.into())));
|
||||
let Reported { picked, closed, picked_document, closed_documents, menu, item, commands, toggle_minimized, torn_off, drag_started, tabs, .. } =
|
||||
self.reported;
|
||||
let Reported {
|
||||
picked, closed, picked_document, closed_documents, menu, item, commands, toggle_minimized, torn_off, drag_started, tabs, ..
|
||||
} = self.reported;
|
||||
DockGroupOutput {
|
||||
response: cx.response.clone(),
|
||||
picked,
|
||||
@@ -898,12 +899,13 @@ fn tab_row<P: Clone + PartialEq + Debug + Hash + Send + Sync + 'static>(row: &mu
|
||||
let drags = crate::runtime::drag::started(ui.ctx());
|
||||
let tabs = strip_tabs(panels, closable);
|
||||
let source = keyed(&tabs, |tab: &StripTab<'_>| tab.key).with_text(|tab| Cow::Borrowed(tab.title.as_str()));
|
||||
let kind = if tabs.iter().any(|tab| tab.closes) { TabKind::Document } else { TabKind::Panel };
|
||||
let tabs = &tabs;
|
||||
let documents: Vec<PanelKey> = tabs.iter().filter(|tab| tab.document).map(|tab| tab.key).collect();
|
||||
// A press on a panel tab leaves key focus where it is, as in Photoshop: the document's keys keep working.
|
||||
let mut strip = TabStrip::new(name)
|
||||
.kind(kind)
|
||||
// Panel tabs whether or not they close: they shrink to their first letters, and the closable ones show a close
|
||||
// button.
|
||||
.kind(TabKind::Panel)
|
||||
.keeps_focus()
|
||||
.panel_look(row.look)
|
||||
.shown(shown_tab(panels, active))
|
||||
|
||||
@@ -293,3 +293,40 @@ fn a_press_on_an_app_drag_widget_in_the_body_is_no_surface_press() {
|
||||
fixture.click_at(Pos2::new(rect.center().x, rect.bottom() + 80.0)).settle();
|
||||
assert!(surface.get(), "a press on the empty body is the group's surface press");
|
||||
}
|
||||
|
||||
// Closable panel tabs stay panel tabs: in a group too narrow for their whole names every tab keeps its place in the
|
||||
// strip, the shown one its whole name, and each shows a close button. A middle click closes an inactive tab without
|
||||
// showing it first.
|
||||
#[trial]
|
||||
fn closable_panel_tabs_shrink_and_close_on_a_middle_click() {
|
||||
let (picked, closed) = (Rc::new(Cell::new(None::<PanelKey>)), Rc::new(Cell::new(None::<PanelKey>)));
|
||||
let (picks, closes) = (Rc::clone(&picked), Rc::clone(&closed));
|
||||
let names = ["Character", "Paragraph", "Glyphs"];
|
||||
let panels: Vec<PanelTitle> = (0..3).map(|at| PanelTitle::new(PanelKey::new(at as u64 + 1), names[at]).closable(true)).collect();
|
||||
let keys: Vec<PanelKey> = panels.iter().map(|panel| panel.key).collect();
|
||||
let group_rect = Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(300.0, 200.0));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let out = DockGroup::new(1usize, Titles::from_content(panels.clone()), panels[0].key).closable().show_at(ui, group_rect, |_, _| ());
|
||||
if let Some(key) = out.output.picked {
|
||||
picks.set(Some(key));
|
||||
}
|
||||
if let Some(key) = out.output.closed.first() {
|
||||
closes.set(Some(*key));
|
||||
}
|
||||
});
|
||||
fixture.settle();
|
||||
let tabs: Vec<_> = names.iter().map(|name| fixture.by_label(name).unwrap_or_else(|| panic!("the {name} tab stays in the strip"))).collect();
|
||||
for (name, tab) in names.iter().zip(&tabs) {
|
||||
assert!(group_rect.contains_rect(tab.rect), "the {name} tab {:?} lies inside the group", tab.rect);
|
||||
assert!(fixture.by_label(&format!("Close {name}")).is_some(), "the {name} tab shows a close button");
|
||||
}
|
||||
// Paragraph's name is as long as Character's: shrunk beside the shown tab, which keeps its whole name.
|
||||
assert!(tabs[1].rect.width() < tabs[0].rect.width() - 0.5, "the other tabs shrink and the shown tab keeps its whole name");
|
||||
assert!(fixture.by_label("More tabs").is_none(), "no tab goes to the overflow menu while its first letters fit");
|
||||
let at = tabs[2].center();
|
||||
fixture.frame(vec![pointer::moved(at)]);
|
||||
fixture.frame(vec![pointer::middle(at, true)]);
|
||||
fixture.frame(vec![pointer::middle(at, false)]).settle();
|
||||
assert_eq!(closed.get(), Some(keys[2]), "the middle click closes the tab");
|
||||
assert_eq!(picked.get(), None, "the middle click does not show the tab first");
|
||||
}
|
||||
|
||||
@@ -41,7 +41,8 @@ pub enum TabKind {
|
||||
/// may give them PdfCraft's look ([`DocumentTabLook::Shrinking`]).
|
||||
#[default]
|
||||
Document,
|
||||
/// Panel tabs: tabs shrink before they overflow, no close button, only the hidden tabs in the overflow menu.
|
||||
/// Panel tabs: tabs shrink to their first letters before they overflow, only the hidden tabs in the overflow menu,
|
||||
/// and a close button only on the tabs the host makes closable (`Tab::closable`, `TabStrip::can_close`).
|
||||
Panel,
|
||||
/// Text tabs: only the labels, the selected one in the text colour and the others dimmed, no tab surface,
|
||||
/// divider or close button. Whole titles, every tab in the overflow menu (a row of categories in a dialog).
|
||||
@@ -146,11 +147,16 @@ impl TabKind {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether closable tabs show a close button on the tab; a panel's Close is in the host's context menu.
|
||||
pub fn shows_close(self) -> bool {
|
||||
/// Whether a tab closes unless the host says otherwise (`Tab::closable`, `TabStrip::can_close`): document tabs.
|
||||
pub fn closes_by_default(self) -> bool {
|
||||
self == Self::Document
|
||||
}
|
||||
|
||||
/// Whether closable tabs show a close button on the tab: document and panel tabs.
|
||||
pub fn shows_close(self) -> bool {
|
||||
matches!(self, Self::Document | Self::Panel)
|
||||
}
|
||||
|
||||
/// Whether tabs draw only their label, with no surface, indicator or strip fill: text and ribbon tabs.
|
||||
pub fn text_only(self) -> bool {
|
||||
matches!(self, Self::Text | Self::Ribbon)
|
||||
@@ -161,9 +167,9 @@ impl TabKind {
|
||||
self == Self::Ribbon
|
||||
}
|
||||
|
||||
/// Whether a middle click closes a closable tab.
|
||||
/// Whether a middle click closes a closable tab: document and panel tabs.
|
||||
pub fn middle_click_closes(self) -> bool {
|
||||
self == Self::Document
|
||||
matches!(self, Self::Document | Self::Panel)
|
||||
}
|
||||
|
||||
/// Whether only a primary press shows a tab: a secondary click on a document tab opens its menu and a middle click
|
||||
|
||||
@@ -546,8 +546,9 @@ pub struct TabStrip<'a, K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + '
|
||||
/// Which tabs may be dragged.
|
||||
#[prop(func = "Fn(&K) -> bool")]
|
||||
can_drag: Option<RowTest<'a, K>>,
|
||||
/// Which tabs a middle click closes, where the kind closes on one (`TabKind::middle_click_closes`): every tab
|
||||
/// when unset. A host with tabs that cannot close (`Tab::closable(false)`) names the ones that can.
|
||||
/// Which tabs a middle click closes, where the kind closes on one (`TabKind::middle_click_closes`). Unset, the
|
||||
/// kind's default (`TabKind::closes_by_default`): every document tab, no panel tab. A host whose tabs differ from
|
||||
/// that default (`Tab::closable`) names the ones that close.
|
||||
#[prop(func = "Fn(&K) -> bool")]
|
||||
can_close: Option<RowTest<'a, K>>,
|
||||
/// The role of each tab, an input of the fit: contextual tabs are kept ahead of plain tabs.
|
||||
@@ -987,7 +988,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Component f
|
||||
|
||||
fn finish(self, mut cx: Cx<'_, Self>) -> TabStripOutput<K> {
|
||||
let tab_rects = self.tab_rects(&cx);
|
||||
let TabStrip { picked, torn, wheeled, empty_drag, empty_double, can_close, .. } = self;
|
||||
let TabStrip { picked, torn, wheeled, empty_drag, empty_double, can_close, kind, .. } = self;
|
||||
let mut response = cx.collection_response();
|
||||
let (mut closed, mut menu) = (Vec::new(), None);
|
||||
let mut kept = Vec::with_capacity(response.events.len());
|
||||
@@ -995,7 +996,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Component f
|
||||
match event {
|
||||
CollectionEvent::Item(key, TabPartEvent::Close) => closed.push(key),
|
||||
CollectionEvent::MiddleClick(key) => {
|
||||
if can_close.as_ref().is_none_or(|test| test.passes(&key)) {
|
||||
if can_close.as_ref().map_or(kind.closes_by_default(), |test| test.passes(&key)) {
|
||||
closed.push(key);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ impl ItemSource<usize> for Documents<'_> {}
|
||||
/// A strip of `count` tabs; a closed tab leaves the story's list.
|
||||
fn strip(ui: &mut Ui, data: &mut StoryData, strip: TabStrip<'_, usize>, titles: &'static [&'static str], count: usize, kind: TabKind) -> StoryOutput {
|
||||
let open = data.value("open", (0..count).collect::<Vec<usize>>());
|
||||
let out = strip.kind(kind).show_items(ui, &Documents { open, titles }, &mut |key, _| Tab::new(titles[*key]).closable(true));
|
||||
let out = strip.kind(kind).show_items(ui, &Documents { open, titles }, &mut |key, _| Tab::new(titles[*key]));
|
||||
open.retain(|key| !out.closed.contains(key));
|
||||
out.into()
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
//!
|
||||
//! Selection, the lead, the focus ring and the drag ghost belong to the collection that shows the tab; the tab draws
|
||||
//! the look the item step gives it and reports what its own parts do. The strip gives the tab a rect: the tab's
|
||||
//! measure is its natural width (`Proposal::Min` the room for the first letters of its title), and a narrower rect cuts the label
|
||||
//! first, then the detail, and shows the whole text as a tooltip.
|
||||
//! measure is its natural width (`Proposal::Min` the room for the first letters of its title beside its icon and close
|
||||
//! button), and a narrower rect cuts the label first, then the detail, and shows the whole text as a tooltip.
|
||||
|
||||
use super::rules::{CloseSide, PanelTabLook, TabKind, close_side};
|
||||
use super::words::TabWords;
|
||||
@@ -261,7 +261,7 @@ impl<'a> Tab<'a> {
|
||||
|
||||
/// Whether the tab can be closed.
|
||||
fn can_close(&self) -> bool {
|
||||
self.closable.unwrap_or(self.kind.shows_close())
|
||||
self.closable.unwrap_or(self.kind.closes_by_default())
|
||||
}
|
||||
|
||||
/// Whether the tab shows a close button.
|
||||
@@ -486,23 +486,28 @@ impl Component for Tab<'_> {
|
||||
let sizes = &cx.style.sizes;
|
||||
let shown = self.shown_label(cx.style, cx.resolved.label);
|
||||
let label: &str = &shown;
|
||||
// What `place` takes before the label: the tail, the icon and the close button, each with its gap.
|
||||
let mut parts = sizes.tail;
|
||||
if self.icon.is_some() {
|
||||
parts += sizes.icon + sizes.gap;
|
||||
}
|
||||
if self.shows_close() {
|
||||
parts += sizes.gap + sizes.close_box;
|
||||
}
|
||||
// The first letters of the title: the least a shrunk tab shows.
|
||||
let letters = || {
|
||||
let first: String = label.chars().take(MIN_LETTERS).collect();
|
||||
cx.layout_at(&self.label_run(cx.style, &first), f32::INFINITY).size().x
|
||||
};
|
||||
if cx.proposal == Proposal::Min && !cx.style.shrinking {
|
||||
return Vec2::new(letters(), sizes.label_line);
|
||||
return Vec2::new(parts + letters(), sizes.label_line);
|
||||
}
|
||||
let width = |run: &TextRun<'_>| cx.layout_at(run, f32::INFINITY).size().x;
|
||||
let label_width = width(&self.label_run(cx.style, label));
|
||||
// A document tab never takes more than a name of `MAX_NAME_DIGITS` digits' width, as production's tabs cut a long
|
||||
// name short so the other tabs and the Open control keep their room; the label is cut with an ellipsis.
|
||||
let capped = if cx.style.shrinking { label_width.min(width(&self.label_run(cx.style, &"0".repeat(MAX_NAME_DIGITS)))) } else { label_width };
|
||||
let mut natural = sizes.tail + capped;
|
||||
if self.icon.is_some() {
|
||||
natural += sizes.icon + sizes.gap;
|
||||
}
|
||||
let mut natural = parts + capped;
|
||||
if let Some(detail) = self.detail_text() {
|
||||
natural += sizes.gap + width(&TextRun::new(detail).role(cx.style.detail.role));
|
||||
}
|
||||
@@ -512,14 +517,11 @@ impl Component for Tab<'_> {
|
||||
if self.dotted(cx.style) {
|
||||
natural += sizes.gap + cx.style.marker;
|
||||
}
|
||||
if self.shows_close() {
|
||||
natural += sizes.gap + sizes.close_box;
|
||||
}
|
||||
let measured = if cx.proposal == Proposal::Min {
|
||||
let end = label.char_indices().nth(12).map_or(label.len(), |(index, _)| index);
|
||||
let prefix = label.get(..end).unwrap_or(label);
|
||||
let readable = natural - capped + width(&self.label_run(cx.style, prefix));
|
||||
readable.max(letters()).min(natural)
|
||||
readable.max(parts + letters()).min(natural)
|
||||
} else {
|
||||
natural
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user