AnyBot/src/app/kit.rs

210 lines
10 KiB
Rust
Raw Normal View History

2026-11-16 14:07:07 +00:00
//! 共用元件:每個畫面都從這裡拿,不自己畫框、不自己定字級。
//!
//! 依 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 設定的幾段)。
//! 卡片裡放列、欄位、文字——**不再放卡片**(卡中卡是畫面亂的主因)。
//! - **欄位** `field_label`(13 灰小標)+輸入框;**表單列** `form_row`(固定寬的標籤欄)。
//! - **說明** `hint`:13 px 灰字,設定下面的一兩句話。`help_mark` 是名稱旁的「?」。
//! - **按鈕** `primary_button`(實心,一個畫面一顆)、`secondary_button`(淡底)、`danger_button`;
//! 刪除這種不能回頭的,用紅字無底按鈕並按兩次確認(由呼叫的地方做)。
//! - **分段選擇** `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) {
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 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());
// 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(&mut left, help, p);
} 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(ui, help, p);
});
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) {
ui.add(egui::Label::new(RichText::new(t(&text.into())).size(FONT_SM).color(p.text2)).wrap());
}
/// 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.
pub(super) fn form_row(ui: &mut egui::Ui, label: &str, p: Palette, body: impl FnOnce(&mut egui::Ui)) {
ui.horizontal(|ui| {
let (rect, _) = ui.allocate_exact_size(vec2(FORM_LABEL_WIDTH, 28.0), egui::Sense::hover());
// (English labels are longer than the column; painted un-truncated they run under the
// control on the right. Cut them to the column width.)
let label = sidebar_ui::shorten_width(ui, &t(label), rect.width(), FONT_SM);
ui.painter().text(rect.left_center(), egui::Align2::LEFT_CENTER, label, egui::FontId::proportional(FONT_SM), p.text2);
body(ui);
});
}
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));
});
}