mirror of
https://github.com/storytold/soundcraft.git
synced 2026-12-01 18:26:25 -06:00
624 lines
26 KiB
Rust
624 lines
26 KiB
Rust
//! Design tokens. Colours were chosen by eye to give a dark, studio-style look; they are ours.
|
|
|
|
use egui::{Color32, FontFamily, FontId};
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
/// Whether the Light palette is active. Dark is the default (the incumbent is dark-only).
|
|
static LIGHT_THEME: AtomicBool = AtomicBool::new(false);
|
|
|
|
/// Unit tests that change or read the process-wide palette must own it until their assertions
|
|
/// finish. The lock is re-entrant per thread, so a test may hold it and still build apps
|
|
/// (`SoundApp::new` takes it too) without deadlocking itself.
|
|
#[cfg(test)]
|
|
pub(crate) fn lock_test_palette() -> TestPaletteGuard {
|
|
TestPaletteGuard::acquire()
|
|
}
|
|
|
|
/// RAII handle for the re-entrant, per-thread test palette lock; see [`lock_test_palette`].
|
|
#[cfg(test)]
|
|
pub(crate) struct TestPaletteGuard;
|
|
|
|
#[cfg(test)]
|
|
impl TestPaletteGuard {
|
|
fn acquire() -> Self {
|
|
palette_lock::acquire();
|
|
Self
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
impl Drop for TestPaletteGuard {
|
|
fn drop(&mut self) {
|
|
palette_lock::release();
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod palette_lock {
|
|
use std::cell::{Cell, RefCell};
|
|
use std::sync::{Mutex, MutexGuard, PoisonError};
|
|
|
|
static PALETTE: Mutex<()> = Mutex::new(());
|
|
|
|
thread_local! {
|
|
/// How many guards this thread holds; the OS lock is taken on 0→1 and released on 1→0.
|
|
static DEPTH: Cell<usize> = const { Cell::new(0) };
|
|
static GUARD: RefCell<Option<MutexGuard<'static, ()>>> = const { RefCell::new(None) };
|
|
}
|
|
|
|
pub(super) fn acquire() {
|
|
DEPTH.with(|depth| {
|
|
if depth.get() == 0 {
|
|
GUARD.with(|guard| *guard.borrow_mut() = Some(PALETTE.lock().unwrap_or_else(PoisonError::into_inner)));
|
|
}
|
|
depth.set(depth.get() + 1);
|
|
});
|
|
}
|
|
|
|
pub(super) fn release() {
|
|
DEPTH.with(|depth| {
|
|
let left = depth.get().saturating_sub(1);
|
|
depth.set(left);
|
|
if left == 0 {
|
|
GUARD.with(|guard| *guard.borrow_mut() = None);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/// The Appearance setting (`ui.theme {mode}`). Dark by default; System and Light are opt-in.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
#[serde(rename_all = "lowercase")]
|
|
pub enum ThemeMode {
|
|
System,
|
|
Light,
|
|
#[default]
|
|
Dark,
|
|
}
|
|
|
|
impl ThemeMode {
|
|
pub const ALL: [Self; 3] = [Self::Dark, Self::Light, Self::System];
|
|
|
|
pub const fn label(self) -> &'static str {
|
|
match self {
|
|
Self::System => "System",
|
|
Self::Light => "Light",
|
|
Self::Dark => "Dark",
|
|
}
|
|
}
|
|
|
|
/// The id used in `ui.theme {"mode": …}` and in the saved UI prefs.
|
|
pub const fn id(self) -> &'static str {
|
|
match self {
|
|
Self::System => "system",
|
|
Self::Light => "light",
|
|
Self::Dark => "dark",
|
|
}
|
|
}
|
|
|
|
pub fn parse(s: &str) -> Option<Self> {
|
|
Self::ALL.into_iter().find(|m| m.id().eq_ignore_ascii_case(s.trim()))
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
pub struct Tokens {
|
|
pub window_bg: Color32,
|
|
pub panel_bg: Color32,
|
|
pub panel_bg2: Color32,
|
|
pub toolbar_bg: Color32,
|
|
pub toolbar_group: Color32,
|
|
pub border: Color32,
|
|
pub border_light: Color32,
|
|
/// The lit top/left edge of raised panels: track headers and Mix strips.
|
|
pub edge_light: Color32,
|
|
pub text: Color32,
|
|
pub text_dim: Color32,
|
|
pub text_dark: Color32,
|
|
pub header_text: Color32,
|
|
pub counter_bg: Color32,
|
|
pub counter_text: Color32,
|
|
pub counter_label: Color32,
|
|
pub accent: Color32,
|
|
pub accent_dark: Color32,
|
|
pub mode_on: Color32,
|
|
pub mode_on_text: Color32,
|
|
pub mode_off_text: Color32,
|
|
pub button: Color32,
|
|
pub button_hi: Color32,
|
|
pub button_border: Color32,
|
|
pub name_field: Color32,
|
|
pub name_field_sel: Color32,
|
|
pub ruler_bg: Color32,
|
|
pub ruler_label_bg: Color32,
|
|
pub ruler_text: Color32,
|
|
pub ruler_tick: Color32,
|
|
pub tempo_ruler: Color32,
|
|
pub meter_ruler: Color32,
|
|
pub marker_ruler: Color32,
|
|
pub playlist_bg: Color32,
|
|
pub playlist_alt: Color32,
|
|
pub grid_line: Color32,
|
|
pub bar_line: Color32,
|
|
pub selection: Color32,
|
|
pub playhead: Color32,
|
|
pub insertion: Color32,
|
|
pub solo: Color32,
|
|
pub mute: Color32,
|
|
pub rec: Color32,
|
|
pub input: Color32,
|
|
pub auto_read: Color32,
|
|
pub auto_write: Color32,
|
|
pub auto_touch: Color32,
|
|
pub auto_latch: Color32,
|
|
pub meter_green: Color32,
|
|
pub meter_yellow: Color32,
|
|
pub meter_red: Color32,
|
|
pub fader_track: Color32,
|
|
pub strip_bg: Color32,
|
|
pub strip_section: Color32,
|
|
pub slot_bg: Color32,
|
|
pub automation_line: Color32,
|
|
/// Menu bar background.
|
|
pub menu_bar: Color32,
|
|
/// Outline of toolbar groups.
|
|
pub group_border: Color32,
|
|
/// Edit-mode cell, not selected.
|
|
pub mode_off: Color32,
|
|
/// Counter-panel toggle (Grid, Count Off), off.
|
|
pub counter_off: Color32,
|
|
/// Outline of the counter panels.
|
|
pub counter_border: Color32,
|
|
/// Divider inside the counter panels.
|
|
pub counter_rule: Color32,
|
|
/// Smart-tool bracket, off.
|
|
pub tool_bracket_off: Color32,
|
|
/// Selected row in the Tracks, Groups and Clips lists.
|
|
pub row_selected: Color32,
|
|
/// Track-visible dot in the Tracks list.
|
|
pub visible_dot: Color32,
|
|
/// Big Counter background.
|
|
pub big_counter_bg: Color32,
|
|
/// Edit-window track header, selected.
|
|
pub header_selected: Color32,
|
|
/// Mix-window strip, selected.
|
|
pub strip_selected: Color32,
|
|
/// Marker names on the marker ruler.
|
|
pub marker_text: Color32,
|
|
/// Alternate-playlist lanes.
|
|
pub playlist_lane: Color32,
|
|
/// Universe overview background.
|
|
pub universe_bg: Color32,
|
|
/// Universe visible-area outline.
|
|
pub universe_view: Color32,
|
|
/// Meter background.
|
|
pub meter_bg: Color32,
|
|
/// Meter clip indicator, not clipped.
|
|
pub meter_clip_off: Color32,
|
|
/// Pan knob body.
|
|
pub knob_bg: Color32,
|
|
/// Pan knob outline.
|
|
pub knob_ring: Color32,
|
|
/// Pan knob pointer.
|
|
pub knob_pointer: Color32,
|
|
/// Mix-window section captions (INSERTS A-E, I/O…).
|
|
pub section_label: Color32,
|
|
/// Plugin slots, as Pro Tools draws them: the well they sit in, an empty slot, a used slot's
|
|
/// face (top, bottom), a bypassed one's, and the dots, letters and arrows on them.
|
|
pub slot_well: Color32,
|
|
pub slot_empty: Color32,
|
|
pub slot_face: [Color32; 2],
|
|
pub slot_bypassed: [Color32; 2],
|
|
pub slot_mark: Color32,
|
|
}
|
|
|
|
impl Tokens {
|
|
pub const DARK: Tokens = Tokens {
|
|
window_bg: Color32::from_rgb(30, 30, 30),
|
|
panel_bg: Color32::from_rgb(37, 37, 38),
|
|
panel_bg2: Color32::from_rgb(44, 44, 45),
|
|
toolbar_bg: Color32::from_rgb(30, 30, 31),
|
|
toolbar_group: Color32::from_rgb(24, 24, 25),
|
|
border: Color32::from_rgb(14, 14, 14),
|
|
border_light: Color32::from_rgb(64, 64, 66),
|
|
edge_light: Color32::from_rgba_premultiplied(20, 20, 20, 20),
|
|
text: Color32::from_rgb(214, 214, 214),
|
|
text_dim: Color32::from_rgb(150, 150, 152),
|
|
text_dark: Color32::from_rgb(16, 16, 16),
|
|
header_text: Color32::from_rgb(196, 196, 198),
|
|
counter_bg: Color32::from_rgb(4, 4, 4),
|
|
counter_text: Color32::from_rgb(104, 220, 120),
|
|
counter_label: Color32::from_rgb(200, 200, 200),
|
|
accent: Color32::from_rgb(64, 132, 196),
|
|
accent_dark: Color32::from_rgb(40, 92, 146),
|
|
mode_on: Color32::from_rgb(92, 196, 108),
|
|
mode_on_text: Color32::from_rgb(10, 30, 12),
|
|
mode_off_text: Color32::from_rgb(98, 196, 112),
|
|
button: Color32::from_rgb(58, 58, 60),
|
|
button_hi: Color32::from_rgb(78, 78, 80),
|
|
button_border: Color32::from_rgb(20, 20, 20),
|
|
name_field: Color32::from_rgb(208, 208, 208),
|
|
name_field_sel: Color32::from_rgb(232, 232, 232),
|
|
ruler_bg: Color32::from_rgb(40, 40, 41),
|
|
ruler_label_bg: Color32::from_rgb(40, 40, 41),
|
|
ruler_text: Color32::from_rgb(178, 178, 180),
|
|
ruler_tick: Color32::from_rgb(96, 96, 98),
|
|
tempo_ruler: Color32::from_rgb(52, 128, 88),
|
|
meter_ruler: Color32::from_rgb(44, 108, 140),
|
|
marker_ruler: Color32::from_rgb(46, 46, 48),
|
|
playlist_bg: Color32::from_rgb(36, 36, 37),
|
|
playlist_alt: Color32::from_rgb(40, 40, 41),
|
|
grid_line: Color32::from_rgb(52, 52, 54),
|
|
bar_line: Color32::from_rgb(66, 66, 70),
|
|
selection: Color32::from_rgba_premultiplied(40, 70, 110, 110),
|
|
playhead: Color32::from_rgb(232, 60, 50),
|
|
insertion: Color32::from_rgb(230, 230, 230),
|
|
solo: Color32::from_rgb(222, 196, 52),
|
|
mute: Color32::from_rgb(232, 148, 40),
|
|
rec: Color32::from_rgb(214, 52, 46),
|
|
input: Color32::from_rgb(70, 170, 90),
|
|
auto_read: Color32::from_rgb(96, 200, 110),
|
|
auto_write: Color32::from_rgb(220, 70, 60),
|
|
auto_touch: Color32::from_rgb(230, 170, 50),
|
|
auto_latch: Color32::from_rgb(160, 120, 220),
|
|
meter_green: Color32::from_rgb(60, 200, 80),
|
|
meter_yellow: Color32::from_rgb(230, 210, 60),
|
|
meter_red: Color32::from_rgb(230, 50, 40),
|
|
fader_track: Color32::from_rgb(12, 12, 12),
|
|
strip_bg: Color32::from_rgb(46, 46, 48),
|
|
strip_section: Color32::from_rgb(36, 36, 38),
|
|
slot_bg: Color32::from_rgb(28, 28, 29),
|
|
automation_line: Color32::from_rgb(240, 240, 240),
|
|
menu_bar: Color32::from_rgb(22, 22, 23),
|
|
group_border: Color32::from_rgb(8, 8, 8),
|
|
mode_off: Color32::from_rgb(20, 36, 22),
|
|
counter_off: Color32::from_rgb(30, 50, 32),
|
|
counter_border: Color32::from_rgb(50, 50, 50),
|
|
counter_rule: Color32::from_rgb(40, 40, 40),
|
|
tool_bracket_off: Color32::from_rgb(64, 64, 66),
|
|
row_selected: Color32::from_rgb(52, 70, 96),
|
|
visible_dot: Color32::from_rgb(200, 200, 200),
|
|
big_counter_bg: Color32::from_rgb(0, 0, 0),
|
|
header_selected: Color32::from_rgb(52, 58, 66),
|
|
strip_selected: Color32::from_rgb(54, 58, 64),
|
|
marker_text: Color32::from_rgb(236, 236, 236),
|
|
playlist_lane: Color32::from_rgb(30, 30, 31),
|
|
universe_bg: Color32::from_rgb(18, 18, 19),
|
|
universe_view: Color32::from_rgb(255, 255, 255),
|
|
meter_bg: Color32::from_rgb(8, 8, 8),
|
|
meter_clip_off: Color32::from_rgb(60, 20, 20),
|
|
knob_bg: Color32::from_rgb(26, 26, 28),
|
|
knob_ring: Color32::from_rgb(150, 150, 154),
|
|
knob_pointer: Color32::from_rgb(230, 230, 230),
|
|
section_label: Color32::from_rgb(200, 200, 200),
|
|
slot_well: Color32::from_rgb(43, 43, 43),
|
|
slot_empty: Color32::from_rgb(39, 39, 39),
|
|
slot_face: [Color32::from_rgb(92, 92, 92), Color32::from_rgb(75, 75, 75)],
|
|
slot_bypassed: [Color32::from_rgb(66, 96, 146), Color32::from_rgb(50, 76, 120)],
|
|
slot_mark: Color32::from_rgb(159, 159, 159),
|
|
};
|
|
|
|
pub const LIGHT: Tokens = Tokens {
|
|
window_bg: Color32::from_rgb(184, 186, 190),
|
|
panel_bg: Color32::from_rgb(204, 206, 210),
|
|
panel_bg2: Color32::from_rgb(216, 218, 222),
|
|
toolbar_bg: Color32::from_rgb(194, 196, 200),
|
|
toolbar_group: Color32::from_rgb(208, 210, 214),
|
|
border: Color32::from_rgb(100, 103, 109),
|
|
border_light: Color32::from_rgb(150, 153, 159),
|
|
edge_light: Color32::from_rgba_premultiplied(150, 150, 150, 150),
|
|
text: Color32::from_rgb(24, 25, 28),
|
|
text_dim: Color32::from_rgb(68, 70, 76),
|
|
text_dark: Color32::from_rgb(16, 17, 20),
|
|
header_text: Color32::from_rgb(30, 32, 36),
|
|
counter_bg: Color32::from_rgb(224, 226, 228),
|
|
counter_text: Color32::from_rgb(18, 108, 40),
|
|
counter_label: Color32::from_rgb(54, 57, 63),
|
|
accent: Color32::from_rgb(62, 116, 180),
|
|
accent_dark: Color32::from_rgb(43, 88, 145),
|
|
mode_on: Color32::from_rgb(75, 158, 88),
|
|
mode_on_text: Color32::from_rgb(255, 255, 255),
|
|
mode_off_text: Color32::from_rgb(38, 106, 52),
|
|
button: Color32::from_rgb(214, 216, 220),
|
|
button_hi: Color32::from_rgb(200, 203, 208),
|
|
button_border: Color32::from_rgb(108, 111, 117),
|
|
name_field: Color32::from_rgb(236, 237, 239),
|
|
name_field_sel: Color32::from_rgb(224, 232, 244),
|
|
ruler_bg: Color32::from_rgb(198, 200, 204),
|
|
ruler_label_bg: Color32::from_rgb(198, 200, 204),
|
|
ruler_text: Color32::from_rgb(48, 50, 56),
|
|
ruler_tick: Color32::from_rgb(108, 111, 118),
|
|
tempo_ruler: Color32::from_rgb(92, 156, 112),
|
|
meter_ruler: Color32::from_rgb(80, 144, 172),
|
|
marker_ruler: Color32::from_rgb(186, 189, 194),
|
|
playlist_bg: Color32::from_rgb(210, 212, 216),
|
|
playlist_alt: Color32::from_rgb(202, 204, 208),
|
|
grid_line: Color32::from_rgb(188, 191, 196),
|
|
bar_line: Color32::from_rgb(162, 166, 172),
|
|
selection: Color32::from_rgba_premultiplied(100, 145, 205, 90),
|
|
playhead: Color32::from_rgb(210, 48, 42),
|
|
insertion: Color32::from_rgb(35, 36, 40),
|
|
solo: Color32::from_rgb(178, 145, 20),
|
|
mute: Color32::from_rgb(194, 106, 22),
|
|
rec: Color32::from_rgb(190, 42, 38),
|
|
input: Color32::from_rgb(45, 126, 64),
|
|
auto_read: Color32::from_rgb(52, 142, 68),
|
|
auto_write: Color32::from_rgb(190, 50, 45),
|
|
auto_touch: Color32::from_rgb(180, 122, 24),
|
|
auto_latch: Color32::from_rgb(118, 80, 170),
|
|
meter_green: Color32::from_rgb(36, 160, 58),
|
|
meter_yellow: Color32::from_rgb(190, 162, 20),
|
|
meter_red: Color32::from_rgb(195, 42, 35),
|
|
fader_track: Color32::from_rgb(120, 123, 129),
|
|
strip_bg: Color32::from_rgb(198, 200, 204),
|
|
strip_section: Color32::from_rgb(210, 212, 216),
|
|
slot_bg: Color32::from_rgb(194, 196, 201),
|
|
automation_line: Color32::from_rgb(30, 32, 38),
|
|
menu_bar: Color32::from_rgb(204, 206, 210),
|
|
group_border: Color32::from_rgb(118, 121, 127),
|
|
mode_off: Color32::from_rgb(202, 212, 204),
|
|
counter_off: Color32::from_rgb(192, 212, 198),
|
|
counter_border: Color32::from_rgb(118, 121, 127),
|
|
counter_rule: Color32::from_rgb(188, 191, 196),
|
|
tool_bracket_off: Color32::from_rgb(172, 175, 181),
|
|
row_selected: Color32::from_rgb(174, 196, 226),
|
|
visible_dot: Color32::from_rgb(86, 89, 96),
|
|
big_counter_bg: Color32::from_rgb(224, 226, 228),
|
|
header_selected: Color32::from_rgb(184, 200, 224),
|
|
strip_selected: Color32::from_rgb(184, 200, 224),
|
|
marker_text: Color32::from_rgb(24, 25, 28),
|
|
playlist_lane: Color32::from_rgb(200, 202, 206),
|
|
universe_bg: Color32::from_rgb(198, 200, 204),
|
|
universe_view: Color32::from_rgb(40, 42, 48),
|
|
meter_bg: Color32::from_rgb(52, 54, 58),
|
|
meter_clip_off: Color32::from_rgb(92, 54, 54),
|
|
knob_bg: Color32::from_rgb(232, 233, 236),
|
|
knob_ring: Color32::from_rgb(116, 120, 128),
|
|
knob_pointer: Color32::from_rgb(30, 32, 38),
|
|
section_label: Color32::from_rgb(56, 58, 64),
|
|
slot_well: Color32::from_rgb(212, 214, 218),
|
|
slot_empty: Color32::from_rgb(198, 200, 205),
|
|
slot_face: [Color32::from_rgb(244, 245, 247), Color32::from_rgb(214, 216, 220)],
|
|
slot_bypassed: [Color32::from_rgb(190, 210, 240), Color32::from_rgb(160, 186, 228)],
|
|
slot_mark: Color32::from_rgb(92, 95, 102),
|
|
};
|
|
|
|
pub fn current() -> Tokens {
|
|
if is_light() { Self::LIGHT } else { Self::DARK }
|
|
}
|
|
}
|
|
|
|
/// Whether the Light palette is the active one.
|
|
pub fn is_light() -> bool {
|
|
LIGHT_THEME.load(Ordering::Relaxed)
|
|
}
|
|
|
|
/// Resolve each frame from the integration's current OS appearance, or on Linux the desktop's own
|
|
/// setting when egui reports none. Unknown uses Dark.
|
|
pub fn wants_light(ctx: &egui::Context, mode: ThemeMode) -> bool {
|
|
match mode {
|
|
ThemeMode::Light => true,
|
|
ThemeMode::Dark => false,
|
|
ThemeMode::System => crate::appearance::resolve(ctx.system_theme(), || crate::appearance::desktop_theme(ctx)) == Some(egui::Theme::Light),
|
|
}
|
|
}
|
|
|
|
/// A track's header and lane backgrounds in the Edit window, tinted with its colour by `tint` (0..1).
|
|
pub fn track_backgrounds(color: [u8; 3], selected: bool, tint: f32) -> (Color32, Color32) {
|
|
let t = Tokens::current();
|
|
let k = tint.clamp(0.0, 1.0);
|
|
if k <= 0.0 {
|
|
return if selected { (t.header_selected, t.playlist_alt) } else { (t.panel_bg2, t.playlist_bg) };
|
|
}
|
|
let c = rgb(color);
|
|
let header = t.panel_bg2.lerp_to_gamma(c, k * 0.55);
|
|
let header = if selected { crate::widgets::shade(header, 0.1) } else { header };
|
|
let lane = if selected { t.playlist_alt } else { t.playlist_bg }.lerp_to_gamma(c, k * 0.2);
|
|
(header, lane)
|
|
}
|
|
|
|
/// Track colours tinted for clip bodies.
|
|
pub fn clip_colors(rgb: [u8; 3], selected: bool) -> (Color32, Color32, Color32) {
|
|
let [r, g, b] = rgb;
|
|
let shade = |k: f32| Color32::from_rgb((f32::from(r) * k) as u8, (f32::from(g) * k) as u8, (f32::from(b) * k) as u8);
|
|
let tint = |k: f32| {
|
|
let t = |c: u8| (f32::from(c) + (255.0 - f32::from(c)) * k) as u8;
|
|
Color32::from_rgb(t(r), t(g), t(b))
|
|
};
|
|
let bar = shade(0.9);
|
|
if is_light() {
|
|
// A paler body and a waveform in a deep shade of the clip's own colour.
|
|
return if selected { (tint(0.2), bar, shade(0.22)) } else { (tint(0.45), bar, shade(0.32)) };
|
|
}
|
|
// A dark body and a light waveform in the clip's own colour; selection inverts them.
|
|
if selected { (shade(0.7), bar, Color32::from_rgb(16, 16, 18)) } else { (shade(0.34), bar, tint(0.35)) }
|
|
}
|
|
|
|
pub fn rgb(c: [u8; 3]) -> Color32 {
|
|
Color32::from_rgb(c[0], c[1], c[2])
|
|
}
|
|
|
|
pub fn bold(size: f32) -> FontId {
|
|
FontId::new(size, FontFamily::Name("bold".into()))
|
|
}
|
|
|
|
pub fn regular(size: f32) -> FontId {
|
|
FontId::new(size, FontFamily::Proportional)
|
|
}
|
|
|
|
pub fn mono(size: f32) -> FontId {
|
|
FontId::new(size, FontFamily::Monospace)
|
|
}
|
|
|
|
/// Make `light` (or dark) the active palette and apply its visuals. Call only when it changes.
|
|
pub fn apply(ctx: &egui::Context, light: bool) {
|
|
LIGHT_THEME.store(light, Ordering::Relaxed);
|
|
let t = Tokens::current();
|
|
let mut v = if light { egui::Visuals::light() } else { egui::Visuals::dark() };
|
|
v.panel_fill = t.panel_bg;
|
|
v.window_fill = t.panel_bg2;
|
|
v.extreme_bg_color = t.slot_bg;
|
|
v.faint_bg_color = t.panel_bg2;
|
|
v.selection.bg_fill = t.accent;
|
|
v.widgets.inactive.weak_bg_fill = t.button;
|
|
v.widgets.inactive.bg_fill = t.button;
|
|
v.widgets.hovered.weak_bg_fill = t.button_hi;
|
|
v.widgets.noninteractive.fg_stroke.color = t.text;
|
|
v.widgets.inactive.fg_stroke.color = t.text;
|
|
v.window_corner_radius = egui::CornerRadius::same(6);
|
|
v.menu_corner_radius = egui::CornerRadius::same(4);
|
|
// Keep receiving native OS appearance changes even when our palette is manual. On macOS,
|
|
// pinning egui's theme also pins the window appearance and hides later OS changes.
|
|
ctx.set_theme(egui::ThemePreference::System);
|
|
// Both egui style branches must use our resolved palette: egui can select either branch
|
|
// as the OS changes, including while SoundCraft's manual Light/Dark choice stays fixed.
|
|
ctx.all_styles_mut(|s| {
|
|
s.visuals = v.clone();
|
|
s.spacing.item_spacing = egui::vec2(6.0, 4.0);
|
|
s.spacing.button_padding = egui::vec2(6.0, 2.0);
|
|
s.interaction.tooltip_delay = 0.4;
|
|
});
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{Services, SoundApp, UiState};
|
|
use serde_json::json;
|
|
|
|
#[test]
|
|
fn test_palette_lock_is_reentrant_and_excludes_other_threads() {
|
|
// Regression for the parallel-run flakes: the process-wide palette must be owned for a
|
|
// whole test, re-entrant on the owning thread (so `SoundApp::new` can take it too), and
|
|
// exclusive against other threads (so a frame cannot flip it mid-test).
|
|
let outer = lock_test_palette();
|
|
let inner = lock_test_palette();
|
|
let app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services::default());
|
|
let (tx, rx) = std::sync::mpsc::channel();
|
|
std::thread::spawn(move || {
|
|
let _held = lock_test_palette();
|
|
let _ = tx.send(());
|
|
});
|
|
assert!(rx.recv_timeout(std::time::Duration::from_millis(100)).is_err(), "another thread must wait for the palette lock");
|
|
drop(inner);
|
|
drop(outer);
|
|
drop(app);
|
|
// Other tests compete for the lock too, and some hold it for seconds: allow for a queue.
|
|
assert!(rx.recv_timeout(std::time::Duration::from_secs(120)).is_ok(), "the palette lock must be released when every holder drops it");
|
|
}
|
|
|
|
#[test]
|
|
fn dark_is_the_default() {
|
|
assert_eq!(ThemeMode::default(), ThemeMode::Dark);
|
|
assert_eq!(UiState::default().theme, ThemeMode::Dark);
|
|
// Prefs saved before the setting existed load as Dark.
|
|
let old: UiState = serde_json::from_value(json!({"show_tracks_list": true})).unwrap_or_default();
|
|
assert_eq!(old.theme, ThemeMode::Dark);
|
|
}
|
|
|
|
#[test]
|
|
fn prefs_saved_before_track_tint_get_the_default() {
|
|
let ui: UiState = serde_json::from_value(json!({"theme": "light"})).unwrap();
|
|
assert!((ui.track_tint - 0.5).abs() < f32::EPSILON);
|
|
}
|
|
|
|
#[test]
|
|
fn modes_round_trip_through_prefs() {
|
|
for mode in ThemeMode::ALL {
|
|
assert_eq!(ThemeMode::parse(mode.id()), Some(mode));
|
|
let ui = UiState { theme: mode, ..UiState::default() };
|
|
let back: UiState = serde_json::from_value(serde_json::to_value(&ui).unwrap_or_default()).unwrap_or_default();
|
|
assert_eq!(back.theme, mode);
|
|
}
|
|
assert_eq!(ThemeMode::parse(" Light "), Some(ThemeMode::Light));
|
|
assert_eq!(ThemeMode::parse("sepia"), None);
|
|
}
|
|
|
|
#[test]
|
|
fn theme_command_sets_and_reports_the_mode() {
|
|
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services::default());
|
|
assert_eq!(app.run("ui.theme", json!({})), Ok(json!({"mode": "dark"})));
|
|
assert_eq!(app.run("ui.theme", json!({"mode": "light"})), Ok(json!({"mode": "light"})));
|
|
assert_eq!(app.ui.theme, ThemeMode::Light);
|
|
assert!(app.run("ui.theme", json!({"mode": "sepia"})).is_err());
|
|
assert_eq!(app.ui.theme, ThemeMode::Light);
|
|
assert_eq!(app.run("ui.theme", json!({"mode": "system"})), Ok(json!({"mode": "system"})));
|
|
assert!(crate::menus::UI_COMMANDS.iter().any(|c| c.0 == "ui.theme"));
|
|
}
|
|
|
|
#[test]
|
|
fn application_frames_follow_system_appearance_and_preserve_manual_palettes() {
|
|
// Mixer rendering tests also change the process-wide custom-painted palette.
|
|
let _palette = lock_test_palette();
|
|
fn frame(app: &mut SoundApp, ctx: &egui::Context, system_theme: Option<egui::Theme>, light: bool) {
|
|
let mode = app.ui.theme;
|
|
let colors = |app: &SoundApp| {
|
|
app.engine
|
|
.session()
|
|
.tracks
|
|
.iter()
|
|
.map(|track| {
|
|
(track.color, track.playlists.iter().flat_map(|playlist| &playlist.clips).map(|clip| clip.color).collect::<Vec<_>>())
|
|
})
|
|
.collect::<Vec<_>>()
|
|
};
|
|
let before = colors(app);
|
|
let mut output = ctx.run_ui(egui::RawInput { system_theme, ..Default::default() }, |ui| {
|
|
let ctx = ui.ctx().clone();
|
|
app.logic(&ctx);
|
|
let tokens = if light { Tokens::LIGHT } else { Tokens::DARK };
|
|
assert_eq!(is_light(), light, "custom palette for {mode:?} with {system_theme:?}");
|
|
assert_eq!(Tokens::current().panel_bg, tokens.panel_bg);
|
|
assert_eq!(ctx.options(|options| options.theme_preference), egui::ThemePreference::System);
|
|
for theme in [egui::Theme::Light, egui::Theme::Dark] {
|
|
let style = ctx.style_of(theme);
|
|
assert_eq!(style.visuals.dark_mode, !light);
|
|
assert_eq!(style.visuals.panel_fill, tokens.panel_bg);
|
|
assert_eq!(style.visuals.widgets.inactive.bg_fill, tokens.button);
|
|
assert_eq!(style.visuals.widgets.inactive.fg_stroke.color, tokens.text);
|
|
assert_eq!(style.spacing.button_padding, egui::vec2(6.0, 2.0));
|
|
}
|
|
app.ui(ui);
|
|
assert_eq!(ui.visuals().panel_fill, tokens.panel_bg);
|
|
assert_eq!(ui.visuals().widgets.inactive.bg_fill, tokens.button);
|
|
});
|
|
output.textures_delta.clear();
|
|
assert_eq!(app.ui.theme, mode);
|
|
assert_eq!(serde_json::to_value(&app.ui).unwrap()["theme"], mode.id());
|
|
assert_eq!(colors(app), before, "appearance must preserve track and clip colors");
|
|
}
|
|
|
|
let appearances = [Some(egui::Theme::Light), Some(egui::Theme::Dark), Some(egui::Theme::Light), None];
|
|
for initial in [Some(egui::Theme::Light), Some(egui::Theme::Dark), None] {
|
|
let ctx = egui::Context::default();
|
|
let mut app = SoundApp::new(soundcraft_engine::Engine::default(), None, Services::default());
|
|
app.ui = serde_json::from_value(json!({"theme": "system"})).unwrap();
|
|
frame(&mut app, &ctx, initial, initial == Some(egui::Theme::Light));
|
|
}
|
|
|
|
let ctx = egui::Context::default();
|
|
let mut engine = soundcraft_engine::demo::demo_engine();
|
|
let track = engine.session_mut().tracks.first_mut().unwrap();
|
|
track.color = [19, 83, 147];
|
|
track.playlists.iter_mut().flat_map(|playlist| &mut playlist.clips).next().unwrap().color = Some([211, 57, 99]);
|
|
let mut app = SoundApp::new(engine, None, Services::default());
|
|
for mode in [ThemeMode::System, ThemeMode::Light, ThemeMode::Dark] {
|
|
app.run("ui.theme", json!({"mode": mode.id()})).unwrap();
|
|
for appearance in appearances {
|
|
let light = mode == ThemeMode::Light || (mode == ThemeMode::System && appearance == Some(egui::Theme::Light));
|
|
frame(&mut app, &ctx, appearance, light);
|
|
}
|
|
if mode != ThemeMode::System {
|
|
let latest = if mode == ThemeMode::Light { egui::Theme::Dark } else { egui::Theme::Light };
|
|
frame(&mut app, &ctx, Some(latest), mode == ThemeMode::Light);
|
|
app.run("ui.theme", json!({"mode": "system"})).unwrap();
|
|
// Resolve the current context immediately, without another OS appearance event.
|
|
app.logic(&ctx);
|
|
assert_eq!(is_light(), latest == egui::Theme::Light);
|
|
assert_eq!(ctx.global_style().visuals.dark_mode, latest != egui::Theme::Light);
|
|
frame(&mut app, &ctx, Some(latest), latest == egui::Theme::Light);
|
|
}
|
|
}
|
|
}
|
|
}
|