mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 16:59:56 +01:00
Panel tabs clip and overflow: drop the ellipsis shrink
Panel tabs keep the framework's clip and overflow: they shrink to their first letters with the label clipped at the tab's edge, the shown tab whole, and the tabs that do not fit go to the strip's overflow menu. PanelTabShrink::Ellipsis goes with the option that chose it: DockConfig::tab_shrink, the group's tab_shrink, TabStrip::panel_shrink, Tab::panel_shrink, FitMode::ShrinkToLeast, TabKind::fit_mode_for and clips_label_in, and craft_behavior::fit::fit_to_least with its docs. Trials: the two ellipsis trials and the helpers they alone used are removed with the option.
This commit is contained in:
1 parent
2e7fcfacc8
commit
cac8725f59
11 files changed
+22
-313
No files matched your search
@@ -1,7 +1,6 @@
|
||||
//! The fit rules: which tabs, bar items or titles stay in a strip and which go to an overflow menu. [`fit`],
|
||||
//! [`fit_to_least`] and [`fit_whole`] assign tab widths (tabs shrink before they overflow, or keep their width);
|
||||
//! [`fit_items`] shortens items that have a short form, then drops command items and titles from the end. All of them
|
||||
//! read no egui types.
|
||||
//! The fit rules: which tabs, bar items or titles stay in a strip and which go to an overflow menu. [`fit`] and
|
||||
//! [`fit_whole`] assign tab widths (tabs shrink before they overflow, or keep their width); [`fit_items`] shortens
|
||||
//! items that have a short form, then drops command items and titles from the end. All of them read no egui types.
|
||||
|
||||
/// Tabs visible on a strip and tabs that need an overflow menu, all in original order.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
@@ -176,51 +175,6 @@ pub fn fit_whole(natural: &[f32], selected: usize, kept: &[bool], available: f32
|
||||
TabFit { shown, overflow, overflow_width }
|
||||
}
|
||||
|
||||
/// Fit tab widths into `available` points, each tab shrinking to its own `least` before any tab overflows.
|
||||
/// Every tab shrinks, the selected one too: the widest tabs shrink first to a common cap, and each keeps at least
|
||||
/// `min(natural, least)` of its own. Only when the tabs do not all fit at their least is an overflow button
|
||||
/// `overflow_width` wide reserved; the selected tab stays, the others follow in order while their least widths fit
|
||||
/// beside it, and the rest overflow, the last ones first. A selected tab whose least alone is wider than the strip
|
||||
/// less the button takes the room left. A tab past the end of `least` keeps its natural width as its least.
|
||||
///
|
||||
/// Dimensions are sanitized as in [`fit`], and the shown widths plus `overflow_width` never exceed the sanitized
|
||||
/// available width. Work and storage are linear in the input count (with at most 32 fitting passes).
|
||||
pub fn fit_to_least(natural: &[f32], least: &[f32], selected: usize, available: f32, overflow_width: f32) -> TabFit {
|
||||
let available = tab_extent(available);
|
||||
let natural: Vec<f32> = natural.iter().map(|&w| tab_extent(w)).collect();
|
||||
let floors: Vec<f32> = natural.iter().enumerate().map(|(i, &w)| least.get(i).map_or(w, |&least| w.min(tab_extent(least)))).collect();
|
||||
let all: Vec<usize> = (0..natural.len()).collect();
|
||||
let total = |widths: &[f32]| widths.iter().map(|&w| f64::from(w)).sum::<f64>();
|
||||
if total(natural.as_slice()) <= f64::from(available) {
|
||||
return TabFit { shown: all.into_iter().zip(natural).collect(), ..Default::default() };
|
||||
}
|
||||
// One tab has nowhere to overflow; all available space belongs to it.
|
||||
if natural.len() == 1 {
|
||||
return TabFit { shown: vec![(0, available)], ..Default::default() };
|
||||
}
|
||||
let floor = |i: usize| floors.get(i).copied().unwrap_or(0.0);
|
||||
if total(floors.as_slice()) <= f64::from(available) {
|
||||
return TabFit { shown: squeeze_floored(&natural, &all, available, 0.0, floor), ..Default::default() };
|
||||
}
|
||||
let overflow_width = tab_extent(overflow_width).min(available);
|
||||
let budget = available - overflow_width;
|
||||
let selected = selected.min(natural.len().saturating_sub(1));
|
||||
let mut used = f64::from(floor(selected));
|
||||
let mut open = true;
|
||||
let (shown, overflow): (Vec<usize>, Vec<usize>) = all.into_iter().partition(|&i| {
|
||||
if i == selected {
|
||||
return true;
|
||||
}
|
||||
open = open && used + f64::from(floor(i)) <= f64::from(budget);
|
||||
if open {
|
||||
used += f64::from(floor(i));
|
||||
}
|
||||
open
|
||||
});
|
||||
let shown = squeeze_floored(&natural, &shown, budget, 0.0, floor);
|
||||
TabFit { shown, overflow, overflow_width }
|
||||
}
|
||||
|
||||
fn squeeze(natural: &[f32], indices: &[usize], total: f32, minimum: f32) -> Vec<(usize, f32)> {
|
||||
squeeze_floored(natural, indices, total, minimum, |i| natural.get(i).copied().unwrap_or(0.0).min(minimum))
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use super::targets::ZoneRule;
|
||||
use super::{ColumnCards, IconicMode};
|
||||
use crate::tabs::{PanelTabLook, PanelTabShrink};
|
||||
use crate::tabs::PanelTabLook;
|
||||
use craft_behavior::{DockSide, IconId};
|
||||
|
||||
/// A set of the sides of the layout's outer edge or of a group.
|
||||
@@ -109,10 +109,6 @@ pub struct DockConfig {
|
||||
/// filled rounded pill ([`PanelTabLook::Pill`]), or a square filled block on a canvas strip
|
||||
/// ([`PanelTabLook::Block`]).
|
||||
pub tab_look: PanelTabLook,
|
||||
/// How far the groups' panel tabs shrink before they overflow: to their first letters, clipped
|
||||
/// ([`PanelTabShrink::FirstLetters`]), or each to a readable prefix ending in "…", the shown tab too
|
||||
/// ([`PanelTabShrink::Ellipsis`]), the tabs that do not fit at that width going to the strip's overflow menu.
|
||||
pub tab_shrink: PanelTabShrink,
|
||||
/// A floating window opens in an OS window of its own where the window host opens OS windows and reports where
|
||||
/// they are on the desktop (not on Wayland) and its panels allow it (`PanelSpec::allow_native`). Elsewhere, and
|
||||
/// with this off, it shows in the app window. A window that takes over a held press (`DockArea::continue_move`)
|
||||
@@ -150,7 +146,6 @@ impl Default for DockConfig {
|
||||
titled_headers: false,
|
||||
wheel_selects_tabs: false,
|
||||
tab_look: PanelTabLook::Plain,
|
||||
tab_shrink: PanelTabShrink::FirstLetters,
|
||||
native_windows: true,
|
||||
group_order_items: false,
|
||||
panel_menu_always: false,
|
||||
@@ -280,13 +275,6 @@ impl DockConfig {
|
||||
self
|
||||
}
|
||||
|
||||
/// How far the groups' panel tabs shrink ([`tab_shrink`](Self::tab_shrink)).
|
||||
#[must_use]
|
||||
pub fn tab_shrink(mut self, shrink: PanelTabShrink) -> Self {
|
||||
self.tab_shrink = shrink;
|
||||
self
|
||||
}
|
||||
|
||||
/// Flush docks on or off ([`flush_docks`](Self::flush_docks)).
|
||||
#[must_use]
|
||||
pub fn flush_docks(mut self, on: bool) -> Self {
|
||||
|
||||
@@ -22,7 +22,7 @@ use crate::runtime::focus::{self, FocusRequest, FocusTarget};
|
||||
use crate::runtime::memory::sub;
|
||||
use crate::sections::FooterBar;
|
||||
use crate::style::{ComponentStyle, Corners, RuleLook, RuleWidth, StyleColor, StyleCx, SurfaceKind, TypeRole};
|
||||
use crate::tabs::{PanelTabLook, PanelTabShrink, Tab, TabKind, TabStrip, TornOff};
|
||||
use crate::tabs::{PanelTabLook, Tab, TabKind, TabStrip, TornOff};
|
||||
use craft_behavior::{CursorIcon, CursorPriority, DockSide, IconId, Metrics, PartKey, PressPhase, Sense, keyed};
|
||||
use craft_theme::{ControlSize, GripMark, GripShape, Tokens};
|
||||
use egui::{Id, Pos2, Rect, Response, Ui, Vec2};
|
||||
@@ -374,9 +374,6 @@ pub struct DockGroup<'a, P: Clone + Hash + Send + Sync + 'static> {
|
||||
wheel_selects_tabs: bool,
|
||||
/// The look of the tabs: [`tab_look`](DockGroup::tab_look).
|
||||
tab_look: PanelTabLook,
|
||||
/// How far the tabs shrink ([`DockConfig::tab_shrink`](super::DockConfig::tab_shrink)).
|
||||
#[prop]
|
||||
tab_shrink: PanelTabShrink,
|
||||
/// The header shows the shown panel's title (with its count) and a close button in place of the tab strip, as a
|
||||
/// single titled panel does: the panels are shown from a rail, not by tabs.
|
||||
#[prop(flag)]
|
||||
@@ -441,7 +438,6 @@ impl<'a, P: Clone + Hash + Send + Sync + PartialEq + Debug + 'static> DockGroup<
|
||||
closable: false,
|
||||
wheel_selects_tabs: false,
|
||||
tab_look: PanelTabLook::Plain,
|
||||
tab_shrink: PanelTabShrink::FirstLetters,
|
||||
titled: false,
|
||||
take_focus: false,
|
||||
dock: None,
|
||||
@@ -766,7 +762,6 @@ impl<P: Clone + Hash + Send + Sync + PartialEq + Debug + 'static> Container for
|
||||
closable: self.closable,
|
||||
wheel_selects: self.wheel_selects_tabs,
|
||||
look: self.tab_look,
|
||||
shrink: self.tab_shrink,
|
||||
take_focus: self.take_focus,
|
||||
dock: self.dock,
|
||||
sent: Outbox::new(&mut sent),
|
||||
@@ -861,7 +856,6 @@ struct TabRow<'a, P> {
|
||||
closable: bool,
|
||||
wheel_selects: bool,
|
||||
look: PanelTabLook,
|
||||
shrink: PanelTabShrink,
|
||||
/// The strip takes key focus in this pass (`DockGroup::take_focus`): in the key, so the row runs and the strip
|
||||
/// takes it while it runs.
|
||||
take_focus: bool,
|
||||
@@ -950,7 +944,6 @@ fn tab_row<P: Clone + PartialEq + Debug + Hash + Send + Sync + 'static>(row: &mu
|
||||
.kind(TabKind::Panel)
|
||||
.keeps_focus()
|
||||
.panel_look(row.look)
|
||||
.panel_shrink(row.shrink)
|
||||
.shown(shown_tab(panels, active))
|
||||
.tear_off()
|
||||
.natural_height(true)
|
||||
|
||||
@@ -1246,7 +1246,6 @@ impl<P: Clone + Eq + Hash + Debug + Send + Sync + 'static> Pass<'_, P> {
|
||||
.panel_menu_shown(*hosted || scene.config.panel_menu_always)
|
||||
.panel_menu_icon(scene.config.panel_menu_icon.clone())
|
||||
.tab_look(scene.config.tab_look)
|
||||
.tab_shrink(scene.config.tab_shrink)
|
||||
.command_states(states.clone());
|
||||
if look.in_window {
|
||||
group = group.in_window();
|
||||
|
||||
@@ -6,7 +6,7 @@ mod words;
|
||||
mod rules;
|
||||
pub(crate) mod tab;
|
||||
|
||||
pub use rules::{CloseSide, DocumentTabLook, FitMode, OverflowMenu, OverflowPlace, PanelTabLook, PanelTabShrink, TabCloseLook, TabKind, close_side};
|
||||
pub use rules::{CloseSide, DocumentTabLook, FitMode, OverflowMenu, OverflowPlace, PanelTabLook, TabCloseLook, TabKind, close_side};
|
||||
pub use tab::{Tab, TabOutput, TabPartEvent, TabRole, TabStyle};
|
||||
pub use words::TabWords;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! The pure rules of a tab strip: what each kind of strip does, and which side a tab's close button is on.
|
||||
|
||||
use craft_behavior::fit::{TabFit, fit, fit_to_least, fit_whole};
|
||||
use craft_behavior::fit::{TabFit, fit, fit_whole};
|
||||
use craft_host::target::Os;
|
||||
|
||||
/// The look of document tabs ([`TabKind::Document`]) app-wide. An app sets it once at its root (`RootEnv::values`);
|
||||
@@ -51,22 +51,6 @@ pub enum PanelTabLook {
|
||||
Block,
|
||||
}
|
||||
|
||||
/// How far panel tabs ([`TabKind::Panel`]) shrink before they overflow, and how a label too long for its tab ends: a
|
||||
/// strip's choice ([`TabStrip::panel_shrink`](super::TabStrip)), which a dock makes for its groups' tabs.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, crate::contract::MemoProps)]
|
||||
#[non_exhaustive]
|
||||
pub enum PanelTabShrink {
|
||||
/// Tabs shrink to their title's first letters, the label clipped at the tab's edge with no ellipsis.
|
||||
#[default]
|
||||
FirstLetters,
|
||||
/// Tabs shrink no further than their label's readable least, its first
|
||||
/// [`TextRun::READABLE_CHARACTERS`](crate::primitives::TextRun::READABLE_CHARACTERS) characters and an ellipsis (a
|
||||
/// title at most one character longer stays whole), and a cut label ends in "…". Every tab shrinks, the shown one
|
||||
/// too, the widest first; only the tabs that do not fit at their least go to the overflow menu, the last ones
|
||||
/// first ([`FitMode::ShrinkToLeast`]).
|
||||
Ellipsis,
|
||||
}
|
||||
|
||||
/// The kind of a strip. It sets the defaults below; the host picks one per strip.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
#[non_exhaustive]
|
||||
@@ -99,22 +83,16 @@ pub enum FitMode {
|
||||
/// Whether the selected tab keeps its natural width.
|
||||
keep_selected: bool,
|
||||
},
|
||||
/// Every tab shrinks towards its own least width, the selected one too, the widest first; only the tabs that do
|
||||
/// not fit at their least overflow, the last ones first (`fit_to_least`). Panel tabs under
|
||||
/// [`PanelTabShrink::Ellipsis`].
|
||||
ShrinkToLeast,
|
||||
}
|
||||
|
||||
impl FitMode {
|
||||
/// Fits tabs of the `natural` widths into `available`, keeping the `selected` tab. Whole widths give the tabs
|
||||
/// marked in `kept` the room before the other tabs (`fit_whole`); shrinking tabs have no kept tabs. `minimum` is
|
||||
/// the narrowest a shrinking tab becomes and `overflow` the width of the overflow button; [`FitMode::ShrinkToLeast`]
|
||||
/// takes it as every tab's least (a strip passes each tab's own to `fit_to_least`).
|
||||
/// the narrowest a shrinking tab becomes and `overflow` the width of the overflow button.
|
||||
pub fn fit(self, natural: &[f32], selected: usize, kept: &[bool], available: f32, minimum: f32, overflow: f32) -> TabFit {
|
||||
match self {
|
||||
FitMode::Whole => fit_whole(natural, selected, kept, available, overflow),
|
||||
FitMode::Shrink { keep_selected } => fit(natural, selected, available, minimum, overflow, keep_selected),
|
||||
FitMode::ShrinkToLeast => fit_to_least(natural, &vec![minimum; natural.len()], selected, available, overflow),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,20 +132,12 @@ impl TabKind {
|
||||
if self.shrinks_in(look) { FitMode::Shrink { keep_selected: self.keeps_active_width() } } else { self.fit_mode() }
|
||||
}
|
||||
|
||||
/// How tab widths are fitted under the app's document tab look and the strip's panel shrink: panel tabs under
|
||||
/// [`PanelTabShrink::Ellipsis`] all shrink to their own readable least, the shown tab too, before any overflows
|
||||
/// ([`FitMode::ShrinkToLeast`]); every other kind and shrink is [`TabKind::fit_mode_in`].
|
||||
pub fn fit_mode_for(self, look: DocumentTabLook, shrink: PanelTabShrink) -> FitMode {
|
||||
if self == Self::Panel && shrink == PanelTabShrink::Ellipsis { FitMode::ShrinkToLeast } else { self.fit_mode_in(look) }
|
||||
}
|
||||
|
||||
/// Whether these are document tabs under [`DocumentTabLook::Shrinking`].
|
||||
pub fn shrinks_in(self, look: DocumentTabLook) -> bool {
|
||||
self == Self::Document && look == DocumentTabLook::Shrinking
|
||||
}
|
||||
|
||||
/// Whether the shown tab keeps its whole title while the other tabs shrink: panel tabs that shrink to their first
|
||||
/// letters (under [`PanelTabShrink::Ellipsis`] the shown tab shrinks with the others, [`TabKind::fit_mode_for`]).
|
||||
/// Whether the shown tab keeps its whole title while the other tabs shrink: panel tabs.
|
||||
pub fn keeps_active_width(self) -> bool {
|
||||
self == Self::Panel
|
||||
}
|
||||
@@ -178,13 +148,6 @@ impl TabKind {
|
||||
self == Self::Panel
|
||||
}
|
||||
|
||||
/// Whether a label too long for its tab is clipped at the tab's edge under the strip's `shrink`: panel tabs that
|
||||
/// shrink to their first letters ([`PanelTabShrink::FirstLetters`]); under [`PanelTabShrink::Ellipsis`] they end
|
||||
/// in an ellipsis as the other kinds do.
|
||||
pub fn clips_label_in(self, shrink: PanelTabShrink) -> bool {
|
||||
self.clips_label() && shrink == PanelTabShrink::FirstLetters
|
||||
}
|
||||
|
||||
/// Where the overflow button sits: at the strip's end for document and text tabs, after the last tab for panel
|
||||
/// tabs.
|
||||
pub fn overflow_place(self) -> OverflowPlace {
|
||||
|
||||
@@ -30,7 +30,7 @@ use crate::primitives::{PaintShape, StrokeWidth};
|
||||
use crate::runtime::commands::FireSource;
|
||||
use crate::runtime::env::Label;
|
||||
use crate::style::{ComponentStyle, StyleColor, StyleCx};
|
||||
use craft_behavior::fit::{FitItem, fit_items, fit_to_least};
|
||||
use craft_behavior::fit::{FitItem, fit_items};
|
||||
use craft_behavior::{
|
||||
Flow, HitLevel, HitPriority, IconId, Insets, ItemLayout, ItemSource, Metrics, NavShape, PartKey, PressPhase, ScrollPos, Selection,
|
||||
SelectionChange, Sense,
|
||||
@@ -332,21 +332,8 @@ impl StripLayout {
|
||||
.collect();
|
||||
let selected = lane.iter().position(|place| matches!(*place, Lane::Tab(at) if at == *selected)).unwrap_or(usize::MAX);
|
||||
let available = (*width - ends_room).max(0.0) + tab_gap;
|
||||
let fit = if *mode == FitMode::ShrinkToLeast {
|
||||
// Each tab shrinks no further than its own least; a tab fading out keeps its width.
|
||||
let least: Vec<f32> = lane
|
||||
.iter()
|
||||
.map(|place| match *place {
|
||||
Lane::Tab(at) => min.get(at).copied().unwrap_or(0.0),
|
||||
Lane::Ghost(k) => ghosts.get(k).map_or(0.0, |ghost| ghost.width),
|
||||
})
|
||||
.map(|width| width + tab_gap)
|
||||
.collect();
|
||||
fit_to_least(&padded, &least, selected, available, *height)
|
||||
} else {
|
||||
let minimum = min.iter().copied().fold(0.0, f32::max);
|
||||
mode.fit(&padded, selected, &held, available, minimum + tab_gap, *height)
|
||||
};
|
||||
let minimum = min.iter().copied().fold(0.0, f32::max);
|
||||
let fit = mode.fit(&padded, selected, &held, available, minimum + tab_gap, *height);
|
||||
*shown = vec![false; count];
|
||||
*left = vec![*width; count];
|
||||
*wide = vec![0.0; count];
|
||||
@@ -528,9 +515,8 @@ impl<K> TabRoles<'_, K> {
|
||||
/// asks for the tab's context menu.
|
||||
///
|
||||
/// The `kind` picks the rules: document and text tabs keep whole titles and send the rest to an overflow menu of
|
||||
/// every tab; panel tabs shrink before they overflow and the menu lists the hidden ones. Panel tabs shrink to their
|
||||
/// first letters, clipped, beside the shown tab whole, or with `panel_shrink(PanelTabShrink::Ellipsis)` each to a
|
||||
/// readable prefix ending in "…", the shown tab too, before any overflows. The shown tab is always visible. A tab
|
||||
/// every tab; panel tabs shrink to their first letters, clipped, beside the shown tab whole, before they overflow,
|
||||
/// and the menu lists the hidden ones. The shown tab is always visible. A tab
|
||||
/// dragged out of the strip and released where no other strip, dock or drop target takes it is reported as
|
||||
/// `torn_off`.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
@@ -551,11 +537,6 @@ pub struct TabStrip<'a, K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + '
|
||||
/// The look of a panel strip's tabs ([`TabKind::Panel`]).
|
||||
#[prop]
|
||||
panel_look: super::PanelTabLook,
|
||||
/// How far a panel strip's tabs ([`TabKind::Panel`]) shrink before they overflow: to their first letters, clipped
|
||||
/// (the default), or each to a readable prefix ending in an ellipsis, the shown tab too
|
||||
/// ([`PanelTabShrink::Ellipsis`](super::PanelTabShrink)).
|
||||
#[prop]
|
||||
panel_shrink: super::PanelTabShrink,
|
||||
/// The tab the host shows. A click, Space, Enter or a drop reports the new one in the output.
|
||||
#[prop]
|
||||
shown: Option<K>,
|
||||
@@ -628,7 +609,6 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> TabStri
|
||||
kind: TabKind::Document,
|
||||
large: false,
|
||||
panel_look: super::PanelTabLook::Plain,
|
||||
panel_shrink: super::PanelTabShrink::FirstLetters,
|
||||
shown: None,
|
||||
tear_off: false,
|
||||
wheel_selects: false,
|
||||
@@ -1085,7 +1065,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Collection
|
||||
// Each end item is measured in this pass, with its label and icon only.
|
||||
let ends = self.trailing.iter().map(|end| end.fit_item(cx)).collect();
|
||||
let look = cx.env::<super::DocumentTabLook>();
|
||||
let mode = self.kind.fit_mode_for(look, self.panel_shrink);
|
||||
let mode = self.kind.fit_mode_in(look);
|
||||
let fitting = Fitting { selected, mode, kept, ends, gap: cx.tokens.space.gap.inline, ghosts, departed };
|
||||
// Shrinking document tabs stand apart by the inline gap; the other kinds and looks touch.
|
||||
let tab_gap = if self.kind.shrinks_in(look) { cx.tokens.space.gap.inline } else { 0.0 };
|
||||
@@ -1094,7 +1074,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Collection
|
||||
|
||||
/// Every tab takes the strip's kind, panel look, panel shrink and size.
|
||||
fn dress<'r>(&self, tab: Self::Item<'r>, _: &RowState) -> Self::Item<'r> {
|
||||
let tab = tab.kind(self.kind).panel_look(self.panel_look).panel_shrink(self.panel_shrink);
|
||||
let tab = tab.kind(self.kind).panel_look(self.panel_look);
|
||||
if self.large { tab.large() } else { tab }
|
||||
}
|
||||
|
||||
|
||||
@@ -3,10 +3,9 @@
|
||||
//! 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 beside its icon and close
|
||||
//! button, or for a panel tab under `PanelTabShrink::Ellipsis` its label's readable least), and a narrower rect cuts the
|
||||
//! label first, then the detail, and shows the whole text as a tooltip.
|
||||
//! 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, PanelTabShrink, TabCloseLook, TabKind, close_side};
|
||||
use super::rules::{CloseSide, PanelTabLook, TabCloseLook, TabKind, close_side};
|
||||
use super::words::TabWords;
|
||||
use std::borrow::Cow;
|
||||
|
||||
@@ -208,9 +207,6 @@ pub struct Tab<'a> {
|
||||
#[style_key]
|
||||
#[prop]
|
||||
panel_look: PanelTabLook,
|
||||
/// How far a panel tab ([`TabKind::Panel`]) shrinks and how its cut label ends; the other kinds have their own.
|
||||
#[prop]
|
||||
panel_shrink: PanelTabShrink,
|
||||
/// Secondary text after the label (a colour mode, a zoom).
|
||||
#[prop(into)]
|
||||
detail: Option<Label>,
|
||||
@@ -252,7 +248,6 @@ impl<'a> Tab<'a> {
|
||||
role: TabRole::default(),
|
||||
large: false,
|
||||
panel_look: PanelTabLook::Plain,
|
||||
panel_shrink: PanelTabShrink::FirstLetters,
|
||||
detail: None,
|
||||
icon: None,
|
||||
modified: false,
|
||||
@@ -277,12 +272,6 @@ impl<'a> Tab<'a> {
|
||||
self.can_close() && self.kind.shows_close()
|
||||
}
|
||||
|
||||
/// Whether the tab shrinks no further than its label's readable least, the cut label ending in an ellipsis: a
|
||||
/// panel tab under [`PanelTabShrink::Ellipsis`].
|
||||
fn shrinks_to_ellipsis(&self) -> bool {
|
||||
self.kind == TabKind::Panel && self.panel_shrink == PanelTabShrink::Ellipsis
|
||||
}
|
||||
|
||||
fn detail_text(&self) -> Option<&str> {
|
||||
self.detail.as_ref().map(Label::as_str).filter(|text| !text.is_empty())
|
||||
}
|
||||
@@ -518,7 +507,7 @@ impl Component for Tab<'_> {
|
||||
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 && !self.shrinks_to_ellipsis() {
|
||||
if cx.proposal == Proposal::Min && !cx.style.shrinking {
|
||||
return Vec2::new(parts + letters(), sizes.label_line);
|
||||
}
|
||||
let width = |run: &TextRun<'_>| cx.layout_at(run, f32::INFINITY).size().x;
|
||||
@@ -536,10 +525,7 @@ impl Component for Tab<'_> {
|
||||
if self.dotted(cx.style) {
|
||||
natural += sizes.gap + cx.style.marker;
|
||||
}
|
||||
let measured = if cx.proposal == Proposal::Min && self.shrinks_to_ellipsis() {
|
||||
// The label at its readable least, its first characters and the ellipsis, beside the tab's other parts.
|
||||
(natural - capped + cx.least(&self.label_run(cx.style, label).elide()).width).min(natural)
|
||||
} else if cx.proposal == Proposal::Min {
|
||||
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));
|
||||
@@ -638,7 +624,7 @@ impl Component for Tab<'_> {
|
||||
}
|
||||
let role = if selected { style.label.role } else { style.idle_role };
|
||||
let label = label_text(style, &*shown).role(role).color(cx.resolve(&style.label.color));
|
||||
draw.text(placed.label, &if self.kind.clips_label_in(self.panel_shrink) { label.wrap(Wrap::Clip) } else { label.elide() });
|
||||
draw.text(placed.label, &if self.kind.clips_label() { label.wrap(Wrap::Clip) } else { label.elide() });
|
||||
if let (Some(at), Some(detail)) = (placed.detail, self.detail_text()) {
|
||||
draw.text(at, &TextRun::new(detail).role(style.detail.role).color(cx.resolve(&style.detail.color)).elide());
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
use craft_behavior::{SelectionChange, keyed};
|
||||
use craft_ui::contract::{CollectionEvent, Role, ShowCollection as _};
|
||||
use craft_ui::runtime::egui::access::AccessTree;
|
||||
use craft_ui::tabs::{DocumentTabLook, PanelTabShrink, Tab, TabKind, TabRole, TabStrip};
|
||||
use craft_ui::tabs::{DocumentTabLook, Tab, TabKind, TabRole, TabStrip};
|
||||
use craft_ui::testing::focus::AccessActionKind;
|
||||
use craft_ui::testing::pointer::{moved, press, release, secondary};
|
||||
use craft_ui::testing::{Fixture, Frames, trial};
|
||||
@@ -873,158 +873,6 @@ fn panel_tabs_keep_the_shown_tab_whole_and_overflow_from_the_end() {
|
||||
assert!(fewer < PANELS.len(), "the narrowest room overflows");
|
||||
}
|
||||
|
||||
/// The panels of a narrow colour column, as a dock group lists them.
|
||||
const COLOUR_PANELS: [&str; 4] = ["Stroke", "Color", "Swatches", "Gradient"];
|
||||
|
||||
/// A panel strip `room` wide of `COLOUR_PANELS`, the first one shown, its tabs shrinking as `shrink` says.
|
||||
fn colour_strip(shrink: PanelTabShrink, room: f32) -> Fixture {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let source = keyed(&COLOUR_PANELS, |title: &&'static str| *title).with_text(|title| std::borrow::Cow::Borrowed(*title));
|
||||
let strip = TabStrip::new("Panels").kind(TabKind::Panel).panel_shrink(shrink).shown(COLOUR_PANELS[0]);
|
||||
let _ = strip.show_items(ui, &source, &mut |key, _| Tab::new(*key));
|
||||
});
|
||||
fixture.room(egui::vec2(room, 100.0)).settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
/// Tabs by title, each with the text its label paints and whether egui clipped that text at its room; `None` for a tab
|
||||
/// that paints no label.
|
||||
type ShownLabels = Vec<(&'static str, Option<(String, bool)>)>;
|
||||
|
||||
/// The tabs of `COLOUR_PANELS` the strip shows in a new frame, with their labels: the text painted inside each tab.
|
||||
fn shown_labels(fixture: &mut Fixture) -> ShownLabels {
|
||||
let shapes = fixture.frame_shapes();
|
||||
let tree = fixture.tree();
|
||||
COLOUR_PANELS
|
||||
.iter()
|
||||
.filter_map(|title| {
|
||||
let tab = tab_rects(tree, title).first().copied()?;
|
||||
let label = shapes.iter().find_map(|shape| match shape {
|
||||
egui::Shape::Text(text) if tab.contains(text.pos + text.galley.rect.center().to_vec2()) => {
|
||||
Some((text.galley.text().to_owned(), text.galley.elided))
|
||||
}
|
||||
_ => None,
|
||||
});
|
||||
Some((*title, label))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Under `PanelTabShrink::Ellipsis` panel tabs shrink no further than their readable least: in every room, each
|
||||
/// inactive tab shows its whole title or its title's first characters ending in "…", never a label clipped at its
|
||||
/// edge, and where the tabs do not fit at that width the rest are in the overflow menu. The default strip in the same
|
||||
/// room clips its labels with no ellipsis.
|
||||
#[trial]
|
||||
fn panel_tabs_shrinking_to_an_ellipsis_keep_a_readable_prefix_and_overflow_the_rest() {
|
||||
use craft_ui::primitives::TextRun;
|
||||
let mut cut_beside_overflow = None;
|
||||
for room in (60..=320).step_by(6) {
|
||||
let room = room as f32;
|
||||
let shown = shown_labels(&mut colour_strip(PanelTabShrink::Ellipsis, room));
|
||||
let mut cut = false;
|
||||
for (title, label) in shown.iter().filter(|(title, _)| *title != COLOUR_PANELS[0]) {
|
||||
let (text, clipped) = label.as_ref().unwrap_or_else(|| panic!("in {room}: the {title} tab paints its label"));
|
||||
assert!(!clipped, "in {room}: the {title} tab's label is not clipped at its edge: {text:?}");
|
||||
if text == title {
|
||||
continue;
|
||||
}
|
||||
let kept = text.strip_suffix('…').unwrap_or_else(|| panic!("in {room}: the {title} tab's cut label ends in an ellipsis: {text:?}"));
|
||||
let readable: String = title.chars().take(TextRun::READABLE_CHARACTERS).collect();
|
||||
assert!(
|
||||
title.starts_with(kept) && kept.starts_with(readable.trim_end()),
|
||||
"in {room}: the {title} tab keeps at least {readable:?} before its ellipsis: {text:?}"
|
||||
);
|
||||
cut = true;
|
||||
}
|
||||
if cut && shown.len() < COLOUR_PANELS.len() && cut_beside_overflow.is_none() {
|
||||
cut_beside_overflow = Some(room);
|
||||
}
|
||||
}
|
||||
let room = cut_beside_overflow.expect("a room where a cut label stands beside the overflow button");
|
||||
|
||||
let mut fixture = colour_strip(PanelTabShrink::Ellipsis, room);
|
||||
let hidden: Vec<&str> = COLOUR_PANELS.iter().copied().filter(|title| tab_rects(fixture.tree(), title).is_empty()).collect();
|
||||
let more = fixture.by_label("More tabs").expect("the overflow button").center();
|
||||
fixture.click_at(more).settle();
|
||||
let tree = fixture.tree();
|
||||
for title in hidden {
|
||||
assert!(
|
||||
[Role::MENU_ITEM, Role::MENU_ITEM_CHECK_BOX, Role::MENU_ITEM_RADIO]
|
||||
.into_iter()
|
||||
.flat_map(|role| tree.with_role(role))
|
||||
.any(|key| tree.name_of(key) == Some(title)),
|
||||
"in {room}: the overflow menu lists the hidden {title} tab"
|
||||
);
|
||||
}
|
||||
|
||||
let plain = shown_labels(&mut colour_strip(PanelTabShrink::FirstLetters, room));
|
||||
let labels: Vec<&(String, bool)> = plain.iter().filter_map(|(_, label)| label.as_ref()).collect();
|
||||
assert!(labels.iter().all(|(text, _)| !text.ends_with('…')), "in {room}: the default strip ends no label in an ellipsis: {plain:?}");
|
||||
assert!(labels.iter().any(|(_, clipped)| *clipped), "in {room}: the default strip clips a label at its tab's edge: {plain:?}");
|
||||
}
|
||||
|
||||
/// A colour group of three short panels, as DesignCraft's dock lists Stroke, Color and Swatches.
|
||||
const COLOUR_GROUP: [&str; 3] = ["Stroke", "Color", "Swatches"];
|
||||
|
||||
/// A panel strip `room` wide of `COLOUR_GROUP` under `PanelTabShrink::Ellipsis`, the last one, Swatches, shown.
|
||||
fn colour_group(room: f32) -> Fixture {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let source = keyed(&COLOUR_GROUP, |title: &&'static str| *title).with_text(|title| std::borrow::Cow::Borrowed(*title));
|
||||
let strip = TabStrip::new("Panels").kind(TabKind::Panel).panel_shrink(PanelTabShrink::Ellipsis).shown(COLOUR_GROUP[2]);
|
||||
let _ = strip.show_items(ui, &source, &mut |key, _| Tab::new(*key));
|
||||
});
|
||||
fixture.room(egui::vec2(room, 100.0)).settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
/// Under `PanelTabShrink::Ellipsis` every panel tab shrinks to its readable least before any tab overflows, the shown
|
||||
/// tab too: in the narrowest room that shows Stroke, Color and Swatches, Swatches shown, nothing is in the overflow
|
||||
/// menu, so no overflow button shows, and the shown Swatches tab reads its first characters and "…" like the others;
|
||||
/// a point narrower a tab other than the shown one goes to the overflow menu.
|
||||
#[trial]
|
||||
fn panel_tabs_shrinking_to_an_ellipsis_all_reach_their_least_before_any_overflows() {
|
||||
use craft_ui::primitives::TextRun;
|
||||
let all_shown = |room: f32| shown_labels(&mut colour_group(room)).len() == COLOUR_GROUP.len();
|
||||
let (mut narrow, mut wide) = (40.0_f32, 400.0_f32);
|
||||
assert!(!all_shown(narrow) && all_shown(wide), "the group overflows in {narrow} and fits whole in {wide}");
|
||||
while wide - narrow > 1.0 {
|
||||
let middle = ((narrow + wide) / 2.0).floor();
|
||||
if all_shown(middle) {
|
||||
wide = middle;
|
||||
} else {
|
||||
narrow = middle;
|
||||
}
|
||||
}
|
||||
|
||||
let mut fixture = colour_group(wide);
|
||||
assert!(fixture.by_label("More tabs").is_none(), "in {wide}, the narrowest room that shows every tab, nothing overflows");
|
||||
let shown = shown_labels(&mut fixture);
|
||||
for (title, label) in &shown {
|
||||
let (text, clipped) = label.as_ref().unwrap_or_else(|| panic!("in {wide}: the {title} tab paints its label"));
|
||||
assert!(!clipped, "in {wide}: the {title} tab's label is not clipped at its edge: {text:?}");
|
||||
if text == title {
|
||||
continue;
|
||||
}
|
||||
let kept = text.strip_suffix('…').unwrap_or_else(|| panic!("in {wide}: the {title} tab's cut label ends in an ellipsis: {text:?}"));
|
||||
let readable: String = title.chars().take(TextRun::READABLE_CHARACTERS).collect();
|
||||
assert!(
|
||||
title.starts_with(kept) && kept.starts_with(&readable),
|
||||
"in {wide}: the {title} tab keeps {readable:?} before its ellipsis: {text:?}"
|
||||
);
|
||||
}
|
||||
let swatches = shown.iter().find_map(|(title, label)| (*title == COLOUR_GROUP[2]).then_some(label.as_ref())).flatten();
|
||||
assert!(
|
||||
swatches.is_some_and(|(text, _)| text.ends_with('…')),
|
||||
"in {wide}: the shown Swatches tab shrinks with the others before any tab overflows: {shown:?}"
|
||||
);
|
||||
|
||||
let mut fixture = colour_group(narrow);
|
||||
let fewer = shown_labels(&mut fixture);
|
||||
assert!(fewer.len() < COLOUR_GROUP.len(), "in {narrow}, a point narrower, a tab overflows: {fewer:?}");
|
||||
assert!(fewer.iter().any(|(title, _)| *title == COLOUR_GROUP[2]), "in {narrow}: the shown tab stays: {fewer:?}");
|
||||
assert!(fixture.by_label("More tabs").is_some(), "in {narrow}: the overflow button shows");
|
||||
}
|
||||
|
||||
/// A strip places its tabs by their measure in its first frame: a panel strip too narrow for its tabs fits them by
|
||||
/// their measured widths and takes their measured height, in a frame that runs once and measures each tab's height
|
||||
/// at most twice (at its ideal width, and at the width the fit shrinks it to), and the strip and every tab have the
|
||||
|
||||
@@ -715,7 +715,6 @@ The fit rules: which tabs, bar items or titles stay in a strip and which go to a
|
||||
- `craft_behavior::fit::TabFit`: `pub struct TabFit { pub shown: Vec<(usize, f32)>, pub overflow: Vec<usize>, pub overflow_width: f32 }`. Tabs visible on a strip and tabs that need an overflow menu, all in original order.
|
||||
- `craft_behavior::fit::fit`: `pub fn fit(natural: &[f32], selected: usize, available: f32, minimum_width: f32, overflow_width: f32, keep_selected: bool) -> TabFit`. Fit tab widths into `available` points, shrinking the widest tabs first to a common cap.
|
||||
- `craft_behavior::fit::fit_items`: `pub fn fit_items(items: &[FitItem], available: f32, gap: f32, section_gap: f32, overflow_extent: f32) -> Fit`. Decides which items stay in a strip `available` long. Everything fits and no item is `Menu`: all items show.
|
||||
- `craft_behavior::fit::fit_to_least`: `pub fn fit_to_least(natural: &[f32], least: &[f32], selected: usize, available: f32, overflow_width: f32) -> TabFit`. Fit tab widths into `available` points, each tab shrinking to its own `least` before any tab overflows.
|
||||
- `craft_behavior::fit::fit_whole`: `pub fn fit_whole(natural: &[f32], selected: usize, kept: &[bool], available: f32, overflow_width: f32) -> TabFit`. Fit tabs at their natural widths; the tabs that do not fit go to an overflow menu.
|
||||
- `craft_behavior::fit::run_extent`: `pub fn run_extent(items: &[FitItem], shown: &[bool], gap: f64, section_gap: f64) -> f64`. The length of the items marked in `shown`, with `gap` between neighbours of a section and `section_gap` between
|
||||
|
||||
|
||||
@@ -108,7 +108,6 @@ The host handles these `DockEvent`s:
|
||||
| `title_bar_docks` | on | Dragging a floating window's title bar onto a target docks the whole window. |
|
||||
| `close_menu_items`, `close_button` | on | Built-in Close items, and the close action on floating windows. |
|
||||
| `tab_close_buttons` | off | Close buttons on tabs (middle click closes too), for tabs that act as documents. |
|
||||
| `tab_shrink` | `PanelTabShrink::FirstLetters` | How far panel tabs shrink before they overflow: to their first letters, clipped at the tab's edge, or with `PanelTabShrink::Ellipsis` each to a readable prefix ending in "…" (`TextRun::READABLE_CHARACTERS` characters), the shown tab too, the tabs that do not fit at that width going to the strip's overflow menu. |
|
||||
| `panel_menu_always` | off | Every group shows its panel menu button, with the dock's own rows when the shown panel has none. Off: only a panel with rows of its own (`DockPanels::panel_rows`) gets the button. |
|
||||
| `panel_menu_icon` | `more` | The panel menu button's icon; `menu` gives the three-line mark. |
|
||||
| `native_windows` | on | Floating windows open as OS windows where the window host opens them and reports their positions (not on Wayland) and `PanelSpec::allow_native` allows. |
|
||||
|
||||
Reference in new issue
Block a user