AnyBot/src/app/kit.rs

360 lines
19 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.

//! 共用元件:每個畫面都從這裡拿,不自己畫框、不自己定字級。
//!
//! 依 Claude 的設計原則(卡片樣式按角色用、重複的元素長得一樣)與設計規格(theme.rs):
//!
//! - **區段** `section` / `section_with`:13 px 灰色粗體小標,上方 24、下方 8。設定的每一頁都用它。
//! - **群組** `rows_card`:一張卡(elevated 底、細框、圓角 14),裝一組**設定列**;列與列之間
//! 自動一條細線。一頁的設定就是「區段+群組」一組一組往下排。
//! - **設定列** `card_row`:左邊名稱(14)、可選的一行狀態(13 灰)和「?」說明,右邊控制項;
//! 控制項先排、名稱拿剩下的寬度(太長就「…」)。高 52,有狀態 62。
//! - **卡片** `setting_card` + `setting_title`:給一整塊有標題、有欄位的內容(Bot 設定的幾段)。
//! 卡片裡放列、欄位、文字——**不再放卡片**(卡中卡是畫面亂的主因)。
//! - **欄位** `form_row`:名稱在上(13 px 灰)、輸入框在下滿寬——整個 App 只有這一種標法;
//! 兩個短欄位並排用 `form_pair`(帳號/密碼)。`field_label` 是單獨的欄位名稱。
//! - **狀態列** `status_row`:清單裡有即時狀態的一列(連接器、訊息管道):名稱、色點+一行狀態、右邊控制項。
//! - **說明** `hint`:13 px 灰字,**一行**(長的結尾「…」,滑過看全文)。`help_mark` 是名稱旁的「?」,
//! 設定列上的只在滑鼠移到那一列時出現。
//! - **按鈕** `primary_button`(實心,一個畫面一顆)、`secondary_button`(淡底)、`danger_button`;
//! 刪除這種不能回頭的,用紅字無底按鈕並按兩次確認(由呼叫的地方做)。
//! - **等你的卡片** `needs_you_card`:允許、問題、交給你處理的步驟——同一種黃(黃=需要你)。
//! - **分段選擇** `segmented`;**對話框底部** `dialog_footer`(左:影響內容本身的;右:完成)。
use super::*;
use crate::theme::sp;
/// A group heading inside a settings page (like the original's section titles).
pub(super) fn section(ui: &mut egui::Ui, title: &str, p: Palette) {
section_with(ui, title, p, |_| {});
}
/// A section heading with something at its right end (a name, a button).
pub(super) fn section_with(ui: &mut egui::Ui, title: &str, p: Palette, accessory: impl FnOnce(&mut egui::Ui)) {
ui.add_space(sp::S5);
ui.horizontal(|ui| {
ui.add_space(sp::S1);
ui.label(RichText::new(t(title)).size(FONT_SM).family(crate::bold()).color(p.text2));
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
ui.add_space(sp::S1);
accessory(ui);
});
});
ui.add_space(sp::S2);
}
/// A small「?」after a setting's name. Its explanation shows as soon as the pointer is on it (no
/// waiting for the pointer to be still), and a click keeps it open until the next click.
pub(super) fn help_mark(ui: &mut egui::Ui, text: &str, p: Palette) {
help_mark_when(ui, text, p, true);
}
/// The 「?」 of a settings row: drawn only while the pointer is on its row (`visible`) or its
/// note is pinned open — a page of rows doesn't show a page of question marks. Its place is
/// kept either way, so the name beside it never moves.
pub(super) fn help_mark_when(ui: &mut egui::Ui, text: &str, p: Palette, visible: bool) {
if text.trim().is_empty() {
return;
}
let (rect, r) = ui.allocate_exact_size(vec2(18.0, 18.0), egui::Sense::click());
let pinned_id = r.id.with("pinned");
let mut pinned = ui.ctx().data(|d| d.get_temp::<bool>(pinned_id)).unwrap_or(false);
if !visible && !pinned && !r.hovered() {
return;
}
if r.clicked() {
pinned = !pinned;
} else if pinned && ui.input(|i| i.pointer.any_pressed()) && !r.hovered() {
pinned = false;
}
ui.ctx().data_mut(|d| d.insert_temp(pinned_id, pinned));
let show = r.hovered() || pinned;
let col = if show { p.text } else { p.text3 };
ui.painter().circle_stroke(rect.center(), 6.5, Stroke::new(1.1, col));
// (A glyph sized to its 14 px ring, an icon rather than text on the type scale.)
const HELP_GLYPH: f32 = 10.0;
ui.painter().text(rect.center() + vec2(0.0, 0.5), egui::Align2::CENTER_CENTER, "?", egui::FontId::proportional(HELP_GLYPH), col);
if show {
let text = t(text);
egui::Area::new(r.id.with("help"))
.order(egui::Order::Tooltip)
.fixed_pos(rect.left_bottom() + vec2(-4.0, 6.0))
.constrain(true)
.interactable(false)
.show(ui.ctx(), |ui| {
Frame::popup(ui.style()).corner_radius(R_M).inner_margin(Margin::symmetric(12, 10)).show(ui, |ui| {
ui.set_max_width(300.0);
ui.add(egui::Label::new(RichText::new(text).size(FONT_SM).color(p.text)).wrap());
});
});
}
r.on_hover_cursor(egui::CursorIcon::Help);
}
/// A group of settings: one quiet card, its rows (`card_row`) divided by hairlines.
pub(super) fn rows_card(ui: &mut egui::Ui, p: Palette, rows: impl FnOnce(&mut egui::Ui)) {
Frame::new().fill(p.elevated).stroke(Stroke::new(1.0, p.border)).corner_radius(R_L).inner_margin(Margin::symmetric(16, 1)).show(ui, |ui| {
ui.set_width(ui.available_width());
ui.spacing_mut().item_spacing.y = 0.0;
rows(ui);
});
}
/// One setting in a card: its name (and at most one small line under it) on the left, its
/// control on the right; anything longer is behind the ? (`help`).
pub(super) fn card_row(ui: &mut egui::Ui, p: Palette, label: &str, sub: &str, help: &str, control: impl FnOnce(&mut egui::Ui)) {
let top = ui.cursor().top();
if top > ui.max_rect().top() + 1.0 {
ui.painter().hline(ui.max_rect().x_range(), top, Stroke::new(1.0, p.border));
}
let h = if sub.is_empty() { 52.0 } else { 62.0 };
let (rect, _) = ui.allocate_exact_size(vec2(ui.available_width(), h), egui::Sense::hover());
let row_hovered = ui.rect_contains_pointer(rect);
// The control first, from the right edge; the words get the room that is left.
let mut right = ui.new_child(egui::UiBuilder::new().max_rect(rect).layout(Layout::right_to_left(Align::Center)));
right.spacing_mut().item_spacing.x = 8.0;
control(&mut right);
let words = egui::Rect::from_min_max(rect.min, egui::pos2((right.min_rect().left() - 12.0).max(rect.left() + 80.0), rect.bottom()));
if sub.is_empty() {
let mut left = ui.new_child(egui::UiBuilder::new().max_rect(words).layout(Layout::left_to_right(Align::Center)));
left.spacing_mut().item_spacing.x = 4.0;
left.add(egui::Label::new(RichText::new(t(label)).size(FONT_BASE).color(p.text)).truncate());
help_mark_when(&mut left, help, p, row_hovered);
} else {
let mut left = ui.new_child(egui::UiBuilder::new().max_rect(words).layout(Layout::top_down(Align::Min)));
left.spacing_mut().item_spacing = vec2(4.0, 1.0);
left.add_space((h - 42.0) / 2.0);
left.horizontal(|ui| {
ui.add(egui::Label::new(RichText::new(t(label)).size(FONT_BASE).color(p.text)).truncate());
help_mark_when(ui, help, p, row_hovered);
});
left.add(egui::Label::new(RichText::new(t(sub)).size(FONT_SM).color(p.text2)).truncate());
}
}
/// A choice among a few: the one chosen is the raised, bold one. Returns a new choice.
pub(super) fn segmented<T: PartialEq + Copy>(ui: &mut egui::Ui, p: Palette, cur: T, options: &[(T, &str)]) -> Option<T> {
let mut picked = None;
Frame::new().fill(p.active).corner_radius(R_M).inner_margin(Margin::same(3)).show(ui, |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 2.0;
// (In a row laid out from the right, the first choice is still the left one.)
let reversed = ui.layout().prefer_right_to_left();
let order: Vec<&(T, &str)> = if reversed { options.iter().rev().collect() } else { options.iter().collect() };
for (v, label) in order {
let on = *v == cur;
let text = RichText::new(t(label)).size(FONT_SM).color(if on { p.text } else { p.text2 });
let text = if on { text.family(crate::bold()) } else { text };
if ui.add(egui::Button::new(text).fill(if on { p.elevated } else { Color32::TRANSPARENT }).stroke(Stroke::NONE).corner_radius(R_M).min_size(vec2(0.0, 26.0))).clicked() && !on {
picked = Some(*v);
}
}
});
});
picked
}
pub(super) fn setting_title(ui: &mut egui::Ui, title: &str, desc: &str, p: Palette) {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 4.0;
ui.label(RichText::new(t(title)).strong().family(crate::bold()).size(FONT_BASE));
help_mark(ui, desc, p);
});
ui.add_space(4.0);
}
/// Small grey text under a setting.
pub(super) fn hint(ui: &mut egui::Ui, text: impl Into<String>, p: Palette) {
// One line: a settings page is names and controls, not paragraphs. A longer note ends in
// 「…」 and shows whole under the pointer.
ui.add(egui::Label::new(RichText::new(t(&text.into())).size(FONT_SM).color(p.text2)).truncate());
}
/// A rounded group of related settings, like a macOS settings card.
pub(super) fn setting_card<R>(ui: &mut egui::Ui, p: Palette, body: impl FnOnce(&mut egui::Ui) -> R) -> R {
let r = Frame::new()
.fill(p.elevated)
.stroke(Stroke::new(1.0, p.border))
.corner_radius(R_L)
.inner_margin(Margin::symmetric(16, 14))
.show(ui, |ui| {
ui.set_width(ui.available_width());
body(ui)
})
.inner;
ui.add_space(12.0);
r
}
/// A label column so form rows line up.
/// A form field: its name above (13 px grey), the input below at full width. The app's one way
/// to label an input (a label column on the left, a label inline, or none at all made a form
/// whose boxes started at different places).
pub(super) fn form_row(ui: &mut egui::Ui, label: &str, p: Palette, body: impl FnOnce(&mut egui::Ui)) {
field_label(ui, label, p);
ui.horizontal(|ui| body(ui));
}
/// Guidance in a flow (開始使用, a form's how-to): 13 px grey that wraps — unlike `hint`, which is
/// one line on a settings page. For the few places where the explanation is the point.
pub(super) fn note(ui: &mut egui::Ui, text: impl Into<String>, p: Palette) {
ui.add(egui::Label::new(RichText::new(t(&text.into())).size(FONT_SM).color(p.text2)).wrap());
}
/// A form field whose name has a 「?」 beside it (what the field does, on hover) instead of a
/// sentence under the box.
pub(super) fn form_row_help(ui: &mut egui::Ui, label: &str, help: &str, p: Palette, body: impl FnOnce(&mut egui::Ui)) {
ui.add_space(sp::S2);
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = sp::S1;
ui.label(RichText::new(t(label)).size(FONT_SM).color(p.text2));
help_mark(ui, help, p);
});
ui.add_space(sp::S1 * 0.5);
ui.horizontal(|ui| body(ui));
}
/// Two short fields side by side (帳號 and 密碼), each with its name above it.
pub(super) fn form_pair(ui: &mut egui::Ui, p: Palette, left_label: &str, left: impl FnOnce(&mut egui::Ui), right_label: &str, right: impl FnOnce(&mut egui::Ui)) {
let gap = sp::S3;
let w = ((ui.available_width() - gap) / 2.0).max(80.0);
ui.horizontal_top(|ui| {
ui.spacing_mut().item_spacing.x = gap;
ui.vertical(|ui| {
ui.set_width(w);
field_label(ui, left_label, p);
ui.horizontal(|ui| left(ui));
});
ui.vertical(|ui| {
ui.set_width(w);
field_label(ui, right_label, p);
ui.horizontal(|ui| right(ui));
});
});
}
/// A card's title row that folds the card: the title, one grey line of how it is set (「11/11
/// 類」), the 「?」 (on hover) and ▸/▾ at the right end. Folded until clicked — what most people
/// never change stays one line. Returns whether the card is open.
pub(super) fn fold_title(ui: &mut egui::Ui, p: Palette, id: impl std::hash::Hash + std::fmt::Debug, title: &str, summary: &str, help: &str) -> bool {
let id = egui::Id::unique(("fold", id));
let mut open = ui.data(|d| d.get_temp::<bool>(id)).unwrap_or(false);
let (rect, r) = ui.allocate_exact_size(vec2(ui.available_width(), 24.0), egui::Sense::click());
let hovered = r.hovered();
if r.on_hover_cursor(egui::CursorIcon::PointingHand).clicked() {
open = !open;
ui.data_mut(|d| d.insert_temp(id, open));
}
let mut row = ui.new_child(egui::UiBuilder::new().max_rect(rect).layout(Layout::left_to_right(Align::Center)));
row.spacing_mut().item_spacing.x = sp::S1;
row.label(RichText::new(t(title)).strong().family(crate::bold()).size(FONT_BASE));
help_mark_when(&mut row, help, p, hovered);
if !summary.is_empty() {
row.add_space(sp::S1);
row.add(egui::Label::new(RichText::new(summary).size(FONT_SM).color(p.text3)).truncate());
}
ui.painter().text(rect.right_center() - vec2(4.0, 0.0), egui::Align2::RIGHT_CENTER, if open { "▾" } else { "▸" }, egui::FontId::proportional(FONT_READ), p.text3);
if open {
ui.add_space(sp::S2);
}
open
}
/// The card of something waiting for you — an approval, a question, a step handed to you on a
/// computer: the one yellow (yellow means 「需要你」 everywhere), a soft tint and a thin edge.
/// One look for all three (the hand-off card used to be outlined in the accent and read as red).
pub(super) fn needs_you_card<R>(ui: &mut egui::Ui, p: Palette, body: impl FnOnce(&mut egui::Ui) -> R) -> egui::InnerResponse<R> {
Frame::new().fill(p.attention_bg).stroke(Stroke::new(1.5, p.attention)).corner_radius(R_L).inner_margin(Margin::symmetric(16, 14)).show(ui, body)
}
/// A row of a list with a live state: the name, then a coloured dot and the state in one grey
/// line under it (an error in red), the controls on the right — a connector, a channel.
pub(super) fn status_row(ui: &mut egui::Ui, p: Palette, label: &str, status: &str, dot: Color32, control: impl FnOnce(&mut egui::Ui)) {
let top = ui.cursor().top();
if top > ui.max_rect().top() + 1.0 {
ui.painter().hline(ui.max_rect().x_range(), top, Stroke::new(1.0, p.border));
}
let (rect, _) = ui.allocate_exact_size(vec2(ui.available_width(), 62.0), egui::Sense::hover());
let mut right = ui.new_child(egui::UiBuilder::new().max_rect(rect).layout(Layout::right_to_left(Align::Center)));
right.spacing_mut().item_spacing.x = sp::S2;
control(&mut right);
let words = egui::Rect::from_min_max(rect.min, egui::pos2((right.min_rect().left() - 12.0).max(rect.left() + 80.0), rect.bottom()));
let mut left = ui.new_child(egui::UiBuilder::new().max_rect(words).layout(Layout::top_down(Align::Min)));
left.spacing_mut().item_spacing = vec2(6.0, 2.0);
left.add_space(10.0);
left.add(egui::Label::new(RichText::new(label).size(FONT_BASE).family(crate::bold()).color(p.text)).truncate());
left.horizontal(|ui| {
let (r, _) = ui.allocate_exact_size(vec2(8.0, 14.0), egui::Sense::hover());
ui.painter().circle_filled(r.center(), 3.5, dot);
let col = if dot == p.danger { p.danger } else { p.text2 };
ui.add(egui::Label::new(RichText::new(status).size(FONT_SM).color(col)).truncate());
});
}
/// A row that opens a page of its own (a connector, a skill), as other apps' settings lists do:
/// the name and one line under it (with a coloured dot when it is a live state), its quick
/// control (a switch) and a › on the right. A press anywhere else on the row opens it: true.
pub(super) fn nav_row(ui: &mut egui::Ui, p: Palette, label: &str, sub: &str, dot: Option<Color32>, control: impl FnOnce(&mut egui::Ui)) -> bool {
let top = ui.cursor().top();
if top > ui.max_rect().top() + 1.0 {
ui.painter().hline(ui.max_rect().x_range(), top, Stroke::new(1.0, p.border));
}
let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 58.0), egui::Sense::click());
if resp.hovered() {
ui.painter().rect_filled(rect.shrink2(vec2(-8.0, 3.0)), R_M, p.hover);
}
let mut right = ui.new_child(egui::UiBuilder::new().max_rect(rect).layout(Layout::right_to_left(Align::Center)));
right.spacing_mut().item_spacing.x = sp::S2;
// (Not selectable: a press on it opens the row too.)
right.add(egui::Label::new(RichText::new("›").size(FONT_H3).color(p.text3)).selectable(false));
control(&mut right);
let words = egui::Rect::from_min_max(rect.min, egui::pos2((right.min_rect().left() - 12.0).max(rect.left() + 80.0), rect.bottom()));
let mut left = ui.new_child(egui::UiBuilder::new().max_rect(words).layout(Layout::top_down(Align::Min)));
left.spacing_mut().item_spacing = vec2(6.0, 2.0);
left.add_space(if sub.is_empty() { 19.0 } else { 9.0 });
left.add(egui::Label::new(RichText::new(label).size(FONT_BASE).color(p.text)).truncate().selectable(false));
if !sub.is_empty() {
left.horizontal(|ui| {
if let Some(dot) = dot {
let (r, _) = ui.allocate_exact_size(vec2(8.0, 14.0), egui::Sense::hover());
ui.painter().circle_filled(r.center(), 3.5, dot);
}
let col = if dot == Some(p.danger) { p.danger } else { p.text2 };
ui.add(egui::Label::new(RichText::new(sub).size(FONT_SM).color(col)).truncate().selectable(false));
});
}
resp.on_hover_cursor(egui::CursorIcon::PointingHand).clicked()
}
/// 「‹ 連接器」: back from a page of its own to the list it came from.
pub(super) fn back_link(ui: &mut egui::Ui, text: &str, p: Palette) -> bool {
ui.add(egui::Button::new(RichText::new(format!("‹ {}", t(text))).size(FONT_SM).color(p.text2)).frame(false)).on_hover_cursor(egui::CursorIcon::PointingHand).clicked()
}
pub(super) fn field_label(ui: &mut egui::Ui, text: &str, p: Palette) {
ui.add_space(sp::S2);
ui.label(RichText::new(t(text)).size(FONT_SM).color(p.text2));
ui.add_space(sp::S1 * 0.5);
}
pub(super) fn primary_button(text: &str, p: Palette) -> egui::Button<'static> {
egui::Button::new(RichText::new(t(text)).color(p.on_primary).strong().family(crate::bold())).fill(p.primary).corner_radius(R_M).min_size(vec2(72.0, 30.0))
}
pub(super) fn secondary_button(text: &str) -> egui::Button<'static> {
egui::Button::new(t(text)).corner_radius(R_M).min_size(vec2(64.0, 30.0))
}
pub(super) fn danger_button(text: &str, p: Palette) -> egui::Button<'static> {
egui::Button::new(RichText::new(t(text)).color(Color32::WHITE).strong().family(crate::bold())).fill(p.danger).corner_radius(R_M).min_size(vec2(72.0, 30.0))
}
/// The bottom bar of a dialog: a line above, buttons laid out right to left.
pub(super) fn dialog_footer(ui: &mut egui::Ui, p: Palette, body: impl FnOnce(&mut egui::Ui)) {
let top = ui.cursor().top();
ui.painter().hline(ui.max_rect().x_range(), top, Stroke::new(1.0, p.border));
Frame::new().inner_margin(Margin::symmetric(20, 12)).show(ui, |ui| {
ui.set_width(ui.available_width());
ui.spacing_mut().item_spacing.y = 6.0;
ui.horizontal(|ui| body(ui));
});
}