Slint Kit Documentation

Navigation

Panneau latéral pour Slint

KitDrawer · Slint Kit 0.3.0

Panneau modal latéral borné à la largeur de sa fenêtre hôte.

Panneau modal latéral borné à la largeur de sa fenêtre hôte.
Application native de l’exemple ci-dessous.

Essayer le composant

Télécharger le projet complet, le décompresser et lancer cargo run --locked dans son dossier. Rust 1.92+ ; Slint Kit 0.3.0 depuis crates.io. Vous pouvez aussi copier les quatre fichiers ci-dessous.

Ouvrez les détails, archivez le projet ou fermez avec Échap. La fermeture rétablit le focus sur le bouton d’ouverture.

ui/app.slint

export { KitFormValidation, HtmlInputKind, NumberInputFormat, KitInputConstraints, KitValidationResult, KitValidityState } from "@kit/kit.slint";
// SPDX-License-Identifier: GPL-3.0-or-later
import { KitDrawer, KitDialogPanel, KitButton, DialogTabPolicy, CloseReason, KitTheme, KitThemes } from "@kit/kit.slint";
export component App inherits Window {
    title: "KitDrawer example";
    preferred-width: 520px;
    preferred-height: 400px;
    max-width: 100000px;
    max-height: 100000px;
    background: root.theme.canvas;
    in-out property <KitTheme> theme: KitThemes.light;
    in property <bool> showing;
    in property <string> result: "Project active";
    callback open();
    callback close(CloseReason);
    callback archive();
    public function focus-opener() { opener.take-focus(); }
    public function focus-dialog() { drawer.take-focus(); }

    VerticalLayout {
        padding: 24px;
        spacing: 16px;
        alignment: start;
        opener := KitButton { theme: root.theme; text: "Open project details"; disabled: root.showing; activated => { root.open(); } }
        Text { text: root.result; color: root.theme.text-primary; }

    }
    drawer := KitDrawer {
        theme: root.theme; title: "Project details"; width: root.width; height: root.height;
        state: { open: root.showing, topmost: root.showing, busy: false };
        tab-policy: DialogTabPolicy.native;
        dismiss(reason) => { root.close(reason); }
        restore-focus => { root.focus-opener(); }
            Text { text: "Archive this project?"; color: root.theme.text-primary; font-size: 20px * root.theme.text-scale; wrap: word-wrap; }
            archive-button := KitButton { theme: root.theme; text: "Archive project"; activated => { root.archive(); } }
    }
}

src/main.rs

// SPDX-License-Identifier: GPL-3.0-or-later
use slint::ComponentHandle;
use slint_kit::{OverlayStack, OverlaySpec, OverlayId, DismissReason, CloseResult};
mod ui { slint::include_modules!(); }
fn main() -> Result<(), slint::PlatformError> {
    let app = ui::App::new()?;
    slint_kit::init_form_validation!(ui, app);
    let overlays = std::rc::Rc::new(std::cell::RefCell::new(OverlayStack::new()));
    let weak = app.as_weak();
    let stack = overlays.clone();
    app.on_open(move || {
        if let Some(app) = weak.upgrade() {
            if stack.borrow().is_empty() {
                stack.borrow_mut().push(OverlaySpec::new(OverlayId(1), None, true)).expect("single modal");
                app.set_showing(true);
                app.invoke_focus_dialog();
            }
        }
    });
    let weak = app.as_weak();
    let stack = overlays.clone();
    app.on_close(move |reason| {
        let reason = match reason {
            ui::CloseReason::Escape => DismissReason::Escape,
            ui::CloseReason::OutsidePress => DismissReason::OutsidePress,
            ui::CloseReason::SystemBack => DismissReason::SystemBack,
            ui::CloseReason::TriggerToggle => DismissReason::TriggerToggle,
            ui::CloseReason::Programmatic => DismissReason::Programmatic,
            ui::CloseReason::HostClosed => DismissReason::HostClosed,
        };
        let closed = matches!(stack.borrow_mut().request_close(reason), CloseResult::Closed(_));
        if closed { if let Some(app) = weak.upgrade() { app.set_showing(false); app.invoke_focus_opener(); } }
    });
    let weak = app.as_weak();
    app.on_archive(move || { if let Some(app) = weak.upgrade() { app.set_result("Project archived".into()); app.invoke_close(ui::CloseReason::Programmatic); } });
    app.run()
}

Cargo.toml et build.rs

Cargo.toml

[package]
name = "slint-kit-drawer-example"
version = "0.1.0"
edition = "2024"
rust-version = "1.92"
license = "GPL-3.0-or-later"
publish = false

[dependencies]
slint-kit = "=0.3.0"
slint = { version = "=1.18.1", default-features = false, features = ["std", "compat-1-18", "backend-winit", "renderer-skia", "accessibility"] }

[build-dependencies]
slint-kit-build = "=0.3.0"

[workspace]

build.rs

fn main() { slint_kit_build::compile("ui/app.slint").expect("compile component example"); }

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

Montez-le à la racine de la fenêtre après le contenu défilant et donnez-lui tout le viewport. Connectez les callbacks hérités dismiss et focus-next, puis restore-focus, à la pile de modales et au registre de focus.

Le panneau est ancré au bord final et borne panel-width à la largeur disponible. L’ouverture place le focus sur Fermer ; l’hôte doit enregistrer les champs et restaurer l’ouvreur exact à la fermeture.

Composition visuelle

Le fichier ci-dessous produit la capture. Pour les composants pilotés par un contrôleur, il montre l’état affiché sans fournir le branchement Rust. Créer un projet.

Afficher le fichier de la capture
Panneau modal latéral borné à la largeur de sa fenêtre hôte.
Capture native du même exemple, moteur Skia.
// Public label overrides localize this fixture; toolkit/host defaults keep their language.
// SPDX-License-Identifier: GPL-3.0-or-later
import { KitButton, KitDrawer, KitTextField, KitTheme, KitThemes } from "@kit/kit.slint";

export component Example inherits Window {
    title: "KitDrawer — exemple";
    preferred-width: 560px;
    preferred-height: 420px;
    max-width: 100000px;
    max-height: 100000px;
    background: root.theme.canvas;
    in-out property <KitTheme> theme: KitThemes.light;
    in-out property <string> status;
    KitDrawer { close-label: "Fermer le panneau"; theme: root.theme; panel-width: 380px * root.theme.text-scale; width: parent.width; height: parent.height; title: "Détails du projet"; state: { open: true, topmost: true, busy: false }; Text { text: "Notes de terrain"; color: root.theme.text-primary; font-size: 14px * root.theme.text-scale; wrap: word-wrap; vertical-stretch: 0; }KitTextField { character-count-label: "caractères"; checking-label: "Vérification…"; clear-label: "Effacer la recherche"; hide-label: "Masquer le mot de passe"; reveal-label: "Afficher le mot de passe"; theme: root.theme; label: "Nom du projet"; text: "Notes de terrain"; }KitButton { theme: root.theme; text: "Enregistrer les modifications"; activated => { root.status = "Action demandée"; } } if root.status != "": Text { text: root.status; color: root.theme.text-secondary; font-size: 12px * root.theme.text-scale; wrap: word-wrap; vertical-stretch: 0; } }
}

Télécharger cet exemple

API publique

Hérite de KitDialogSurface.

Propriétés

Nom Type Sens
control-size ControlSize in
title string in
close-label string in
panel-width length in
Propriétés héritées disponibles
Nom Type Définie sur
theme KitTheme KitDialogSurface
state DialogState KitDialogSurface
dismiss-on-backdrop bool KitDialogSurface
tab-policy DialogTabPolicy KitDialogSurface

Types et variantes

Type Déclaration
enum ControlSize compact, medium, comfortable

Signaux

Fonctions

Voir aussi