feat(app-iced): M90 —— app.rs 拆成 src/app/ + presenter 真接线 + 实例页端到端

拆分(行为不变,截图四件套逐字节相同):
- 400 行的 app.rs 拆成 src/app/{mod,state,message,update,subscription,view,boot,tests}.rs,
  每个文件 ≤ 300 行;components 的 #[path] 跟着搬到 src/app/mod.rs(逻辑路径仍是 crate::app::components),
  可见性放到 pub(crate) 让页面层能用真组件。
- 拆分前后各出 shell/gallery 浅深四张图,cmp 全部 IDENTICAL。

presenter 接线(crate 文档 §4):
- App 持有 Box<dyn Backend> + SettingsBridge + AppState;启动 build_backend + SettingsBridge::open + AppState::new。
- 截图通道强制 mock(D30 可复现);交互模式走真 host,--data-dir 从 M90 起真的被消费。
- Boot 抽干首屏快照(第 3 帧落盘,等节拍会把空页面拍进图);节拍 = 帧事件 + 33ms 时间闸门
  (iced::time::every 要 tokio/smol,见 finding),tick → drain_events → AppState::apply。
- 用户动作不乐观回显:Launch/Stop 走 Backend::dispatch,状态等事件回来才变。
- 路由:pages::Message::Navigate(Route) 直接进 AppState::navigate,选中项 = 路由栈顶(不再有本地副本);
  首屏自动选中第一行(list-detail 桌面惯例),只做一次。

实例页打通(其余三页保留占位视觉 + Message/update + 路由):
- 列表行换成 components::navigation::ListItem(真五态/选中),页头与详情动作换成 components::buttons
  (Play 是唯一的实心 accent),分隔线换成 components::divider。
- 数据全部来自 presenter 快照:VERSION/LOADER/ISOLATION/VERSION ID/FILES/LAST PLAYED + 运行状态行。
- 导航轨换成真 NavigationRail(88),5 项各自压/替换路由。

版面约束未破(实测):列表列 336、正文列 424(元信息卡 206+12+206,右缘 896)、rail 88(药丸 16..71)、
断点 600/840 走 frame_metrics;新增 pages/tests.rs 的 9 条数值断言钉住它们。
This commit is contained in:
AskaEth committed 2026-11-02 15:56:57 +08:00
1 parent 270026338f
commit b484998fc9
17 files changed
+1990 -676

No files matched your search

+191 -161
View File
@@ -1,4 +1,4 @@
//! 实例页:**list-detail** 的唯一用例。
//! 实例页:**list-detail** 的唯一用例,也是 M90 唯一打通到「真组件 + 真数据 + 真命令」的页。
//!
//! ## 版式(由 [`super::layout`] 决议,页面不自己算宽度)
//!
@@ -10,57 +10,82 @@
//! 为什么窄窗不是「上下堆」:官方 compact 下 list-detail 的口径是一次只显示一个 pane(列表 → 详情,
//! 整页切换)。上下堆会把详情挤到折叠线以下,等于详情不存在。
//!
//! ## 判据
//! ## M90:占位控件 → 真组件(逐项对照)
//!
//! 列表行 `Component::ListItem`(BodyLarge / BodySmall,圆角 `Small`),行高 = `row_height + Space::X3`
//! = 56(两行行文;`row_height` 44 是单行列表的节奏)。选中行 `SecondaryContainer` + full 圆角(M3 列表)。
//! | 位置 | M84(占位) | M90(真组件) |
//! |---|---|---|
//! | 列表行 | 手搓 `container` + `text_role`(行高 56) | `navigation::list_item`(一/两/三行 44/60/76,五态状态层) |
//! | 页头动作 | `frame::text_action`(纯 `text` + 手搓 hover) | `buttons::text`(`Button` 五式 + 状态层) |
//! | 「Play」 | `frame::text_action` | `buttons::filled`(**全屏唯一实心 accent**,rubric K2) |
//! | 分隔线 | `frame::divider` | `divider::full` |
//! | 数据 | `DEMO` 常量 | [`super::model::Instances`](真页面由 presenter 快照转,演示由 `DEMO` 转) |
//!
//! 判据没有因此放松:列宽仍由 [`super::layout`] 给(见 `pages/tests.rs` 的 424 断言)。
//!
//! ## 行高为什么从 56 变成 60(M90 登记)
//!
//! M84 的行高是「`row_height` 44 + `Space::X3` 12 = 56」的手工值;真 `ListItem` 的两行档是
//! **60**(令牌 `row_height` 44 + 每多一行 16,A1 是 72)。差异来自组件自己的几何真源,
//! 页面不许覆盖——这正是「接真组件」的应有结果。列表仍高于可视区,独立滚动条照样可见。
mod data;
mod demo;
#[cfg(test)]
mod tests;
pub(super) use data::SELECTED;
use data::{Entry, DEMO, NOTE};
#[cfg(test)]
pub(crate) use data::{DEMO, NOTE, SELECTED};
pub(super) use demo::demo;
use iced::widget::{column, container, row, scrollable};
use iced::{Alignment, Background, Border, Element, Length, Padding};
use unlucky_design::color::Role;
use unlucky_design::component_map::Component;
use unlucky_design::elevation::MaterialLayer;
use unlucky_design::icons::IconKind;
use unlucky_design::shape::ShapeScale;
use unlucky_design::spacing::Space;
use unlucky_design::typography::TypeRole;
use unlucky_presenter::router::Route;
use crate::app::components::buttons::{self, Button};
use crate::app::components::divider;
use crate::app::components::navigation::list_item::{list_item, ListItem};
use crate::theme::Ui;
use super::frame::{self, Header};
use super::layout::{self, FrameMetrics, PageLayout};
use super::model::{Detail, Instances, Row};
use super::{Action, Message};
/// 整页(`selected = None` 时详情区显示空态)。
pub fn view<'a>(
ui: &'a Ui,
metrics: FrameMetrics,
selected: Option<usize>,
) -> Element<'a, Message> {
/// 列表行的间距(`Space::X1`;与 M84 同值,行高差异全部来自 `ListItem` 自己的几何)。
const ROW_GAP: Space = Space::X1;
/// 整页(`block.detail = None` 时详情区显示空态)。
pub fn view<'a>(ui: &'a Ui, metrics: FrameMetrics, block: &'a Instances) -> Element<'a, Message> {
let header = Header::new("Instances")
.subtitle("Local versions and their mod loaders")
.action(frame::text_action(ui, "New instance", Action::NewInstance))
.action(frame::text_action(ui, "Import", Action::Import));
.action(buttons::text(
ui,
Button::new("New instance", Message::Action(Action::NewInstance)),
))
.action(buttons::text(
ui,
Button::new("Import", Message::Action(Action::Import)),
));
let body = match metrics.layout {
PageLayout::ListDetail => {
let detail = match selected {
Some(_) => detail(ui, metrics),
PageLayout::ListDetail => frame::panes(
metrics,
list_column(ui, block),
match &block.detail {
Some(detail) => detail_column(ui, metrics, detail),
None => detail_empty(ui),
};
frame::panes(metrics, list_column(ui, selected), detail)
}
_ => match selected {
Some(_) => narrow_detail(ui, metrics),
},
),
_ => match &block.detail {
Some(detail) => narrow_detail(ui, metrics, detail),
None => frame::single_column(metrics, detail_empty(ui)),
},
};
@@ -70,7 +95,7 @@ pub fn view<'a>(
// --------------------------------------------------------------- 列表列
fn list_column<'a>(ui: &'a Ui, selected: Option<usize>) -> Element<'a, Message> {
fn list_column<'a>(ui: &'a Ui, block: &'a Instances) -> Element<'a, Message> {
let head = container(
row![
container(frame::text_role(
@@ -82,11 +107,11 @@ fn list_column<'a>(ui: &'a Ui, selected: Option<usize>) -> Element<'a, Message>
))
.width(Length::Fill)
.align_y(Alignment::Center),
frame::text_role(
frame::text_role_owned(
ui,
TypeRole::LabelMedium,
false,
"12",
block.rows.len().to_string(),
ui.color(Role::OnSurfaceVariant),
),
]
@@ -102,12 +127,13 @@ fn list_column<'a>(ui: &'a Ui, selected: Option<usize>) -> Element<'a, Message>
});
let rows = column(
DEMO.iter()
.enumerate()
.map(|(index, entry)| list_row(ui, entry, selected == Some(index)))
block
.rows
.iter()
.map(|entry| list_row(ui, entry, is_selected(block, entry)))
.collect::<Vec<_>>(),
)
.spacing(ui.space(Space::X1))
.spacing(ui.space(ROW_GAP))
.padding(Padding::from([0.0, ui.space(Space::X2)]))
.width(Length::Fill);
@@ -136,182 +162,190 @@ fn list_column<'a>(ui: &'a Ui, selected: Option<usize>) -> Element<'a, Message>
.into()
}
fn list_row<'a>(ui: &'a Ui, entry: &'a Entry, selected: bool) -> Element<'a, Message> {
let spec = Component::ListItem.spec();
let height = ui.metrics().row_height + Space::X3.dp();
fn is_selected(block: &Instances, entry: &Row) -> bool {
block
.detail
.as_ref()
.is_some_and(|detail| detail.id == entry.id)
}
let name_color = if selected {
ui.color(Role::OnSecondaryContainer)
} else {
ui.color(Role::OnSurface)
};
let support_color = if selected {
ui.color(Role::OnSecondaryContainer)
} else {
ui.color(Role::OnSurfaceVariant)
};
/// 一行(`ListItem` 的两/三行档:overline = 运行状态,headline = 名字,supporting = 版本 + 加载器)。
///
/// 点一行 = **压一层路由栈**(`Route::Detail(id)`),页面自己不留「选中项」副本——
/// 于是「当前选中」与「返回栈」永远是同一份真相(`presenter::router`)。
fn list_row<'a>(ui: &'a Ui, entry: &'a Row, selected: bool) -> Element<'a, Message> {
let mut item = ListItem::new(
entry.name.as_str(),
Message::Navigate(Route::Detail(entry.id.clone())),
)
.supporting(entry.supporting.as_str())
.leading(IconKind::Instances)
.trailing(IconKind::ChevronRight);
let texts = column![
frame::text_role(ui, spec.text, false, entry.name, name_color),
frame::text_role(
ui,
spec.support.unwrap_or(TypeRole::BodySmall),
false,
entry.loader,
support_color,
),
]
.spacing(0.0)
.width(Length::Fill);
if let Some(status) = &entry.status {
item = item.overline(status.as_str());
}
if selected {
item = item.selected();
}
let inner = row![
frame::glyph_box(IconKind::Instances, support_color, frame::ICON_PX),
texts,
frame::glyph(IconKind::ChevronRight, support_color, frame::ICON_SMALL_PX),
]
.spacing(ui.space(Space::X3))
.width(Length::Fill)
.align_y(Alignment::Center);
container(inner)
.width(Length::Fill)
.height(height)
.padding(Padding::from([0.0, ui.space(Space::X2)]))
.align_y(Alignment::Center)
.style(move |_theme| container::Style {
background: selected.then(|| Background::Color(ui.color(Role::SecondaryContainer))),
border: Border {
radius: ui.radius(ShapeScale::Full, height),
..Border::default()
},
..container::Style::default()
})
.into()
list_item(ui, item)
}
// --------------------------------------------------------------- 详情列
fn detail<'a>(ui: &'a Ui, metrics: FrameMetrics) -> Element<'a, Message> {
let entry = &DEMO[SELECTED];
fn detail_column<'a>(
ui: &'a Ui,
metrics: FrameMetrics,
detail: &'a Detail,
) -> Element<'a, Message> {
let mut body = column![
detail_head(ui, detail),
meta_grid(ui, metrics, detail),
divider::full(ui),
]
.spacing(ui.space(Space::X4))
.width(Length::Fill);
frame::left_aligned(
metrics.column,
column![
detail_head(ui, entry),
meta_grid(ui, metrics, entry),
frame::divider(ui),
body_paragraph(ui),
frame::divider(ui),
action_row(ui),
]
.spacing(ui.space(Space::X4))
.width(Length::Fill),
)
body = push_text(ui, body, detail);
// 页头 + 元信息网格 + **一条**分隔线以下才是正文与操作区(M84 那两条线是占位时期的排版残留)
frame::left_aligned(metrics.column, body.push(action_row(ui)))
}
fn narrow_detail<'a>(ui: &'a Ui, metrics: FrameMetrics) -> Element<'a, Message> {
fn narrow_detail<'a>(
ui: &'a Ui,
metrics: FrameMetrics,
detail: &'a Detail,
) -> Element<'a, Message> {
// 窄窗:列表退成上一屏,本屏只放详情(官方 compact 的 list-detail 口径)。
let entry = &DEMO[SELECTED];
let mut body = column![
row![buttons::text(
ui,
Button::new("Back to instances", Message::Navigate(Route::Instances)),
)]
.width(Length::Fill)
.align_y(Alignment::Center),
detail_head(ui, detail),
meta_grid(ui, metrics, detail),
divider::full(ui),
]
.spacing(ui.space(Space::X4))
.width(Length::Fill);
frame::single_column(
metrics,
column![
row![frame::text_action(ui, "Back to instances", Action::Back)]
.width(Length::Fill)
.align_y(Alignment::Center),
detail_head(ui, entry),
meta_grid(ui, metrics, entry),
frame::divider(ui),
body_paragraph(ui),
frame::divider(ui),
frame::text_role(
ui,
TypeRole::BodySmall,
false,
"Below 840 px the list and the detail share one column, so opening an instance replaces \
this screen (full-page switch). Widen the window to see both panes again.",
ui.color(Role::OnSurfaceVariant),
),
action_row(ui),
]
.spacing(ui.space(Space::X4))
.width(Length::Fill),
)
body = push_text(ui, body, detail);
body = body.push(frame::text_role(
ui,
TypeRole::BodySmall,
false,
"Below 840 px the list and the detail share one column, so opening an instance replaces \
this screen (full-page switch). Widen the window to see both panes again.",
ui.color(Role::OnSurfaceVariant),
));
frame::single_column(metrics, body.push(action_row(ui)))
}
fn detail_head<'a>(ui: &'a Ui, entry: &'a Entry) -> Element<'a, Message> {
/// 正文段(演示数据才有)与运行状态行(真数据才有)——两者都是可选的,顺序固定。
fn push_text<'a>(
ui: &'a Ui,
mut body: iced::widget::Column<'a, Message>,
detail: &'a Detail,
) -> iced::widget::Column<'a, Message> {
if let Some(note) = &detail.note {
body = body.push(frame::text_role(
ui,
TypeRole::BodyMedium,
false,
note.as_str(),
ui.color(Role::OnSurfaceVariant),
));
}
if let Some(status) = &detail.status {
body = body.push(frame::text_role(
ui,
TypeRole::BodyMedium,
false,
status.as_str(),
ui.color(Role::OnSurface),
));
}
body
}
fn detail_head<'a>(ui: &'a Ui, detail: &'a Detail) -> Element<'a, Message> {
let titles = column![
frame::text_role(
ui,
TypeRole::TitleLarge,
true,
entry.name,
detail.name.as_str(),
ui.color(Role::OnSurface)
),
frame::text_role(
ui,
TypeRole::BodySmall,
false,
entry.version,
detail.subtitle.as_str(),
ui.color(Role::OnSurfaceVariant),
),
]
.spacing(ui.space(Space::X1))
.width(Length::Fill);
row![
titles,
frame::text_action(ui, "Play", Action::Play),
frame::text_action(ui, "More", Action::More),
]
.spacing(ui.space(Space::X2))
.width(Length::Fill)
.align_y(Alignment::Center)
.into()
// 主操作:**全屏唯一**实心 accent(rubric K2)。不可用时退化成禁用态(只清消息,不改身份)。
let launch = if detail.can_launch {
buttons::filled(ui, Button::new("Play", Message::Launch(detail.id.clone())))
} else {
buttons::filled(ui, Button::disabled("Play"))
};
let stop = if detail.can_stop {
buttons::text(ui, Button::new("Stop", Message::Stop(detail.id.clone())))
} else {
buttons::text(ui, Button::disabled("Stop"))
};
row![titles, launch, stop]
.spacing(ui.space(Space::X2))
.width(Length::Fill)
.align_y(Alignment::Center)
.into()
}
/// 元信息网格:列数由 [`layout::meta_columns`](按正文列宽)决定 —— **1180 与 900 都是 2 列**。
///
/// 3 列需要 `3 × META_CARD_MIN_WIDTH + 2 × META_GAP` = 504px,而行宽上限是 424,**数学上不可行**;
/// 硬塞回 3 列会让 `NeoForge 20.4.237`(118px)这类值按词折行、同排卡底不齐。
fn meta_grid<'a>(ui: &'a Ui, metrics: FrameMetrics, entry: &'a Entry) -> Element<'a, Message> {
let cells = [
("VERSION", entry.version),
("LOADER", entry.loader),
("MEMORY", entry.memory),
("JAVA", entry.java),
("MODS", entry.mods),
("LAST PLAYED", entry.last_played),
];
fn meta_grid<'a>(ui: &'a Ui, metrics: FrameMetrics, detail: &'a Detail) -> Element<'a, Message> {
let mut grid = column![].spacing(layout::META_GAP).width(Length::Fill);
let columns = metrics.meta_columns.clamp(1, cells.len());
let columns = metrics.meta_columns.clamp(1, detail.meta.len().max(1));
for chunk in cells.chunks(columns) {
for chunk in detail.meta.chunks(columns) {
let mut cells_row = row![].spacing(layout::META_GAP).width(Length::Fill);
for (label, value) in chunk {
let cell = frame::card(
for cell in chunk {
let card = frame::card(
ui,
column![
frame::text_role(
ui,
TypeRole::LabelMedium,
false,
label,
cell.label.as_str(),
ui.color(Role::OnSurfaceVariant),
),
frame::text_role(
ui,
TypeRole::BodyMedium,
false,
value,
cell.value.as_str(),
ui.color(Role::OnSurface)
),
]
.spacing(ui.space(Space::X1))
.width(Length::Fill),
);
cells_row = cells_row.push(container(cell).width(Length::Fill));
cells_row = cells_row.push(container(card).width(Length::Fill));
}
grid = grid.push(cells_row);
}
@@ -319,21 +353,17 @@ fn meta_grid<'a>(ui: &'a Ui, metrics: FrameMetrics, entry: &'a Entry) -> Element
grid.into()
}
fn body_paragraph<'a>(ui: &'a Ui) -> Element<'a, Message> {
frame::text_role(
ui,
TypeRole::BodyMedium,
false,
NOTE,
ui.color(Role::OnSurfaceVariant),
)
}
fn action_row<'a>(ui: &'a Ui) -> Element<'a, Message> {
row![
frame::text_action(ui, "Duplicate", Action::Duplicate),
frame::text_action(ui, "Open folder", Action::OpenFolder),
frame::text_action(ui, "Delete", Action::Delete),
buttons::text(
ui,
Button::new("Duplicate", Message::Action(Action::Duplicate))
),
buttons::text(
ui,
Button::new("Open folder", Message::Action(Action::OpenFolder))
),
buttons::text(ui, Button::new("Delete", Message::Action(Action::Delete))),
]
.spacing(ui.space(Space::X2))
.align_y(Alignment::Center)