Merge pull request #519 from luniiya/feat/system-appearance

This commit is contained in:
Claude
2026-10-10 12:53:19 +00:00
15 changed files with 686 additions and 19 deletions
+166 -2
View File
@@ -8,12 +8,12 @@
//! `settings.cancel`, `settings.previous`, `settings.next`.
use crate::i18n::{tr, tr_args};
use effectcraft_engine::prefs::{Item, Kind, Page, pages};
use effectcraft_engine::prefs::{APPEARANCE_MODES, DARK_THEMES, Item, Kind, LIGHT_THEMES, Page, pages};
use effectcraft_engine::segment::Task;
use egui::{Color32, RichText, vec2};
use serde_json::{Value, json};
use crate::theme::Tokens;
use crate::theme::{ThemeKind, Tokens};
use crate::{Dialog, EffectcraftApp};
/// Open the dialog on a page, remembering the settings for Cancel.
@@ -88,6 +88,10 @@ fn page_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, page: &Page, cur: &Value
ui.label(RichText::new(tr("No device list from this host; the system default output is used.")).color(t.text_dim));
}
}
// Appearance Mode with the light and dark theme cards (the per-mode themes are
// chosen on the cards).
Item::Setting { key: "appearance.appearanceMode", .. } => appearance_ui(app, ui, cur, t, acts),
Item::Setting { key: "appearance.lightTheme" | "appearance.darkTheme", .. } => {}
Item::Setting { key, label, kind, live } => {
let v = get(key);
let id = format!("settings.{key}");
@@ -201,6 +205,166 @@ fn page_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, page: &Page, cur: &Value
}
}
/// A small EffectCraft window painted with theme `kind`'s tokens: the Tools bar, the Project
/// panel, the Composition panel, the Preview and Properties column and the Timeline with its
/// layer bars.
fn theme_preview(ui: &mut egui::Ui, kind: ThemeKind, width: f32) {
use egui::{Rect, pos2};
let p = Tokens::for_kind(kind);
let (rect, _) = ui.allocate_exact_size(vec2(width, 118.0), egui::Sense::hover());
let painter = ui.painter_at(rect);
painter.rect_filled(rect, 3.0, p.app_bg);
let bar = Rect::from_min_size(rect.min, vec2(rect.width(), 13.0));
painter.rect_filled(bar, 0.0, p.header_bg);
// The brand mark, the tools (the Selection tool is active) and the Discord pill.
painter.rect_filled(Rect::from_min_size(bar.min + vec2(4.0, 3.0), vec2(7.0, 7.0)), 2.0, p.accent);
for i in 0..7 {
let c = if i == 0 { p.accent } else { p.icon };
painter.rect_filled(Rect::from_min_size(bar.min + vec2(16.0 + i as f32 * 8.0, 4.0), vec2(5.0, 5.0)), 1.0, c);
}
painter.rect_filled(
Rect::from_min_max(pos2(bar.right() - 22.0, bar.top() + 3.0), pos2(bar.right() - 4.0, bar.bottom() - 3.0)),
3.5,
egui::Color32::from_rgb(0x58, 0x65, 0xf2),
);
let gap = 2.0;
let body = Rect::from_min_max(pos2(rect.left() + gap, bar.bottom() + gap), pos2(rect.right() - gap, rect.bottom() - gap));
let right_w = (body.width() * 0.22).round();
let timeline_h = (body.height() * 0.42).round();
let left_w = (body.width() * 0.2).round();
let upper = Rect::from_min_max(body.min, pos2(body.right() - right_w - gap, body.bottom() - timeline_h - gap));
let panel = |r: Rect, title_w: f32| {
painter.rect_filled(r, 2.0, p.panel_bg);
painter.rect_filled(Rect::from_min_size(r.min + vec2(4.0, 3.0), vec2(title_w, 2.0)), 1.0, p.tab_text_active);
};
// Project panel: rows with label swatches.
let project = Rect::from_min_max(upper.min, pos2(upper.left() + left_w, upper.bottom()));
panel(project, 14.0);
for (i, label) in [5usize, 5, 2, 2].into_iter().enumerate() {
let y = project.top() + 10.0 + i as f32 * 6.0;
painter.rect_filled(Rect::from_min_size(pos2(project.left() + 4.0, y), vec2(3.0, 3.0)), 0.5, p.labels.get(label).copied().unwrap_or(p.icon));
painter.rect_filled(Rect::from_min_size(pos2(project.left() + 9.0, y + 0.5), vec2(project.width() * 0.45, 2.0)), 1.0, p.text_dim);
}
// Composition panel: the pasteboard and the comp frame with a title.
let comp = Rect::from_min_max(pos2(project.right() + gap, upper.top()), upper.max);
panel(comp, 22.0);
let paste = Rect::from_min_max(comp.min + vec2(2.0, 8.0), comp.max - vec2(2.0, 2.0));
painter.rect_filled(paste, 0.0, p.pasteboard);
let frame_h = (paste.height() - 6.0).max(4.0);
let frame = Rect::from_center_size(paste.center(), vec2((frame_h * 16.0 / 9.0).min(paste.width() - 6.0), frame_h));
painter.rect_filled(frame, 0.0, egui::Color32::from_rgb(0x14, 0x1c, 0x4a));
painter.circle_stroke(frame.center(), frame.height() * 0.36, egui::Stroke::new(0.8, egui::Color32::from_rgb(0x4a, 0x7c, 0xe8)));
painter.rect_filled(Rect::from_center_size(frame.center(), vec2(frame.width() * 0.5, 2.5)), 1.0, egui::Color32::WHITE);
// Preview and Properties.
let column = Rect::from_min_max(pos2(body.right() - right_w, body.top()), body.max);
let preview = Rect::from_min_max(column.min, pos2(column.right(), column.top() + column.height() * 0.3));
panel(preview, 12.0);
painter.rect_filled(Rect::from_min_size(preview.left_bottom() + vec2(4.0, -5.0), vec2(preview.width() * 0.25, 1.5)), 0.5, p.cache_green);
let props = Rect::from_min_max(pos2(column.left(), preview.bottom() + gap), column.max);
panel(props, 18.0);
for i in 0..5 {
let y = props.top() + 10.0 + i as f32 * 6.0;
painter.rect_filled(Rect::from_min_size(pos2(props.left() + 4.0, y), vec2(props.width() * 0.35, 2.0)), 1.0, p.text_dim);
painter.rect_filled(Rect::from_min_size(pos2(props.left() + props.width() * 0.55, y), vec2(props.width() * 0.25, 2.0)), 1.0, p.hot_text);
}
// Timeline: the layer list and the time graph, with coloured layer bars and the time marker.
let timeline = Rect::from_min_max(pos2(body.left(), upper.bottom() + gap), pos2(upper.right(), body.bottom()));
panel(timeline, 18.0);
painter.rect_filled(Rect::from_min_size(timeline.min + vec2(4.0, 8.0), vec2(14.0, 3.0)), 1.0, p.timecode);
let graph = Rect::from_min_max(pos2(timeline.left() + timeline.width() * 0.38, timeline.top() + 8.0), timeline.max - vec2(2.0, 2.0));
painter.rect_filled(graph, 0.0, p.tl_bg);
painter.rect_filled(Rect::from_min_size(graph.min, vec2(graph.width(), 4.0)), 0.0, p.tl_ruler_bg);
for (i, label) in [1usize, 1, 8, 8, 1].into_iter().enumerate() {
let y = graph.top() + 7.0 + i as f32 * 5.5;
if y + 3.5 > graph.bottom() {
break;
}
let swatch = p.labels.get(label).copied().unwrap_or(p.icon);
let row_l = timeline.left() + 4.0;
if i == 1 {
painter.rect_filled(Rect::from_min_max(pos2(timeline.left(), y - 1.0), pos2(graph.left(), y + 4.5)), 0.0, p.row_selected);
}
painter.rect_filled(Rect::from_min_size(pos2(row_l, y), vec2(3.0, 3.0)), 0.5, swatch);
painter.rect_filled(Rect::from_min_size(pos2(row_l + 6.0, y + 0.5), vec2(graph.left() - row_l - 14.0, 2.0)), 1.0, p.text_dim);
let bar = Rect::from_min_max(pos2(graph.left() + 1.0, y), pos2(graph.right() - 1.0, y + 3.5));
painter.rect_filled(bar, 1.0, swatch.gamma_multiply(0.8));
}
let cti = graph.left() + graph.width() * 0.3;
painter.line_segment([pos2(cti, graph.top()), pos2(cti, graph.bottom())], egui::Stroke::new(1.0, p.cti));
}
/// One theme family's card: its (translated) name, Active when the appearance shows it now, a preview of the
/// chosen theme and a radio button per theme.
fn theme_card(
app: &mut EffectcraftApp,
ui: &mut egui::Ui,
t: &Tokens,
key: &str,
title: &str,
active: bool,
cur: &str,
options: &[(&str, &str)],
acts: &mut Vec<Act>,
) {
egui::Frame::new()
.fill(t.field_bg)
.stroke(egui::Stroke::new(1.0, if active { t.accent } else { t.field_border }))
.corner_radius(6.0)
.inner_margin(egui::Margin::same(8))
.show(ui, |ui| {
ui.set_width(ui.available_width());
ui.horizontal(|ui| {
ui.label(RichText::new(title).font(Tokens::semibold(12.5)).color(t.text));
if active {
ui.label(RichText::new(tr("Active")).color(t.accent).small());
}
});
ui.add_space(4.0);
let kind = ThemeKind::from_name(cur).unwrap_or(if key.ends_with("lightTheme") { ThemeKind::Light } else { ThemeKind::Dark });
theme_preview(ui, kind, ui.available_width());
ui.add_space(4.0);
for (label, value) in options {
let r = ui.radio(cur == *value, tr(label));
reg(app, &format!("settings.{key}.{value}"), &r, label);
if r.clicked() && cur != *value {
acts.push(Act::Set(key.into(), json!(value)));
}
}
});
}
/// Settings ▸ Appearance: Appearance Mode (Sync with System, Dark, Light) above the light and dark
/// theme cards.
fn appearance_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, cur: &Value, t: &Tokens, acts: &mut Vec<Act>) {
let get = |key: &str| cur.pointer(&format!("/appearance/{key}")).and_then(Value::as_str).unwrap_or_default().to_string();
let (mode, light, dark) = (get("appearanceMode"), get("lightTheme"), get("darkTheme"));
ui.horizontal(|ui| {
ui.add_sized(vec2(250.0, 18.0), egui::Label::new(format!("{}:", tr("Appearance Mode"))).truncate());
let shown = APPEARANCE_MODES.iter().find(|(_, v)| *v == mode).map_or(mode.as_str(), |(l, _)| *l);
let r = egui::ComboBox::from_id_salt(("settings", "appearance.appearanceMode"))
.selected_text(tr(shown))
.width(220.0)
.show_ui(ui, |ui| {
for (l, v) in APPEARANCE_MODES {
if ui.selectable_label(*v == mode, tr(l)).clicked() {
acts.push(Act::Set("appearance.appearanceMode".into(), json!(v)));
}
}
})
.response;
reg(app, "settings.appearance.appearanceMode", &r, "Appearance Mode");
});
ui.add_space(6.0);
let system_light = app.system_light(ui.ctx());
let light_active = mode == "light" || (mode == "auto" && system_light == Some(true));
ui.columns(2, |cols| {
let [l, d] = cols else { return };
theme_card(app, l, t, "appearance.lightTheme", tr("Light Theme"), light_active, &light, LIGHT_THEMES, acts);
theme_card(app, d, t, "appearance.darkTheme", tr("Dark Theme"), !light_active, &dark, DARK_THEMES, acts);
});
ui.add_space(6.0);
}
/// Bytes as "12.3 MB".
pub fn human_bytes(b: u64) -> String {
let units = ["bytes", "KB", "MB", "GB", "TB"];