ui_controls

source of choices

1// Choices (decisions/F11.md, F11b): Select, ChoiceList and2// Radios, and F11c's Tabs, each one element drawing its items, which3// reach screen readers as its parts. What they do with their labels is written once over4// `Labels`, whatever their values' type; only finding the chosen value5// and giving one back is generic.67import std::string;8import std::vector;9import std::option;10import std::math;11import std::time;12import std::equal;13import pkg::alloy::window;14import pkg::alloy::draw;15import pkg::alloy::text as text;1617import pkg::alloy::ui;18import pkg::alloy::ui::store;19import pkg::alloy::ui::tree;20import pkg::alloy::ui::layout;21import pkg::alloy::ui::painting;22import pkg::alloy::ui::events;23import pkg::alloy::ui::widgets;24import pkg::alloy::ui::fields;25import pkg::alloy::ui::scrolling;26import pkg::alloy::ui::running;27import pkg::alloy::ui::timers;28import pkg::alloy::ui::layers;29import pkg::alloy::ui::motion;30import pkg::alloy::ui::transitions;31import pkg::alloy::ui::languages;32import pkg::alloy::ui::extensions;33import pkg::alloy::ui::views;34import ui_controls;3536/* --- Labels --- */3738// The choosing widgets' labels, whatever their values.39pub interface Labels {40    fn count(self: &) -> u64;41    fn label(self: &, k: u64) -> &String;42    fn enabled(self: &, k: u64) -> bool;43}4445pub type ChoicesOf<T: Equal> : Labels = struct {46    pub choices: &[Choice<T>],47};4849pub fn count<T: Equal>(self c: &ChoicesOf<T>) -> u64 => c.choices.length();5051pub fn label<T: Equal>(self c: &ChoicesOf<T>, k: u64) -> &String => &c.choices[k].label;5253pub fn enabled<T: Equal>(self c: &ChoicesOf<T>, k: u64) -> bool => c.choices[k].enabled;5455// Where the value is among the choices.56pub fn index_of<T: Equal>(choices: &[Choice<T>], value: &Option<T>) -> Option<u64> {57    if (value is ::Some |&v|) {58        for (choices, [..choices.length()]) |&choice, k| {59            if (choice.value == v) {60                return ::Some(k);61            }62        }63    }64    return ::None;65}6667// The enabled choice `step` from `from`, going round, or the first68// enabled one when there is none to start from; a step of 0 finds the69// first, and those past an end stop there.70fn stepped(labels: &Labels, from: Option<u64>, step: i64, round: bool) -> Option<u64> {71    const count = labels.count() to i64;72    if (count == 0) {73        return ::None;74    }75    const start = if (from is ::Some |f|) f to i64 else -1;76    if (step == 0 || start < 0) {77        for ([..labels.count()]) |k| {78            if (labels.enabled(k)) {79                return ::Some(k);80            }81        }82        return ::None;83    }84    const direction: i64 = if (step > 0) 1 else -1;85    var at = start;86    var found = from;87    var left = abs(step);88    for ([..labels.count()]) {89        at += direction;90        if (round) {91            at = (at + count) % count;92        } else if (at < 0 || at >= count) {93            break;94        }95        if (labels.enabled(at to u64)) {96            found = ::Some(at to u64);97            left -= 1;98            if (left == 0) {99                break;100            }101        }102    }103    return found;104}105106// The last enabled choice.107fn last_enabled(labels: &Labels) -> Option<u64> {108    var k = labels.count();109    while (k > 0) {110        k -= 1;111        if (labels.enabled(k)) {112            return ::Some(k);113        }114    }115    return ::None;116}117118// The text's widest line and tallest, laid out at the theme's size.119fn measure(120    fonts: &var text::Fonts,121    style: &text::TextStyle,122    labels: &Labels,123    also: &[u8]124) -> [f32 : 2] {125    var widest: f32 = 0.0;126    var line: f32 = 0.0;127    for ([..labels.count() + 1]) |k| {128        const shown: &[u8] = if (k < labels.count()) &labels.label(k).view() else &also;129        const laid = text::Layout::fitted(&fonts, &shown, &style, UNBOUNDED);130        widest = max(widest, laid.width);131        line = max(line, laid.height);132    }133    return [widest, line];134}135136/* --- Type-ahead --- */137138// How long typing waits before what was typed is forgotten.139const FORGET: u64 = 1000;140141// What was typed, and when last.142pub type TypeAhead = struct {143    pub typed: String = String::empty(),144    pub at: Duration = Duration::from_nanoseconds(0),145};146147// The character typed at the time: the next enabled choice whose label148// starts with all typed within a second of each other, compared after149// simple case folding, from the one at `from` on; the same letter again150// and again goes through those starting with it.151pub fn type_ahead(152    self t: &var TypeAhead,153    labels: &Labels,154    from: Option<u64>,155    c: u32,156    now: Duration157) -> Option<u64> {158    if (159        now.nanoseconds < t.at.nanoseconds160        || now.nanoseconds - t.at.nanoseconds > FORGET * 1000000161    ) {162        t.typed.clear();163    }164    t.at = now;165    var one = String::empty();166    put_char(&var one, c);167    t.typed.append(&one.view());168    const typed = &t.typed.view();169    const letter = &one.view();170    var repeated = true;171    var k: u64 = 0;172    while (k < typed.length()) {173        if (k + letter.length() <= typed.length() && typed[k..k + letter.length()] == letter) {174            k += letter.length();175        } else {176            repeated = false;177            break;178        }179    }180    const prefix: &[u8] = if (repeated) &letter else &typed;181    const count = labels.count();182    var start: u64 = 0;183    if (from is ::Some |f|) {184        // a new prefix may stay where it is; one letter again moves on185        start = if (repeated) f + 1 else f;186    }187    for ([..count]) |n| {188        const at = (start + n) % count;189        if (labels.enabled(at) && text::starts_folded(&labels.label(at).view(), &prefix)) {190            return ::Some(at);191        }192    }193    return ::None;194}195196// The code point in UTF-8.197fn put_char(out: &var String, c: u32) {198    if (c < 0x80) {199        out.push(c to u8);200    } else if (c < 0x800) {201        out.push((0xC0 | (c >> 6)) to u8);202        out.push((0x80 | (c & 0x3F)) to u8);203    } else if (c < 0x10000) {204        out.push((0xE0 | (c >> 12)) to u8);205        out.push((0x80 | ((c >> 6) & 0x3F)) to u8);206        out.push((0x80 | (c & 0x3F)) to u8);207    } else {208        out.push((0xF0 | (c >> 18)) to u8);209        out.push((0x80 | ((c >> 12) & 0x3F)) to u8);210        out.push((0x80 | ((c >> 6) & 0x3F)) to u8);211        out.push((0x80 | (c & 0x3F)) to u8);212    }213}214215// The character a key types into a choice's search: a letter, a digit,216// a sign, with no Control, Alt or Command; not Space, which chooses.217fn typed_character(k: &KeyEvent) -> Option<u32> {218    if (k.key is ::Character |c| && c > ' ' to u32) {219        if (!k.modifiers.control && !k.modifiers.alt && !k.modifiers.super) {220            return ::Some(c);221        }222    }223    return ::None;224}225226// Whether the key is Space.227exp fn is_space(k: &KeyEvent) -> bool => match (k.key) {228    ::Character |c| c == ' ' to u32;229    else false;230};231232/* --- A list's rows, whatever their values --- */233234const LIST_PADDING: f32 = 4.0;235const ROW_PADDING: Padding = Padding { .start = 12.0, .top = 6.0, .end = 12.0, .bottom = 6.0 };236const CHECK: f32 = 14.0; // the chosen row's check mark237const CHECK_GAP: f32 = 12.0;238239// The rows a Select's list shows at most before it scrolls.240const POPUP_ROWS: f32 = 12.0;241242// A list's own state: the row the keys and the pointer are on, how far243// it is scrolled, its rows' height, its widest label, and the rows in244// view, laid out.245pub type ListState = struct {246    pub highlight: Option<u64> = ::None,247    pub offset: f32 = 0.0,248    pub row: f32 = 0.0,249    pub widest: Option<f32> = ::None, // measured once a change250    pub shown: Vector<ShownRow> = Vector<ShownRow>::empty(),251    pub width: f32 = 0.0,252    pub length: f32 = 0.0, // its height as laid out253    pub grab: Option<f32> = ::None, // where the bar was grabbed, while dragged254    pub typed: TypeAhead = TypeAhead {},255    pub follow: bool = false, // the highlight scrolled into view as it lays out256};257258pub type ShownRow = struct {259    pub index: u64,260    pub laid: text::Layout,261};262263// What an input to a list did.264type Did = enum {265    Nothing, // it goes on to the ancestors266    Took,267    Chose: u64,268    Close, // Tab in a Select's list with nothing to choose269};270271fn content(272    self rows: &ListState,273    count: u64274) -> f32 => count to f32 * rows.row + 2.0 * LIST_PADDING;275276// The row at the point, in the list's coordinates.277fn row_at(self rows: &ListState, count: u64, at: Point) -> Option<u64> {278    const y = at.y + rows.offset - LIST_PADDING;279    if (y < 0.0 || rows.row <= 0.0 || at.x < 0.0 || at.x >= rows.width) {280        return ::None;281    }282    const k = floor(y / rows.row) to u64;283    return if (k < count) ::Some(k) else ::None;284}285286// Whether the point is on the bar, which shows along the end edge (the287// left right to left) when the rows do not fit.288fn on_bar(self rows: &ListState, count: u64, p: Point, rtl: bool) -> bool {289    return rows.content(count) > rows.length290        && (if (rtl) p.x <= BAR_REACH else p.x >= rows.width - BAR_REACH);291}292293// The row's area, in the list's coordinates.294pub fn row_area(self rows: &ListState, k: u64) -> Rect {295    return Rect::at(296        LIST_PADDING,297        LIST_PADDING + k to f32 * rows.row - rows.offset,298        max(rows.width - 2.0 * LIST_PADDING, 0.0),299        rows.row300    );301}302303fn lay_out_rows(304    self rows: &var ListState,305    labels: &Labels,306    arrange: &var Arrange,307    limits: Limits308) -> Extent {309    const style = arrange.text_style(arrange.theme().text_size);310    const fonts = &var arrange.fonts();311    if (rows.widest is ::None) {312        const measured = measure(&var fonts, &style, &labels, &"");313        rows.widest = ::Some(measured[0]);314        rows.row = measured[1] + ROW_PADDING.top + ROW_PADDING.bottom;315    }316    const widest = if (rows.widest is ::Some |w|) w else 0.0;317    const count = labels.count();318    const content = rows.content(count);319    const extent = limits.constrain(Extent {320        .width = widest + ROW_PADDING.start + ROW_PADDING.end + CHECK_GAP + CHECK321            + 2.0 * LIST_PADDING,322        .height = content323    });324    rows.width = extent.width;325    rows.length = extent.height;326    if (rows.follow && rows.highlight is ::Some |h|) {327        rows.follow = false;328        const top = h to f32 * rows.row;329        if (top < rows.offset) {330            rows.offset = top;331        } else if (top + rows.row + 2.0 * LIST_PADDING > rows.offset + rows.length) {332            rows.offset = top + rows.row + 2.0 * LIST_PADDING - rows.length;333        }334    }335    rows.offset = clamp(rows.offset, 0.0, max(content - rows.length, 0.0));336    // the rows in view, laid out to be painted337    rows.shown.clear();338    if (rows.row > 0.0 && count > 0) {339        const first = floor(max(rows.offset - LIST_PADDING, 0.0) / rows.row) to u64;340        const end = min(ceil((rows.offset + rows.length) / rows.row) to u64, count);341        for ([first..end]) |k| {342            rows.shown.push(ShownRow {343                .index = k,344                .laid = text::Layout::fitted(&fonts, &labels.label(k).view(), &style, UNBOUNDED)345            });346        }347    }348    return extent;349}350351fn paint_rows(352    self rows: &ListState,353    labels: &Labels,354    chosen: Option<u64>,355    popup: bool,356    enabled: bool,357    error: bool,358    look: &Look,359    paint: &var Paint360) {361    const theme = &look.theme;362    if (!popup) {363        const area = Rect::sized(look.size.width, look.size.height);364        var surface = theme.surface;365        if (!enabled) {366            surface.a = 0.5;367        }368        paint.rounded_rect(area, Radii::all(theme.radius), Fill::Color(surface));369        if (error) {370            paint.border(371                area,372                Radii::all(theme.radius),373                if (look.focus_ring) 2.0 else 1.5,374                theme.danger375            );376        } else if (look.focus_ring) {377            paint.border(area, Radii::all(theme.radius), 2.0, theme.focus);378        } else {379            paint.border(380                area,381                Radii::all(theme.radius),382                1.0,383                if (look.hovered && enabled) theme.muted else theme.border384            );385        }386    }387    for (rows.shown.view()) |&shown| {388        const k = shown.index;389        const area = rows.row_area(k);390        const lit = if (popup) rows.highlight == ::Some(k) else chosen == ::Some(k) || (391            look.focused && rows.highlight == ::Some(k)392        );393        const usable = enabled && labels.enabled(k);394        if (lit && usable) {395            var fill = theme.primary;396            fill.a = if (look.pressed) 0.24 else 0.14;397            paint.rounded_rect(area, Radii::all(max(theme.radius - 2.0, 0.0)), Fill::Color(fill));398        }399        var ink = theme.text;400        if (!usable) {401            ink.a = 0.45;402        }403        // the label at the row's start, the check mark at its end404        const rtl = look.direction is ::RightToLeft;405        paint.text(406            &shown.laid,407            Point {408                .x = if (409                    rtl410                ) area.x + area.width - ROW_PADDING.start - shown.laid.width else area.x411                    + ROW_PADDING.start,412                .y = area.y + (area.height - shown.laid.height) / 2.0413            },414            ink415        );416        if (chosen == ::Some(k) && look.marks.check is ::Some |mark|) {417            paint.mask(418                Rect::at(419                    if (420                        rtl421                    ) area.x + ROW_PADDING.end else area.x + area.width - ROW_PADDING.end - CHECK,422                    area.y + (area.height - CHECK) / 2.0,423                    CHECK,424                    CHECK425                ),426                mark,427                if (usable) theme.primary else ink428            );429        }430    }431}432433// Scrolls the rows by the distance, within their ends; whether they434// moved, laid out again to show what came into view.435fn scroll_rows(self rows: &var ListState, ui: &var Ui, count: u64, offset: f32) -> bool {436    const next = clamp(offset, 0.0, max(rows.content(count) - rows.length, 0.0));437    if (next == rows.offset) {438        return false;439    }440    rows.offset = next;441    ui.refresh(Change::Layout);442    return true;443}444445// Puts the highlight on the row and scrolls it into view.446fn highlight_row(self rows: &var ListState, ui: &var Ui, k: u64) {447    if (rows.highlight != ::Some(k)) {448        rows.highlight = ::Some(k);449        rows.follow = true;450        ui.refresh(Change::Layout);451    }452}453454// What the input does to the rows: moving the highlight, scrolling, or455// a row chosen. In a Select's list (`popup`) moving chooses nothing; on456// its own the choice follows the highlight.457fn rows_input(458    self rows: &var ListState,459    labels: &Labels,460    ui: &var Ui,461    input: &Input,462    popup: bool,463    chosen: Option<u64>464) -> Did {465    const count = labels.count();466    const rtl = ui.handling_rtl();467    const usable = |&labels| (k: Option<u64>) -> Option<u64> {468        if (k is ::Some |at| && labels.enabled(at)) {469            return k;470        }471        return ::None;472    };473    match (input) {474        ::Hover |p| {475            // a Select's list follows the pointer, one on its own keeps476            // the keyboard's row477            if (popup && usable(rows.row_at(count, p)) is ::Some |k|) {478                if (rows.highlight != ::Some(k)) {479                    rows.highlight = ::Some(k);480                    ui.refresh(Change::Paint);481                }482            }483            return Did::Took;484        }485        ::Press |p| {486            if (rows.on_bar(count, p, rtl)) {487                // the bar: the thumb grabbed, or centered on the press488                const t = thumb(rows.offset, rows.content(count), rows.length);489                if (p.y < t.start || p.y > t.start + t.length) {490                    rows.scroll_rows(491                        &var ui,492                        count,493                        offset_at(p.y - t.length / 2.0, &t, rows.content(count), rows.length)494                    );495                }496                rows.grab = ::Some(497                    p.y - thumb(rows.offset, rows.content(count), rows.length).start498                );499            } else if (usable(rows.row_at(count, p)) is ::Some |k|) {500                rows.highlight = ::Some(k);501                ui.refresh(Change::Paint);502            }503            return Did::Took;504        }505        ::Moved |p| {506            if (rows.grab is ::Some |grab|) {507                const t = thumb(rows.offset, rows.content(count), rows.length);508                rows.scroll_rows(509                    &var ui,510                    count,511                    offset_at(p.y - grab, &t, rows.content(count), rows.length)512                );513            } else if (usable(rows.row_at(count, p)) is ::Some |k| && rows.highlight != ::Some(k)) {514                rows.highlight = ::Some(k);515                ui.refresh(Change::Paint);516            }517            return Did::Took;518        }519        ::Release {520            rows.grab = ::None;521            return Did::Took;522        }523        ::Click |p| {524            if (!rows.on_bar(count, p, rtl) && usable(rows.row_at(count, p)) is ::Some |k|) {525                return Did::Chose(k);526            }527            return Did::Took;528        }529        ::Scroll |delta| {530            return if (rows.scroll_rows(531                &var ui,532                count,533                rows.offset + delta.y534            )) Did::Took else Did::Nothing;535        }536        ::Focus |focused| {537            if (focused && rows.highlight is ::None) {538                rows.highlight = if (chosen is ::Some) chosen else stepped(539                    &labels,540                    ::None,541                    0,542                    false543                );544                rows.follow = true;545            }546            ui.refresh(Change::Layout);547            return Did::Nothing;548        }549        ::Key |&k| {550            // a page: the rows in view, but one551            const page = max((rows.length / max(rows.row, 1.0)) to i64 - 1, 1);552            const step: Option<i64> = match (k.key) {553                ::Down ::Some(1);554                ::Up ::Some(-1);555                ::PageDown ::Some(page);556                ::PageUp ::Some(-page);557                else ::None;558            };559            var moved = Option<u64>::None;560            if (step is ::Some |s|) {561                moved = stepped(&labels, rows.highlight, s, false);562            } else if (k.key is ::Home) {563                moved = stepped(&labels, ::None, 0, false);564            } else if (k.key is ::End) {565                moved = last_enabled(&labels);566            } else if (typed_character(&k) is ::Some |c|) {567                if (k.pressed) {568                    moved = rows.typed.type_ahead(&labels, rows.highlight, c, ui.now());569                    if (moved is ::None) {570                        return Did::Took;571                    }572                }573            } else if (k.key is ::Enter || is_space(&k) || (k.key is ::Tab && popup)) {574                if (k.pressed && usable(rows.highlight) is ::Some |at|) {575                    return Did::Chose(at);576                }577                return if (k.key is ::Tab) Did::Close else Did::Took;578            } else {579                return Did::Nothing;580            }581            if (!k.pressed) {582                return Did::Took;583            }584            if (moved is ::Some |at|) {585                rows.highlight_row(&var ui, at);586                if (!popup && chosen != ::Some(at)) {587                    return Did::Chose(at);588                }589            }590            return Did::Took;591        }592        ::Part |&request| {593            const k = request.part to u64;594            if (k >= count) {595                return Did::Took;596            }597            match (request.action) {598                ::Click, ::Select {599                    if (usable(::Some(k)) is ::Some) {600                        return Did::Chose(k);601                    }602                }603                ::Focus {604                    rows.highlight_row(&var ui, k);605                }606                ::ScrollIntoView {607                    const top = k to f32 * rows.row;608                    if (top < rows.offset || top + rows.row > rows.offset + rows.length) {609                        rows.scroll_rows(&var ui, count, top);610                    }611                }612                else {}613            }614            return Did::Took;615        }616        else {617            return Did::Nothing;618        }619    }620}621622// Each choice as an option to a screen reader, the highlighted one623// holding the focus.624fn row_parts(625    self rows: &ListState,626    labels: &Labels,627    chosen: Option<u64>,628    popup: bool629) -> Vector<Part> {630    const count = labels.count();631    var parts = Vector<Part>::with_capacity(count);632    for ([..count]) |k| {633        const selected = if (popup) rows.highlight == ::Some(k) else chosen == ::Some(k);634        parts.push(Part {635            .semantics = Semantics {636                .role = Role::ListOption,637                .label = copy labels.label(k),638                .selected = ::Some(selected),639                .disabled = !labels.enabled(k),640                .focusable = labels.enabled(k),641                .position = ::Some([k to u32 + 1, count to u32])642            },643            .bounds = rows.row_area(k),644            .focused = rows.highlight == ::Some(k)645        });646    }647    return move parts;648}649650/* --- ChoiceList --- */651652pub fn layout<T: Equal>(653    self l: &var ChoiceList<T>,654    arrange: &var Arrange,655    limits: Limits656) -> Extent {657    const labels = ChoicesOf<T> { .choices = &l.choices.view() };658    if (l.rows.highlight is ::None && !l.popup) {659        l.rows.highlight = index_of(&l.choices.view(), &l.selected);660    }661    return l.rows.lay_out_rows(&labels, &var arrange, limits);662}663664exp fn paint<T: Equal>(self l: &ChoiceList<T>, look: &Look, paint: &var Paint) {665    const labels = ChoicesOf<T> { .choices = &l.choices.view() };666    const chosen = index_of(&l.choices.view(), &l.selected);667    l.rows.paint_rows(&labels, chosen, l.popup, l.enabled, l.error.length() > 0, &look, &var paint);668}669670pub fn overlay<T: Equal>(self l: &ChoiceList<T>, look: &Look, paint: &var Paint) {671    paint_bar(&look, &var paint, l.rows.offset, l.rows.content(l.choices.length()), Axis::Vertical);672}673674pub fn event<T: Equal>(self l: &var ChoiceList<T>, ui: &var Ui, input: &Input) -> bool {675    if (!l.enabled) {676        return claims(&input);677    }678    const labels = ChoicesOf<T> { .choices = &l.choices.view() };679    const chosen = index_of(&l.choices.view(), &l.selected);680    match (l.rows.rows_input(&labels, &var ui, &input, l.popup, chosen)) {681        ::Nothing {682            return false;683        }684        ::Took {685            return true;686        }687        ::Chose |k| {688            l.choose(&var ui, k);689            return true;690        }691        ::Close {692            close_own(&var ui);693            return true;694        }695    }696}697698// Closes the layer of the element handling an input.699exp fn close_own(ui: &var Ui) {700    if (ui.handling is ::Some |h| && ui.layer_of(h) is ::Some |layer|) {701        ui.close_at(layer);702    }703}704705// Gives the program the choice; a Select's list closes first, the706// Select hearing it then.707fn choose<T: Equal>(self l: &var ChoiceList<T>, ui: &var Ui, k: u64) {708    var value = copy l.choices.get(k).value;709    const changed = copy l.on_change;710    const same = index_of(&l.choices.view(), &l.selected) == ::Some(k);711    if (l.popup) {712        close_own(&var ui);713    }714    if (!same) {715        changed(&var ui, move value);716    }717}718719exp fn renew<T: Equal>(self l: &var ChoiceList<T>, next: ChoiceList<T>) -> Change {720    const relist = l.choices != next.choices;721    const reselect = l.selected != next.selected;722    const relayout = relist || l.sizing != next.sizing;723    const repaint = reselect || l.enabled != next.enabled || l.accessible != next.accessible724        || l.error != next.error725        || l.required != next.required;726    l.choices = move next.choices;727    l.selected = move next.selected;728    l.accessible = move next.accessible;729    l.on_change = move next.on_change;730    l.enabled = next.enabled;731    l.error = move next.error;732    l.required = next.required;733    l.sizing = next.sizing;734    if (relist) {735        l.rows.widest = ::None;736    }737    if (relist || reselect) {738        // the highlight follows the choice, kept in view739        l.rows.highlight = index_of(&l.choices.view(), &l.selected);740        l.rows.follow = true;741        return Change::Layout;742    }743    return if (relayout) Change::Layout else if (repaint) Change::Paint else Change::Nothing;744}745746pub fn sizing<T: Equal>(self l: &ChoiceList<T>) -> Sizing => l.sizing;747pub fn interactive<T: Equal>(self l: &ChoiceList<T>) -> bool => true;748pub fn focusable<T: Equal>(self l: &ChoiceList<T>) -> bool => l.enabled;749pub fn clips<T: Equal>(self l: &ChoiceList<T>) -> bool => true;750pub fn wants_hover<T: Equal>(self l: &ChoiceList<T>) -> bool => true;751752pub fn semantics<T: Equal>(self l: &ChoiceList<T>) -> Semantics => Semantics {753    .role = Role::ListBox,754    .label = copy l.accessible,755    .disabled = !l.enabled,756    .focusable = l.enabled,757    .invalid = l.error.length() > 0,758    .required = l.required,759    .description = copy l.error760};761762pub fn parts<T: Equal>(self l: &ChoiceList<T>) -> Vector<Part> {763    const labels = ChoicesOf<T> { .choices = &l.choices.view() };764    return l.rows.row_parts(&labels, index_of(&l.choices.view(), &l.selected), l.popup);765}766767/* --- Select --- */768769const FIELD_PADDING: Padding = Padding { .start = 10.0, .top = 7.0, .end = 10.0, .bottom = 7.0 };770const CHEVRON: f32 = 12.0;771const CHEVRON_GAP: f32 = 10.0;772773// What a popup keeps from the window's edges and from what it is below,774// as layers place it.775const EDGE: f32 = 16.0;776const BELOW: f32 = 4.0;777778exp fn layout<T: Equal>(self s: &var Select<T>, arrange: &var Arrange, limits: Limits) -> Extent {779    if (arrange.ui.marks.chevron is ::None) {780        var made = Option<Picture>::None;781        if (arrange.ui.scene is ::Some |&var scene|) {782            made = ::Some(chevron_mark(&var scene.canvas, &var scene.device()));783        }784        arrange.ui.marks.chevron = made;785    }786    const labels = ChoicesOf<T> { .choices = &s.choices.view() };787    const size = arrange.theme().text_size;788    const style = arrange.text_style(size);789    const fonts = &var arrange.fonts();790    if (s.widest is ::None) {791        const measured = measure(&var fonts, &style, &labels, &s.placeholder.view());792        s.widest = ::Some(measured[0]);793        s.tallest = measured[1];794    }795    const shown: &[u8] = if (796        index_of(&s.choices.view(), &s.selected) is ::Some |k|797    ) &s.choices.get(k).label.view() else &s.placeholder.view();798    const laid = text::Layout::fitted(&fonts, &shown, &style, UNBOUNDED);799    const line = if (laid.lines.try_get(0) is ::Some |&first|) first.height else size * 1.25;800    s.laid = ::Some(new laid);801    const widest = if (s.widest is ::Some |w|) w else 0.0;802    return limits.constrain(Extent {803        .width = FIELD_PADDING.start + widest + CHEVRON_GAP + CHEVRON + FIELD_PADDING.end,804        .height = FIELD_PADDING.top + line + FIELD_PADDING.bottom805    });806}807808exp fn paint<T: Equal>(self s: &Select<T>, look: &Look, paint: &var Paint) {809    const theme = &look.theme;810    const area = Rect::sized(look.size.width, look.size.height);811    var surface = theme.surface;812    if (!s.enabled) {813        surface.a = 0.5;814    }815    paint.rounded_rect(area, Radii::all(theme.radius), Fill::Color(surface));816    if (s.error.length() > 0) {817        paint.border(818            area,819            Radii::all(theme.radius),820            if (look.focus_ring) 2.0 else 1.5,821            theme.danger822        );823    } else if (look.focus_ring) {824        paint.border(area, Radii::all(theme.radius), 2.0, theme.focus);825    } else {826        paint.border(827            area,828            Radii::all(theme.radius),829            1.0,830            if (s.open is ::Some) theme.primary else if (831                look.hovered && s.enabled832            ) theme.muted else theme.border833        );834    }835    const chosen = index_of(&s.choices.view(), &s.selected) is ::Some;836    var ink = if (chosen) theme.text else theme.muted;837    if (!s.enabled) {838        ink.a = 0.5;839    }840    if (s.laid is ::Some |&laid|) {841        paint.text(842            &laid,843            Point {844                .x = look.from_start(FIELD_PADDING.start, laid.width),845                .y = (look.size.height - laid.height) / 2.0846            },847            ink848        );849    }850    // the chevron at the end, pointing down whichever way the field reads851    if (look.marks.chevron is ::Some |mark|) {852        var arrow = theme.muted;853        if (!s.enabled) {854            arrow.a = 0.5;855        }856        paint.mask(857            Rect::at(858                look.from_start(look.size.width - FIELD_PADDING.end - CHEVRON, CHEVRON),859                (look.size.height - CHEVRON) / 2.0,860                CHEVRON,861                CHEVRON862            ),863            mark,864            arrow865        );866    }867}868869exp fn event<T: Equal>(self s: &var Select<T>, ui: &var Ui, input: &Input) -> bool {870    return s.handle(&var ui, &input, (ui, select) => select_closed<T>(&var ui, select));871}872873// The Select's input; `closed` tells the element holding it, which may874// be another widget's (a TimePicker's), that its list closed.875exp fn handle<T: Equal>(876    self s: &var Select<T>,877    ui: &var Ui,878    input: &Input,879    closed: (&var Ui, Element)880) -> bool {881    if (!s.enabled) {882        return claims(&input);883    }884    match (input) {885        ::Press {886            // the pointer goes on to the list, whose row a release after887            // a drag chooses888            s.open_list(&var ui, false, closed);889            ui.uncapture();890        }891        ::Click {892            // a screen reader's press, the pointer's having opened it893            s.open_list(&var ui, true, closed);894        }895        ::Key |&k| {896            const opens = match (k.key) {897                ::Enter, ::Down, ::Up true;898                ::Character |c| c == ' ' to u32;899                else false;900            };901            if (opens) {902                if (k.pressed) {903                    s.open_list(&var ui, true, closed);904                }905                return true;906            }907            const c = if (typed_character(&k) is ::Some |c|) c else {908                return false;909            };910            if (k.pressed) {911                // closed, the item typed to is chosen at once912                const labels = ChoicesOf<T> { .choices = &s.choices.view() };913                const chosen = index_of(&s.choices.view(), &s.selected);914                if (s.typed.type_ahead(&labels, chosen, c, ui.now()) is ::Some |at|) {915                    if (chosen != ::Some(at)) {916                        var value = copy s.choices.get(at).value;917                        s.on_change(&var ui, move value);918                    }919                }920            }921        }922        ::Action |&a| {923            match (a) {924                ::Expand {925                    s.open_list(&var ui, true, closed);926                }927                ::Collapse {928                    if (s.open is ::Some |layer|) {929                        ui.close(layer);930                    }931                }932                else {}933            }934        }935        else {936            return claims(&input);937        }938    }939    return true;940}941942// Opens the list below the Select, or above it with more room there,943// at least as wide as the Select and at most POPUP_ROWS rows tall, the944// chosen row highlighted in view; the list takes the keyboard.945fn open_list<T: Equal>(946    self s: &var Select<T>,947    ui: &var Ui,948    keyboard: bool,949    closed: (&var Ui, Element)950) {951    if (s.open is ::Some) {952        return;953    }954    const select = ui.source();955    const frame = ui.frame_of(select);956    const row = s.tallest + ROW_PADDING.top + ROW_PADDING.bottom;957    const below = ui.viewport.height - EDGE - (frame.y + frame.height + BELOW);958    const above = frame.y - BELOW - EDGE;959    const room = max(below, above);960    const tallest = POPUP_ROWS * row + 2.0 * LIST_PADDING;961    const labels = ChoicesOf<T> { .choices = &s.choices.view() };962    const chosen = index_of(&s.choices.view(), &s.selected);963    var list = ChoiceList<T> {964        .choices = copy s.choices,965        .selected = copy s.selected,966        .accessible = copy s.accessible,967        .on_change = copy s.on_change,968        .popup = true,969        .sizing = Sizing {970            .min_width = frame.width,971            .max_height = max(min(room, tallest), row + 2.0 * LIST_PADDING)972        },973        .rows = ListState {974            .highlight = if (chosen is ::Some) chosen else stepped(&labels, ::None, 0, false),975            .follow = true,976            .widest = s.widest,977            .row = row978        }979    };980    if (keyboard) {981        ui.focus_ring = true;982    }983    var overlay = Overlay::popup(select);984    overlay.tabbed = true;985    overlay.on_close = |select, closed| (ui: &var Ui) => closed(&var ui, select);986    const layer = ui.open(987        move overlay,988        |move list| (ui: &var Ui, layer: Layer) => ui.add(copy list)989    );990    s.open = ::Some(layer);991    ui.press_opened(layer);992    ui.refresh(Change::Paint);993}994995// The Select's list closed.996fn select_closed<T: Equal>(ui: &var Ui, select: Element) {997    if (998        ui.exists(select)999        && ui.node_var(select.index).widget is ::Some |&var widget|1000        && widget is Select<T> |&var s|1001    ) {1002        s.open = ::None;1003        ui.apply(select.index, Change::Paint);1004    }1005}10061007exp fn take_closing<T: Equal>(self s: &var Select<T>) -> Option<Layer> {1008    const closing = s.closing;1009    s.closing = ::None;1010    return closing;1011}10121013exp fn renew<T: Equal>(self s: &var Select<T>, next: Select<T>) -> Change {1014    const relist = s.choices != next.choices;1015    const reselect = s.selected != next.selected;1016    const relayout = relist || reselect || s.placeholder != next.placeholder1017        || s.sizing != next.sizing;1018    const repaint = s.enabled != next.enabled || s.accessible != next.accessible1019        || s.error != next.error1020        || s.required != next.required;1021    // the list open on other options closes1022    if (relist || reselect || s.enabled != next.enabled) {1023        if (s.open is ::Some |layer|) {1024            s.closing = ::Some(layer);1025            s.open = ::None;1026        }1027    }1028    if (relist || s.placeholder != next.placeholder) {1029        s.widest = ::None;1030    }1031    s.choices = move next.choices;1032    s.selected = move next.selected;1033    s.placeholder = move next.placeholder;1034    s.accessible = move next.accessible;1035    s.on_change = move next.on_change;1036    s.enabled = next.enabled;1037    s.error = move next.error;1038    s.required = next.required;1039    s.sizing = next.sizing;1040    return if (relayout) Change::Layout else if (repaint) Change::Paint else Change::Nothing;1041}10421043exp fn sizing<T: Equal>(self s: &Select<T>) -> Sizing => s.sizing;1044exp fn interactive<T: Equal>(self s: &Select<T>) -> bool => true;1045exp fn focusable<T: Equal>(self s: &Select<T>) -> bool => s.enabled;1046pub fn cursor<T: Equal>(1047    self s: &Select<T>1048) -> Cursor => if (s.enabled) Cursor::Hand else Cursor::Arrow;1049exp fn clips<T: Equal>(self s: &Select<T>) -> bool => true;10501051exp fn semantics<T: Equal>(self s: &Select<T>) -> Semantics {1052    var value = String::empty();1053    if (index_of(&s.choices.view(), &s.selected) is ::Some |k|) {1054        value = copy s.choices.get(k).label;1055    }1056    return Semantics {1057        .role = Role::ComboBox,1058        .label = copy s.accessible,1059        .value = move value,1060        .disabled = !s.enabled,1061        .focusable = s.enabled,1062        .expanded = ::Some(s.open is ::Some),1063        .invalid = s.error.length() > 0,1064        .required = s.required,1065        .description = copy s.error1066    };1067}10681069/* --- Radios --- */10701071const DOT: f32 = 18.0; // a radio's circle1072const DOT_GAP: f32 = 8.0; // between it and its label1073const INNER: f32 = 8.0; // the chosen one's dot10741075// A group's or a tab bar's own state: its labels laid out, each item's1076// area, the one holding the focus while the element has it and the one1077// under the pointer.1078pub type ItemState = struct {1079    pub laid: Vector<text::Layout> = Vector<text::Layout>::empty(),1080    pub boxes: Vector<Rect> = Vector<Rect>::empty(),1081    pub focus: Option<u64> = ::None,1082    pub hovered: Option<u64> = ::None,1083    // whether the last options chose another item, and which was chosen1084    // before, which the choice moves from1085    pub switched: bool = false,1086    pub before: Option<u64> = ::None,1087};10881089// The radio at the point.1090fn item_at(self r: &ItemState, at: Point) -> Option<u64> {1091    for (r.boxes.view(), [..r.boxes.length()]) |&b, k| {1092        if (holds(&b, at)) {1093            return ::Some(k);1094        }1095    }1096    return ::None;1097}10981099pub fn layout<T: Equal>(self r: &var Radios<T>, arrange: &var Arrange, limits: Limits) -> Extent {1100    const size = arrange.theme().text_size;1101    const state = &var r.state;1102    state.laid.clear();1103    state.boxes.clear();1104    var x: f32 = 0.0;1105    var y: f32 = 0.0;1106    var extent = Extent::zero();1107    for (r.choices.view(), [..r.choices.length()]) |&choice, k| {1108        if (k > 0) {1109            if (r.horizontal) {1110                x += r.gap;1111            } else {1112                y += r.gap;1113            }1114        }1115        const room = if (r.horizontal) UNBOUNDED else max(limits.max_width - DOT - DOT_GAP, 1.0);1116        var laid = text_layout(&var arrange, &choice.label.view(), size, REGULAR, room);1117        const item = Extent {1118            .width = DOT + DOT_GAP + laid.width,1119            .height = max(DOT, laid.height)1120        };1121        state.boxes.push(Rect::at(x, y, item.width, item.height));1122        state.laid.push(move laid);1123        extent.width = max(extent.width, x + item.width);1124        extent.height = max(extent.height, y + item.height);1125        if (r.horizontal) {1126            x += item.width;1127        } else {1128            y += item.height;1129        }1130    }1131    const size = limits.constrain(extent);1132    mirror_boxes(&var state.boxes, size.width, arrange.direction());1133    return size;1134}11351136// Right to left, boxes laid out from the start mirrored, as the Ui places1137// children: the first at the right.1138fn mirror_boxes(boxes: &var Vector<Rect>, width: f32, direction: LayoutDirection) {1139    if (direction is ::RightToLeft) {1140        for (boxes.items()) |&var b| {1141            b.x = width - b.x - b.width;1142        }1143    }1144}11451146exp fn paint<T: Equal>(self r: &Radios<T>, look: &Look, paint: &var Paint) {1147    const theme = &look.theme;1148    const chosen = index_of(&r.choices.view(), &r.selected);1149    const state = &r.state;1150    for (state.boxes.view(), [..state.boxes.length()]) |&b, k| {1151        const usable = r.enabled && r.choices.get(k).enabled;1152        const hovered = usable && look.hovered && state.hovered == ::Some(k);1153        // the circle at the radio's start, its label after it1154        const rtl = look.direction is ::RightToLeft;1155        const circle = Rect::at(1156            if (rtl) b.x + b.width - DOT else b.x,1157            b.y + (b.height - DOT) / 2.0,1158            DOT,1159            DOT1160        );1161        const alpha: f32 = if (usable) 1.0 else 0.5;1162        if (chosen == ::Some(k)) {1163            var fill = shade(theme.primary, if (hovered) theme.hover else 0.0, false);1164            fill.a = fill.a * alpha;1165            paint.rounded_rect(circle, Radii::all(DOT / 2.0), Fill::Color(fill));1166            var dot = theme.on_primary;1167            dot.a = dot.a * alpha;1168            // the dot of one just chosen grows, or fades in under reduced1169            // motion1170            var inner = INNER;1171            if (state.switched) {1172                const t = settled(&look);1173                if (look.reduced) {1174                    dot.a = dot.a * t;1175                } else {1176                    inner = INNER * t;1177                }1178            }1179            paint.rounded_rect(1180                Rect::at(1181                    circle.x + (DOT - inner) / 2.0,1182                    circle.y + (DOT - inner) / 2.0,1183                    inner,1184                    inner1185                ),1186                Radii::all(inner / 2.0),1187                Fill::Color(dot)1188            );1189        } else {1190            var fill = theme.surface;1191            fill.a = fill.a * alpha;1192            paint.rounded_rect(circle, Radii::all(DOT / 2.0), Fill::Color(fill));1193            var ring = if (r.error.length() > 0) theme.danger else if (1194                hovered1195            ) theme.muted else theme.border;1196            ring.a = ring.a * alpha;1197            paint.border(circle, Radii::all(DOT / 2.0), 1.5, ring);1198        }1199        if (look.focus_ring && state.focus == ::Some(k)) {1200            focus_ring(&look, &var paint, circle, DOT / 2.0);1201        }1202        if (state.laid.try_get(k) is ::Some |&laid|) {1203            var ink = theme.text;1204            ink.a = ink.a * alpha;1205            paint.text(1206                &laid,1207                Point {1208                    .x = if (1209                        rtl1210                    ) b.x + b.width - DOT - DOT_GAP - laid.width else b.x + DOT + DOT_GAP,1211                    .y = b.y + (b.height - laid.height) / 2.01212                },1213                ink1214            );1215        }1216    }1217}12181219pub fn event<T: Equal>(self r: &var Radios<T>, ui: &var Ui, input: &Input) -> bool {1220    if (!r.enabled) {1221        return claims(&input);1222    }1223    const labels = ChoicesOf<T> { .choices = &r.choices.view() };1224    const chosen = index_of(&r.choices.view(), &r.selected);1225    match (input) {1226        ::Hover |p| {1227            const under = r.state.item_at(p);1228            if (under != r.state.hovered) {1229                r.state.hovered = under;1230                ui.refresh(Change::Paint);1231            }1232        }1233        ::Click |p| {1234            if (r.state.item_at(p) is ::Some |k| && labels.enabled(k)) {1235                r.choose(&var ui, k);1236            }1237        }1238        ::Focus |focused| {1239            if (focused) {1240                r.state.focus = if (1241                    chosen is ::Some |k| && labels.enabled(k)1242                ) chosen else stepped(&labels, ::None, 0, false);1243            }1244            ui.refresh(Change::Paint);1245            return false;1246        }1247        ::Key |&k| {1248            // Left and Right the way they point: to the next right to left1249            const ahead: i64 = if (ui.handling_rtl()) -1 else 1;1250            const step: i64 = match (k.key) {1251                ::Down 1;1252                ::Up -1;1253                ::Right ahead;1254                ::Left -ahead;1255                else 0;1256            };1257            if (step == 0 && !is_space(&k)) {1258                return false;1259            }1260            if (k.pressed) {1261                const at = if (step != 0) stepped(1262                    &labels,1263                    r.state.focus,1264                    step,1265                    true1266                ) else r.state.focus;1267                if (at is ::Some |next| && next < labels.count() && labels.enabled(next)) {1268                    r.choose(&var ui, next);1269                }1270            }1271        }1272        ::Part |&request| {1273            const k = request.part to u64;1274            if (k < r.choices.length()) {1275                match (request.action) {1276                    ::Click, ::Select {1277                        if (labels.enabled(k)) {1278                            r.choose(&var ui, k);1279                        }1280                    }1281                    ::Focus {1282                        r.state.focus = ::Some(k);1283                        ui.refresh(Change::Paint);1284                    }1285                    else {}1286                }1287            }1288        }1289        else {1290            return claims(&input);1291        }1292    }1293    return true;1294}12951296// Moves the focus to the radio and gives the program its value, when1297// not chosen already.1298fn choose<T: Equal>(self r: &var Radios<T>, ui: &var Ui, k: u64) {1299    r.state.focus = ::Some(k);1300    ui.refresh(Change::Paint);1301    if (index_of(&r.choices.view(), &r.selected) != ::Some(k)) {1302        var value = copy r.choices.get(k).value;1303        r.on_change(&var ui, move value);1304    }1305}13061307exp fn renew<T: Equal>(self r: &var Radios<T>, next: Radios<T>) -> Change {1308    const relayout = r.choices != next.choices || r.horizontal != next.horizontal1309        || r.gap != next.gap1310        || r.sizing != next.sizing;1311    const repaint = r.selected != next.selected || r.enabled != next.enabled1312        || r.accessible != next.accessible1313        || r.error != next.error1314        || r.required != next.required;1315    if (r.choices != next.choices) {1316        // the radio focused or hovered may be gone1317        r.state.focus = index_of(&next.choices.view(), &next.selected);1318        r.state.hovered = ::None;1319    }1320    r.state.switched = r.choices == next.choices && r.selected != next.selected;1321    r.state.before = index_of(&r.choices.view(), &r.selected);1322    r.choices = move next.choices;1323    r.selected = move next.selected;1324    r.accessible = move next.accessible;1325    r.on_change = move next.on_change;1326    r.horizontal = next.horizontal;1327    r.gap = next.gap;1328    r.enabled = next.enabled;1329    r.error = move next.error;1330    r.required = next.required;1331    r.sizing = next.sizing;1332    return if (relayout) Change::Layout else if (repaint) Change::Paint else Change::Nothing;1333}13341335pub fn sizing<T: Equal>(self r: &Radios<T>) -> Sizing => r.sizing;13361337pub fn settling<T: Equal>(self r: &Radios<T>, since: Duration) -> bool {1338    return r.state.switched && since.nanoseconds < SETTLE;1339}1340pub fn interactive<T: Equal>(self r: &Radios<T>) -> bool => true;1341pub fn focusable<T: Equal>(self r: &Radios<T>) -> bool => r.enabled;1342pub fn wants_hover<T: Equal>(self r: &Radios<T>) -> bool => true;1343pub fn cursor<T: Equal>(1344    self r: &Radios<T>1345) -> Cursor => if (r.enabled) Cursor::Hand else Cursor::Arrow;13461347pub fn semantics<T: Equal>(self r: &Radios<T>) -> Semantics => Semantics {1348    .role = Role::RadioGroup,1349    .label = copy r.accessible,1350    .disabled = !r.enabled,1351    .focusable = r.enabled,1352    .invalid = r.error.length() > 0,1353    .required = r.required,1354    .description = copy r.error1355};13561357pub fn parts<T: Equal>(self r: &Radios<T>) -> Vector<Part> {1358    const chosen = index_of(&r.choices.view(), &r.selected);1359    const count = r.choices.length();1360    var parts = Vector<Part>::with_capacity(count);1361    for (r.choices.view(), [..count]) |&choice, k| {1362        const usable = r.enabled && choice.enabled;1363        parts.push(Part {1364            .semantics = Semantics {1365                .role = Role::RadioButton,1366                .label = copy choice.label,1367                .checked = ::Some(chosen == ::Some(k)),1368                .disabled = !usable,1369                .focusable = usable,1370                .position = ::Some([k to u32 + 1, count to u32])1371            },1372            .bounds = if (r.state.boxes.try_get(k) is ::Some |&b|) b else Rect::empty(),1373            .focused = r.state.focus == ::Some(k)1374        });1375    }1376    return move parts;1377}13781379/* --- Tabs --- */13801381const TAB_ACROSS: f32 = 16.0; // a tab's padding beside its label1382const TAB_DOWN: f32 = 10.0; // above and below it1383const UNDERLINE: f32 = 2.0; // the selected tab's13841385pub fn layout<T: Equal>(self t: &var Tabs<T>, arrange: &var Arrange, limits: Limits) -> Extent {1386    const size = arrange.theme().text_size;1387    const state = &var t.state;1388    state.laid.clear();1389    state.boxes.clear();1390    var x: f32 = 0.0;1391    var height: f32 = 0.0;1392    for (t.tabs.view()) |&tab| {1393        var laid = text_layout(&var arrange, &tab.label.view(), size, REGULAR, UNBOUNDED);1394        const width = laid.width + 2.0 * TAB_ACROSS;1395        height = max(height, laid.height + 2.0 * TAB_DOWN + UNDERLINE);1396        state.boxes.push(Rect::at(x, 0.0, width, 0.0));1397        state.laid.push(move laid);1398        x += width;1399    }1400    for (state.boxes.items()) |&var b| {1401        b.height = height;1402    }1403    const size = limits.constrain(Extent { .width = x, .height = height });1404    mirror_boxes(&var state.boxes, size.width, arrange.direction());1405    return size;1406}14071408exp fn paint<T: Equal>(self t: &Tabs<T>, look: &Look, paint: &var Paint) {1409    const theme = &look.theme;1410    const chosen = index_of(&t.tabs.view(), &::Some(copy t.selected));1411    const state = &t.state;1412    paint.rect(1413        Rect::at(0.0, look.size.height - 1.0, look.size.width, 1.0),1414        Fill::Color(theme.border)1415    );1416    for (state.boxes.view(), [..state.boxes.length()]) |&b, k| {1417        const usable = t.enabled && t.tabs.get(k).enabled;1418        const selected = chosen == ::Some(k);1419        if (usable && look.hovered && state.hovered == ::Some(k)) {1420            paint.rounded_rect(1421                Rect::at(b.x, b.y, b.width, b.height - UNDERLINE),1422                Radii::all(theme.radius),1423                Fill::Color(shade(theme.text, theme.hover, true))1424            );1425        }1426        if (selected) {1427            // the underline slides from the tab selected before1428            var x = b.x;1429            var width = b.width;1430            if (1431                state.switched1432                && !look.reduced1433                && state.before is ::Some |was|1434                && state.boxes.try_get(was) is ::Some |&from|1435            ) {1436                const t = settled(&look);1437                x = from.x + (b.x - from.x) * t;1438                width = from.width + (b.width - from.width) * t;1439            }1440            paint.rect(1441                Rect::at(x, b.y + b.height - UNDERLINE, width, UNDERLINE),1442                Fill::Color(theme.primary)1443            );1444        }1445        if (look.focus_ring && state.focus == ::Some(k)) {1446            focus_ring(1447                &look,1448                &var paint,1449                Rect::at(b.x + 3.0, b.y + 3.0, b.width - 6.0, b.height - UNDERLINE - 6.0),1450                theme.radius1451            );1452        }1453        if (state.laid.try_get(k) is ::Some |&laid|) {1454            var ink = if (selected) theme.primary else theme.muted;1455            if (usable && !selected && look.hovered && state.hovered == ::Some(k)) {1456                ink = theme.text;1457            }1458            ink.a = ink.a * if (usable) 1.0 else 0.5;1459            paint.text(1460                &laid,1461                Point {1462                    .x = b.x + TAB_ACROSS,1463                    .y = b.y + (b.height - UNDERLINE - laid.height) / 2.01464                },1465                ink1466            );1467        }1468    }1469}14701471pub fn event<T: Equal>(self t: &var Tabs<T>, ui: &var Ui, input: &Input) -> bool {1472    if (!t.enabled) {1473        return claims(&input);1474    }1475    const labels = ChoicesOf<T> { .choices = &t.tabs.view() };1476    const chosen = index_of(&t.tabs.view(), &::Some(copy t.selected));1477    match (input) {1478        ::Hover |p| {1479            const under = t.state.item_at(p);1480            if (under != t.state.hovered) {1481                t.state.hovered = under;1482                ui.refresh(Change::Paint);1483            }1484        }1485        ::Click |p| {1486            if (t.state.item_at(p) is ::Some |k| && labels.enabled(k)) {1487                t.choose(&var ui, k);1488            }1489        }1490        ::Focus |focused| {1491            if (focused) {1492                t.state.focus = if (1493                    chosen is ::Some |k| && labels.enabled(k)1494                ) chosen else stepped(&labels, ::None, 0, false);1495            }1496            ui.refresh(Change::Paint);1497            return false;1498        }1499        ::Key |&k| {1500            const taken = match (k.key) {1501                ::Right, ::Left, ::Home, ::End, ::Enter true;1502                else is_space(&k);1503            };1504            if (!taken) {1505                return false;1506            }1507            // the first tab at the start: Right goes back right to left1508            const ahead: i64 = if (ui.handling_rtl()) -1 else 1;1509            const at = match (k.key) {1510                ::Right stepped(&labels, t.state.focus, ahead, true);1511                ::Left stepped(&labels, t.state.focus, -ahead, true);1512                ::Home stepped(&labels, ::None, 0, false);1513                ::End last_enabled(&labels);1514                else t.state.focus;1515            };1516            if (k.pressed && at is ::Some |next| && next < labels.count() && labels.enabled(next)) {1517                t.choose(&var ui, next);1518            }1519        }1520        ::Part |&request| {1521            const k = request.part to u64;1522            if (k < t.tabs.length()) {1523                match (request.action) {1524                    ::Click, ::Select {1525                        if (labels.enabled(k)) {1526                            t.choose(&var ui, k);1527                        }1528                    }1529                    ::Focus {1530                        t.state.focus = ::Some(k);1531                        ui.refresh(Change::Paint);1532                    }1533                    else {}1534                }1535            }1536        }1537        else {1538            return claims(&input);1539        }1540    }1541    return true;1542}15431544// Moves the focus to the tab and gives the program its value, when not1545// selected already.1546fn choose<T: Equal>(self t: &var Tabs<T>, ui: &var Ui, k: u64) {1547    t.state.focus = ::Some(k);1548    ui.refresh(Change::Paint);1549    if (index_of(&t.tabs.view(), &::Some(copy t.selected)) != ::Some(k)) {1550        var value = copy t.tabs.get(k).value;1551        t.on_change(&var ui, move value);1552    }1553}15541555exp fn renew<T: Equal>(self t: &var Tabs<T>, next: Tabs<T>) -> Change {1556    const relayout = t.tabs != next.tabs || t.sizing != next.sizing;1557    const repaint = t.selected != next.selected || t.enabled != next.enabled1558        || t.accessible != next.accessible;1559    t.state.switched = t.tabs == next.tabs && t.selected != next.selected;1560    t.state.before = index_of(&t.tabs.view(), &::Some(copy t.selected));1561    if (t.tabs != next.tabs) {1562        // the tab focused or hovered may be gone1563        t.state.focus = index_of(&next.tabs.view(), &::Some(copy next.selected));1564        t.state.hovered = ::None;1565    } else if (t.selected != next.selected) {1566        t.state.focus = index_of(&t.tabs.view(), &::Some(copy next.selected));1567    }1568    t.tabs = move next.tabs;1569    t.selected = move next.selected;1570    t.accessible = move next.accessible;1571    t.on_change = move next.on_change;1572    t.enabled = next.enabled;1573    t.sizing = next.sizing;1574    return if (relayout) Change::Layout else if (repaint) Change::Paint else Change::Nothing;1575}15761577pub fn sizing<T: Equal>(self t: &Tabs<T>) -> Sizing => t.sizing;15781579pub fn settling<T: Equal>(self t: &Tabs<T>, since: Duration) -> bool {1580    return t.state.switched && since.nanoseconds < SETTLE;1581}1582pub fn interactive<T: Equal>(self t: &Tabs<T>) -> bool => true;1583pub fn focusable<T: Equal>(self t: &Tabs<T>) -> bool => t.enabled;1584pub fn wants_hover<T: Equal>(self t: &Tabs<T>) -> bool => true;1585pub fn cursor<T: Equal>(1586    self t: &Tabs<T>1587) -> Cursor => if (t.enabled) Cursor::Hand else Cursor::Arrow;15881589pub fn semantics<T: Equal>(self t: &Tabs<T>) -> Semantics => Semantics {1590    .role = Role::TabList,1591    .label = copy t.accessible,1592    .disabled = !t.enabled,1593    .focusable = t.enabled1594};15951596pub fn parts<T: Equal>(self t: &Tabs<T>) -> Vector<Part> {1597    const chosen = index_of(&t.tabs.view(), &::Some(copy t.selected));1598    const count = t.tabs.length();1599    var parts = Vector<Part>::with_capacity(count);1600    for (t.tabs.view(), [..count]) |&tab, k| {1601        const usable = t.enabled && tab.enabled;1602        parts.push(Part {1603            .semantics = Semantics {1604                .role = Role::Tab,1605                .label = copy tab.label,1606                .selected = ::Some(chosen == ::Some(k)),1607                .disabled = !usable,1608                .focusable = usable,1609                .position = ::Some([k to u32 + 1, count to u32])1610            },1611            .bounds = if (t.state.boxes.try_get(k) is ::Some |&b|) b else Rect::empty(),1612            .focused = t.state.focus == ::Some(k)1613        });1614    }1615    return move parts;1616}