介面組件庫(ui_kit: section/card/field/switch/seg/pill/disc/empty/ghost)+全域圓角、muted 輸入底、細卷軸;Bot 頭像可自訂顏色(色相/飽和/明度,存 #hex)

This commit is contained in:
daniel wang 2026-11-13 01:08:52 +08:00
parent 4b53ad41eb
commit c58f14e274
6 changed files with 353 additions and 23 deletions

View File

@ -33,6 +33,7 @@ mod sidebar_ui;
mod menu_ui;
mod vault_ui;
mod markdown_ui;
mod ui_kit;
mod welcome_ui;
mod mirror_ui;
mod pages_ui;
@ -269,6 +270,9 @@ pub struct App {
faces: HashMap<u64, sidebar_ui::Face>,
/// When each bot last finished a turn well (its face above the input smiles for a moment).
cheer: HashMap<u64, Instant>,
/// The bot's writing face: (words in the answer, when the last one arrived), so the nod
/// keeps time with the words flowing out.
write_len: HashMap<u64, (usize, f64)>,
/// When each bot's request card was first shown (Enter answers it only after a moment).
ask_shown: HashMap<u64, Instant>,
/// 問題卡: the question whose field was last given the caret (once per question).
@ -437,6 +441,7 @@ impl App {
menu_for: None,
faces: HashMap::new(),
cheer: HashMap::new(),
write_len: HashMap::new(),
ask_shown: HashMap::new(),
ask_forms: HashMap::new(),
ask_focus: None,
@ -935,6 +940,13 @@ impl App {
v.widgets.inactive.bg_stroke = Stroke::NONE;
v.widgets.hovered.bg_stroke = Stroke::NONE;
v.widgets.noninteractive.bg_stroke = Stroke::new(1.0, p.border);
// Text edits, sliders, progress: the same corner ladder as the buttons, and the quiet
// 'muted' fill — inputs read as filled wells you type into (Geist), not white slabs.
v.widgets.noninteractive.corner_radius = CornerRadius::same(8);
v.widgets.noninteractive.bg_fill = p.muted;
v.widgets.noninteractive.weak_bg_fill = p.muted;
v.selection.bg_fill = p.accent.gamma_multiply(0.26);
v.selection.stroke = Stroke::new(1.0, p.text);
ctx.set_visuals(v);
ctx.all_styles_mut(|s| {
// Menus like the original's: 16 px corners, 6 px inside, rows touching.
@ -944,6 +956,12 @@ impl App {
s.visuals.popup_shadow = egui::epaint::Shadow { offset: [0, 10], blur: 30, spread: 0, color: Color32::from_black_alpha(70) };
s.spacing.item_spacing = vec2(8.0, 6.0);
s.spacing.button_padding = vec2(10.0, 5.0);
// Scrollbars like the web's: a thin quiet rail that widens under the pointer.
s.spacing.scroll.floating = true;
s.spacing.scroll.bar_width = 8.0;
s.spacing.scroll.floating_width = 5.0;
s.spacing.scroll.floating_allocated_width = 0.0;
s.spacing.scroll.foreground_color = true;
s.text_styles.insert(egui::TextStyle::Body, egui::FontId::proportional(FONT_BASE));
s.text_styles.insert(egui::TextStyle::Button, egui::FontId::proportional(FONT_BASE));
s.text_styles.insert(egui::TextStyle::Small, egui::FontId::proportional(FONT_XS));
@ -3717,6 +3735,35 @@ impl App {
}
}
});
// Custom: a rainbow ring; click it and three sliders appear. The colour is
// kept as its hex ("#rrggbb"), so the picker and the saved bot agree.
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 6.0;
let (rect, r) = ui.allocate_exact_size(vec2(28.0, 28.0), egui::Sense::click());
for i in 0..12 {
let a = i as f32 / 12.0 * std::f32::consts::TAU;
let c = egui::pos2(rect.center().x + 10.0 * a.cos(), rect.center().y + 10.0 * a.sin());
ui.painter().circle_filled(c, 3.0, avatar::color(&avatar::hex_of_hsv([i as f32 / 12.0 * 360.0, 0.85, 0.85])));
}
if avatar::is_custom(&b.color) {
ui.painter().circle_stroke(rect.center(), 13.0, Stroke::new(2.0, p.focus));
}
if r.on_hover_text(t("自訂顏色")).clicked() && !avatar::is_custom(&b.color) {
b.color = avatar::hex_of_hsv(avatar::hsv_of(&b.color));
}
ui.label(RichText::new(t("自訂")).size(FONT_XS).color(p.text3));
});
if avatar::is_custom(&b.color) {
let mut hsv = avatar::hsv_of(&b.color);
ui.horizontal(|ui| {
ui.add(egui::Slider::new(&mut hsv[0], 0.0..=360.0).text(t("色相").to_string()));
ui.add(egui::Slider::new(&mut hsv[1], 0.0..=1.0).text(t("飽和").to_string()));
ui.add(egui::Slider::new(&mut hsv[2], 0.0..=1.0).text(t("明度").to_string()));
let (sr, _) = ui.allocate_exact_size(vec2(24.0, 24.0), egui::Sense::hover());
ui.painter().circle_filled(sr.center(), 9.0, avatar::color(&avatar::hex_of_hsv(hsv)));
});
b.color = avatar::hex_of_hsv(hsv);
}
});
setting_card(ui, p, |ui| {
setting_title(ui, "AI 供應商與模型", "", p);

View File

@ -772,7 +772,8 @@ impl App {
pub(super) fn working_phrase(&self, now: f64, bot: &Bot) -> Option<String> {
let live = self.streams.get(&bot.id)?;
let waiting = self.approvals.contains_key(&bot.id);
let last = bot.messages.iter().rev().find(|m| m.role == Role::Assistant && !m.note);
let last = bot.messages[bot.messages.iter().rposition(|m| m.role == Role::User && !m.steer).unwrap_or(0)..]
.iter().rev().find(|m| m.role == Role::Assistant && !m.note); // this turn's words only
let running = last.and_then(|m| m.tool_calls.iter().rev().find(|c| matches!(c.status, ToolStatus::Running | ToolStatus::Waiting)));
// Where it works, by the computer's name (its own computer is 「<name> 的電腦」).
let named = running.and_then(|c| tools::args_of(&c.args)["computer"].as_str().map(String::from));

View File

@ -19,6 +19,8 @@ pub struct Face {
next_blink: f64,
blink_at: f64,
poke: Option<(Poke, f64)>,
/// Current turn of the thinking spin, degrees (only the face above the input spins).
spin: f32,
}
impl Face {
@ -98,16 +100,17 @@ impl App {
} else {
pointer_target
};
self.settle_face(ctx, id, target, working)
self.settle_face(ctx, id, target, working, false)
}
/// A face (kept under `key`) turning its eyes toward `target`, blinking now and then, and
/// carrying out a poke; `busy` keeps the window drawing.
fn settle_face(&mut self, ctx: &egui::Context, key: u64, target: egui::Vec2, busy: bool) -> avatar::Pose {
/// carrying out a poke; `busy` keeps the window drawing. `spin`: turn slowly while working
/// (Grok-style), easing back upright when the bot finishes.
fn settle_face(&mut self, ctx: &egui::Context, key: u64, target: egui::Vec2, busy: bool, spin: bool) -> avatar::Pose {
let now = ctx.input(|i| i.time);
let id = key;
let working = busy;
let f = self.faces.entry(key).or_insert_with(|| Face { gaze: egui::Vec2::ZERO, last: now, next_blink: now + 1.0 + 4.0 * rand01(key), blink_at: -10.0, poke: None });
let f = self.faces.entry(key).or_insert_with(|| Face { gaze: egui::Vec2::ZERO, last: now, next_blink: now + 1.0 + 4.0 * rand01(key), blink_at: -10.0, poke: None, spin: 0.0 });
let dt = (now - f.last).clamp(0.0, 0.1) as f32;
f.last = now;
let k = 1.0 - 0.86f32.powf(dt * 60.0);
@ -119,7 +122,22 @@ impl App {
}
let bt = ((now - f.blink_at) / BLINK) as f32;
let blink = if (0.0..1.0).contains(&bt) { (PI * bt).sin() } else { 0.0 };
let mut pose = avatar::Pose { gaze: f.gaze, blink, ..Default::default() };
// The thinking spin: one turn every 18 seconds; when the work is done the face eases
// back upright in about half a second, so it never rests crooked.
if spin {
if working {
f.spin = (f.spin + dt * 20.0).rem_euclid(360.0);
} else if f.spin != 0.0 {
// Ease back to upright the short way: 350° turns +10°, it doesn't run back.
let mut a = f.spin.rem_euclid(360.0);
if a > 180.0 {
a -= 360.0;
}
let na = a * (1.0 - k);
f.spin = if na.abs() < 0.5 { 0.0 } else { na };
}
}
let mut pose = avatar::Pose { gaze: f.gaze, blink, spin: f.spin, ..Default::default() };
if let Some((kind, at)) = f.poke {
let e = ((now - at) / duration(kind)) as f32;
if e >= 1.0 {
@ -137,7 +155,7 @@ impl App {
}
}
}
let moving = f.poke.is_some() || (f.gaze - target).length() > 0.002 || blink > 0.0 || working;
let moving = f.poke.is_some() || (f.gaze - target).length() > 0.002 || blink > 0.0 || working || f.spin != 0.0;
if moving {
ctx.request_repaint();
} else {
@ -150,7 +168,7 @@ impl App {
if let Some(f) = self.faces.get_mut(&id) {
f.poke = Some((kind, now));
} else {
self.faces.insert(id, Face { gaze: egui::Vec2::ZERO, last: now, next_blink: now + 2.0, blink_at: -10.0, poke: Some((kind, now)) });
self.faces.insert(id, Face { gaze: egui::Vec2::ZERO, last: now, next_blink: now + 2.0, blink_at: -10.0, poke: Some((kind, now)), spin: 0.0 });
}
}
@ -177,27 +195,60 @@ impl App {
let now = ctx.input(|i| i.time) as f32;
let waiting = self.approvals.contains_key(&bot.id);
let working = self.streams.contains_key(&bot.id);
let last = bot.messages.iter().rev().find(|m| m.role == Role::Assistant && !m.note);
// Only this turn's words count (see working_phrase): the last turn's answer is long
// over by now, and must not make a thinking face pretend it is still writing.
let from = bot.messages.iter().rposition(|m| m.role == Role::User && !m.steer).unwrap_or(0);
let last = bot.messages[from..].iter().rev().find(|m| m.role == Role::Assistant && !m.note);
let running = working && last.is_some_and(|m| m.tool_calls.iter().any(|c| matches!(c.status, ToolStatus::Running | ToolStatus::Waiting)));
let cheer = self.cheer.get(&bot.id).is_some_and(|t| t.elapsed().as_secs_f32() < 2.0);
let failed = !working && bot.messages.last().is_some_and(|m| m.error);
// Writing: words are flowing out and no step runs — the face bows to its own writing.
// Thinking: it works, nothing runs and no words yet — three small balls circle its head.
let has_text = last.is_some_and(|m| !m.text.trim().is_empty());
let writing = working && has_text && !running && !waiting;
let thinking = working && !running && !waiting && !has_text;
let target = if waiting {
vec2(0.0, 0.1)
} else if running {
vec2(0.9, 0.45 + 0.06 * (now * 2.0).sin())
} else if writing {
vec2(0.5, -0.8)
} else if working {
vec2(0.55 * (now * 0.9).sin(), 0.85)
} else {
vec2(0.15, -0.75)
};
// (Its own state, apart from the same bot's face in the sidebar, which looks elsewhere.)
let mut pose = self.settle_face(&ctx, !bot.id, target, working);
// The spin rests here: thinking and writing have their own motions (balls, nod).
let mut pose = self.settle_face(&ctx, !bot.id, target, working, false);
pose.eyes = if cheer { avatar::Eyes::Smile } else if failed { avatar::Eyes::Flat } else { avatar::Eyes::Open };
if writing {
// Head bowed over the words, nodding in time with them: the nod runs while words
// arrived in the last blink of an eye, and rests when the flowing stops.
let len = last.map(|m| m.text.chars().count()).unwrap_or(0);
let (prev, at) = self.write_len.get(&bot.id).copied().unwrap_or((len, now as f64));
// The count moving at all (a new word, or a fresh answer) means words came just now.
let grow_at = if len != prev { now as f64 } else { at };
self.write_len.insert(bot.id, (len, grow_at));
let flowing = now as f64 - grow_at < 0.7;
pose.pitch = 7.0 + if flowing { 3.5 * (now * 6.5).sin() } else { 0.0 };
}
if cheer {
ctx.request_repaint_after(std::time::Duration::from_millis(200));
}
let col = self.bot_color(bot);
avatar::paint_posed(ui.painter(), rect, &bot.shape, col, pose);
if thinking {
// Three balls circling the head on a flat ring, inside the face's own square (they
// never reach the words beside it). The far side is small and dim, the near large.
let r = rect.width() / 2.0 - 3.0;
for k in 0..3 {
let a = now * 2.2 + k as f32 * 2.0 * PI / 3.0;
let d = (a.sin() + 1.0) / 2.0;
let at = rect.center() + vec2(a.cos() * r * 0.95, a.sin() * r * 0.45);
ui.painter().circle_filled(at, 1.5 + 1.3 * d, col.gamma_multiply(0.45 + 0.55 * d));
}
}
if waiting {
// (Sized with the face.)
let k = rect.width() / 64.0;

141
src/app/ui_kit.rs Normal file
View File

@ -0,0 +1,141 @@
// The component kit: the small repeated pieces of every settings-like page, in the app's own
// hand-drawn style (like the shadcn/Geist systems: a radius ladder, a quiet 'muted' layer,
// filled inputs, iOS switches, pill statuses, ghost empty states). Everything here is a plain
// function over (&mut Ui, Palette) so any page can use it without state.
use eframe::egui::{self, vec2, Color32, Frame, Margin, RichText, Sense, Stroke, Ui, Vec2, epaint::Shadow};
use crate::i18n::t;
use crate::icons::{self, Icon};
use crate::theme::{self, Palette};
// The radius ladder (shadcn's): small controls, buttons and chips, cards and rows, bubbles, modals.
pub const R_SM: f32 = 8.0;
pub const R_MD: f32 = 10.0;
pub const R_LG: f32 = 14.0;
pub const R_XL: f32 = 18.0;
pub const R_2XL: f32 = 22.0;
/// The card shadow: present, never heavy.
pub const SOFT: Shadow = Shadow { offset: [0, 2], blur: 12, spread: 0, color: Color32::from_black_alpha(22) };
/// A section: bold title, one quiet line under it, a hairline, then the controls. The unit
/// every settings page is built from.
pub fn section(ui: &mut Ui, p: Palette, title: &str, desc: &str, add: impl FnOnce(&mut Ui)) {
ui.add_space(16.0);
ui.label(RichText::new(t(title)).size(13.5).strong().family(crate::bold()).color(p.text));
if !desc.is_empty() {
ui.label(RichText::new(t(desc)).size(12.0).color(p.text3));
}
ui.add_space(8.0);
let r = ui.available_rect_before_wrap();
ui.painter().hline(r.x_range(), r.min.y, Stroke::new(1.0, p.border));
ui.add_space(10.0);
add(ui);
ui.add_space(8.0);
}
/// A quiet card: elevated fill, hairline, soft shadow.
pub fn card(ui: &mut Ui, p: Palette, add: impl FnOnce(&mut Ui)) {
Frame::new().fill(p.elevated).stroke(Stroke::new(1.0, p.border)).corner_radius(R_LG).inner_margin(Margin::same(14)).shadow(SOFT).show(ui, add);
}
/// A field: the label rides above the control, a small hint under it (labels above, never
/// inside — the field keeps its meaning while you type).
pub fn field(ui: &mut Ui, p: Palette, label: &str, hint: &str, add: impl FnOnce(&mut Ui)) {
if !label.is_empty() {
ui.label(RichText::new(t(label)).size(12.0).color(p.text2));
}
add(ui);
if !hint.is_empty() {
ui.label(RichText::new(t(hint)).size(11.5).color(p.text3));
}
}
/// The iOS switch (38×22): the checkbox's grown-up version. Knobs slide; returns the click.
pub fn switch(ui: &mut Ui, p: Palette, on: bool) -> egui::Response {
let (rect, resp) = ui.allocate_exact_size(vec2(38.0, 22.0), Sense::click());
let k = ui.ctx().animate_bool_with_time(resp.id, on, 0.16) as f32;
let off = if p.pane.r() < 128 { Color32::from_gray(0x3a) } else { Color32::from_gray(0xd6) };
let painter = ui.painter();
painter.rect_filled(rect, 11.0, off.lerp_to_gamma(p.accent, k));
if resp.hovered() {
painter.rect_filled(rect, 11.0, p.hover);
}
let cy = rect.center().y;
let x = rect.left() + 3.0 + 16.0 * k;
painter.circle_filled(egui::pos2(x, cy), 8.0, Color32::WHITE);
resp.on_hover_cursor(egui::CursorIcon::PointingHand)
}
/// A status pill: a coloured dot and a word, on a disc of its own colour (連線中 / 離線).
pub fn pill(ui: &mut Ui, p: Palette, text: &str, col: Color32) {
let g = ui.painter().layout_no_wrap(t(text), egui::FontId::proportional(11.5), col);
let size = Vec2::new(g.rect.width() + 22.0, 20.0);
let (rect, _) = ui.allocate_exact_size(size, Sense::hover());
ui.painter().rect_filled(rect, 10.0, theme::mist(col, p));
ui.painter().circle_filled(egui::pos2(rect.left() + 9.0, rect.center().y), 3.0, col);
ui.painter().galley(rect.center() - g.rect.center().to_vec2() + Vec2::new(8.5, 0.0), g, col);
}
/// A segmented control: pills sliding inside a quiet tray (Geist's tabs for small choices).
/// Returns the newly picked row.
pub fn seg(ui: &mut Ui, p: Palette, items: &[&str], selected: usize) -> Option<usize> {
let mut out = None;
Frame::new().fill(p.muted).corner_radius(R_MD + 2.0).inner_margin(Margin::same(2)).show(ui, |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 2.0;
for (i, it) in items.iter().enumerate() {
let on = i == selected;
let b = egui::Button::new(RichText::new(t(it)).size(12.5).color(if on { p.text } else { p.text3 }))
.fill(if on { p.elevated } else { Color32::TRANSPARENT })
.stroke(if on { Stroke::new(1.0, p.border) } else { Stroke::NONE })
.corner_radius(R_MD)
.min_size(Vec2::new(0.0, 26.0));
if ui.add(b).clicked() {
out = Some(i);
}
}
});
});
out
}
/// A round disc with an icon (list rows' leading mark): the icon's colour thinned over the
/// page, the icon itself solid.
pub fn disc(ui: &mut Ui, p: Palette, icon: Icon, size: f32, col: Color32) {
let (rect, _) = ui.allocate_exact_size(Vec2::splat(size), Sense::hover());
ui.painter().circle_filled(rect.center(), size / 2.0, theme::mist(col, p));
icons::paint(ui.painter(), egui::Rect::from_center_size(rect.center(), Vec2::splat(size * 0.52)), icon, col);
}
/// The empty state: a big ghost mark, one line, a quieter line (so an empty page is a page too).
pub fn empty(ui: &mut Ui, p: Palette, icon: Icon, text: &str, hint: &str) {
ui.vertical_centered(|ui| {
ui.add_space(28.0);
let (rect, _) = ui.allocate_exact_size(Vec2::splat(56.0), Sense::hover());
ui.painter().circle_filled(rect.center(), 28.0, p.hover);
icons::paint(ui.painter(), egui::Rect::from_center_size(rect.center(), Vec2::splat(26.0)), icon, p.text3);
ui.add_space(12.0);
ui.label(RichText::new(t(text)).size(13.5).color(p.text2));
if !hint.is_empty() {
ui.label(RichText::new(t(hint)).size(12.0).color(p.text3));
}
ui.add_space(24.0);
});
}
/// A row's trailing ghost button: quiet until hovered (deletes, opens, connects).
pub fn ghost(ui: &mut Ui, p: Palette, label: &str) -> egui::Response {
ui.add(egui::Button::new(RichText::new(t(label)).size(12.5).color(p.text2)).fill(Color32::TRANSPARENT).corner_radius(R_MD).min_size(Vec2::new(0.0, 28.0)))
}
/// A ghost icon button (row menus): the ⋯ that whispers until you reach it.
pub fn ghost_icon(ui: &mut Ui, p: Palette, icon: Icon) -> egui::Response {
let (rect, resp) = ui.allocate_exact_size(Vec2::splat(28.0), Sense::click());
if resp.hovered() {
ui.painter().rect_filled(rect, R_MD, p.hover);
}
icons::paint(ui.painter(), egui::Rect::from_center_size(rect.center(), Vec2::splat(15.0)), icon, p.text3);
resp.on_hover_cursor(egui::CursorIcon::PointingHand)
}

View File

@ -40,11 +40,64 @@ pub const COLORS: [(&str, &str, [u8; 3]); 10] = [
("brown", "棕", [0x86, 0x59, 0x2e]),
];
/// A colour is a preset name, or custom: the name is then its hex, "#rrggbb".
pub fn color(name: &str) -> Color32 {
if let Some(c) = hex_color(name) {
return c;
}
let c = COLORS.iter().find(|c| c.0 == name).map(|c| c.2).unwrap_or(COLORS[0].2);
Color32::from_rgb(c[0], c[1], c[2])
}
pub fn is_custom(name: &str) -> bool {
name.starts_with('#')
}
fn hex_color(name: &str) -> Option<Color32> {
let h = name.trim().trim_start_matches('#');
if h.len() == 6 && h.chars().all(|c| c.is_ascii_hexdigit()) {
let v = u32::from_str_radix(h, 16).ok()?;
return Some(Color32::from_rgb((v >> 16) as u8, (v >> 8) as u8, v as u8));
}
None
}
/// The colour as hue (0..360), saturation and value (0..1) — for the picker's sliders.
pub fn hsv_of(name: &str) -> [f32; 3] {
let c = color(name);
let (r, g, b) = (c.r() as f32 / 255.0, c.g() as f32 / 255.0, c.b() as f32 / 255.0);
let (mx, mn) = (r.max(g).max(b), r.min(g).min(b));
let d = mx - mn;
let h = if d == 0.0 {
0.0
} else if mx == r {
60.0 * (((g - b) / d) % 6.0)
} else if mx == g {
60.0 * ((b - r) / d + 2.0)
} else {
60.0 * ((r - g) / d + 4.0)
};
[if h < 0.0 { h + 360.0 } else { h }, if mx == 0.0 { 0.0 } else { d / mx }, mx]
}
/// The picker's answer: sliders back to a hex name.
pub fn hex_of_hsv(hsv: [f32; 3]) -> String {
let (h, s, v) = (hsv[0].rem_euclid(360.0), hsv[1].clamp(0.0, 1.0), hsv[2].clamp(0.0, 1.0));
let c = v * s;
let x = c * (1.0 - ((h / 60.0) % 2.0 - 1.0).abs());
let m = v - c;
let (r, g, b) = match (h / 60.0) as u32 {
0 => (c, x, 0.0),
1 => (x, c, 0.0),
2 => (0.0, c, x),
3 => (0.0, x, c),
4 => (x, 0.0, c),
_ => (c, 0.0, x),
};
let q = |f: f32| (f * 255.0).round().clamp(0.0, 255.0) as u8;
format!("#{:02x}{:02x}{:02x}", q(r + m), q(g + m), q(b + m))
}
fn ellipse_points(c: Pos2, rx: f32, ry: f32, rot: f32, n: usize) -> Vec<Pos2> {
let (s, co) = rot.sin_cos();
(0..n)
@ -99,14 +152,23 @@ pub struct Pose {
pub yaw: f32,
/// Nod: eyes drop a little.
pub pitch: f32,
/// Turn around the middle, degrees (the face spins slowly while the bot thinks).
pub spin: f32,
}
impl Default for Pose {
fn default() -> Self {
Pose { eyes: Eyes::Open, gaze: egui::Vec2::ZERO, blink: 0.0, squash: vec2(1.0, 1.0), lift: 0.0, yaw: 0.0, pitch: 0.0 }
Pose { eyes: Eyes::Open, gaze: egui::Vec2::ZERO, blink: 0.0, squash: vec2(1.0, 1.0), lift: 0.0, yaw: 0.0, pitch: 0.0, spin: 0.0 }
}
}
/// A point turned around a middle (the thinking spin).
fn spin_about(p: Pos2, c: Pos2, a: f32) -> Pos2 {
let d = p - c;
let (s, co) = a.sin_cos();
pos2(c.x + d.x * co - d.y * s, c.y + d.x * s + d.y * co)
}
/// A face with a pose: the body leans toward the gaze (the original turns it up to 26° sideways
/// and 19° up/down; drawn flat here as a shift), the eyes move further, blink and spin.
pub fn paint_posed(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32, pose: Pose) {
@ -120,7 +182,8 @@ pub fn paint_posed(painter: &egui::Painter, rect: Rect, shape: &str, col: Color3
let body = Rect::from_center_size(rect.center() + shift, vec2(rect.width() * sx, rect.height() * sy));
// Squash from the feet: keep the bottom where it was when it flattens.
let body = body.translate(vec2(0.0, (1.0 - sy) * rect.height() / 2.0));
paint_body(painter, body, shape, col);
let spin = pose.spin.to_radians();
paint_body(painter, body, shape, col, spin);
if turn <= 0.0 {
return;
}
@ -130,20 +193,21 @@ pub fn paint_posed(painter: &egui::Painter, rect: Rect, shape: &str, col: Color3
let open = (1.0 - pose.blink).clamp(0.06, 1.0);
let look = vec2(pose.gaze.x * 0.16 * s * turn, -pose.gaze.y * 0.12 * s + pose.pitch * 0.01 * s);
for dx in [-0.27, 0.27] {
let center = pos2(c.x + dx * body.width() / 2.0, c.y + 0.02 * bs) + look;
let center = spin_about(pos2(c.x + dx * body.width() / 2.0, c.y + 0.02 * bs) + look, c, spin);
match pose.eyes {
Eyes::Open => {
painter.add(Shape::convex_polygon(ellipse_points(center, s * 0.11 * sx, s * 0.22 * open * sy, -0.35, 20), eye, Stroke::NONE));
painter.add(Shape::convex_polygon(ellipse_points(center, s * 0.11 * sx, s * 0.22 * open * sy, -0.35 + spin, 20), eye, Stroke::NONE));
}
// An arc, open downward (a smiling eye).
Eyes::Smile => {
let (w, h) = (s * 0.17 * sx, s * 0.17 * sy);
let pts: Vec<Pos2> = (0..=12).map(|k| { let a = PI + k as f32 / 12.0 * PI; pos2(center.x + w * a.cos(), center.y + h * 0.5 + h * a.sin()) }).collect();
let pts: Vec<Pos2> = (0..=12).map(|k| { let a = PI + k as f32 / 12.0 * PI; spin_about(pos2(center.x + w * a.cos(), center.y + h * 0.5 + h * a.sin()), c, spin) }).collect();
painter.add(Shape::line(pts, Stroke::new((s * 0.1).max(1.5), eye)));
}
Eyes::Flat => {
let w = s * 0.17 * sx;
painter.add(Shape::line(vec![pos2(center.x - w, center.y), pos2(center.x + w, center.y)], Stroke::new((s * 0.1).max(1.5), eye)));
let (a, b) = (spin_about(pos2(center.x - w, center.y), c, spin), spin_about(pos2(center.x + w, center.y), c, spin));
painter.add(Shape::line(vec![a, b], Stroke::new((s * 0.1).max(1.5), eye)));
}
}
}
@ -154,7 +218,7 @@ fn eye_colour(col: Color32) -> Color32 {
}
pub fn paint(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32) {
paint_body(painter, rect, shape, col);
paint_body(painter, rect, shape, col, 0.0);
let c = rect.center();
let s = rect.width().min(rect.height()) / 2.0;
let eye = eye_colour(col);
@ -163,10 +227,12 @@ pub fn paint(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32) {
}
}
fn paint_body(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32) {
fn paint_body(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32, spin: f32) {
let c = rect.center();
let s = rect.width().min(rect.height()) / 2.0;
let body = |pts: Vec<Pos2>| Shape::convex_polygon(pts, col, Stroke::NONE);
// The whole body (every shape, every circle) turns around the middle while the bot thinks.
let body = |pts: Vec<Pos2>| Shape::convex_polygon(if spin == 0.0 { pts } else { pts.into_iter().map(|p| spin_about(p, c, spin)).collect() }, col, Stroke::NONE);
let cc = |p: Pos2| if spin == 0.0 { p } else { spin_about(p, c, spin) };
match shape {
"squircle" => painter.add(body(rounded_poly(Rect::from_center_size(c, vec2(2.0 * s, 2.0 * s) * 0.92), s * 0.42))),
"capsule" => painter.add(body(rounded_poly(Rect::from_center_size(c, vec2(1.35 * s, 2.0 * s)), s * 0.66))),
@ -199,13 +265,13 @@ fn paint_body(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32) {
"pebble" => painter.add(body(ellipse_points(c, s, s * 0.78, -0.25, 48))),
"cloud" => {
for (dx, dy, r) in [(-0.45, 0.15, 0.55), (0.45, 0.15, 0.55), (0.0, -0.2, 0.65), (0.0, 0.35, 0.55)] {
painter.circle_filled(pos2(c.x + dx * s, c.y + dy * s), r * s, col);
painter.circle_filled(cc(pos2(c.x + dx * s, c.y + dy * s)), r * s, col);
}
painter.add(Shape::Noop)
}
"heart" => {
painter.circle_filled(pos2(c.x - s * 0.45, c.y - s * 0.25), s * 0.55, col);
painter.circle_filled(pos2(c.x + s * 0.45, c.y - s * 0.25), s * 0.55, col);
painter.circle_filled(cc(pos2(c.x - s * 0.45, c.y - s * 0.25)), s * 0.55, col);
painter.circle_filled(cc(pos2(c.x + s * 0.45, c.y - s * 0.25)), s * 0.55, col);
painter.add(body(vec![pos2(c.x - s * 0.97, c.y - s * 0.1), pos2(c.x + s * 0.97, c.y - s * 0.1), pos2(c.x, c.y + s)]))
}
"star" => {
@ -215,7 +281,7 @@ fn paint_body(painter: &egui::Painter, rect: Rect, shape: &str, col: Color32) {
}
painter.add(body((0..10).filter(|i| i % 2 == 1).map(|i| pt(i, s * 0.5)).collect()))
}
_ => painter.add(Shape::convex_polygon(ellipse_points(c, rect.width() / 2.0 * 0.95, rect.height() / 2.0 * 0.95, 0.0, 48), col, Stroke::NONE)),
_ => painter.add(Shape::convex_polygon(ellipse_points(c, rect.width() / 2.0 * 0.95, rect.height() / 2.0 * 0.95, spin, 48), col, Stroke::NONE)),
};
}
@ -224,3 +290,22 @@ pub fn avatar(ui: &mut egui::Ui, size: f32, shape: &str, col: Color32) -> egui::
paint(ui.painter(), rect, shape, col);
resp
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn custom_hex_roundtrip() {
assert!(is_custom("#7a4bd6"));
assert!(!is_custom("blue"));
assert_eq!(color("#ff8000"), Color32::from_rgb(0xff, 0x80, 0x00));
// The picker's sliders are a stable round trip for every solid colour.
for name in ["#000000", "#ffffff", "#ff0000", "#00ff00", "#0000ff", "#7a4bd6", "#1c1c1c"] {
let hex = hex_of_hsv(hsv_of(name));
assert_eq!(color(&hex), color(name), "{name} -> {hex}");
}
// Presets answer too, so "make it custom" starts from the colour you had.
assert_eq!(color(&hex_of_hsv(hsv_of("blue"))), color("blue"));
}
}

View File

@ -24,6 +24,9 @@ pub struct Palette {
pub attention: Color32,
/// Behind a request's card.
pub attention_bg: Color32,
/// The quiet fill behind secondary blocks: filled inputs, list trays, segmented tracks
/// (the shadcn 'muted' layer — it is what makes groups read as groups).
pub muted: Color32,
}
const fn hexa(r: u8, g: u8, b: u8, a: u8) -> Color32 {
@ -48,6 +51,7 @@ pub const LIGHT: Palette = Palette {
danger: hexa(0xcd, 0x28, 0x34, 0xff),
attention: hexa(0xff, 0xc8, 0x3d, 0xff),
attention_bg: hexa(0xff, 0xf6, 0xd6, 0xff),
muted: hexa(0xf1, 0xf1, 0xee, 0xff),
};
// From the original's renderer (index.eager-common: appearance primitives dark neutral/0..6 and the
@ -73,6 +77,7 @@ pub const DARK: Palette = Palette {
danger: hexa(0xfc, 0x2c, 0x3e, 0xff),
attention: hexa(0xff, 0xc8, 0x3d, 0xff),
attention_bg: hexa(0x3a, 0x32, 0x12, 0xff),
muted: hexa(0x1d, 0x1d, 0x1d, 0xff),
};
/// A bot's colour as a quiet background (its selected tile, its cards): a little of it over the