// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module Nostro.Ui:Widgets_impl; import std; import :Widgets; import :Geometry; import :Text; import :Edit; import :Draw; import :Theme; import :Input; import :Context; import :Layout; using namespace Nostro::Ui; namespace { // A line around a region that has the keyboard's focus. void FocusRing(Frame& frame, Box box, std::string_view key, float radius) { if(frame.FocusRing(key)) { frame.draw.Rect(RectItem{.box = box, .color = frame.Colors().accent.WithAlpha(0), .corners = Corners::All(radius), .outline = frame.Dp(2), .outlineColor = frame.Colors().accent}); } } // A ground's colour as the pointer has it: over it, or pressing it. Color Felt(const Frame& frame, std::string_view key, Color ground) { if(frame.Pressed(key)) { return frame.Colors().Pressed(ground); } return frame.Hovered(key) ? frame.Colors().Hovered(ground) : ground; } float Width(const Frame& frame, std::string_view text, const TextStyle& style) { return TextWidth(frame.Measure(), text, style.face, frame.Dp(style.sizeDp)); } // The area that takes a press on a button: at least as tall as a finger needs, whatever is drawn, the rest taken // evenly from above and below it. Box Reach(const Frame& frame, Box box) { const float more = std::max(0.0f, frame.Dp(Size::touch) - box.h); return {box.x, box.y - more / 2, box.w, box.h + more}; } // How wide a sheet is: the whole width of a window as narrow as a phone, a dialog's own width in a wider one. float SheetWidth(const Frame& frame) { return frame.sizeClass == SizeClass::Compact ? frame.bounds.w : std::min(frame.bounds.w - frame.Dp(Size::gutter) * 2, frame.Dp(480)); } // A widget's segments or tabs: equal parts of a box, each a button. void Choices(Frame& frame, Box box, const ChoiceOptions& options, const std::function& draw) { const float each = box.w / static_cast(std::max(1, options.labels.size())); for(std::size_t i = 0; i < options.labels.size(); i++) { const Box part{box.x + each * static_cast(i), box.y, each, box.h}; const std::string key = std::format("{}.{}", options.key, i); draw(part, i, key); Region region; region.box = part; region.key = key; region.label = options.labels[i]; region.enabled = options.enabled; region.focusable = true; if(options.onSelect) { region.onTap = [select = options.onSelect, i] { select(i); }; } frame.Add(std::move(region)); } } } TextStyle Nostro::Ui::Body(const Frame& frame) { return {FontFace::Regular, TextSize::body, frame.Colors().text, TextAlign::Left}; } TextStyle Nostro::Ui::Strong(const Frame& frame) { return {FontFace::Medium, TextSize::body, frame.Colors().text, TextAlign::Left}; } TextStyle Nostro::Ui::Muted(const Frame& frame) { return {FontFace::Regular, TextSize::label, frame.Colors().muted, TextAlign::Left}; } TextStyle Nostro::Ui::Caption(const Frame& frame) { return {FontFace::Regular, TextSize::caption, frame.Colors().muted, TextAlign::Left}; } TextStyle Nostro::Ui::Heading(const Frame& frame) { return {FontFace::Medium, TextSize::heading, frame.Colors().text, TextAlign::Left}; } void Nostro::Ui::Label(Frame& frame, Box box, std::string_view text, const TextStyle& style) { const float px = frame.Dp(style.sizeDp); const float x = style.align == TextAlign::Left ? box.x : (style.align == TextAlign::Center ? box.Center().x : box.Right()); frame.draw.Text(Ellipsize(frame.Measure(), text, style.face, px, box.w), x, CenteredBaseline(frame.Measure(), box, style.face, px), style.face, px, style.color, style.align); } float Nostro::Ui::Paragraph(Frame& frame, Box box, std::string_view text, const TextStyle& style) { const float px = frame.Dp(style.sizeDp); const float lineHeight = frame.Measure().lineHeight(style.face, px); const float x = style.align == TextAlign::Left ? box.x : (style.align == TextAlign::Center ? box.Center().x : box.Right()); float baseline = box.y + frame.Measure().ascent(style.face, px); // A copy of the lines: the wrapper's own are gone at its next call. const std::vector lines = frame.Wrapper().Wrap(text, style.face, px, box.w); for(const WrappedLine& line : lines) { frame.draw.Text(text.substr(line.begin, line.end - line.begin), x, baseline, style.face, px, style.color, style.align); baseline += lineHeight; } return lineHeight * static_cast(lines.size()); } float Nostro::Ui::ParagraphHeight(Frame& frame, float width, std::string_view text, const TextStyle& style) { const float px = frame.Dp(style.sizeDp); return frame.Measure().lineHeight(style.face, px) * static_cast(frame.Wrapper().Wrap(text, style.face, px, width).size()); } void Nostro::Ui::Button(Frame& frame, Box box, const ButtonOptions& options) { const Theme& theme = frame.Colors(); Color ground = theme.accent; Color color = theme.onAccent; if(!options.enabled) { ground = theme.surfaceAlt; color = theme.muted; } else if(options.kind == ButtonKind::Tonal) { ground = theme.surfaceAlt; color = theme.text; } else if(options.kind == ButtonKind::Danger) { ground = theme.danger; color = theme.onDanger; } else if(options.kind == ButtonKind::Plain) { ground = theme.surface; color = theme.accent; } const float radius = frame.Dp(Size::controlRadius); // Text alone has no ground until the pointer is on it. if(options.kind != ButtonKind::Plain || frame.Pressed(options.key) || frame.Hovered(options.key)) { frame.draw.Rect(box, options.enabled ? Felt(frame, options.key, ground) : ground, radius); } Label(frame, box, options.label, {FontFace::Medium, TextSize::body, color, TextAlign::Center}); FocusRing(frame, box, options.key, radius); Region region; region.box = Reach(frame, box); region.key = options.key; region.label = options.label; region.enabled = options.enabled; region.focusable = true; region.onTap = options.onTap; frame.Add(std::move(region)); } float Nostro::Ui::ButtonWidth(const Frame& frame, std::string_view label) { return TextWidth(frame.Measure(), label, FontFace::Medium, frame.Dp(TextSize::body)) + frame.Dp(Size::gutter) * 2; } void Nostro::Ui::ListRow(Frame& frame, Box box, const RowOptions& options) { const Theme& theme = frame.Colors(); const bool acts = options.enabled && options.onTap != nullptr; if(acts && (frame.Pressed(options.key) || frame.Hovered(options.key))) { frame.draw.Rect(box, Felt(frame, options.key, theme.surface)); } float left = box.x + frame.Dp(Size::gutter); float right = box.Right() - frame.Dp(Size::gutter); if(options.selected) { // A radio button: a ring, with a dot in the one that is chosen. const Point center{left + frame.Dp(10), box.Center().y}; const Color ring = *options.selected ? theme.accent : theme.muted; frame.draw.Circle(CircleItem{.center = center, .radius = frame.Dp(10), .color = ring.WithAlpha(0), .outline = frame.Dp(2), .outlineColor = ring}); if(*options.selected) { frame.draw.Circle(center, frame.Dp(5), theme.accent); } left += frame.Dp(36); } if(options.chevron) { frame.draw.Text("›", right, CenteredBaseline(frame.Measure(), box, FontFace::Regular, frame.Dp(TextSize::title)), FontFace::Regular, frame.Dp(TextSize::title), theme.muted, TextAlign::Right); right -= frame.Dp(20); } const Color ink = options.enabled ? theme.text : theme.muted; const bool twoLines = !options.subtitle.empty(); // Two lines sit above and below the row's middle, one in it. const Box upper = twoLines ? Box{box.x, box.Center().y - frame.Dp(22), box.w, frame.Dp(22)} : box; const Box lower{box.x, box.Center().y, box.w, frame.Dp(20)}; float trailingWidth = 0; if(!options.trailing.empty()) { const TextStyle style{FontFace::Medium, TextSize::body, options.trailingColor.value_or(ink), TextAlign::Right}; trailingWidth = Width(frame, options.trailing, style); Label(frame, {left, options.trailingSub.empty() ? box.y : upper.y, right - left, options.trailingSub.empty() ? box.h : upper.h}, options.trailing, style); } if(!options.trailingSub.empty()) { const TextStyle style{FontFace::Regular, TextSize::caption, theme.muted, TextAlign::Right}; trailingWidth = std::max(trailingWidth, Width(frame, options.trailingSub, style)); Label(frame, {left, options.trailing.empty() ? box.y : lower.y, right - left, options.trailing.empty() ? box.h : lower.h}, options.trailingSub, style); } if(trailingWidth > 0) { right -= trailingWidth + frame.Dp(Size::unit * 3); } float titleRoom = std::max(0.0f, right - left); if(!options.badge.empty()) { // The mark follows the title, and the title makes room for it. const float badgeWidth = ChipWidth(frame, options.badge); titleRoom = std::max(0.0f, titleRoom - badgeWidth - frame.Dp(Size::unit * 2)); const float titleWidth = std::min(titleRoom, Width(frame, options.title, {FontFace::Regular, TextSize::body, ink, TextAlign::Left})); Chip(frame, {left + titleWidth + frame.Dp(Size::unit * 2), upper.Center().y - frame.Dp(10), badgeWidth, frame.Dp(20)}, options.badge, theme.surfaceAlt, theme.muted); } Label(frame, {left, upper.y, titleRoom, upper.h}, options.title, {FontFace::Regular, TextSize::body, ink, TextAlign::Left}); if(twoLines) { Label(frame, {left, lower.y, std::max(0.0f, right - left), lower.h}, options.subtitle, Muted(frame)); } if(options.divider) { // To the window's edge on a phone, as its lists have it; in a wider window as far as the row's text goes. const float end = frame.sizeClass == SizeClass::Compact ? box.Right() : box.Right() - frame.Dp(Size::gutter); Divider(frame, {left, box.y, end - left, box.h}); } FocusRing(frame, box.Inset(Insets::All(frame.Dp(2))), options.key, frame.Dp(Size::controlRadius)); Region region; region.box = box; region.key = options.key; region.role = Role::Row; region.label = options.title; region.enabled = options.enabled; region.focusable = options.onTap != nullptr; region.onTap = options.onTap; region.onLongPress = options.onLongPress; frame.Add(std::move(region)); } float Nostro::Ui::ValueRowHeight(Frame& frame, float width, const ValueOptions& options) { const float room = std::max(0.0f, width - frame.Dp(Size::unit * 4) - frame.Dp(options.labelWidth)); // A finger's height where a finger presses it; closer together under a pointer. const float least = frame.Dp(frame.sizeClass == SizeClass::Compact ? Size::touch : 36.0f); return std::max(least, ParagraphHeight(frame, room, options.value, Body(frame)) + frame.Dp(Size::unit * 3)); } float Nostro::Ui::ValueRow(Frame& frame, Box box, const ValueOptions& options) { const TextStyle label = Muted(frame); const TextStyle value = Body(frame); const Box row{box.x, box.y, box.w, ValueRowHeight(frame, box.w, options)}; const float radius = frame.Dp(Size::controlRadius); const bool acts = options.onTap != nullptr; if(acts && (frame.Pressed(options.key) || frame.Hovered(options.key))) { frame.draw.Rect(row, Felt(frame, options.key, frame.Colors().surface), radius); } const float inset = frame.Dp(Size::unit * 2); const float labelWidth = frame.Dp(options.labelWidth); const float room = std::max(0.0f, row.w - inset * 2 - labelWidth); // The value in the middle of the row's height, and the label on the line the value begins on, whatever their sizes. const float top = row.y + (row.h - ParagraphHeight(frame, room, options.value, value)) / 2; const float labelPx = frame.Dp(label.sizeDp); frame.draw.Text(Ellipsize(frame.Measure(), options.label, label.face, labelPx, std::max(0.0f, labelWidth - inset)), row.x + inset, top + frame.Measure().ascent(value.face, frame.Dp(value.sizeDp)), label.face, labelPx, label.color); Paragraph(frame, {row.x + inset + labelWidth, top, room, 0}, options.value, value); if(acts) { FocusRing(frame, row, options.key, radius); Region region; region.box = row; region.key = options.key; region.role = Role::Row; region.label = options.label; region.focusable = true; region.onTap = options.onTap; region.onLongPress = options.onLongPress; frame.Add(std::move(region)); } return row.h; } float Nostro::Ui::TextField(Frame& frame, Box box, EditState& edit, const FieldOptions& options) { const Theme& theme = frame.Colors(); const TextMeasure& measure = frame.Measure(); float y = box.y; if(!options.label.empty()) { Label(frame, {box.x, y, box.w, frame.Dp(20)}, options.label, Muted(frame)); y += frame.Dp(20) + frame.Dp(Size::unit); } const Box control{box.x, y, box.w, frame.Dp(Size::control)}; const bool focused = frame.Focused(options.key) && options.enabled; const Color edge = !options.error.empty() ? theme.danger : (focused ? theme.accent : theme.divider); frame.draw.Rect(RectItem{.box = control, .color = options.enabled ? theme.surface : theme.surfaceAlt, .corners = Corners::All(frame.Dp(Size::controlRadius)), .outline = frame.Dp(focused ? 2 : 1), .outlineColor = edge}); const float px = frame.Dp(TextSize::body); constexpr FontFace face = FontFace::Regular; const float baseline = CenteredBaseline(measure, control, face, px); float textX = control.x + frame.Dp(12); if(!options.prefix.empty()) { textX += frame.draw.Text(options.prefix, textX, baseline, face, px, theme.muted) + frame.Dp(6); } const float room = std::max(0.0f, control.Right() - frame.Dp(12) - textX); // What the field shows, and where its cursor is in that: a secret is a dot for each of its code points. const bool masked = edit.kind == FieldKind::Secret; const std::string shown = edit.Shown(); const std::string drawn = FieldText(edit, masked); const auto drawnOffset = [&shown, masked](std::size_t shownByte) { return masked ? CountCodepoints(std::string_view(shown).substr(0, shownByte)) * std::string_view("•").size() : shownByte; }; const std::size_t cursor = drawnOffset(edit.ShownCursor()); // A text too long for the field is shown from where the cursor still fits at its right end; without the focus, // from its start. std::size_t first = 0; if(focused) { while(first < cursor && TextWidth(measure, std::string_view(drawn).substr(first, cursor - first), face, px) > room) { first = NextCodepoint(drawn, first); } } std::size_t last = first; for(float width = 0; last < drawn.size();) { std::size_t next = last; width += measure.advance(face, DecodeUtf8(drawn, next), px); if(width > room) { break; } last = next; } const auto xAt = [&](std::size_t drawnByte) { const std::size_t to = std::clamp(drawnByte, first, last); return textX + TextWidth(measure, std::string_view(drawn).substr(first, to - first), face, px); }; if(drawn.empty()) { frame.draw.Text(Ellipsize(measure, options.placeholder, face, px, room), textX, baseline, face, px, theme.muted); } else { if(focused && edit.HasSelection() && edit.preedit.empty()) { const float from = xAt(drawnOffset(edit.SelectionBegin())); frame.draw.Rect({from, control.y + frame.Dp(10), xAt(drawnOffset(edit.SelectionEnd())) - from, control.h - frame.Dp(20)}, theme.accent.WithAlpha(0.3f)); } // Up to three runs: before, inside and after the composing text, which shows in the accent colour until the input // method commits it. Without one the text is a single run. const std::size_t composingFrom = edit.preedit.empty() ? last : std::clamp(drawnOffset(edit.cursor), first, last); const std::array bounds = {first, composingFrom, edit.preedit.empty() ? last : std::clamp(cursor, first, last), last}; float x = textX; for(std::size_t run = 0; run < 3; run++) { if(bounds[run + 1] > bounds[run]) { x += frame.draw.Text(std::string_view(drawn).substr(bounds[run], bounds[run + 1] - bounds[run]), x, baseline, face, px, run == 1 ? theme.accent : (options.enabled ? theme.text : theme.muted)); } } } const Box caret{xAt(cursor), control.y + frame.Dp(12), frame.Dp(2), control.h - frame.Dp(24)}; if(focused && frame.CaretOn()) { frame.draw.Rect(caret, theme.accent); } Region region; region.box = control; region.key = options.key; region.role = Role::Field; region.label = options.label; region.enabled = options.enabled; region.focusable = true; region.onChange = options.onChange; region.onSubmit = options.onSubmit; region.edit = &edit; // What an on-screen keyboard is asked for, by what the field holds. static constexpr std::array, 5> byKind = {{ {TextPurpose::Normal, TextHint::prose}, {TextPurpose::Normal, TextHint::none}, {TextPurpose::Number, TextHint::none}, {TextPurpose::Digits, TextHint::none}, {TextPurpose::Password, TextHint::secret}, }}; const std::pair& asked = byKind[static_cast(edit.kind)]; region.field = {textX, face, px, first, masked, caret, options.purpose.value_or(asked.first), options.hints.value_or(asked.second)}; frame.Add(std::move(region)); const std::string& under = options.error.empty() ? options.helper : options.error; if(!under.empty()) { TextStyle style = Caption(frame); if(!options.error.empty()) { style.color = theme.danger; } Paragraph(frame, {box.x, control.Bottom() + frame.Dp(Size::unit), box.w, 0}, under, style); } return FieldHeight(frame, box.w, options); } float Nostro::Ui::FieldHeight(Frame& frame, float width, const FieldOptions& options) { float height = frame.Dp(Size::control); if(!options.label.empty()) { height += frame.Dp(20) + frame.Dp(Size::unit); } const std::string& under = options.error.empty() ? options.helper : options.error; if(!under.empty()) { height += frame.Dp(Size::unit) + ParagraphHeight(frame, width, under, Caption(frame)); } return height; } void Nostro::Ui::Segmented(Frame& frame, Box box, const ChoiceOptions& options) { const Theme& theme = frame.Colors(); const float radius = frame.Dp(Size::controlRadius); frame.draw.Rect(box, theme.surfaceAlt, radius); Choices(frame, box, options, [&](Box part, std::size_t i, const std::string& key) { const bool selected = i == options.selected; const Box inner = part.Inset(Insets::All(frame.Dp(3))); if(selected) { frame.draw.Rect(inner, theme.Taken(), radius - frame.Dp(2)); } else if(options.enabled && (frame.Pressed(key) || frame.Hovered(key))) { frame.draw.Rect(inner, Felt(frame, key, theme.surfaceAlt), radius - frame.Dp(2)); } Label(frame, inner, options.labels[i], {selected ? FontFace::Medium : FontFace::Regular, TextSize::label, options.enabled ? theme.text : theme.muted, TextAlign::Center}); FocusRing(frame, inner, key, radius - frame.Dp(2)); }); } void Nostro::Ui::Tabs(Frame& frame, Box box, const ChoiceOptions& options) { const Theme& theme = frame.Colors(); Divider(frame, box); // The labels a size down where they would not all fit beside each other. const float each = box.w / static_cast(std::max(1, options.labels.size())); float size = TextSize::caption; if(std::ranges::all_of(options.labels, [&](const std::string& label) { return TextWidth(frame.Measure(), label, FontFace::Medium, frame.Dp(TextSize::label)) + frame.Dp(Size::unit * 2) <= each; })) { size = TextSize::label; } Choices(frame, box, options, [&](Box part, std::size_t i, const std::string& key) { const bool selected = i == options.selected; if(options.enabled && (frame.Pressed(key) || frame.Hovered(key))) { frame.draw.Rect(part, Felt(frame, key, theme.surface)); } Label(frame, part, options.labels[i], {FontFace::Medium, size, selected ? theme.accent : theme.muted, TextAlign::Center}); if(selected) { frame.draw.Rect({part.x + frame.Dp(Size::unit * 2), part.Bottom() - frame.Dp(3), std::max(0.0f, part.w - frame.Dp(Size::unit * 4)), frame.Dp(3)}, theme.accent, frame.Dp(1.5f)); } FocusRing(frame, part.Inset(Insets::All(frame.Dp(2))), key, frame.Dp(Size::controlRadius)); }); } float Nostro::Ui::Banner(Frame& frame, Box box, const BannerOptions& options) { const Theme& theme = frame.Colors(); Color ground = theme.surfaceAlt; Color ink = theme.text; if(options.kind == BannerKind::Warning) { ground = theme.warning; ink = theme.onWarning; } else if(options.kind == BannerKind::Error) { ground = theme.danger; ink = theme.onDanger; } const float pad = frame.Dp(12); const float close = options.onDismiss ? frame.Dp(Size::touch) : 0.0f; TextStyle style = Muted(frame); style.color = ink; const float textWidth = std::max(0.0f, box.w - pad * 2 - close); const float textHeight = ParagraphHeight(frame, textWidth, options.text, style); const float actionHeight = options.action.empty() ? 0.0f : frame.Dp(40); const float height = std::max(pad * 2 + textHeight + actionHeight, close); const Box ground_{box.x, box.y, box.w, height}; frame.draw.Rect(ground_, ground, frame.Dp(Size::controlRadius)); Paragraph(frame, {box.x + pad, box.y + pad, textWidth, 0}, options.text, style); if(!options.action.empty()) { const float width = ButtonWidth(frame, options.action); const Box button{box.x + pad, box.y + pad + textHeight + frame.Dp(Size::unit), width, frame.Dp(36)}; // The action in the notice's own ink: an accent on a warning's ground would not be read. frame.draw.Rect(RectItem{.box = button, .color = Felt(frame, std::format("{}.action", options.key), ground), .corners = Corners::All(frame.Dp(Size::controlRadius)), .outline = frame.Dp(1), .outlineColor = ink}); Label(frame, button, options.action, {FontFace::Medium, TextSize::label, ink, TextAlign::Center}); FocusRing(frame, button, std::format("{}.action", options.key), frame.Dp(Size::controlRadius)); Region region; region.box = Reach(frame, button); region.key = std::format("{}.action", options.key); region.label = options.action; region.focusable = true; region.onTap = options.onAction; frame.Add(std::move(region)); } if(options.onDismiss) { const Box button{box.Right() - close, box.y, close, close}; Label(frame, button, "×", {FontFace::Regular, TextSize::title, ink, TextAlign::Center}); FocusRing(frame, button.Inset(Insets::All(frame.Dp(6))), std::format("{}.dismiss", options.key), frame.Dp(Size::controlRadius)); Region region; region.box = button; region.key = std::format("{}.dismiss", options.key); region.label = "Dismiss"; region.focusable = true; region.onTap = options.onDismiss; frame.Add(std::move(region)); } return height; } void Nostro::Ui::Chip(Frame& frame, Box box, std::string_view text, Color ground, Color color) { frame.draw.Rect(box, ground, box.h / 2); Label(frame, box, text, {FontFace::Medium, TextSize::caption, color, TextAlign::Center}); } float Nostro::Ui::ChipWidth(const Frame& frame, std::string_view text) { return TextWidth(frame.Measure(), text, FontFace::Medium, frame.Dp(TextSize::caption)) + frame.Dp(Size::unit * 4); } void Nostro::Ui::Divider(Frame& frame, Box box) { frame.draw.Rect({box.x, box.Bottom() - frame.Dp(1), box.w, frame.Dp(1)}, frame.Colors().divider); } void Nostro::Ui::Skeleton(Frame& frame, Box box, float fraction) { frame.draw.Rect({box.x, box.y, box.w * fraction, box.h}, frame.Colors().surfaceAlt, box.h / 2); } Box Nostro::Ui::TopBar(Frame& frame, Box area, const TopBarOptions& options) { const Theme& theme = frame.Colors(); const Layer before = frame.draw.CurrentLayer(); frame.draw.SetLayer(Layer::Chrome); const Box bar{area.x, area.y, area.w, frame.Dp(Size::bar)}; frame.draw.Rect(bar, theme.surface); Divider(frame, bar); Region ground; ground.box = bar; ground.key = "bar"; ground.role = Role::Panel; frame.Add(std::move(ground)); Row row(bar.Inset(Insets::Symmetric(frame.Dp(Size::unit), 0)), frame.Dp(Size::unit)); if(options.onBack) { const Box button = row.TakeLeft(frame.Dp(Size::touch)); const Box felt = button.Inset(Insets::Symmetric(0, frame.Dp(Size::unit))); if(frame.Pressed("bar.back") || frame.Hovered("bar.back")) { frame.draw.Rect(felt, Felt(frame, "bar.back", theme.surface), frame.Dp(Size::controlRadius)); } Label(frame, button, "‹", {FontFace::Regular, TextSize::display, theme.accent, TextAlign::Center}); FocusRing(frame, felt, "bar.back", frame.Dp(Size::controlRadius)); Region region; region.box = button; region.key = "bar.back"; region.label = "Back"; region.focusable = true; region.onTap = options.onBack; frame.Add(std::move(region)); frame.Shortcut(Key::Escape, {}, options.onBack); } else { row.TakeLeft(frame.Dp(Size::gutter - Size::unit * 2)); } // From the right, in the order they are named. for(const BarAction& action : std::views::reverse(options.actions)) { Button(frame, row.TakeRight(ButtonWidth(frame, action.label)).Inset(Insets::Symmetric(0, frame.Dp(Size::unit))), {.key = action.key, .label = action.label, .kind = ButtonKind::Plain, .enabled = action.enabled, .onTap = action.onTap}); } Label(frame, row.Rest(), options.title, {FontFace::Medium, TextSize::heading, theme.text, TextAlign::Left}); frame.draw.SetLayer(before); return {area.x, bar.Bottom(), area.w, std::max(0.0f, area.h - bar.h)}; } Box Nostro::Ui::NavBar(Frame& frame, Box area, std::span items) { const Theme& theme = frame.Colors(); const Layer before = frame.draw.CurrentLayer(); frame.draw.SetLayer(Layer::Chrome); const float height = std::min(area.h, frame.Dp(Size::bar)); const Box bar{area.x, area.Bottom() - height, area.w, height}; frame.draw.Rect(bar, theme.surface); frame.draw.Rect({bar.x, bar.y, bar.w, frame.Dp(1)}, theme.divider); Region ground; ground.box = bar; ground.key = "nav"; ground.role = Role::Panel; frame.Add(std::move(ground)); const float each = bar.w / static_cast(std::max(1, items.size())); for(std::size_t i = 0; i < items.size(); i++) { const NavItem& item = items[i]; const Box part{bar.x + each * static_cast(i), bar.y, each, bar.h}; // The place the window is at stands on a pill. const float pillWidth = std::min(part.w - frame.Dp(Size::unit * 2), TextWidth(frame.Measure(), item.label, FontFace::Medium, frame.Dp(TextSize::label)) + frame.Dp(Size::gutter * 2)); const Box pill{part.Center().x - pillWidth / 2, part.Center().y - frame.Dp(16), pillWidth, frame.Dp(32)}; if(item.selected) { frame.draw.Rect(pill, theme.accent.WithAlpha(theme.dark ? 0.28f : 0.14f), frame.Dp(16)); } else if(frame.Pressed(item.key) || frame.Hovered(item.key)) { frame.draw.Rect(pill, Felt(frame, item.key, theme.surface), frame.Dp(16)); } Label(frame, part, item.label, {FontFace::Medium, TextSize::label, item.selected ? theme.accent : theme.muted, TextAlign::Center}); FocusRing(frame, pill, item.key, frame.Dp(16)); Region region; region.box = part; region.key = item.key; region.label = item.label; region.focusable = true; region.onTap = item.onTap; frame.Add(std::move(region)); } frame.draw.SetLayer(before); return {area.x, area.y, area.w, area.h - height}; } Box Nostro::Ui::Sidebar(Frame& frame, Box area, float widthDp, std::span items) { const Theme& theme = frame.Colors(); const Layer before = frame.draw.CurrentLayer(); frame.draw.SetLayer(Layer::Chrome); const float width = std::min(area.w, frame.Dp(widthDp)); const Box bar{area.x, area.y, width, area.h}; frame.draw.Rect(bar, theme.surface); frame.draw.Rect({bar.Right() - frame.Dp(1), bar.y, frame.Dp(1), bar.h}, theme.divider); Region ground; ground.box = bar; ground.key = "side"; ground.role = Role::Panel; frame.Add(std::move(ground)); // A long list of accounts scrolls within the bar. const float offset = frame.BeginScroll("side.scroll", bar); Column column({bar.x, bar.y - offset + frame.Dp(Size::unit * 2), bar.w, bar.h}); for(const NavItem& item : items) { if(item.header) { Label(frame, column.Take(frame.Dp(32)).Inset(Insets::Symmetric(frame.Dp(Size::gutter), 0)), item.label, Caption(frame)); continue; } const Box row = column.Take(frame.Dp(44)); const Box pill = row.Inset(Insets::Symmetric(frame.Dp(Size::unit * 2), frame.Dp(2))); if(item.selected) { frame.draw.Rect(pill, theme.accent.WithAlpha(theme.dark ? 0.28f : 0.14f), frame.Dp(Size::controlRadius)); } else if(frame.Pressed(item.key) || frame.Hovered(item.key)) { frame.draw.Rect(pill, Felt(frame, item.key, theme.surface), frame.Dp(Size::controlRadius)); } const Box text = pill.Inset(Insets::Symmetric(frame.Dp(Size::unit * 2), 0)); float detailWidth = 0; if(!item.detail.empty()) { const TextStyle style{FontFace::Regular, TextSize::caption, theme.muted, TextAlign::Right}; detailWidth = Width(frame, item.detail, style) + frame.Dp(Size::unit * 2); // The detail gives way where the bar is too narrow for both. if(detailWidth * 2 < text.w) { Label(frame, text, item.detail, style); } else { detailWidth = 0; } } Label(frame, {text.x, text.y, text.w - detailWidth, text.h}, item.label, {item.selected ? FontFace::Medium : FontFace::Regular, TextSize::label, item.selected ? theme.accent : theme.text, TextAlign::Left}); FocusRing(frame, pill, item.key, frame.Dp(Size::controlRadius)); Region region; region.box = row; region.key = item.key; region.label = item.label; region.focusable = true; region.onTap = item.onTap; frame.Add(std::move(region)); } frame.EndScroll(column.Used() + frame.Dp(Size::unit * 4)); frame.draw.SetLayer(before); return {area.x + width, area.y, area.w - width, area.h}; } void Nostro::Ui::Sheet(Frame& frame, const SheetOptions& options, const std::function& body) { const Theme& theme = frame.Colors(); const Layer before = frame.draw.CurrentLayer(); frame.draw.SetLayer(Layer::Overlay); frame.draw.Rect(frame.bounds, theme.scrim); Region scrim; scrim.box = frame.bounds; scrim.key = std::format("{}.scrim", options.key); scrim.role = Role::Scrim; scrim.onTap = options.onDismiss; frame.Add(std::move(scrim)); if(options.onDismiss) { frame.Shortcut(Key::Escape, {}, options.onDismiss); } const float pad = frame.Dp(Size::gutter); const float titleHeight = options.title.empty() ? 0.0f : frame.Dp(40); const bool narrow = frame.sizeClass == SizeClass::Compact; const float width = SheetWidth(frame); // No taller than the window leaves room for: the body scrolls what does not fit. const float height = std::min(options.contentHeight + titleHeight + pad * 2, frame.bounds.h - frame.Dp(Size::touch)); const Box panel = narrow ? Box{frame.bounds.x, frame.bounds.Bottom() - height, width, height} : Box{frame.bounds.Center().x - width / 2, frame.bounds.Center().y - height / 2, width, height}; const float radius = frame.Dp(Size::radius); frame.draw.Rect(RectItem{.box = panel, .color = theme.surface, .corners = narrow ? Corners::Top(radius) : Corners::All(radius)}); Region ground; ground.box = panel; ground.key = std::format("{}.panel", options.key); ground.role = Role::Panel; frame.Add(std::move(ground)); if(!options.title.empty()) { Label(frame, {panel.x + pad, panel.y + pad, panel.w - pad * 2, frame.Dp(28)}, options.title, Heading(frame)); } body({panel.x + pad, panel.y + pad + titleHeight, panel.w - pad * 2, std::max(0.0f, panel.h - pad * 2 - titleHeight)}); frame.draw.SetLayer(before); } float Nostro::Ui::SheetBodyWidth(const Frame& frame) { return SheetWidth(frame) - frame.Dp(Size::gutter) * 2; }