Slint Kit Documentation

Choix et valeurs

Évaluation pour Slint

KitRating · Slint Kit 0.3.0

Choix d’une note avec action explicite d’effacement.

Choix d’une note avec action explicite d’effacement.
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.

Choisissez 2 stars puis Clear : Rust conserve Score::Two ou une évaluation explicitement absente.

ui/app.slint

export { KitFormValidation, HtmlInputKind, NumberInputFormat, KitInputConstraints, KitValidationResult, KitValidityState } from "@kit/kit.slint";
// SPDX-License-Identifier: GPL-3.0-or-later
import { KitRating, OptionRow, SelectState, NavigationStep, CloseReason, SelectionAvailability, KitTheme, KitThemes } from "@kit/kit.slint";
export component App inherits Window {
    title: "KitRating example";
    preferred-width: 520px;
    preferred-height: 240px;
    max-width: 100000px;
    max-height: 100000px;
    background: root.theme.canvas;
    in-out property <KitTheme> theme: KitThemes.light;
    in property <[OptionRow]> rows;
    in property <SelectState> select-state;
    in property <string> selected-label: "1 star";
    in-out property <string> query;
    callback select-open();
    callback select-navigate(NavigationStep);
    callback select-activate(int, int);
    callback select-dismiss(CloseReason);
    callback select-query(string);
    callback select-typeahead(string);
    callback clear();
    VerticalLayout {
        padding: 24px;
        spacing: 16px;
        alignment: start;
        control := KitRating {
            theme: root.theme; label: "Your rating";
            scores: [{ token: root.rows[0].token, label: root.rows[0].label, disabled: false, selected: root.select-state.selected.present && root.select-state.selected.token == root.rows[0].token }, { token: root.rows[1].token, label: root.rows[1].label, disabled: false, selected: root.select-state.selected.present && root.select-state.selected.token == root.rows[1].token }, { token: root.rows[2].token, label: root.rows[2].label, disabled: false, selected: root.select-state.selected.present && root.select-state.selected.token == root.rows[2].token }];
            generation: root.select-state.generation; active-token: root.select-state.active.token;
            navigate(step) => { root.select-navigate(step); }
            choose(token, generation) => { root.select-activate(token, generation); }
            clear-requested => { root.clear(); }
        }
        result := Text { text: "Rating: " + root.selected-label; color: root.theme.text-primary; }
    }

}

src/main.rs

// SPDX-License-Identifier: GPL-3.0-or-later
use slint::ComponentHandle;
use slint_kit::{Ownership, SelectOption, SelectionIntent};
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum Score { One, Two, Three }
impl Score {
    fn label(self) -> &'static str {
        match self { Self::One => "1 star", Self::Two => "2 stars", Self::Three => "3 stars" }
    }
}

mod ui { slint::include_modules!(); }
fn main() -> Result<(), slint::PlatformError> {
    let app = ui::App::new()?;
    slint_kit::init_form_validation!(ui, app);
    let selection = slint_kit::bind_select!(ui, app, vec![
        SelectOption::new(Score::One, "1 star"),
        SelectOption::new(Score::Two, "2 stars"),
        SelectOption::new(Score::Three, "3 stars"),
    ], Some(Score::One), Ownership::Local);
    let weak = app.as_weak();
    selection.on_intent(move |intent| {
        if let Some(app) = weak.upgrade() {
            app.set_selected_label(match intent {
                SelectionIntent::Commit(key) => key.label(),
                SelectionIntent::Clear => "No selection",
            }.into());
        }
    });
    selection.open();
    let target = selection.clone();
    let weak = app.as_weak();
    app.on_clear(move || { target.set_selected(None); target.open(); if let Some(app) = weak.upgrade() { app.set_selected_label("No rating".into()); } });
    let persistent = selection.clone();
    app.on_select_activate(move |token, generation| {
        persistent.activate(token, generation);
        persistent.open();
    });
    app.run()
}

Cargo.toml et build.rs

Cargo.toml

[package]
name = "slint-kit-rating-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

Fournissez scores: [ChoiceRow], generation et active-token depuis le contrôleur de notation. Traitez choose(token, generation) et clear-requested séparément.

La présentation compose KitSegmentedControl et une action d’effacement. L’absence de note reste une décision explicite du domaine, distincte de la première note.

Choisir entre une option intégrée et une composition personnalisée

Options partagées

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
Choix d’une note avec action explicite d’effacement.
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 { KitRating, KitTheme, KitThemes } from "@kit/kit.slint";

export component Example inherits Window {
    title: "KitRating — exemple";
    preferred-width: 560px;
    preferred-height: 170px;
    max-width: 100000px;
    max-height: 100000px;
    background: root.theme.canvas;
    in-out property <KitTheme> theme: KitThemes.light;
    in-out property <string> status;
    VerticalLayout {
        padding: 24px; spacing: 16px; alignment: start;
        Text { text: "Notation"; color: root.theme.text-primary; font-size: 20px * root.theme.text-scale; font-weight: 600; }
        KitRating { theme: root.theme; label: "Votre note"; generation: 1; active-token: 4; scores: [{ token: 1, label: "1 étoile", selected: false, disabled: false }, { token: 2, label: "2 étoiles", selected: false, disabled: false }, { token: 3, label: "3 étoiles", selected: false, disabled: false }, { token: 4, label: "4 étoiles", selected: true, disabled: false }, { token: 5, label: "5 étoiles", selected: false, disabled: false }]; }
        if root.status != "": Text { text: root.status; color: root.theme.text-secondary; font-size: 12px * root.theme.text-scale; wrap: word-wrap; }
    }
}

Télécharger cet exemple

API publique

Hérite de Rectangle.

Propriétés

Nom Type Sens
theme KitTheme in
control-size ControlSize in
access FieldAccess in
scores [ChoiceRow] in
generation int in
active-token int in
label string in

Types et variantes

Type Déclaration
struct ChoiceRow token: int, label: string, disabled: bool, selected: bool
enum ControlSize compact, medium, comfortable
enum FieldAccess editable, read-only, disabled
enum NavigationStep next, previous, first, last, page-next, page-previous

Signaux

Voir aussi