Actively developed · Breaking changes are still expected.

Slint Kit Documentation

Search the documentation

    ↑ ↓ to browse · Enter to open · Escape to close

    Navigation

    Context Menu for Slint

    KitContextMenu · Slint Kit 0.9.0

    Content area with a menu of actions opened at the pointer by the right button or the menu key.

    The still preview and demo follow the documentation theme. You can change it before or during the demo.

    Content area with a menu of actions opened at the pointer by the right button or the menu key.
    Slint components with their built-in Tailwind appearance, rendered in the browser.

    This preview runs the actual Slint components and their Rust code compiled to WebAssembly. Slint draws the interface in the browser; the downloadable project compiles the same example into a native application for your platform.

    Try the component

    Download the complete project, extract it and run cargo run --locked in its folder. To add this component to an application, follow Project setup, then copy the two files below.

    The kit supplies the component's behavior and styles; Rust callbacks connect its typed values to your application.

    Right-click the target, or focus it and use the menu key or Shift+F10. Choose an action to update the status.

    ui/app.slint

    import { KitThemeContext } from "@kit";
    import { InterFont } from "@kit-fonts/inter.slint";
    // SPDX-License-Identifier: GPL-3.0-or-later
    import { KitContextMenu, KitTheme, KitThemes } from "@kit";
    
    export component App inherits Window {
        default-font-family: InterFont.family;
        title: "KitContextMenu example";
        preferred-width: 480px;
        preferred-height: 320px;
        max-width: 100000px;
        max-height: 100000px;
        background: root.theme.canvas;
        in-out property <KitTheme> theme: KitThemeContext.current;
        in-out property <string> status: "No action yet";
        VerticalLayout {
            padding: 24px;
            spacing: 12px;
            alignment: start;
            KitContextMenu {
                theme: root.theme;
                options: [{ token: 11, generation: 1, label: "Rename", description: "Change the project name", disabled: false }, { token: 23, generation: 1, label: "Duplicate", description: "Copy the project", disabled: false }, { token: 38, generation: 1, label: "Archive", description: "Move out of the list", disabled: false }];
                chosen(row) => { root.status = "Chosen: " + row.label; }
                Rectangle {
                    height: 160px;
                    background: root.theme.surface-raised;
                    border-radius: root.theme.shape.radius-container;
                    Text { text: "Right-click here, or press the menu key"; color: root.theme.text-secondary; font-size: 14px * root.theme.text-scale; }
                }
            }
            Text { text: root.status; color: root.theme.text-secondary; font-size: 12px * root.theme.text-scale; }
        }
    }
    
    

    src/main.rs

    // SPDX-License-Identifier: GPL-3.0-or-later
    slint::include_modules!();
    fn main() -> Result<(), slint::PlatformError> {
        let app = App::new()?;
        slint_kit::install(&app);
        app.run()
    }
    
    

    Files: Cargo.toml · build.rs · ui/app.slint · src/main.rs

    Place the content as children, pass options from Choices<K>::model() and handle chosen(OptionRow); Choices::accept validates the callback and returns its domain key. Call slint_kit::install once per window. show-at(x, y) opens the menu at a point of the content from code, show() where the menu key does and dismiss(reason) closes it.

    The right button opens the menu at the pointer point; the menu key or Shift+F10 opens it at keyboard-anchor-x and keyboard-anchor-y from the content, and also from a focusable child that does not use the key itself. The content takes one tab stop. A control inside the content that handles the pointer itself, such as a button or a text field, keeps its own right-button behavior: use show-at from its callback, or open the menu from the surrounding area. The menu reuses the panel, rows, placement and dismissal of KitDropdownMenu, anchored at a point instead of a trigger: it sits below the point, or above it when there is more room, and shifts to stay inside the window. Opening focuses the rows; Enter or a click chooses one, and the menu then closes, emits chosen and returns focus to the content area. Escape and dismiss return focus too; an outside press closes the menu as host-closed without moving focus. dismissed reports escape, programmatic (a choice or dismiss) or host-closed. A disabled content area or an empty options model does not open. Slint's own ContextMenuArea is not used: it draws a native or default-styled menu, ignores the kit theme and rows and reports no dismissal reason.

    Control accessibility

    The content area is a plain container: give interactive content its own accessible names. Rows use the list and list-item roles of KitMenu, because this SDK has no menu roles; Slint 1.18.1 cannot move assistive focus to the active row. Headless tests do not qualify VoiceOver, NVDA or physical IME.

    Visual composition

    The file below describes the captured scene. For controller-driven components, it shows the displayed state without supplying the Rust wiring. Create a project.

    Show the screenshot source
    Content area with a menu of actions opened at the pointer by the right button or the menu key.
    Browser capture of the same Slint example, femtovg renderer.
    import { InterFont } from "@kit-fonts/inter.slint";
    // SPDX-License-Identifier: GPL-3.0-or-later
    import { KitContextMenu, KitTheme, KitThemes } from "@kit";
    
    export component Example inherits Window {
        default-font-family: InterFont.family;
        title: "KitContextMenu example";
        preferred-width: 480px;
        preferred-height: 320px;
        max-width: 100000px;
        max-height: 100000px;
        background: root.theme.canvas;
        in-out property <KitTheme> theme: KitThemes.light;
        in-out property <string> status: "No action yet";
        VerticalLayout {
            padding: 24px;
            spacing: 12px;
            alignment: start;
            KitContextMenu {
                theme: root.theme;
                options: [{ token: 11, generation: 1, label: "Rename", description: "Change the project name", disabled: false }, { token: 23, generation: 1, label: "Duplicate", description: "Copy the project", disabled: false }, { token: 38, generation: 1, label: "Archive", description: "Move out of the list", disabled: false }];
                chosen(row) => { root.status = "Chosen: " + row.label; }
                Rectangle {
                    height: 160px;
                    background: root.theme.surface-raised;
                    border-radius: root.theme.shape.radius-container;
                    Text { text: "Right-click here, or press the menu key"; color: root.theme.text-secondary; font-size: 14px * root.theme.text-scale; }
                }
            }
            Text { text: root.status; color: root.theme.text-secondary; font-size: 12px * root.theme.text-scale; }
        }
    }
    
    

    Download this example

    Public API

    Inherits Rectangle.

    Properties

    The "Default" column repeats the initializer declared in the source; "—" is the zero value of the type, and a composite expression points to the source.

    Name Type Direction Default
    modal-layer int in —
    theme KitTheme in KitThemeContext.current
    control-size ControlSize in ControlSize.medium
    accessible-name string in "Context menu"
    disabled bool in —
    options [OptionRow] in —
    items [MenuItem] in —
    popup-width length in KitMetrics.select-popup-min-width(root.theme)
    keyboard-anchor-x length in root.theme.spacing.unit * 2
    keyboard-anchor-y length in root.theme.spacing.unit * 2
    open bool out —
    has-focus bool out —

    Property details

    Effect, default, states and constraints of the central properties, with a snippet that uses public mechanisms. Inherited properties carry the explanation of the component that declares them. The complete typed example and the Forms guide wire these properties end to end.

    modal-layer int · in

    The modal layer this component belongs to; 0 is the window. While a KitDialogSurface or KitDrawer with a positive modal-layer is open and topmost, the menu cannot open. Give a component placed inside a modal that modal's layer number.

    Default
    0 (window).
    KitContextMenu {
        modal-layer: 1;
    }

    options [OptionRow] · in

    The rows of the menu, from Choices<K>::model(). With no rows the menu does not open.

    options: root.actions;

    items [MenuItem] · in

    Presentation of the rows, one MenuItem per option in the same order, as in KitMenu.

    items: root.action-items;

    popup-width length · in

    Width of the menu, capped at the window width minus a gap on each side.

    Default
    The select popup minimum width.
    popup-width: 240px;

    keyboard-anchor-x length · in

    Horizontal offset, in the content's coordinates, where the menu key and show() open the menu.

    Default
    Two spacing units.
    keyboard-anchor-x: 16px;

    keyboard-anchor-y length · in

    Vertical offset, in the content's coordinates, where the menu key and show() open the menu.

    Default
    Two spacing units.
    keyboard-anchor-y: 16px;

    open bool · out

    True while the menu is presented. It turns false when the menu closes for any reason, including a native outside press.

    out property <bool> expanded: area.open;

    has-focus bool · out

    True while the content area itself owns focus. It is false while a focusable child or the open menu has focus.

    out property <bool> content-focused: area.has-focus;

    Types and variants

    Type Declaration
    enum CloseReason escape, outside-press, system-back, trigger-toggle, close-button, programmatic, host-closed
    enum ControlSize compact, medium, comfortable
    struct MenuItem kind: MenuItemKind, leading-icon: IconGlyph, shortcut: string, destructive: bool, checked: bool
    enum MenuItemKind action, checkbox, radio, separator, group
    struct OptionRow token: int, generation: int, label: string, description: string, disabled: bool

    Callbacks

    Functions

    See also