Slint Kit Documentation

Navigation

Centre de notifications pour Slint

KitNotificationCenter · Slint Kit 0.3.1

Liste de notifications avec aperçu, lecture et retrait par identité stable.

Liste de notifications avec aperçu, lecture et retrait par identité stable.
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 un aperçu, marquez un avis comme lu ou retirez-le. Rust possède la clé stable et l’état de lecture.

ui/app.slint

export { KitFormValidation, HtmlInputKind, NumberInputFormat, KitInputConstraints, KitValidationResult, KitValidityState } from "@kit/kit.slint";
// SPDX-License-Identifier: GPL-3.0-or-later
import { KitNotificationCenter, NotificationRow, KitTheme, KitThemes } from "@kit/kit.slint";
export component App inherits Window {
    title: "KitNotificationCenter example";
    preferred-width: 520px;
    preferred-height: 420px;
    max-width: 100000px;
    max-height: 100000px;
    background: root.theme.canvas;
    in-out property <KitTheme> theme: KitThemes.light;
    in property <[NotificationRow]> notices;
    in property <string> result: "Choose a notification";
    callback open-notice(int, int);
    callback mark-read(int, int);
    callback dismiss-notice(int, int);
    VerticalLayout {
        padding: 24px;
        spacing: 16px;
        alignment: start;
        control := KitNotificationCenter {
            theme: root.theme; rows: root.notices; height: 320px;
            activate(token, generation) => { root.open-notice(token, generation); }
            mark-read(token, generation) => { root.mark-read(token, generation); }
            dismiss-row(token, generation) => { root.dismiss-notice(token, generation); }
        }
        result := Text { text: root.result; color: root.theme.text-primary; }
    }

}

src/main.rs

// SPDX-License-Identifier: GPL-3.0-or-later
use slint::{ComponentHandle, ModelRc, VecModel};
use std::{cell::RefCell, rc::Rc};
mod ui { slint::include_modules!(); }
#[derive(Clone, Copy, PartialEq, Eq)]
enum Notice { SharedDocument, ExportReady }
impl Notice {
    fn token(self) -> i32 { match self { Self::SharedDocument => 11, Self::ExportReady => 23 } }
    fn label(self) -> &'static str { match self { Self::SharedDocument => "Document shared", Self::ExportReady => "Export ready" } }
}
fn publish(app: &ui::App, notices: &[(Notice, bool)]) {
    app.set_notices(ModelRc::new(VecModel::from(notices.iter().map(|(key, read)| ui::NotificationRow {
        item: ui::OptionRow { token: key.token(), generation: 1, label: key.label().into(), description: "Review the latest project update.".into(), disabled: false }, read: *read,
    }).collect::<Vec<_>>())));
}
fn main() -> Result<(), slint::PlatformError> {
    let app = ui::App::new()?;
    slint_kit::init_form_validation!(ui, app);
    let notices = Rc::new(RefCell::new(vec![(Notice::SharedDocument, false), (Notice::ExportReady, false)]));
    publish(&app, &notices.borrow());
    let weak = app.as_weak(); let state = notices.clone();
    app.on_open_notice(move |token, generation| {
        if generation != 1 { return; }
        let label = state.borrow().iter().find(|(key, _)| key.token() == token).map(|(key, _)| key.label());
        if let (Some(app), Some(label)) = (weak.upgrade(), label) { app.set_result(format!("Preview: {label}").into()); }
    });
    let weak = app.as_weak(); let state = notices.clone();
    app.on_mark_read(move |token, generation| { if generation != 1 { return; } if let Some(app) = weak.upgrade() {
        if let Some((_, read)) = state.borrow_mut().iter_mut().find(|(key, _)| key.token() == token) { *read = true; }
        let snapshot = state.borrow().clone(); publish(&app, &snapshot); app.set_result("Notification marked read".into());
    } });
    let weak = app.as_weak();
    app.on_dismiss_notice(move |token, generation| { if generation != 1 { return; } if let Some(app) = weak.upgrade() {
        notices.borrow_mut().retain(|(key, _)| key.token() != token);
        let snapshot = notices.borrow().clone(); publish(&app, &snapshot); app.set_result("Notification dismissed".into());
    } });
    app.run()
}

Cargo.toml et build.rs

Cargo.toml

[package]
name = "slint-kit-notification-center-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 des NotificationRow et acceptez activate, mark-read et dismiss-row par token et génération. Gardez le placement de la popup et les libellés mark-read-label et read-label stables pendant l’ouverture.

Le retrait bloque les actions puis émet la demande après un fondu de 150 ms, supprimé en mouvement réduit ; changer d’identité annule le retrait en attente. Les grandes collections utilisent une liste virtualisée et focus-token avec focus-request permet de retrouver un aperçu.

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
Liste de notifications avec aperçu, lecture et retrait par identité stable.
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 { KitNotificationCenter, KitTheme, KitThemes } from "@kit/kit.slint";

export component Example inherits Window {
    title: "KitNotificationCenter — exemple";
    preferred-width: 620px;
    preferred-height: 440px;
    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: "Centre de notifications"; color: root.theme.text-primary; font-size: 20px * root.theme.text-scale; font-weight: 600; }
        KitNotificationCenter { dismiss-label: "Fermer"; empty-description: "Vous êtes à jour."; empty-label: "Aucune notification"; mark-read-label: "Marquer comme lu"; open-label: "Ouvrir l’aperçu"; read-label: "Lu"; theme: root.theme; height: 300px; rows: [{ item: { token: 11, generation: 1, label: "Jamie a partagé un document", description: "Les notes de recherche sont prêtes pour révision.", disabled: false }, read: false }, { item: { token: 23, generation: 1, label: "Votre exportation est prête", description: "Téléchargez la dernière archive du projet.", disabled: false }, read: true }]; }
        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
rows [NotificationRow] in
focus-token int in
focus-request int in
empty-label string in
empty-description string in
open-label string in
mark-read-label string in
read-label string in
dismiss-label string in
disabled bool in
content-height length out

Types et variantes

Type Déclaration
enum ControlSize compact, medium, comfortable
struct NotificationRow item: OptionRow, read: bool
struct OptionRow token: int, generation: int, label: string, description: string, disabled: bool

Signaux

Voir aussi