Files
Unlucky-Launcher/crates/presenter/src/toast.rs
T
AskaEth f159529936 feat(presenter): 新建 unlucky-presenter,把 host/状态/DTO/设置桥从 app 迁出(M85 第 1 块)
D26(iced 重写)的关键一次性动作:UI 框架无关的宿主层独立成 crate,
Slint 版(crates/app)与 iced 版(crates/app-iced)共用,Slint 退役后不再重写一遍。

- 新建 crates/presenter(package `unlucky-presenter` / lib `unlucky_presenter`),
  workspace 登记 + crate 文档头写清「这层为什么存在」、对外表面、UI 侧允许/禁止、
  iced 接线骨架、以及哪些东西留在了 crates/app 及为什么。
- `git mv`(保留文件历史)迁入:host/(25 文件)、dto/(11 文件)、state.rs(+tests)、
  prefs.rs、settings_bridge.rs、toast.rs、router.rs、dialogs.rs、wizard.rs、
  mock.rs、mock_data.rs、backend.rs。模块路径与公开 API 名称不变(`host::install` 等),
  内部实现零改动。
- 新增 `theme`:把原本混在 ui_bind/theme_runtime 里的**纯判据**(CONFIG_POLL_TICKS /
  SYSTEM_THEME_POLL_TICKS / TILE_REGENERATE_STEP / BackgroundKey / tile_viewport /
  system_theme_value / 两个 poll 判据)抽出来,附 5 个单测;app 侧改成调用它们。
- crates/app 改为消费者:删掉迁走的文件,`crate::host|state|dto|prefs|settings_bridge|
  toast|router|dialogs|wizard|mock|mock_data|backend` 全部改写为 `unlucky_presenter::*`
  (64 处),backend.rs 的 config_path/build_backend 提为 pub。
- crates/app-iced 只登记 unlucky-presenter 依赖,本 mission 不接线。
- prefs.rs 的 2 处 slint 文档引用改写;`grep -rn slint crates/presenter` 现在无输出。
2026-11-02 09:29:31 +08:00

155 lines
4.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Toast 中心:右下角非阻塞提示(下载完成 / 复制成功 / 后台任务结论)。
//!
//! 与模态对话框队列([`crate::dialogs`])分开:Toast 不阻塞、不排序、可叠加,
//! 只做「先进先出 + 上限裁剪 + TTL 到期」。UI 侧由 Rust 定时器每帧调用 [`ToastCenter::expire`]。
use serde::{Deserialize, Serialize};
/// Toast 标识。
pub type ToastId = u64;
/// Toast 语义色。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ToastKind {
/// 一般信息
Info,
/// 成功
Success,
/// 警告
Warn,
/// 错误
Error,
}
/// 一条 Toast;`remaining_ms` 由 [`ToastCenter::expire`] 递减。
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Toast {
/// 队列内唯一 id(由 [`ToastCenter::push_request`] 重新编号)
pub id: ToastId,
/// 语义色
pub kind: ToastKind,
/// 提示文案
pub text: String,
/// 剩余存活毫秒(0 = 用默认 TTL,入队时补)
pub remaining_ms: u64,
}
/// Toast 中心:先进先出,超出上限丢最旧的。
#[derive(Debug)]
pub struct ToastCenter {
items: Vec<Toast>,
next_id: ToastId,
ttl_ms: u64,
}
impl Default for ToastCenter {
fn default() -> Self {
Self::new()
}
}
impl ToastCenter {
/// 默认 4s 后自动消失。
pub fn new() -> Self {
Self {
items: Vec::new(),
next_id: 0,
ttl_ms: 4_000,
}
}
/// 同时最多显示的条数。
pub const MAX_VISIBLE: usize = 4;
/// 入队一条 Toast。
pub fn push(&mut self, kind: ToastKind, text: impl Into<String>) -> ToastId {
self.push_request(Toast {
id: 0,
kind,
text: text.into(),
remaining_ms: 0,
})
}
/// 入队一条已组装好的 Toast(事件通道用);id 重新编号,`remaining_ms == 0` 视为用默认 TTL。
pub fn push_request(&mut self, mut toast: Toast) -> ToastId {
self.next_id += 1;
toast.id = self.next_id;
if toast.remaining_ms == 0 {
toast.remaining_ms = self.ttl_ms;
}
let id = toast.id;
self.items.push(toast);
while self.items.len() > Self::MAX_VISIBLE {
self.items.remove(0);
}
id
}
/// 当前显示列表(UI 模型来源)。
pub fn items(&self) -> &[Toast] {
&self.items
}
/// 手动关闭(点了关闭按钮)。
pub fn dismiss(&mut self, id: ToastId) -> bool {
if let Some(at) = self.items.iter().position(|toast| toast.id == id) {
self.items.remove(at);
true
} else {
false
}
}
/// 时间推进:移除到期 Toast。
pub fn expire(&mut self, elapsed_ms: u64) {
for toast in &mut self.items {
toast.remaining_ms = toast.remaining_ms.saturating_sub(elapsed_ms);
}
self.items.retain(|toast| toast.remaining_ms > 0);
}
/// 当前条数。
pub fn len(&self) -> usize {
self.items.len()
}
/// 是否没有 Toast。
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::{ToastCenter, ToastKind};
#[test]
fn toasts_expire_and_cap_the_stack() {
let mut center = ToastCenter::new();
center.push(ToastKind::Info, "a");
center.push(ToastKind::Info, "b");
assert_eq!(center.len(), 2);
for i in 0..10 {
center.push(ToastKind::Success, format!("t{i}"));
}
assert_eq!(center.len(), ToastCenter::MAX_VISIBLE);
center.expire(3_000);
assert_eq!(center.len(), ToastCenter::MAX_VISIBLE);
center.expire(1_001);
assert!(center.is_empty());
}
#[test]
fn dismiss_removes_one_toast() {
let mut center = ToastCenter::new();
let first = center.push(ToastKind::Info, "a");
center.push(ToastKind::Warn, "b");
assert!(center.dismiss(first));
assert!(!center.dismiss(first));
assert_eq!(center.len(), 1);
}
}