Ribbon menu arrows: RibbonIcons::arrow draws the filled triangle right after the label in its own room

This commit is contained in:
Martin Ball
2026-11-20 02:22:46 +00:00
parent 16cba38396
commit 44114f108b
3 changed files with 50 additions and 25 deletions
+24 -12
View File
@@ -24,6 +24,8 @@ pub(crate) struct FaceLook {
pub divide_gap: f32,
/// Whether a stacked face lays its block out from its top (`stacked_lines`): in a ribbon.
pub stacked_top: bool,
/// The room of a trailing menu arrow where the ribbon sets one (`RibbonIcons::arrow`); else the icon side.
pub arrow: Option<f32>,
}
impl FaceLook {
@@ -35,6 +37,7 @@ impl FaceLook {
stacked_icon: stacked_side(cx.tokens, control.icon, cx.env::<ControlPlace>(), cx.env::<RibbonIcons>()),
divide_gap: cx.tokens.space.gap.tight,
stacked_top: stacked_lines(cx.env::<ControlPlace>()) > 0,
arrow: arrow_room(cx.env::<ControlPlace>(), cx.env::<RibbonIcons>()),
}
}
}
@@ -53,6 +56,11 @@ pub(super) fn stacked_side(tokens: &UiTokens, control_icon: f32, place: ControlP
if place == ControlPlace::Ribbon && icons.large > 0 { f32::from(icons.large) } else { tokens.size.icon.lg.max(control_icon) }
}
/// The room a trailing menu arrow takes in a ribbon that sets one (`RibbonIcons::arrow`).
pub(crate) fn arrow_room(place: ControlPlace, icons: RibbonIcons) -> Option<f32> {
(place == ControlPlace::Ribbon && icons.arrow > 0).then_some(f32::from(icons.arrow))
}
/// 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;
@@ -388,7 +396,7 @@ pub(crate) fn measured_icon<C: Component>(cx: &MeasureCx<'_, C>) -> f32 {
pub(crate) fn measure<C: Component>(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>) -> Vec2 {
let spec = &spec.at(cx.env.get::<GroupLevel>());
if spec.arrangement == Arrangement::Stacked {
let side = measured_icon(cx);
let side = arrow_room(cx.env.get::<ControlPlace>(), cx.env.get::<RibbonIcons>()).unwrap_or_else(|| measured_icon(cx));
let lead_side = stacked_side(cx.tokens, cx.icon(), cx.env.get::<ControlPlace>(), cx.env.get::<RibbonIcons>());
let lines = spec.lines();
let sizes = stacked_sizes(cx, spec, &lines, cx.room.unwrap_or(f32::INFINITY), side);
@@ -403,7 +411,8 @@ pub(crate) fn measure<C: Component>(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>)
return Vec2::new(size.x, size.y.max(icon + keep as f32 * line.max(if spec.trail.is_some() { side } else { 0.0 })));
}
let icon = measured_icon(cx);
let fixed = spec.icons_width(icon, icon) + cx.gap() * spec.gaps() as f32;
let trail = arrow_room(cx.env.get::<ControlPlace>(), cx.env.get::<RibbonIcons>()).unwrap_or(icon);
let fixed = spec.icons_width(icon, trail) + cx.gap() * spec.gaps() as f32;
let text = if spec.label.is_empty() {
Vec2::ZERO
} else if cx.proposal == Proposal::Min {
@@ -430,8 +439,8 @@ struct Slots {
/// Places the icons of `side` and a label `text_width` wide with `gap` between them, in `area` by `spec.align`: the leading
/// icon (or picture) at the reading start, the trailing icon at the reading end.
fn slots(spec: &FaceSpec<'_>, side: f32, gap: f32, text_width: f32, area: Rect, rtl: bool) -> Slots {
let width = spec.icons_width(side, side) + gap * spec.gaps() as f32 + text_width;
fn slots(spec: &FaceSpec<'_>, (side, trail): (f32, f32), gap: f32, text_width: f32, area: Rect, rtl: bool) -> Slots {
let width = spec.icons_width(side, trail) + gap * spec.gaps() as f32 + text_width;
let free = (area.width() - width).max(0.0);
let lead = match spec.align {
HAlign::Start => 0.0,
@@ -461,7 +470,7 @@ fn slots(spec: &FaceSpec<'_>, side: f32, gap: f32, text_width: f32, area: Rect,
if gapped {
take(gap);
}
slots.trail = Some(take(side));
slots.trail = Some(take(trail));
}
slots
}
@@ -493,11 +502,12 @@ pub(crate) fn paint<C: Component>(ui: &PaintUi<'_>, cx: &Cx<'_, C>, ink: (&TextL
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 fixed = spec.icons_width(side, side) + look.gap * spec.gaps() as f32;
let trail_side = look.arrow.unwrap_or(side);
let fixed = spec.icons_width(side, trail_side) + look.gap * spec.gaps() as f32;
let run = spec.run().color(text_color);
let text = if spec.label.is_empty() { None } else { Some(draw.layout(&run, (area.width() - fixed).max(0.0))) };
let text_width = text.as_ref().map_or(0.0, |text| text.size().x);
let slots = slots(spec, side, look.gap, text_width, area, cx.flow.is_rtl());
let slots = slots(spec, (side, trail_side), look.gap, text_width, area, cx.flow.is_rtl());
if let (Some(lead), Some(slot)) = (spec.lead, slots.lead)
&& !spec.loading
{
@@ -509,7 +519,7 @@ pub(crate) fn paint<C: Component>(ui: &PaintUi<'_>, cx: &Cx<'_, C>, ink: (&TextL
draw.galley(pos2(slot.min.x, text_top(&draw, area, text)), text);
}
if let (Some(trail), Some(slot)) = (spec.trail, slots.trail) {
draw.icon(icon_at(slot, area, side), &tinted(trail, icon_color));
draw.icon(icon_at(slot, area, trail_side), &tinted(trail, icon_color));
}
}
@@ -517,7 +527,8 @@ pub(crate) fn paint<C: Component>(ui: &PaintUi<'_>, cx: &Cx<'_, C>, ink: (&TextL
/// last line. A loading face leaves the leading icon's slot (the label's, without an icon) to the spinner.
fn paint_stacked<C: Component>(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 (text_color, icon_color) = (cx.resolve(&text_look.color), cx.resolve(&icon_look.color));
let side = look.arrow.unwrap_or(icon_look.side);
let draw = ui.draw();
let lines = spec.lines();
let colored: Vec<TextRun<'_>> = lines.into_iter().map(|run| run.color(text_color)).collect();
@@ -586,9 +597,10 @@ pub(super) fn spinner_rect<C: Component>(
/// The slots of an inline face in `area`, its label laid out in the room the icons and gaps leave.
fn inline_slots<C: Component>(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>, look: &FaceLook, side: f32, area: Rect, rtl: bool) -> Slots {
let fixed = spec.icons_width(side, side) + look.gap * spec.gaps() as f32;
let trail = look.arrow.unwrap_or(side);
let fixed = spec.icons_width(side, trail) + 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 };
slots(spec, side, look.gap, text_width, area, rtl)
slots(spec, (side, trail), look.gap, text_width, area, rtl)
}
/// The box of the face's leading part in `area`, as `paint` places it: the leading icon's square, or the picture's
@@ -596,7 +608,7 @@ fn inline_slots<C: Component>(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>, look:
pub(super) fn lead_rect<C: Component>(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>, look: &FaceLook, side: f32, area: Rect, rtl: bool) -> Option<Rect> {
let spec = &spec.at(cx.env.get::<GroupLevel>());
if spec.arrangement == Arrangement::Stacked {
return stacked_in(cx, spec, look, side, area, rtl).lead;
return stacked_in(cx, spec, look, look.arrow.unwrap_or(side), area, rtl).lead;
}
let slot = inline_slots(cx, spec, look, side, area, rtl).lead?;
Some(Rect::from_center_size(pos2(slot.center().x, area.center().y), Vec2::new(slot.width(), side)))
+10 -1
View File
@@ -82,12 +82,21 @@ pub struct RibbonIcons {
pub large: u8,
/// Every other button's icon and glyph.
pub small: u8,
/// The room a menu arrow after a label takes. Set, the ribbon's buttons draw that arrow as the small filled
/// triangle right after their label, as the old ribbon drew it, in place of the stroked chevron.
pub arrow: u8,
}
impl RibbonIcons {
/// Stacked icons `large` points square and the others `small`; 0 keeps the framework's side.
pub const fn new(large: u8, small: u8) -> Self {
RibbonIcons { large, small }
RibbonIcons { large, small, arrow: 0 }
}
/// Menu arrows as filled triangles in `arrow` points of room right after the label; 0 keeps the chevron.
pub const fn arrow(mut self, arrow: u8) -> Self {
self.arrow = arrow;
self
}
}
+16 -12
View File
@@ -146,10 +146,13 @@ impl<'a, T: Value + 'static> SplitButton<'a, T> {
}
/// The chevron the arrow part shows.
fn chevron() -> IconSpec<'static> {
crate::controls::disclosure::DisclosureGlyph::menu_button()
fn chevron(look: &FaceLook) -> IconSpec<'static> {
if look.arrow.is_some() { IconSpec::glyph(&RIBBON_ARROW) } else { crate::controls::disclosure::DisclosureGlyph::menu_button() }
}
/// The ribbon's menu arrow where it sets one (`RibbonIcons::arrow`): the small filled triangle, pointing down.
const RIBBON_ARROW: crate::controls::disclosure::DisclosureGlyph = crate::controls::disclosure::DisclosureGlyph::new(1.0);
impl<T: Value + 'static> Component for SplitButton<'_, T> {
const ROLE: Role = Role::BUTTON;
const KEYS: crate::runtime::focus::KeyUse = crate::runtime::focus::KeyUse::of(crate::contract::Input::Pointer).with_moves();
@@ -185,11 +188,12 @@ impl<T: Value + 'static> Component for SplitButton<'_, T> {
/// Inline: the face and the arrow part, which sits in the end padding. Stacked: the face, whose chevron follows
/// the label inside the arrow part.
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
let chevron = chevron();
let arrow = face::arrow_room(cx.env.get::<super::ControlPlace>(), cx.env.get::<super::RibbonIcons>());
let chevron = chevron(&FaceLook { arrow, ..cx.style.face.clone() });
let face = face::measure(cx, &self.spec(cx.resolved.label, &chevron));
match self.arrangement {
Arrangement::Stacked => face,
_ => Vec2::new(face.x + face::measured_icon(cx), face.y),
_ => Vec2::new(face.x + arrow.unwrap_or_else(|| face::measured_icon(cx)), face.y),
}
}
@@ -200,12 +204,12 @@ impl<T: Value + 'static> Component for SplitButton<'_, T> {
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
let arrow = match self.arrangement {
Arrangement::Stacked => {
let chevron = chevron();
let (look, side) = (&cx.style.face, cx.style.icon.side);
let chevron = chevron(&cx.style.face);
let (look, side) = (&cx.style.face, cx.style.face.arrow.unwrap_or(cx.style.icon.side));
let y = face::stacked_divide(cx, &self.spec(cx.resolved.label, &chevron), look, side, cx.content_rect);
Rect::from_min_max(egui::pos2(rect.left(), y.clamp(rect.top(), rect.bottom())), rect.max)
}
_ => Flow::new(rect, cx.flow.is_rtl()).take_end(cx.style.icon.side).0,
_ => Flow::new(rect, cx.flow.is_rtl()).take_end(cx.style.face.arrow.unwrap_or(cx.style.icon.side)).0,
};
let part = cx
.part(ARROW, arrow)
@@ -278,18 +282,18 @@ impl<T: Value + 'static> Component for SplitButton<'_, T> {
return;
}
let (rtl, direction) = (cx.flow.is_rtl(), cx.env.direction());
let (arrow, main) = Flow::new(cx.rect, rtl).take_end(cx.style.icon.side);
let arrow_side = cx.style.face.arrow.unwrap_or(cx.style.icon.side);
let (arrow, main) = Flow::new(cx.rect, rtl).take_end(arrow_side);
let main = main.rect();
let main_fill = cx.root_state().resolve(&cx.style.part_fill);
let draw = ui.draw();
draw.fill(&PaintShape::Rect(main, Corners::Start.apply(cx.style.radius, direction)), main_fill);
draw.fill(&PaintShape::Rect(arrow, Corners::End.apply(cx.style.radius, direction)), cx.resolve_part(ARROW, &cx.style.part_fill));
let area = Flow::new(main, rtl).inset(cx.style.metrics.padding);
let chevron = chevron().color(cx.resolve(&cx.style.chevron));
let chevron = chevron(&cx.style.face).color(cx.resolve(&cx.style.chevron));
face::paint(ui, cx, (&cx.style.text, &cx.style.icon), &cx.style.face, &self.spec(cx.resolved.label, &chevron), area);
let draw = ui.draw();
let side = cx.style.icon.side;
draw.icon(Rect::from_center_size(arrow.center(), Vec2::splat(side)), &chevron);
draw.icon(Rect::from_center_size(arrow.center(), Vec2::splat(arrow_side)), &chevron);
let x = if rtl { arrow.right() } else { arrow.left() };
let inset = cx.style.divider_inset;
let divider = RuleLook { color: cx.resolve(&cx.style.divider).egui(), width: cx.style.divider_width };
@@ -313,7 +317,7 @@ impl<T: Value + 'static> SplitButton<'_, T> {
let draw = ui.draw();
draw.fill(&PaintShape::Rect(main, CornerRadius { sw: 0, se: 0, ..radius }), cx.root_state().resolve(&cx.style.part_fill));
draw.fill(&PaintShape::Rect(arrow, CornerRadius { nw: 0, ne: 0, ..radius }), cx.resolve_part(ARROW, &cx.style.part_fill));
let chevron = chevron().color(cx.resolve(&cx.style.chevron));
let chevron = chevron(&cx.style.face).color(cx.resolve(&cx.style.chevron));
face::paint(ui, cx, (&cx.style.text, &cx.style.icon), &cx.style.face, &self.spec(cx.resolved.label, &chevron), cx.content_rect);
if main.height() > 0.0 {
let inset = cx.style.divider_inset;