diff --git a/crates/ui-contract/src/contract/collection/response.rs b/crates/ui-contract/src/contract/collection/response.rs index da4ca07fe..53614905e 100644 --- a/crates/ui-contract/src/contract/collection/response.rs +++ b/crates/ui-contract/src/contract/collection/response.rs @@ -96,11 +96,19 @@ pub struct ShownRow { pub cells: Vec<(ColumnKey, egui::Rect)>, /// The openers the row's item asked for (`ItemCx::part_opener`), each with the part's response this pass. pub openers: Vec<(crate::contract::ItemOpener, Response)>, + /// The pointer is over the row and the collection owns the hover (no press or layer above holds it). + pub hovered: bool, } impl ShownRow { pub(crate) fn new(key: K, rect: egui::Rect, cells: Vec<(ColumnKey, egui::Rect)>) -> Self { - ShownRow { key, rect, cells, openers: Vec::new() } + ShownRow { key, rect, cells, openers: Vec::new(), hovered: false } + } + + /// The row with its hover fact set. + pub(crate) fn with_hover(mut self, hovered: bool) -> Self { + self.hovered = hovered; + self } } diff --git a/crates/ui-contract/src/contract/cx/pointer.rs b/crates/ui-contract/src/contract/cx/pointer.rs index 3e27d59a1..81efe758e 100644 --- a/crates/ui-contract/src/contract/cx/pointer.rs +++ b/crates/ui-contract/src/contract/cx/pointer.rs @@ -65,8 +65,13 @@ pub trait Pointer { } impl Pointer for CxCore<'_> { + /// Step 12 leaves the report; a run that stopped before it (a sizing pass) reports no stay, so its output never + /// repeats an `entered` or `left` the real pass reported. fn hover(&self) -> Hover { - todo!("cv.K45") + if !self.stated || self.face_mode { + return Hover::default(); + } + self.memory.recall::(crate::contract::steps::ShownState::hover_slot(self.id)) } fn part(&self, part: impl Into) -> &PartResult { diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index c3453a644..19d5c1153 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -662,7 +662,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { row = self.screen(&layout, rect, at, offset, rtl); } } - shown_rows.push(ShownRow::new(key.clone(), row, shown_cells.clone())); + shown_rows.push(ShownRow::new(key.clone(), row, shown_cells.clone()).with_hover(state.hovered)); let Some((mut row_ui, installed)) = row_ui else { debug_assert!(tip.is_none(), "a member of a collection's run takes no rich tooltip"); let shown = shown_rows.len() - 1; diff --git a/crates/ui-contract/src/contract/steps/state.rs b/crates/ui-contract/src/contract/steps/state.rs index ed12751c2..20ac3c8bd 100644 --- a/crates/ui-contract/src/contract/steps/state.rs +++ b/crates/ui-contract/src/contract/steps/state.rs @@ -2,7 +2,7 @@ use super::paint::surface_ink; use crate::contract::run::ErasedPass; -use crate::contract::{Component, Cx, Validity}; +use crate::contract::{Component, Cx, Hover, Validity}; use crate::runtime::access::scope; use crate::runtime::motion::{Drives as _, spec}; use crate::style::{Flags, Ink, StateSet, StyleView}; @@ -98,7 +98,20 @@ pub(crate) fn state(p: &mut dyn ErasedPass<'_>, style: &dyn StyleView, open: boo } else { State::Rest }; - p.shown_state().last_state = (hovered, visible); + let clicked = crate::runtime::egui::response::pointer_clicked(&core.response); + let face = core.face_mode; + let shown = p.shown_state(); + let was = shown.last_state.0; + shown.last_state = (hovered, visible); + // The click is held while the pointer stays over the control and dropped in the pass it leaves. A pass that sees + // no click (a second pass of a frame gets no input) keeps what the first pass held. + shown.clicked_over = hovered && (clicked || shown.clicked_over); + let report = Hover { over: hovered, entered: hovered && !was, left: was && !hovered, clicked: shown.clicked_over }; + // A face shows a look, not a stay of the pointer: it keeps no report. + if !face { + let core = p.core(); + *core.memory.shown::(super::ShownState::hover_slot(core.id)) = report; + } let t = ease(p, [hovered, pressed, flags.intersects(Flags::ON)]); let core = p.core(); core.state = StateSet::new(state, flags, t); diff --git a/crates/ui-contract/src/contract/steps/stops.rs b/crates/ui-contract/src/contract/steps/stops.rs index 3374cb9a0..f156af64d 100644 --- a/crates/ui-contract/src/contract/steps/stops.rs +++ b/crates/ui-contract/src/contract/steps/stops.rs @@ -127,8 +127,11 @@ pub(crate) fn register_stops(input: &StopsIn<'_>) -> Vec { after_header } -/// Whether the component is a focus stop: an enabled one, or a disabled one whose tooltip has a `disabled_text`, which -/// shows its reason on keyboard focus (testing 22.4). Step 9 still drops its activations and edits. +/// Whether the component is a focus stop: an enabled one, or a disabled one with a reason to show on keyboard focus +/// (testing 22.4): its tooltip's `disabled_text`, or its bound command's hint, which its tooltip shows. Step 9 still +/// drops its activations and edits. pub(crate) fn takes_focus(resolved: &Resolved<'_>) -> bool { - resolved.enabled || resolved.tooltip.is_some_and(|tooltip| tooltip.spec().disabled_text.is_some()) + resolved.enabled + || resolved.tooltip.is_some_and(|tooltip| tooltip.spec().disabled_text.is_some()) + || resolved.command.and_then(|command| command.hint.as_deref()).is_some_and(|hint| !hint.is_empty()) } diff --git a/crates/ui-contract/src/contract/steps/value.rs b/crates/ui-contract/src/contract/steps/value.rs index 9554846a3..13768c35d 100644 --- a/crates/ui-contract/src/contract/steps/value.rs +++ b/crates/ui-contract/src/contract/steps/value.rs @@ -24,11 +24,18 @@ pub(crate) struct Shown { pub(crate) struct ShownState { pub last_state: (bool, bool), /// A pointer click on the control, while the pointer has stayed over it since. - #[allow(dead_code)] // P3(ribbon-w2): cv.K45 pub clicked_over: bool, pub tweens: Option, } +impl ShownState { + /// The shown slot under which step 12 leaves the pass's `Hover` report of component `id`, for `Pointer::hover`: + /// the core that answers it cannot name the typed `Shown` record this state lives in. + pub(crate) fn hover_slot(id: Id) -> Id { + crate::runtime::memory::sub(id, "hover") + } +} + impl Clone for Shown { fn clone(&self) -> Self { Shown { copy: self.copy.clone(), drawn: self.drawn, state: self.state.clone() } diff --git a/crates/ui-contract/src/contract/tooltip.rs b/crates/ui-contract/src/contract/tooltip.rs index c7f4f37a1..e21e2c48f 100644 --- a/crates/ui-contract/src/contract/tooltip.rs +++ b/crates/ui-contract/src/contract/tooltip.rs @@ -73,26 +73,33 @@ fn spec_text(env: &crate::runtime::env::EnvView<'_>, spec: &TooltipSpec, enabled /// The text the component's own tooltip shows, or `None` when it has none: its validity message when no container /// shows it (`message`); else the tooltip's text, or its disabled text while disabled, with the bound command's /// chord; else the full label when the paint cut it, or its own label when the component declares it paints no -/// label (`shows_label`). +/// label (`shows_label`), its face dropped the label (`PaintRecord::label_dropped`) or its command has a hint. A +/// bound command's hint follows on a line of its own. fn text_of(core: &super::CxCore<'_>, shows_label: bool, message: Option) -> Option { if message.is_some() { return message; } + let hint = core.command.as_deref().and_then(|command| command.hint.as_deref()).filter(|hint| !hint.is_empty()); let base = match core.resolved.tooltip.map(|tooltip| tooltip.spec()) { Some(spec) => spec_text(&core.env, spec, core.resolved.enabled), None => { let paint = core.paint.borrow(); + let unpainted = !shows_label || paint.label_dropped || hint.is_some(); match paint.elided.clone() { Some(full) => full, - None if !shows_label && !core.resolved.label.is_empty() => core.resolved.label.to_owned(), + None if unpainted && !core.resolved.label.is_empty() => core.resolved.label.to_owned(), None => return None, } } }; let platform = crate::runtime::windows::os_of(&core.ctx); - Some(match core.command.as_deref().and_then(|command| command.chord.as_ref()) { + let text = match core.command.as_deref().and_then(|command| command.chord.as_ref()) { Some(chord) => with_chord(&base, chord, platform), None => base, + }; + Some(match hint { + Some(hint) => format!("{text}\n{hint}"), + None => text, }) } diff --git a/crates/ui-contract/src/primitives/content.rs b/crates/ui-contract/src/primitives/content.rs index cd13babcf..972affcd1 100644 --- a/crates/ui-contract/src/primitives/content.rs +++ b/crates/ui-contract/src/primitives/content.rs @@ -91,6 +91,19 @@ impl<'a> ContentPainterCx<'a> { paint(&faded); } + /// Runs `paint` with a `ContentPainterCx` whose painting is clipped to `rect` (inside the clip it already has). + pub fn clipped(&self, rect: egui::Rect, paint: impl FnOnce(&ContentPainterCx<'_>)) { + let clipped = ContentPainterCx { + painter: self.painter.with_clip_rect(rect.intersect(self.painter.clip_rect())), + tokens: self.tokens, + direction: self.direction, + env: self.env, + ink: self.ink, + data: self.data, + }; + paint(&clipped); + } + /// The one sanctioned text layout for content painters: document runs with their own font, size and colour, /// laid out by the call `TextRun::layout` uses. pub fn layout(&self, text: &DocText<'_>) -> TextLayout { diff --git a/crates/ui-contract/src/runtime/access.rs b/crates/ui-contract/src/runtime/access.rs index 9bf1babcd..ffe882cfe 100644 --- a/crates/ui-contract/src/runtime/access.rs +++ b/crates/ui-contract/src/runtime/access.rs @@ -286,6 +286,8 @@ pub struct Access { pub(crate) required: bool, pub(crate) url: Option, pub(crate) shortcut: Option, + /// The node lists no shortcut, not even the bound command's. + pub(crate) no_shortcut: bool, pub(crate) access_key: Option, pub(crate) relations: Relations, pub(crate) custom_actions: Vec<(i32, Label)>, @@ -384,6 +386,14 @@ impl Access { /// The keyboard shortcut, over the bound command's. pub fn shortcut(&mut self, text: impl Into) -> &mut Self { self.shortcut = Some(text.into()); + self.no_shortcut = false; + self + } + + /// The node lists no keyboard shortcut, though a command bound to the component has a chord. + pub fn no_shortcut(&mut self) -> &mut Self { + self.shortcut = None; + self.no_shortcut = true; self } diff --git a/crates/ui-contract/src/runtime/access/emit.rs b/crates/ui-contract/src/runtime/access/emit.rs index a398579e6..527e18d54 100644 --- a/crates/ui-contract/src/runtime/access/emit.rs +++ b/crates/ui-contract/src/runtime/access/emit.rs @@ -272,7 +272,7 @@ pub(crate) fn report(ctx: &Context, own: &Own<'_>) { spec.has_popup = access.has_popup.or_else(|| popup.map(|popup| popup.kind)); spec.required = access.required; spec.url = access.url.clone(); - spec.shortcut = access.shortcut.clone().or_else(|| own.shortcut.map(str::to_owned)); + spec.shortcut = if access.no_shortcut { None } else { access.shortcut.clone().or_else(|| own.shortcut.map(str::to_owned)) }; spec.access_key = access.access_key; spec.labelled_by = relations.labelled_by.clone(); spec.described_by = relations.described_by.clone(); diff --git a/crates/ui-widgets/src/buttons/button.rs b/crates/ui-widgets/src/buttons/button.rs index cc269248f..0329b1a60 100644 --- a/crates/ui-widgets/src/buttons/button.rs +++ b/crates/ui-widgets/src/buttons/button.rs @@ -97,7 +97,7 @@ impl ButtonOutput { (true, Some(activation)) => activation.mods, _ => Mods::NONE, }; - ButtonOutput { response: cx.response.clone(), clicks, mods, hover: Hover::default() } + ButtonOutput { response: cx.response.clone(), clicks, mods, hover: cx.hover() } } /// Whether the button was clicked this pass, by pointer or by key, by the framework's click rule. It stands in @@ -157,6 +157,7 @@ pub struct Button<'a> { #[prop] align: HAlign, /// How the icon and label are laid out: beside each other, or the icon over the label. + #[style_key] #[prop] arrangement: Arrangement, } @@ -209,6 +210,9 @@ impl Component for Button<'_> { Some(tint) => ButtonStyle::tinted(cx, tint), None => ButtonStyle::of(cx), }; + if key.arrangement == Arrangement::Stacked { + style.metrics = face::stacked_metrics(cx, &cx.variant_look()); + } if let Some(least) = key.least_width { style.metrics.min_size.x = style.metrics.min_size.x.max(least.points(cx.tokens)); } diff --git a/crates/ui-widgets/src/buttons/face.rs b/crates/ui-widgets/src/buttons/face.rs index 324fb097c..5fa1374db 100644 --- a/crates/ui-widgets/src/buttons/face.rs +++ b/crates/ui-widgets/src/buttons/face.rs @@ -1,8 +1,9 @@ //! What a push or toggle button shows: a leading icon (or spinner), a label and a trailing icon, centred in the //! button unless it aligns them to an edge, and the look parts the buttons share. +use super::GroupLevel; use crate::contract::{Component, Cx, MeasureCx, PaintUi, Styled as _}; -use crate::primitives::{HAlign, IconSpec, TextRun}; +use crate::primitives::{HAlign, IconSpec, TextLayout, TextRun}; use crate::style::{FocusLook, GlyphRole, IconLook, StyleColor, StyleCx, SurfaceKind, SurfaceLook, TextLook, TypeRole, UiTokens, VariantParts}; use craft_behavior::{Flow, Insets, Metrics, Proposal}; use craft_theme::{ @@ -16,15 +17,30 @@ use egui::{Rect, Vec2, pos2}; pub(crate) struct FaceLook { /// The gap between icon and label. pub gap: f32, + /// The side of a stacked face's leading icon (`stacked_side`). + pub stacked_icon: f32, + /// How far under a stacked face's leading icon the split button's parts meet. + pub divide_gap: f32, } impl FaceLook { - /// The face look of the instance's variant: the control's gap. + /// The face look of the instance's variant: the control's gap and the stacked icon side. pub(crate) fn of(cx: &StyleCx<'_>) -> FaceLook { - FaceLook { gap: cx.control().gap } + let control = cx.control(); + FaceLook { gap: control.gap, stacked_icon: stacked_side(cx.tokens, control.icon), divide_gap: cx.tokens.space.gap.tight } } } +/// The side of a stacked face's leading icon: the large icon, or the control's own icon when that is larger. The +/// style and the measure both take it from here, so the painted icon is the measured one. +pub(super) fn stacked_side(tokens: &UiTokens, control_icon: f32) -> f32 { + tokens.size.icon.lg.max(control_icon) +} + +/// The most characters a stacked label keeps on one line, as the ribbon's large buttons always kept them: a longer +/// label breaks at the space nearest its middle. +const ONE_LINE_CHARS: usize = 10; + /// The label and icon looks of the instance's variant: the variant's text colours at the control's icon side. pub(super) fn ink(cx: &StyleCx<'_>) -> (TextLook, IconLook) { let color = cx.variant_look().text; @@ -32,7 +48,7 @@ pub(super) fn ink(cx: &StyleCx<'_>) -> (TextLook, IconLook) { } /// How a button lays out its icon and label. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] #[non_exhaustive] pub enum Arrangement { /// The icon beside the label; no label where the `GroupLevel` is `Compact`. @@ -43,9 +59,9 @@ pub enum Arrangement { } /// What a face shows. +#[derive(Clone, Copy)] pub(crate) struct FaceSpec<'a> { /// How the icon and label are laid out. - #[allow(dead_code)] // P3(ribbon-w2): rb.R6 pub arrangement: Arrangement, pub label: &'a str, pub role: Option, @@ -79,6 +95,131 @@ impl FaceSpec<'_> { } } +impl<'a> FaceSpec<'a> { + /// Whether the face drops its label under `level`: an inline face beside a leading icon in a compact group. The + /// icon still shows and the label stays the accessible name and the tooltip. A stacked face keeps its label at + /// every level, and a face without an icon keeps it, since nothing else would show. + fn drops_label(&self, level: GroupLevel) -> bool { + self.arrangement == Arrangement::Inline && level == GroupLevel::Compact && self.lead.is_some() && !self.label.is_empty() + } + + /// The spec as the face shows it under `level`, with the label dropped where `drops_label` says. + fn at(self, level: GroupLevel) -> FaceSpec<'a> { + if self.drops_label(level) { FaceSpec { label: "", mnemonic: None, ..self } } else { self } + } + + /// One line of a stacked label: `text` with its character `at` marked. + fn line(&self, text: &'a str, at: Option) -> TextRun<'a> { + let run = TextRun::mnemonic(text, at).elide(); + match self.role { + Some(role) => run.role(role), + None => run, + } + } + + /// The lines of a stacked label: the label whole, or broken at its middle space when it has more than + /// `ONE_LINE_CHARS` characters. The mnemonic stays on the character it marks. + fn lines(&self) -> Vec> { + if self.label.is_empty() { + return Vec::new(); + } + let label = self.label; + let whole = self.line(label, self.mnemonic); + let split = middle_space(label).filter(|_| label.chars().count() > ONE_LINE_CHARS).and_then(|at| label.split_at_checked(at)); + let Some((first, rest)) = split else { return vec![whole] }; + let (first, second) = (first.trim_end(), rest.trim_start()); + // The characters before the second line: the first line and the spaces between. + let skipped = label.chars().count() - second.chars().count(); + let (on_first, on_second) = match self.mnemonic { + Some(at) if at < first.chars().count() => (Some(at), None), + Some(at) if at >= skipped => (None, Some(at - skipped)), + _ => (None, None), + }; + vec![self.line(first, on_first), self.line(second, on_second)] + } +} + +/// The byte offset of the space nearest the middle of `label` by characters, where a two-line label breaks. +fn middle_space(label: &str) -> Option { + let middle = label.chars().count() / 2; + label.char_indices().enumerate().filter(|(_, (_, c))| *c == ' ').min_by_key(|(n, _)| n.abs_diff(middle)).map(|(_, (at, _))| at) +} + +/// The width line `last` or not of a stacked face has in `room`: the last line leaves the trailing icon (a menu +/// button's chevron) its gap and `side`, so the chevron follows it. +fn line_room(spec: &FaceSpec<'_>, last: bool, room: f32, side: f32, gap: f32) -> f32 { + if last && spec.trail.is_some() { (room - side - gap).max(0.0) } else { room.max(0.0) } +} + +/// Where a stacked face's parts sit. +struct Stack { + /// The content size: the widest row, and the rows' heights with the gap under the icon. + size: Vec2, + lead: Option, + /// Each line's text rect. + lines: Vec, + trail: Option, +} + +/// Lays a stacked face out in `area` from its lines' sizes: the leading icon of `lead_side` centred at the top, a gap, +/// then each line centred under it, the trailing icon of `side` after the last line at its reading end (on a row of +/// its own when there is no line). The block is centred vertically; `size` is what `measure` reports. +fn stack(spec: &FaceSpec<'_>, sides: (f32, f32), gap: f32, lines: &[Vec2], area: Rect, rtl: bool) -> Stack { + let (lead_side, side) = sides; + let trail = spec.trail.is_some(); + let last = lines.len().checked_sub(1); + let mut rows: Vec = lines + .iter() + .enumerate() + .map(|(i, line)| if trail && Some(i) == last { Vec2::new(line.x + gap + side, line.y.max(side)) } else { *line }) + .collect(); + if lines.is_empty() && trail { + rows.push(Vec2::splat(side)); + } + let icon = if spec.lead.is_some() { lead_side } else { 0.0 }; + let under_icon = if spec.lead.is_some() && !rows.is_empty() { gap } else { 0.0 }; + let width = rows.iter().map(|row| row.x).fold(icon, f32::max); + let size = Vec2::new(width, icon + under_icon + rows.iter().map(|row| row.y).sum::()); + let middle = area.center().x; + let mut y = area.top() + (area.height() - size.y).max(0.0) / 2.0; + let lead = spec.lead.map(|_| Rect::from_min_size(pos2(middle - lead_side / 2.0, y), Vec2::splat(lead_side))); + y += icon + under_icon; + let mut placed = Stack { size, lead, lines: Vec::new(), trail: None }; + for (i, row) in rows.iter().enumerate() { + let band = Rect::from_min_size(pos2(middle - row.x / 2.0, y), *row); + match lines.get(i) { + Some(line) => { + let (text, rest) = Flow::new(band, rtl).take_start(line.x); + placed.lines.push(Rect::from_min_size(pos2(text.min.x, y + (row.y - line.y) / 2.0), *line)); + if trail && Some(i) == last { + let (icon, _) = rest.take_start(gap).1.take_start(side); + placed.trail = Some(Rect::from_center_size(icon.center(), Vec2::splat(side))); + } + } + None => placed.trail = Some(band), + } + y += row.y; + } + placed +} + +/// The sizes of a stacked face's `lines` as `measure` takes them: at their readable least for `Proposal::Min`, else +/// laid out in `room` (`line_room`). +fn stacked_sizes(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>, lines: &[TextRun<'_>], room: f32, side: f32) -> Vec { + let count = lines.len(); + lines + .iter() + .enumerate() + .map(|(i, run)| { + if cx.proposal == Proposal::Min { + Vec2::new(cx.least_width(run), cx.layout_at(run, f32::INFINITY).size().y) + } else { + cx.layout_at(run, line_room(spec, i + 1 == count, room, side, cx.gap())).size() + } + }) + .collect() +} + /// The metrics of a button with a face: the control's, with the horizontal padding at least the corner radius plus /// the gap, so the label and icons of a capsule clear its round ends. pub(super) fn metrics(cx: &StyleCx<'_>, parts: &VariantParts) -> Metrics { @@ -87,6 +228,24 @@ pub(super) fn metrics(cx: &StyleCx<'_>, parts: &VariantParts) -> Metrics { Metrics::new().padding(Insets::horizontal(control.pad_x.max(round + control.gap))).min_size(Vec2::new(0.0, control.height)).gap(control.gap) } +/// The metrics of a stacked face: `metrics`, with the control's gap above and below the stack, so its height is the +/// icon, the label lines and that padding, and never less than the control's height. +pub(super) fn stacked_metrics(cx: &StyleCx<'_>, parts: &VariantParts) -> Metrics { + let gap = cx.control().gap; + let mut metrics = metrics(cx, parts); + metrics.padding.top = gap; + metrics.padding.bottom = gap; + metrics +} + +/// `metrics` or `stacked_metrics`, by `arrangement`. +pub(super) fn metrics_of(cx: &StyleCx<'_>, parts: &VariantParts, arrangement: Arrangement) -> Metrics { + match arrangement { + Arrangement::Stacked => stacked_metrics(cx, parts), + _ => metrics(cx, parts), + } +} + /// The surface of a button from its variant: raised unless flat (ghost, or a custom look with no fill or edge at /// rest), the variant's fill and edge. pub(super) fn surface(cx: &StyleCx<'_>, parts: &VariantParts) -> SurfaceLook { @@ -167,8 +326,16 @@ pub(crate) fn focus(cx: &StyleCx<'_>) -> FocusLook { /// The size of the face: its parts and the gaps between them. /// The label elides in the room the icons and gaps leave, so a squeezed face is as wide as its room; at its least -/// the label is at its readable least. +/// the label is at its readable least. The group level comes from the env (`GroupLevel` is a style value, so the +/// measurement is keyed on it): a compact group's inline face measures without its label. pub(crate) fn measure(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>) -> Vec2 { + let spec = &spec.at(cx.env.get::()); + if spec.arrangement == Arrangement::Stacked { + let (side, lead_side) = (cx.icon(), stacked_side(cx.tokens, cx.icon())); + let lines = spec.lines(); + let sizes = stacked_sizes(cx, spec, &lines, cx.room.unwrap_or(f32::INFINITY), side); + return stack(spec, (lead_side, side), cx.gap(), &sizes, Rect::ZERO, false).size; + } let icon = cx.icon(); let fixed = icon * spec.icons() as f32 + cx.gap() * spec.gaps() as f32; let text = if spec.label.is_empty() { @@ -246,7 +413,17 @@ fn text_top(draw: &crate::primitives::Draw<'_>, area: Rect, text: &crate::primit } /// Paints the face in `area`, placed by `spec.align`: the leading icon at the reading start, the trailing icon at the reading end. +/// A face that drops its label (`FaceSpec::drops_label`) says so in the paint record, so the tooltip shows the label. pub(crate) fn paint(ui: &PaintUi<'_>, cx: &Cx<'_, C>, ink: (&TextLook, &IconLook), look: &FaceLook, spec: &FaceSpec<'_>, area: Rect) { + let level = cx.env.get::(); + if spec.drops_label(level) { + ui.draw().drop_label(); + } + let spec = &spec.at(level); + if spec.arrangement == Arrangement::Stacked { + paint_stacked(ui, cx, ink, look, spec, area); + return; + } let (text_look, icon_look) = ink; let (text_color, icon_color, side) = (cx.resolve(&text_look.color), cx.resolve(&icon_look.color), icon_look.side); let draw = ui.draw(); @@ -270,6 +447,54 @@ pub(crate) fn paint(ui: &PaintUi<'_>, cx: &Cx<'_, C>, ink: (&TextL } } +/// Paints a stacked face in `area` (`stack`): the leading icon over the label's lines, the trailing icon after the +/// last line. A loading face leaves the leading icon's slot (the label's, without an icon) to the spinner. +fn paint_stacked(ui: &PaintUi<'_>, cx: &Cx<'_, C>, ink: (&TextLook, &IconLook), look: &FaceLook, spec: &FaceSpec<'_>, area: Rect) { + let (text_look, icon_look) = ink; + let (text_color, icon_color, side) = (cx.resolve(&text_look.color), cx.resolve(&icon_look.color), icon_look.side); + let draw = ui.draw(); + let lines = spec.lines(); + let colored: Vec> = lines.into_iter().map(|run| run.color(text_color)).collect(); + let count = colored.len(); + let layouts: Vec = + colored.iter().enumerate().map(|(i, run)| draw.layout(run, line_room(spec, i + 1 == count, area.width(), side, look.gap))).collect(); + let sizes: Vec = layouts.iter().map(TextLayout::size).collect(); + let placed = stack(spec, (look.stacked_icon, side), look.gap, &sizes, area, cx.flow.is_rtl()); + if let (Some(lead), Some(slot)) = (spec.lead, placed.lead) + && !spec.loading + { + draw.icon(slot, &tinted(lead, icon_color)); + } + if !(spec.loading && spec.lead.is_none()) { + for (text, slot) in layouts.iter().zip(&placed.lines) { + draw.galley(slot.min, text); + } + } + if let (Some(trail), Some(slot)) = (spec.trail, placed.trail) { + draw.icon(slot, &tinted(trail, icon_color)); + } +} + +/// A stacked face placed in `area` as `paint_stacked` places it, its lines laid out for `area`'s width. +fn stacked_in(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>, look: &FaceLook, side: f32, area: Rect, rtl: bool) -> Stack { + let lines = spec.lines(); + let count = lines.len(); + let sizes: Vec = + lines.iter().enumerate().map(|(i, run)| cx.layout_at(run, line_room(spec, i + 1 == count, area.width(), side, look.gap)).size()).collect(); + stack(spec, (look.stacked_icon, side), look.gap, &sizes, area, rtl) +} + +/// Where a stacked split button's parts meet in `area`: halfway down the gap under the face's leading icon, so the +/// icon is the main part and the label and its chevron are the arrow part. Without an icon the parts meet at the +/// content's top: the whole face is the arrow part. +pub(super) fn stacked_divide(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>, look: &FaceLook, side: f32, area: Rect) -> f32 { + let spec = &spec.at(cx.env.get::()); + match stacked_in(cx, spec, look, side, area, false).lead { + Some(lead) => lead.bottom() + look.divide_gap, + None => area.top(), + } +} + /// Where the busy spinner of a loading face goes: the square of the icon side in the leading icon's slot, or in the /// label's slot when there is no leading icon. `None` when the face has neither. pub(super) fn spinner_rect( @@ -280,6 +505,14 @@ pub(super) fn spinner_rect( area: Rect, rtl: bool, ) -> Option { + let spec = &spec.at(cx.env.get::()); + if spec.arrangement == Arrangement::Stacked { + let placed = stacked_in(cx, spec, look, side, area, rtl); + return match placed.lead { + Some(lead) => Some(lead), + None => placed.lines.first().map(|line| Rect::from_center_size(line.center(), Vec2::splat(side))), + }; + } let fixed = side * spec.icons() as f32 + look.gap * spec.gaps() as f32; let text_width = if spec.label.is_empty() { 0.0 } else { cx.layout_at(&spec.run(), (area.width() - fixed).max(0.0)).size().x }; let slots = slots(spec, side, look.gap, text_width, area, rtl); diff --git a/crates/ui-widgets/src/buttons/split_button.rs b/crates/ui-widgets/src/buttons/split_button.rs index 7d3383cc4..062958bfa 100644 --- a/crates/ui-widgets/src/buttons/split_button.rs +++ b/crates/ui-widgets/src/buttons/split_button.rs @@ -3,7 +3,7 @@ use super::face::{self, Arrangement, FaceLook, FaceSpec}; use crate::contract::{ Access, BodyCx, Common, Component, Cx, Entry, Focus as _, Hover, MeasureCx, Name, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx, - Props as _, Role, ShowContainer as _, Styled as _, WordSetCx as _, + Pointer as _, Props, Role, ShowContainer as _, Styled as _, WordSetCx as _, }; use crate::menu::{self, Menu}; use crate::primitives::{IconSpec, PaintShape, RuleKind}; @@ -96,7 +96,9 @@ pub struct SplitButton<'a, T: Value + 'static> { /// A leading icon on the main part. #[prop(into)] icon: Option>, - /// How the icon and label are laid out: beside each other, or the icon over the label. + /// How the icon and label are laid out: beside each other with the arrow part at the end, or the icon over the + /// label with the arrow part (the label and its chevron) under the icon. + #[style_key] #[prop] arrangement: Arrangement, chosen: Option, @@ -105,36 +107,33 @@ pub struct SplitButton<'a, T: Value + 'static> { impl<'a, T: Value + 'static> SplitButton<'a, T> { /// A split button showing `label` whose arrow opens `entries`. pub fn new(label: impl Into