Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

18.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2026 Jareer Abdullah and Concat contributors

import { Theme } from "../theme/theme.slint";
import { Glyph, Icon } from "../icons.slint";
import { FocusRing } from "focus-ring.slint";
import { Tip } from "tooltip.slint";
import { ThinScrollbar } from "scrollbar.slint";
import { Avatar, AvatarData } from "avatar.slint";

// Custom listbox dropdown rather than a native combo box, so options can carry
// icons and match the panel styling. Arrows, Home/End, Enter and Escape all
// work; the list flips above the trigger when there is no room below, which
// Slint's PopupWindow does on its own.
export component Select inherits Rectangle {
    in property <[string]> options;
    /// Optional second column, set against the trailing edge of each row —
    /// the "1:2" beside "Half". Shorter than `options` is fine; rows past its
    /// end simply have none. The trigger never shows it: it is what you open
    /// the list to compare.
    in property <[string]> details;
    in-out property <int> current;
    in property <string> placeholder: "Select";
    /// leading adornment on the trigger
    in property <Glyph> glyph: Glyph.none;
    /// One mark per option, in the same order. Set it and the trigger shows
    /// the current option's mark in place of its words — the words stay in
    /// the list, where there is room for them, and each row carries its mark
    /// beside them so the pairing is learnt by reading the list once. For a
    /// control that is one of several against the edge of a row, where three
    /// words of "Sort: Added" cost more room than they earn. Shorter than
    /// `options` is fine; a row past its end simply has none.
    in property <[Glyph]> icons;
    /// One avatar per option, in the same order: a round picture or a
    /// letter in a colour, before each row's words and before the
    /// trigger's - for a list of who, like voices, where a face or a
    /// colour is found faster than a name is read. Shorter than `options`
    /// is fine; a row past its end simply has none.
    in property <[AvatarData]> avatars;
    in property <bool> enabled: true;
    /// Rows that are shown and cannot be picked, by index: greyed, and a
    /// press or Enter on one does nothing. Shorter than `options` leaves
    /// the rest pickable.
    in property <[bool]> disabled;
    /// drop the field background, for a select used as panel chrome
    in property <bool> flat;
    /// Size `natural-width` to the current option rather than the widest.
    /// For a control at the edge of a crowded tray, where the room a long
    /// option would reserve is room the tray does not have; the trigger
    /// then changes width as the choice does, which is the trade.
    in property <bool> snug;
    /// The corners, a side at a time, so a select can be joined onto a
    /// field as its unit with a square seam between them; see Field.
    in property <length> radius-left: Theme.r;
    in property <length> radius-right: Theme.r;

    callback changed(index: int);

    property <bool> ring;
    property <length> row-h: 26px;
    /// What the rows need, and what the list is given: the rows, capped
    /// at nine or so. Past the cap the list scrolls, under a wheel or a
    /// finger - see `flick`.
    property <length> content-h: root.options.length * root.row-h + 8px;
    property <length> list-h: Math.min(240px, root.content-h);
    /// How far down the rows are parked: the list's Flickable's own
    /// viewport, which is zero or less. Clamped on read, so a list that
    /// reopens shorter cannot leave the rows parked beyond its end.
    property <length> overflow: Math.max(0px, root.content-h - root.list-h);
    property <length> content-y;
    property <length> at: Math.max(0px, Math.min(root.overflow, -root.content-y));

    pure function row-top(index: int) -> length {
        return 4px + index * root.row-h;
    }
    /// Scrolls just enough to bring row `index` into view: from above when
    /// it is above, from below when it is below, not at all when it shows.
    function reveal-row(index: int) {
        if (root.row-top(index) - 4px < root.at) {
            root.content-y = -(root.row-top(index) - 4px);
        } else if (root.row-top(index) + root.row-h + 4px > root.at + root.list-h) {
            root.content-y = -(root.row-top(index) + root.row-h + 4px - root.list-h);
        }
    }
    // Read from the popup rather than tracked here: a click outside closes it
    // without telling us, so a hand-rolled flag goes stale.
    property <bool> open: list.is-open;
    property <int> active;
    property <string> label: (root.current >= 0 && root.current < root.options.length)
        ? root.options[root.current]
        : root.placeholder;
    /// the current option has a mark, so the trigger wears it instead of words
    property <bool> marked: root.current >= 0 && root.current < root.icons.length;
    property <Glyph> mark: root.marked ? root.icons[root.current] : Glyph.none;
    /// the mark on the trigger, at the size of the glyph it replaces
    property <length> mark-size: 14px;
    /// The current option has an avatar, which the trigger shows before
    /// its words.
    property <bool> pictured: root.current >= 0 && root.current < root.avatars.length;
    /// What the avatars add to a row: the circle and the gap after it.
    property <length> avatar-w: root.avatars.length > 0 ? 18px + 6px : 0px;

    /// What the trigger needs to show its options whole: the padding, the
    /// chevron, the optional glyph, and the gaps between them. A select set
    /// against the edge of a row takes this instead of a share of the row —
    /// `width: self.natural-width` at the call site.
    ///
    /// Measured off the widest option rather than the current one, so the
    /// control keeps its size as the selection moves from a long option to a
    /// short one; a trigger that changed width when you picked from it would
    /// walk the controls beside it across the row.
    out property <length> natural-width: 8px + 6px + 12px + 6px
        + (root.marked
            ? root.mark-size
            : (root.snug ? shown.preferred-width : measure.preferred-width)
                + (root.glyph != Glyph.none ? 12px + 6px : 0px)
                + root.avatar-w);

    height: Theme.h;
    min-width: 60px;
    horizontal-stretch: 1;
    border-top-left-radius: root.radius-left;
    border-bottom-left-radius: root.radius-left;
    border-top-right-radius: root.radius-right;
    border-bottom-right-radius: root.radius-right;
    opacity: root.enabled ? 1.0 : 0.45;
    background: root.flat && !root.open ? Colors.transparent
        : (root.open || (trigger.has-hover && root.enabled)) ? Theme.field-hover
        : Theme.field;

    animate background { duration: 120ms; easing: cubic-bezier(0.4, 0, 0.2, 1); }

    /// Whether row `index` is one of the `disabled`.
    pure function off(index: int) -> bool {
        return index < root.disabled.length && root.disabled[index];
    }

    function commit(index: int) {
        if (index >= 0 && index < root.options.length && !root.off(index)) {
            root.current = index;
            root.changed(index);
        }
        list.close();
        fs.focus();
    }

    function reveal() {
        if (!root.enabled) {
            return;
        }
        root.active = Math.max(root.current, 0);
        // Opened on the current row: a list of twelve languages that
        // opened at the top hid whichever was chosen past the ninth.
        root.content-y = 0px;
        root.reveal-row(root.active);
        list.show();
    }

    fs := FocusScope {
        enabled: root.enabled;
        // :focus-visible — keyboard focus only, see IconButton.
        focus-gained(reason) => {
            root.ring = reason == FocusReason.tab-navigation;
        }
        focus-lost(reason) => {
            root.ring = false;
        }
        key-pressed(event) => {
            if (event.text == Key.DownArrow || event.text == Key.UpArrow
                || event.text == "\n" || event.text == " ") {
                root.reveal();
                return EventResult.accept;
            }
            return EventResult.reject;
        }
    }

    HorizontalLayout {
        padding-left: 8px;
        padding-right: 6px;
        spacing: 6px;

        if root.glyph != Glyph.none && !root.marked: VerticalLayout {
            alignment: center;
            Icon { glyph: root.glyph; size: 12px; tint: Theme.fg-dim; }
        }

        // A layout of its own rather than the mark on its own, so a marked
        // trigger given more width than it asked for keeps the mark at the
        // leading edge and the chevron at the trailing one, the way the words
        // it stands in for would have sat.
        if root.marked: HorizontalLayout {
            horizontal-stretch: 1;
            alignment: start;

            VerticalLayout {
                alignment: center;
                Icon {
                    glyph: root.mark;
                    size: root.mark-size;
                    tint: (root.open || trigger.has-hover) ? Theme.fg : Theme.fg-muted;
                }
            }
        }

        if !root.marked && root.pictured: VerticalLayout {
            alignment: center;
            Avatar {
                data: root.avatars[root.current];
                size: 18px;
            }
        }

        if !root.marked: Text {
            horizontal-stretch: 1;
            text: root.label;
            color: root.current >= 0 ? (root.flat ? Theme.fg-muted : Theme.fg) : Theme.fg-dim;
            font-size: Theme.fs;
            vertical-alignment: center;
            overflow: elide;
        }

        VerticalLayout {
            alignment: center;
            Icon { glyph: Glyph.chevron-down; size: 12px; tint: Theme.fg-muted; }
        }
    }

    // The ruler behind `natural-width`. Measured rather than estimated: a Text
    // parked at the origin still reports the real width of its string in the
    // real face — see TooltipLayer — and a child given an `x` is left out of
    // its parent's constraints, so measuring the trigger cannot widen it. The
    // rows are stacked because a VerticalLayout's preferred width is the
    // widest of them, which is the only fold over a model the language has.
    measure := VerticalLayout {
        x: 0;
        y: 0;
        width: 0px;
        height: 0px;
        visible: false;

        for option in root.options: Text {
            text: option;
            font-size: Theme.fs;
        }
        // The current option alone, for a `snug` trigger.
        shown := Text {
            text: root.label;
            font-size: Theme.fs;
        }
    }

    trigger := TouchArea {
        enabled: root.enabled;
        mouse-cursor: pointer;
        clicked => {
            fs.focus();
            root.reveal();
        }
    }

    FocusRing {
        active: root.ring || root.open;
        radius: Theme.r;
    }

    // Only where the mark replaced the words. A trigger showing its label is
    // already saying what a tip would say, and the open list says it too.
    Tip {
        text: root.marked ? root.label : "";
        active: trigger.has-hover && root.enabled && !root.open;
    }

    /// The width the list needs to spell every option out: the widest
    /// option's own width plus the row's marks and padding around it
    /// (6+6 row, 13 icon, 6+6 gaps, 12 check, 8 list padding, 2 border).
    /// A marked trigger is only as wide as its icon, and the list it opens
    /// is the one place the options are written, so the list may not
    /// inherit that width.
    property <length> list-w: Math.max(root.width, measure.preferred-width + 59px + root.avatar-w);

    list := PopupWindow {
        // Right edge on the trigger's right edge: a list wider than an
        // icon-sized trigger at a row's end grows back into the row, not
        // off the pane.
        x: root.width - root.list-w;
        y: root.height + 4px;
        width: root.list-w;
        height: root.list-h;
        close-policy: close-on-click-outside;

        // The popup is torn down on close, so `init` runs on every open.
        init => {
            keys.focus();
        }

        Rectangle {
            background: Theme.raised;
            border-width: 1px;
            border-color: Theme.line-strong;
            border-radius: Theme.r;
            clip: true;

            keys := FocusScope {
                key-pressed(event) => {
                    if (event.text == Key.DownArrow) {
                        root.active = Math.min(root.active + 1, root.options.length - 1);
                        root.reveal-row(root.active);
                        return EventResult.accept;
                    }
                    if (event.text == Key.UpArrow) {
                        root.active = Math.max(root.active - 1, 0);
                        root.reveal-row(root.active);
                        return EventResult.accept;
                    }
                    if (event.text == Key.Home) {
                        root.active = 0;
                        root.reveal-row(root.active);
                        return EventResult.accept;
                    }
                    if (event.text == Key.End) {
                        root.active = root.options.length - 1;
                        root.reveal-row(root.active);
                        return EventResult.accept;
                    }
                    if (event.text == "\n" || event.text == " ") {
                        root.commit(root.active);
                        return EventResult.accept;
                    }
                    if (event.text == Key.Escape) {
                        list.close();
                        fs.focus();
                        return EventResult.accept;
                    }
                    return EventResult.reject;
                }
            }

            // A Flickable, so the list scrolls under a finger as well as a
            // wheel: a list past its nine rows, the languages among them,
            // moved only by its scrollbar on a phone, since a finger has no
            // wheel and each row took the press as a tap. The rows are only
            // ever tapped, never dragged, so nothing loses a gesture to it:
            // a tap reaches the row, a drag past the threshold scrolls.
            flick := Flickable {
                content-y <=> root.content-y;
                content-width: self.width;
                content-height: root.content-h;
                interactive: root.overflow > 0px;

                VerticalLayout {
                    width: flick.width;
                    height: root.content-h;
                    padding: 4px;
                    alignment: start;

                for option[index] in root.options: Rectangle {
                    height: root.row-h;
                    border-radius: Theme.r-sm;
                    background: index == root.active && !root.off(index)
                        ? Theme.accent : Colors.transparent;

                    HorizontalLayout {
                        padding-left: 6px;
                        padding-right: 6px;
                        spacing: 6px;

                        if index < root.icons.length: VerticalLayout {
                            alignment: center;
                            Icon {
                                glyph: root.icons[index];
                                size: 13px;
                                tint: index == root.active ? Theme.on-accent : Theme.fg-dim;
                            }
                        }

                        if index < root.avatars.length: VerticalLayout {
                            alignment: center;
                            Avatar {
                                data: root.avatars[index];
                                size: 18px;
                                opacity: root.off(index) ? 0.45 : 1.0;
                            }
                        }

                        Text {
                            horizontal-stretch: 1;
                            text: option;
                            color: root.off(index) ? Theme.fg-dim
                                : index == root.active ? Theme.on-accent : Theme.fg-muted;
                            font-size: Theme.fs;
                            vertical-alignment: center;
                            overflow: elide;
                        }

                        if index < root.details.length: Text {
                            text: root.details[index];
                            color: index == root.active && !root.off(index) ? Theme.on-accent : Theme.fg-dim;
                            font-family: Theme.font-technical;
                            font-size: Theme.fs-sm;
                            vertical-alignment: center;
                        }

                        if index == root.current: VerticalLayout {
                            alignment: center;
                            Icon {
                                glyph: Glyph.check;
                                size: 12px;
                                tint: index == root.active ? Theme.on-accent : Theme.accent;
                            }
                        }
                    }

                    TouchArea {
                        mouse-cursor: root.off(index) ? MouseCursor.default : MouseCursor.pointer;
                        // pointer-enter has no dedicated callback; has-hover
                        // drives the highlight instead.
                        changed has-hover => {
                            if (self.has-hover && !root.off(index)) {
                                root.active = index;
                            }
                        }
                        clicked => {
                            root.commit(index);
                        }
                    }
                }
                }
            }

            ThinScrollbar {
                x: parent.width - self.width - 1px;
                y: 1px;
                height: parent.height - 2px;
                viewport: root.list-h;
                content: root.content-h;
                offset: root.at;
                scrolled(to) => { root.content-y = -to; }
            }
        }
    }
}