fw sync: L-322: the filled dropdown triangle (DropdownArrow::Triangle) and PdfCraft's document tabs (DocumentTabLook::Shrinking: shrink, inline gap, marker dot for modified, 22-digit name cap) become opt-in env values; upstream's chevron, whole touching tabs and '*' stay the default

This commit is contained in:
Dylan M. Taylor
2026-11-23 02:57:02 -05:00
parent bfe51c7622
commit e9d0bb0267
13 changed files with 198 additions and 68 deletions
+7 -7
View File
@@ -21,7 +21,7 @@ use craft_ui::contract::{
Access, Bind, Common, Component, Cx, Edit as _, EditResponse, Focus as _, Input, MeasureCx, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx,
Pointer as _, Props as _, Remember as _, Role, Styled as _, WordSetCx as _,
};
use craft_ui::controls::disclosure::DisclosureGlyph;
use craft_ui::controls::disclosure::{DisclosureGlyph, DropdownArrow};
use craft_ui::fonts::FontRef;
use craft_ui::list::{ListRow, ThumbSize};
use craft_ui::primitives::{IconSpec, TextKey, TextOptions, TextRun};
@@ -650,12 +650,12 @@ impl<'a> FontPicker<'a> {
/// The adornments at the end of the field: the missing mark when `missing`, then the chevron. The mark is a glyph
/// whose part carries the note.
fn trailing(name: Label, missing: bool) -> Vec<Adornment> {
fn trailing(name: Label, missing: bool, arrow: DropdownArrow) -> Vec<Adornment> {
let mut all = Vec::with_capacity(2);
if missing {
all.push(Adornment::glyph(IconSpec::svg(IconId::new(WARNING_ICON))));
}
all.push(Adornment::button(CHEVRON, DisclosureGlyph::dropdown(), name));
all.push(Adornment::button(CHEVRON, DisclosureGlyph::dropdown_of(arrow), name));
all
}
@@ -796,14 +796,14 @@ impl Component for FontPicker<'_> {
let widest = if ribbon { Vec2::new(cx.tokens.size.length.short, widest.y) } else { widest };
let name = cx.words::<FontPickerWords>().list.clone();
let note = self.missing_note(current, &cx.words::<MissingFontsWords>());
Vec2::new(widest.x + reserve(&cx.style.field, &[], &Self::trailing(name, note.is_some())), widest.y)
Vec2::new(widest.x + reserve(&cx.style.field, &[], &Self::trailing(name, note.is_some(), cx.env.get())), widest.y)
}
fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) {
let name = cx.words::<FontPickerWords>().list.clone();
let family = cx.value().cloned().unwrap_or_default();
let note = self.missing_note(&family, &cx.words::<MissingFontsWords>());
let trailing = Self::trailing(name, note.is_some());
let trailing = Self::trailing(name, note.is_some(), cx.env.get());
let placed = place(cx.content_rect, &cx.style.field, &[], &trailing, cx.flow.is_rtl());
if let (Some(note), Some(at)) = (note, placed.trailing.first()) {
cx.part(WARNING, *at).tooltip(note);
@@ -917,13 +917,13 @@ impl Component for FontPicker<'_> {
let name = cx.words::<FontPickerWords>().list.clone();
let family = cx.value().cloned().unwrap_or_default();
let missing = self.missing_note(&family, &cx.words::<MissingFontsWords>()).is_some();
let trailing = Self::trailing(name.clone(), missing);
let trailing = Self::trailing(name.clone(), missing, cx.env.get());
let placed = place(cx.content_rect, &cx.style.field, &[], &trailing, cx.env.direction().is_rtl());
if let (true, Some(at)) = (missing, placed.trailing.first()) {
let color = cx.resolve(&cx.style.warning);
ui.draw().icon(*at, &IconSpec::svg(IconId::new(WARNING_ICON)).color(color));
}
ui.face(CHEVRON, IconButton::new(DisclosureGlyph::dropdown()).label(name));
ui.face(CHEVRON, IconButton::new(DisclosureGlyph::dropdown_of(cx.env.get())).label(name));
}
fn finish(self, mut cx: Cx<'_, Self>) -> FontPickerOutput {
+1 -1
View File
@@ -82,7 +82,7 @@ impl BarTool {
/// Shows the icon, its arrow and the rule after them.
pub fn show(self, ui: &mut Ui) -> BarToolOutput {
let shown: ButtonOutput = IconButton::new(self.icon).label(self.label).enabled(self.enabled).show(ui);
Icon::new(DisclosureGlyph::dropdown()).enabled(self.enabled).show(ui);
Icon::new(DisclosureGlyph::dropdown_of(crate::runtime::env::Env::of(ui).get())).enabled(self.enabled).show(ui);
Divider::vertical().show(ui);
BarToolOutput { response: shown.response }
}
+28 -3
View File
@@ -36,6 +36,19 @@ pub enum ArrowForm {
Dropdown,
}
/// Which arrow a field or a picker shows after its text ([`DisclosureGlyph::dropdown_of`]). An app sets it once at its
/// root (`RootEnv::values`); the default is the stroked chevron.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, crate::runtime::env::EnvValue, crate::contract::MemoProps)]
#[env(style)]
#[non_exhaustive]
pub enum DropdownArrow {
/// The fine stroked chevron.
#[default]
Chevron,
/// The small filled triangle pointing down, the disclosure triangle opened: PdfCraft's fields and pickers.
Triangle,
}
/// The arrow every component shows for something that opens or unfolds. The disclosure is a filled triangle that
/// turns from pointing to the end of the line (closed) to pointing down (open) as `open` goes from 0 to 1; a
/// dropdown's arrow is the open chevron; a submenu's is the filled arrow to the end of the line. Components name these
@@ -52,8 +65,10 @@ pub struct DisclosureGlyph {
pub stroke: StrokeWidth,
}
/// The arrow of a field or picker: the small filled triangle pointing down.
const DROPDOWN: DisclosureGlyph = DisclosureGlyph::new(1.0);
/// The arrow of a field or picker: the fine stroked chevron.
const DROPDOWN: DisclosureGlyph = DisclosureGlyph::dropdown_form();
/// The arrow of a field or picker under [`DropdownArrow::Triangle`]: the small filled triangle pointing down.
const DROPDOWN_TRIANGLE: DisclosureGlyph = DisclosureGlyph::new(1.0);
/// The arrow of a button that opens a menu.
const MENU_BUTTON: DisclosureGlyph = DisclosureGlyph { open: 1.0, form: ArrowForm::Dropdown, stroke: StrokeWidth::Medium };
/// The submenu arrow.
@@ -75,11 +90,21 @@ impl DisclosureGlyph {
!matches!(self.form, ArrowForm::Dropdown)
}
/// The icon of the arrow after the text of a picker's face or a field with a list.
/// The icon of the arrow after the text of a picker's face or a field with a list: the chevron. A component reads
/// the app's choice through [`DisclosureGlyph::dropdown_of`].
pub fn dropdown() -> crate::primitives::IconSpec<'static> {
crate::primitives::IconSpec::glyph(&DROPDOWN)
}
/// The icon of the arrow after the text of a picker's face or a field with a list, in the form `arrow` names: a
/// component passes the scope's [`DropdownArrow`] (`cx.env.get::<DropdownArrow>()`).
pub fn dropdown_of(arrow: DropdownArrow) -> crate::primitives::IconSpec<'static> {
match arrow {
DropdownArrow::Chevron => crate::primitives::IconSpec::glyph(&DROPDOWN),
DropdownArrow::Triangle => crate::primitives::IconSpec::glyph(&DROPDOWN_TRIANGLE),
}
}
/// The icon of the arrow after the text of a button that opens a menu: the dropdown arrow in the firmer line.
pub fn menu_button() -> crate::primitives::IconSpec<'static> {
crate::primitives::IconSpec::glyph(&MENU_BUTTON)
+5 -5
View File
@@ -134,8 +134,8 @@ impl<'a> ComboBox<'a> {
options
}
fn trailing(name: Label) -> [Adornment; 1] {
[Adornment::button(CHEVRON, crate::controls::disclosure::DisclosureGlyph::dropdown(), name)]
fn trailing(name: Label, arrow: crate::controls::disclosure::DropdownArrow) -> [Adornment; 1] {
[Adornment::button(CHEVRON, crate::controls::disclosure::DisclosureGlyph::dropdown_of(arrow), name)]
}
/// Whether any option's label matches `query`.
@@ -177,13 +177,13 @@ impl Component for ComboBox<'_> {
let texts = self.options.iter().filter_map(|option| option.value().map(String::as_str)).chain([current, placeholder]);
let widest = texts.map(|text| cx.field_text(&TextRun::new(text))).fold(Vec2::ZERO, Vec2::max);
let name = cx.words::<PickerWords>().combo_options.clone();
Vec2::new(widest.x + reserve(cx.style, &[], &Self::trailing(name)), widest.y)
Vec2::new(widest.x + reserve(cx.style, &[], &Self::trailing(name, cx.env.get())), widest.y)
}
fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) {
let value = cx.value();
let name = cx.words::<PickerWords>().combo_options.clone();
let trailing = Self::trailing(name);
let trailing = Self::trailing(name, cx.env.get());
let placed = place(cx.content_rect, cx.style, &[], &trailing, cx.flow.is_rtl());
if let Some(at) = placed.trailing.first() {
cx.part(CHEVRON, *at)
@@ -332,7 +332,7 @@ impl Component for ComboBox<'_> {
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
let name = cx.words::<PickerWords>().combo_options.clone();
ui.face(CHEVRON, IconButton::new(crate::controls::disclosure::DisclosureGlyph::dropdown()).label(name));
ui.face(CHEVRON, IconButton::new(crate::controls::disclosure::DisclosureGlyph::dropdown_of(cx.env.get())).label(name));
}
fn finish(self, cx: Cx<'_, Self>) -> ComboBoxOutput {
+4 -4
View File
@@ -134,8 +134,8 @@ impl<'a> SelectFace<'a> {
self.leading.iter().map(|icon| Adornment::glyph(icon.clone())).collect()
}
fn chevron() -> Adornment {
Adornment::glyph(crate::controls::disclosure::DisclosureGlyph::dropdown())
fn chevron(arrow: crate::controls::disclosure::DropdownArrow) -> Adornment {
Adornment::glyph(crate::controls::disclosure::DisclosureGlyph::dropdown_of(arrow))
}
}
@@ -205,7 +205,7 @@ impl Component for SelectFace<'_> {
(Some(_), false) => cx.style.thumb,
(Some(_), true) => cx.style.thumb + cx.style.caption_gap + caption.y,
};
let beside = self.lead(cx.style, caption.x) + reserve(&cx.style.field, &self.leading_glyphs(), &[Self::chevron()]);
let beside = self.lead(cx.style, caption.x) + reserve(&cx.style.field, &self.leading_glyphs(), &[Self::chevron(cx.env.get())]);
if self.fit && self.name {
// A fitted box is at most a field length, padding included; a longer text elides, down to its readable
// least.
@@ -217,7 +217,7 @@ impl Component for SelectFace<'_> {
}
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
let chevron = Self::chevron();
let chevron = Self::chevron(cx.env.get());
let rtl = cx.flow.is_rtl();
let mut flow = Flow::new(cx.content_rect, rtl);
let caption = self.caption_run(&cx.style.caption).map(|run| ui.draw().layout(&run.color(cx.resolve(&cx.style.caption.color)), f32::INFINITY));
+1 -1
View File
@@ -6,7 +6,7 @@ mod words;
mod rules;
pub(crate) mod tab;
pub use rules::{CloseSide, FitMode, OverflowMenu, OverflowPlace, TabKind, close_side};
pub use rules::{CloseSide, DocumentTabLook, FitMode, OverflowMenu, OverflowPlace, TabKind, close_side};
pub use tab::{Tab, TabOutput, TabPartEvent, TabRole, TabStyle};
pub use words::TabWords;
+30 -4
View File
@@ -3,11 +3,26 @@
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`);
/// the strip and its tabs read it from the env.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, crate::runtime::env::EnvValue, crate::contract::MemoProps)]
#[env(style)]
#[non_exhaustive]
pub enum DocumentTabLook {
/// Whole titles side by side, a `*` after a modified tab's text.
#[default]
Whole,
/// PdfCraft's tabs: they shrink before they overflow (a name at most 22 digits wide, at its least a readable
/// prefix), stand the inline gap apart, and a modified tab shows the marker dot in place of the `*`.
Shrinking,
}
/// 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]
pub enum TabKind {
/// Document tabs: spaced apart, shrinking titles, a close button on every closable tab, every document in the overflow menu.
/// Document tabs: whole titles, a close button on every closable tab, every document in the overflow menu. An app
/// 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.
@@ -68,14 +83,25 @@ pub enum OverflowMenu {
}
impl TabKind {
/// How tab widths are fitted: whole titles for text and ribbon tabs, shrinking for document and panel tabs.
/// How tab widths are fitted: whole titles for document, text and ribbon tabs, shrinking for panel tabs.
pub fn fit_mode(self) -> FitMode {
match self {
Self::Text | Self::Ribbon => FitMode::Whole,
Self::Document | Self::Panel => FitMode::Shrink { keep_selected: self.keeps_active_width() },
Self::Document | Self::Text | Self::Ribbon => FitMode::Whole,
Self::Panel => FitMode::Shrink { keep_selected: self.keeps_active_width() },
}
}
/// How tab widths are fitted under the app's document tab look: [`DocumentTabLook::Shrinking`] shrinks document
/// tabs as panel tabs do; every other kind and look is [`TabKind::fit_mode`].
pub fn fit_mode_in(self, look: DocumentTabLook) -> FitMode {
if self.shrinks_in(look) { FitMode::Shrink { keep_selected: self.keeps_active_width() } } else { self.fit_mode() }
}
/// 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.
pub fn keeps_active_width(self) -> bool {
self == Self::Panel
+4 -3
View File
@@ -1003,9 +1003,10 @@ impl<K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Collection
let (ghosts, departed) = self.reserved(cx, &keys);
// 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 fitting = Fitting { selected, mode: self.kind.fit_mode(), kept, ends, gap: cx.tokens.space.gap.inline, ghosts, departed };
// Document tabs stand apart by the inline gap; the other kinds touch.
let tab_gap = if self.kind == TabKind::Document { cx.tokens.space.gap.inline } else { 0.0 };
let look = cx.env::<super::DocumentTabLook>();
let fitting = Fitting { selected, mode: self.kind.fit_mode_in(look), 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 };
StripLayout::new(count, Vec2::new(width, cx.tokens.size.bar.tab), tab_gap, fitting, self.report.clone(), cx.frame_ctx().clone())
}
+49 -19
View File
@@ -76,6 +76,9 @@ const PROGRESS: PartKey = PartKey::new("progress");
/// How many of a title's first letters a shrunk tab still shows.
const MIN_LETTERS: usize = 3;
/// The modified mark of a tab whose look shows no marker dot for it.
const MARK: &str = "*";
const CLOSE_REASON: &str = "The Close command closes the focused tab";
/// The padding on each side of a ribbon tab's label: the strip insets its sliding underline by it, so the underline
@@ -131,6 +134,9 @@ pub struct TabStyle {
divider: bool,
close_side: CloseSide,
sizes: Sizes,
/// Document tabs in [`DocumentTabLook::Shrinking`](super::DocumentTabLook::Shrinking): a name capped at
/// `MAX_NAME_DIGITS` digits' width, a readable prefix at their least, and the marker dot for a modified tab.
shrinking: bool,
/// The marker dot's diameter.
marker: f32,
/// The marker dot of a marked tab.
@@ -146,7 +152,9 @@ struct Placed {
icon: Option<Rect>,
label: Rect,
detail: Option<Rect>,
/// The marker dot, after the last text: a modified tab's too.
/// The modified mark `*`, right after the last text, where the look shows no dot for it.
mark: Option<Rect>,
/// The marker dot, after the last text: a modified tab's too where the look shows the dot for it.
marker: Option<Rect>,
/// The band the label, detail and marker share.
band: Rect,
@@ -177,7 +185,8 @@ pub struct Tab<'a> {
/// An icon before the label.
#[prop(into)]
icon: Option<IconSpec<'a>>,
/// Unsaved changes: the marker dot after the detail, or after the label without one.
/// Unsaved changes: a `*` after the detail, or after the label without one; the marker dot there under
/// [`DocumentTabLook::Shrinking`](super::DocumentTabLook::Shrinking).
#[prop(flag)]
modified: bool,
/// Whether the tab can be closed; by default whether the kind shows a close button.
@@ -237,10 +246,20 @@ impl<'a> Tab<'a> {
TextRun::new(text).role(style.label.role)
}
/// Whether the marker dot shows: the tab's content has something to show, or the document is modified (the one
/// modified mark of the suite is the dot).
fn dotted(&self) -> bool {
self.marked || self.modified
/// Whether the marker dot shows: the tab's content has something to show, or the document is modified and the
/// look marks a modified tab with the dot.
fn dotted(&self, style: &TabStyle) -> bool {
self.marked || (self.modified && style.shrinking)
}
/// Whether the modified mark `*` shows: a modified tab whose look shows no dot for it.
fn starred(&self, style: &TabStyle) -> bool {
self.modified && !style.shrinking
}
/// The modified mark, in the label's type.
fn mark_run(&self, style: &TabStyle) -> TextRun<'static> {
TextRun::new(MARK).role(style.label.role)
}
/// Where the parts lie in `content`, with `natural` the unbounded width of a run. `parts` registers its parts
@@ -265,11 +284,13 @@ impl<'a> Tab<'a> {
let band = flow.rect();
let label_natural = natural(&self.label_run(style, label));
let detail_natural = self.detail_text().map_or(0.0, |text| natural(&TextRun::new(text).role(style.detail.role)));
// The marker keeps its room; the label is cut first, then the detail.
let marker_width = if self.dotted() { sizes.gap + style.marker } else { 0.0 };
// The marks keep their room; the label is cut first, then the detail.
let mark_width = if self.starred(style) { natural(&self.mark_run(style)) } else { 0.0 };
let marker_width = if self.dotted(style) { sizes.gap + style.marker } else { 0.0 };
let marks = mark_width + marker_width;
let detail_gap = if detail_natural > 0.0 { sizes.gap } else { 0.0 };
let label_width = label_natural.min((band.width() - marker_width - detail_gap - detail_natural).max(0.0));
let detail_width = detail_natural.min((band.width() - marker_width - label_width - detail_gap).max(0.0));
let label_width = label_natural.min((band.width() - marks - detail_gap - detail_natural).max(0.0));
let detail_width = detail_natural.min((band.width() - marks - label_width - detail_gap).max(0.0));
let line = |rect: Rect, height: f32| {
let top = if sizes.text_only && !sizes.middle { content.top() } else { content.center().y - height / 2.0 };
Rect::from_x_y_ranges(rect.x_range(), top..=top + height)
@@ -281,13 +302,19 @@ impl<'a> Tab<'a> {
} else {
(None, rest)
};
let (mark, after) = if mark_width > 0.0 {
let (mark, after) = after.take_start(mark_width);
(Some(line(mark, sizes.label_line)), after)
} else {
(None, after)
};
let label = line(label, sizes.label_line);
let marker = self.dotted().then(|| {
let marker = self.dotted(style).then(|| {
let slot = after.take_start(sizes.gap).1.take_start(style.marker).0;
Rect::from_center_size(Pos2::new(slot.center().x, label.center().y), Vec2::splat(style.marker))
});
let cut = label_width < label_natural || detail_width < detail_natural;
Placed { close, icon, label, detail, marker, band, cut }
Placed { close, icon, label, detail, mark, marker, band, cut }
}
fn name(&self, label: &str, words: &TabWords) -> String {
@@ -388,6 +415,7 @@ impl Component for Tab<'_> {
divider: key.kind.trailing_divider(),
close_side: close_side(cx.os, cx.direction.is_rtl()),
sizes,
shrinking: key.kind.shrinks_in(cx.env::<super::DocumentTabLook>()),
marker: marker.side,
marker_color: marker.color,
marker_edge: marker.edge,
@@ -401,18 +429,14 @@ impl Component for Tab<'_> {
let first: String = cx.resolved.label.chars().take(MIN_LETTERS).collect();
cx.layout_at(&self.label_run(cx.style, &first), f32::INFINITY).size().x
};
if cx.proposal == Proposal::Min && self.kind != TabKind::Document {
if cx.proposal == Proposal::Min && !cx.style.shrinking {
return Vec2::new(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, cx.resolved.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 self.kind == TabKind::Document {
label_width.min(width(&self.label_run(cx.style, &"0".repeat(MAX_NAME_DIGITS))))
} else {
label_width
};
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;
@@ -420,7 +444,10 @@ impl Component for Tab<'_> {
if let Some(detail) = self.detail_text() {
natural += sizes.gap + width(&TextRun::new(detail).role(cx.style.detail.role));
}
if self.dotted() {
if self.starred(cx.style) {
natural += width(&self.mark_run(cx.style));
}
if self.dotted(cx.style) {
natural += sizes.gap + cx.style.marker;
}
if self.shows_close() {
@@ -523,6 +550,9 @@ impl Component for Tab<'_> {
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());
}
if let Some(at) = placed.mark {
draw.text(at, &self.mark_run(style).color(cx.resolve(&style.label.color)));
}
if let Some(at) = placed.marker {
paint_marker(&draw, at.center(), &MarkerStyle { side: style.marker, color: style.marker_color, edge: style.marker_edge });
}
+3 -3
View File
@@ -413,8 +413,8 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
let unit = Adornment::text(unit, cx.style.unit.role).apart().measured(cx);
if shared { unit.in_column(cx.ui.ctx(), &UNITS) } else { unit }
});
let chevrons =
chevrons.iter().map(|(key, name)| Adornment::button(*key, crate::controls::disclosure::DisclosureGlyph::dropdown(), name.clone()));
let arrow = crate::controls::disclosure::DisclosureGlyph::dropdown_of(cx.env.get());
let chevrons = chevrons.iter().map(|(key, name)| Adornment::button(*key, arrow.clone(), name.clone()));
unit.into_iter().chain(chevrons).collect()
}
@@ -737,7 +737,7 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
glyph.paint(ui, *at, cx.resolve(&cx.style.handle), self.options.role);
}
for (key, name) in chevrons {
ui.face(key, IconButton::new(crate::controls::disclosure::DisclosureGlyph::dropdown()).label(name));
ui.face(key, IconButton::new(crate::controls::disclosure::DisclosureGlyph::dropdown_of(cx.env.get())).label(name));
}
}
+5 -5
View File
@@ -310,13 +310,13 @@ impl<C: Codec + Clone + 'static> TextField<'_, C> {
}
/// The adornments after the text, in reading order: the clear button when `clear` names it, then the arrow.
fn trailing(&self, clear: Option<Label>) -> SmallVec<[Adornment; 2]> {
fn trailing(&self, clear: Option<Label>, arrow: crate::controls::disclosure::DropdownArrow) -> SmallVec<[Adornment; 2]> {
let mut trailing = SmallVec::new();
if let Some(name) = clear {
trailing.push(Adornment::button(CLEAR, IconSpec::svg(IconId::new("close")), name));
}
if self.arrow {
trailing.push(Adornment::glyph(crate::controls::disclosure::DisclosureGlyph::dropdown()));
trailing.push(Adornment::glyph(crate::controls::disclosure::DisclosureGlyph::dropdown_of(arrow)));
}
trailing
}
@@ -377,7 +377,7 @@ impl<C: Codec + Clone + 'static> Component for TextField<'_, C> {
}
let clear = (self.search && self.clear_button).then(|| Self::clear_name(&cx.words::<words::TextWords>()));
self.leading = std::mem::take(&mut self.leading).into_iter().map(|adornment| adornment.measured(cx)).collect();
size.x += adorn::reserve(cx.style, &self.leading, &self.trailing(clear));
size.x += adorn::reserve(cx.style, &self.leading, &self.trailing(clear, cx.env.get()));
// Short text keeps a field at `size.length.short`, so a column of fields ends at one edge; a content-sized field
// takes its text and adornments only, as a content-sized number field does.
if cx.proposal != Proposal::Min && cx.resolved.sizing() != Some(Sizing::Content) {
@@ -402,7 +402,7 @@ impl<C: Codec + Clone + 'static> Component for TextField<'_, C> {
fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) {
let value = cx.value();
let clear = self.clear_shown(value).then(|| Self::clear_name(&cx.words::<words::TextWords>()));
let trailing = self.trailing(clear);
let trailing = self.trailing(clear, cx.env.get());
self.leading = std::mem::take(&mut self.leading).into_iter().map(|adornment| adornment.measured(cx)).collect();
let placed = adorn::place(cx.content_rect, cx.style, &self.leading, &trailing, cx.flow.is_rtl());
for (adornment, rect) in trailing.iter().zip(&placed.trailing) {
@@ -441,7 +441,7 @@ impl<C: Codec + Clone + 'static> Component for TextField<'_, C> {
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
let clear = cx.part_if_declared(CLEAR).is_some().then(|| Self::clear_name(&cx.words::<words::TextWords>()));
let trailing = self.trailing(clear);
let trailing = self.trailing(clear, cx.env.get());
let placed = adorn::place(cx.content_rect, cx.style, &self.leading, &trailing, cx.env.direction().is_rtl());
let color = cx.resolve(&cx.style.glyph.color);
for (adornment, rect) in self.leading.iter().chain(&trailing).zip(placed.leading.iter().chain(&placed.trailing)) {
@@ -1,25 +1,44 @@
//! The arrow of a select.
//! The arrow of a select: the stroked chevron by default, the filled down triangle under `DropdownArrow::Triangle`.
use craft_ui::contract::{Bind, Entry, Show as _};
use craft_ui::controls::disclosure::DropdownArrow;
use craft_ui::pickers::Select;
use craft_ui::testing::{Fixture, trial};
use egui::Shape;
/// A select's arrow is a small filled triangle pointing down, as in a document app's choice boxes, not a stroked chevron.
#[trial]
fn a_select_ends_in_a_filled_down_triangle() {
/// Whether a select shown under `arrow` (`None`: the app sets none) paints a filled triangle, and whether it paints a
/// stroked chevron (an open three-point line), inside its rect.
fn arrow_shapes(arrow: Option<DropdownArrow>) -> (bool, bool) {
let rect = std::rc::Rc::new(std::cell::Cell::new(egui::Rect::NOTHING));
let seen = rect.clone();
let mut fixture = Fixture::new(move |ui| {
let entries = [Entry::new(0u8, "One"), Entry::new(1u8, "Two")];
seen.set(Select::new("Pick", Bind::given(1u8), &entries).show(ui).response.rect);
});
if let Some(arrow) = arrow {
fixture.root_value(arrow);
}
fixture.settle();
let select = rect.get();
let shapes = fixture.frame_shapes();
let triangle = shapes.iter().any(|shape| match shape {
Shape::Path(path) => path.points.len() == 3 && path.fill != egui::Color32::TRANSPARENT && select.contains_rect(path.visual_bounding_rect()),
_ => false,
});
assert!(triangle, "the select paints a filled triangle for its arrow");
let inside = |path: &egui::epaint::PathShape| path.points.len() == 3 && select.contains_rect(path.visual_bounding_rect());
let triangle = shapes.iter().any(|shape| matches!(shape, Shape::Path(path) if inside(path) && path.fill != egui::Color32::TRANSPARENT));
let chevron =
shapes.iter().any(|shape| matches!(shape, Shape::Path(path) if inside(path) && !path.closed && path.fill == egui::Color32::TRANSPARENT));
(triangle, chevron)
}
/// Upstream's default: a select ends in the fine stroked chevron.
#[trial]
fn a_select_ends_in_a_chevron_by_default() {
let (triangle, chevron) = arrow_shapes(None);
assert!(chevron && !triangle, "the select paints a stroked chevron for its arrow (triangle {triangle}, chevron {chevron})");
}
/// PdfCraft's look (`DropdownArrow::Triangle`): a small filled triangle pointing down, as in a document app's choice
/// boxes, not a stroked chevron.
#[trial]
fn a_select_ends_in_a_filled_down_triangle_when_the_app_asks() {
let (triangle, chevron) = arrow_shapes(Some(DropdownArrow::Triangle));
assert!(triangle && !chevron, "the select paints a filled triangle for its arrow (triangle {triangle}, chevron {chevron})");
}
+33 -4
View File
@@ -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::{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};
@@ -21,12 +21,14 @@ fn overflowing_document_tabs_remain_readable_and_list_every_document() {
let tabs = [DocumentTab::new(0, "Document num")];
let _ = DocumentTabs::new("Reference", &tabs).selected(0).show(ui);
});
reference.root_value(DocumentTabLook::Shrinking);
reference.room(egui::vec2(600.0, 100.0)).settle();
let minimum = reference.by_label("Document num").expect("reference tab").rect.width();
let mut fixture = Fixture::new(|ui| {
let tabs: Vec<_> = (0..12).map(|i| DocumentTab::new(i, format!("Document number {i}, scanned.pdf"))).collect();
let _ = DocumentTabs::new("Documents", &tabs).selected(0).show(ui);
});
fixture.root_value(DocumentTabLook::Shrinking);
fixture.room(egui::vec2(600.0, 100.0)).settle();
let shown: Vec<_> = (0..12).filter_map(|i| fixture.by_label(&format!("Document number {i}, scanned.pdf")).map(|node| node.rect)).collect();
assert!(shown.len() > 1, "more than one tab remains visible: {shown:?}");
@@ -56,6 +58,7 @@ fn document_tabs_leave_space_between_neighbours() {
let tabs = [DocumentTab::new(1, "One"), DocumentTab::new(2, "Two")];
let _ = DocumentTabs::new("Documents", &tabs).selected(1).show(ui);
});
fixture.root_value(DocumentTabLook::Shrinking);
fixture.settle();
let first = fixture.by_label("One").expect("first tab").rect;
let second = fixture.by_label("Two").expect("second tab").rect;
@@ -64,7 +67,7 @@ fn document_tabs_leave_space_between_neighbours() {
#[trial]
fn document_tabs_shrink_before_overflow() {
let fit = TabKind::Document.fit_mode().fit(&[100.0, 100.0, 100.0], 0, &[], 240.0, 50.0, 20.0);
let fit = TabKind::Document.fit_mode_in(DocumentTabLook::Shrinking).fit(&[100.0, 100.0, 100.0], 0, &[], 240.0, 50.0, 20.0);
assert!(fit.overflow.is_empty(), "tabs that can shrink into the strip remain visible");
assert!(fit.shown.iter().all(|(_, width)| *width < 100.0));
}
@@ -87,6 +90,7 @@ fn document_tabs_with_trailing_control_fill_their_parent_row() {
Button::new("Settings").show(ui);
});
});
fixture.root_value(DocumentTabLook::Shrinking);
fixture.room(egui::vec2(1000.0, 100.0)).settle();
let visible = (0..5).filter(|i| fixture.by_label(&format!("Document number {i}, scanned.pdf")).is_some()).count();
assert!(visible > 1, "the strip uses the available width: {visible} tab visible");
@@ -108,6 +112,7 @@ fn document_tabs_reserve_space_for_the_trailing_control() {
})
.show(ui);
});
fixture.root_value(DocumentTabLook::Shrinking);
fixture.room(egui::vec2(1000.0, 100.0)).settle();
let open = fixture.by_label("Open").unwrap_or_else(|| panic!("trailing button: {:?}", trailing_rect.get())).rect;
for i in 0..5 {
@@ -132,6 +137,7 @@ fn the_trailing_control_follows_the_last_tab_when_they_fit() {
})
.show(ui);
});
fixture.root_value(DocumentTabLook::Shrinking);
fixture.room(egui::vec2(1000.0, 100.0)).settle();
let open = fixture.by_label("Open").expect("trailing button").rect;
let last = fixture.by_label("Doc 1.pdf").expect("last tab").rect;
@@ -158,6 +164,7 @@ fn nested_document_tabs_reserve_space_for_the_trailing_control() {
Button::new("Settings").show(ui);
});
});
fixture.root_value(DocumentTabLook::Shrinking);
fixture.room(egui::vec2(1000.0, 100.0)).settle();
let open = fixture.by_label("Open").expect("trailing button").rect;
for i in 0..5 {
@@ -620,11 +627,13 @@ fn a_secondary_click_on_a_document_tab_asks_for_its_menu_without_showing_it() {
#[trial]
fn a_modified_document_tab_shows_a_dot_and_no_asterisk() {
use craft_ui::tabs::{DocumentTab, DocumentTabs};
let look = DocumentTabLook::Shrinking;
let tab = |modified: bool| {
let mut fixture = Fixture::new(move |ui| {
let tabs = [DocumentTab::new(0, "Report.pdf").modified(modified)];
let _ = DocumentTabs::new("Docs", &tabs).selected(0).show(ui);
});
fixture.root_value(look);
fixture.room(egui::vec2(600.0, 100.0)).settle();
let stars = fixture.texts().iter().filter(|run| run.text.contains('*')).count();
let width = fixture.by_role(Role::TAB).first().map_or(0.0, |tab| tab.rect.width());
@@ -635,6 +644,25 @@ fn a_modified_document_tab_shows_a_dot_and_no_asterisk() {
assert!(modified.1 > plain.1, "the dot takes room beside the name: {} vs {}", modified.1, plain.1);
}
/// Upstream's document tabs by default (no `DocumentTabLook` set): whole titles fitted with no shrink, neighbours
/// touching, and a `*` after a modified tab's name with no dot.
#[trial]
fn document_tabs_keep_whole_touching_titles_and_an_asterisk_by_default() {
use craft_ui::tabs::{DocumentTab, DocumentTabs, FitMode};
assert_eq!(TabKind::Document.fit_mode(), FitMode::Whole);
assert_eq!(TabKind::Document.fit_mode_in(DocumentTabLook::default()), FitMode::Whole);
let mut fixture = Fixture::new(|ui| {
let tabs = [DocumentTab::new(1, "One"), DocumentTab::new(2, "Two").modified(true)];
let _ = DocumentTabs::new("Documents", &tabs).selected(1).show(ui);
});
fixture.room(egui::vec2(600.0, 100.0)).settle();
let first = fixture.by_label("One").expect("first tab").rect;
let second = fixture.by_role(Role::TAB).get(1).map(|tab| tab.rect).expect("second tab");
assert!((second.left() - first.right()).abs() < 0.5, "neighbouring document tabs touch: {first:?}, {second:?}");
let stars = fixture.texts().iter().filter(|run| run.text.contains('*')).count();
assert_eq!(stars, 1, "the modified tab shows its asterisk");
}
/// Production shows the held tab in the strip and no filename badge by the pointer: while a document tab is dragged, its
/// name is painted once.
#[trial]
@@ -659,8 +687,8 @@ fn a_dragged_document_tab_shows_no_badge() {
assert_eq!(shown, 1, "the name is painted once, by the held tab, with no badge beside the pointer");
}
/// A very long document name does not stretch its tab across the strip: the tab stays near 22 characters wide and the
/// next tab keeps its room.
/// Under `DocumentTabLook::Shrinking` (PdfCraft's tabs) a very long document name does not stretch its tab across the
/// strip: the tab stays near 22 characters wide and the next tab keeps its room.
#[trial]
fn a_very_long_document_name_is_cut_to_a_compact_tab() {
use craft_ui::tabs::{DocumentTab, DocumentTabs};
@@ -669,6 +697,7 @@ fn a_very_long_document_name_is_cut_to_a_compact_tab() {
let mut fixture = Fixture::new(move |ui| {
let _ = DocumentTabs::new("Documents", &tabs).selected(0).show(ui);
});
fixture.root_value(DocumentTabLook::Shrinking);
fixture.idle(6);
let tab = fixture.by_label(long).expect("the long tab").rect;
let short = fixture.by_label("Short").expect("the next tab").rect;