diff --git a/app/src/app.rs b/app/src/app.rs index 95144cf..8cfb6a4 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -23,10 +23,10 @@ pub enum Screen { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SettingsTab { - General, + Quick, Recording, Audio, - Daemon, + Advanced, } /// `GetStatusResponse`. @@ -138,7 +138,7 @@ impl App { let live = std::env::args().any(|arg| arg == "--live"); let mut app = Self { screen: Screen::History, - settings_tab: SettingsTab::General, + settings_tab: SettingsTab::Quick, dark: true, daemon: Daemon::start(headless && !live), connected: false, @@ -543,11 +543,11 @@ fn status_color(p: Paint, app: &App) -> (rune::Color, String) { return (p.muted(), "Daemon offline".to_string()); } if app.status.is_recording { - return (p.danger(), "Recording".to_string()); + return (p.danger, "Recording".to_string()); } if app.status.client_connected { return ( - p.success(), + p.success, format!("{} · client connected", app.status.label()), ); } @@ -576,7 +576,7 @@ fn statusbar(p: Paint, app: &App) -> El { .px(14.0) .gap(8.0) .center_y() - .fill(p.t.bg) + .fill(p.chrome) .border_sides([p.hair, 0.0, 0.0, 0.0], p.t.separator) .child(ui::dot(dot_color)) .child( @@ -598,40 +598,48 @@ fn statusbar(p: Paint, app: &App) -> El { } fn titlebar(p: Paint, app: &App) -> El { - let actions = if app.screen == Screen::Settings { - row().key("actions").gap(8.0).center_y().child(ui::button( + // Leaving a screen is on the left, where a window's back arrow lives; the app's own + // actions stay on the right. + let mut leading = row().key("leading").gap(8.0).center_y(); + if app.screen == Screen::Settings { + leading = leading.child(ui::button( p, "back", "← Match history", false, |app: &mut App| app.screen = Screen::History, + )); + } + let leading = leading.child( + text("League Recorder") + .key("title") + .font(heading(13.0)) + .color(p.t.text), + ); + let actions = row() + .key("actions") + .gap(8.0) + .center_y() + .child(ui::button( + p, + "refresh", + "Refresh", + false, + |app: &mut App| { + app.refresh_games(); + app.daemon.get_status(); + }, )) - } else { - row() - .key("actions") - .gap(8.0) - .center_y() - .child(ui::button( - p, - "refresh", - "Refresh", - false, - |app: &mut App| { - app.refresh_games(); - app.daemon.get_status(); - }, - )) - .child(ui::button( - p, - "open-settings", - "Settings", - false, - |app: &mut App| { - screens::review::leave(app); - app.screen = Screen::Settings; - }, - )) - }; + .child(ui::button( + p, + "open-settings", + "Settings", + false, + |app: &mut App| { + screens::review::leave(app); + app.screen = Screen::Settings; + }, + )); row() .key("titlebar") .h(48.0) @@ -641,15 +649,11 @@ fn titlebar(p: Paint, app: &App) -> El { .center_y() .justify_between() .window_drag() - .child( - text("League Recorder") - .key("title") - .font(heading(13.0)) - .color(p.t.text), - ) + .fill(p.chrome) + .border_sides([0.0, 0.0, p.hair, 0.0], p.t.separator) + .child(leading) .child(actions) } - fn notice(p: Paint, notice: &Notice) -> El { row() .key("notice") @@ -659,25 +663,17 @@ fn notice(p: Paint, notice: &Notice) -> El { .gap(8.0) .center_y() .fill(if notice.error { - p.danger().alpha(0.16) + p.danger.alpha(0.16) } else { - p.success().alpha(0.16) + p.success.alpha(0.16) }) .on_press(|app: &mut App| app.notice = None) - .child(ui::dot(if notice.error { - p.danger() - } else { - p.success() - })) + .child(ui::dot(if notice.error { p.danger } else { p.success })) .child( text(notice.text.clone()) .key("notice-text") .font(body(12.0)) - .color(if notice.error { - p.danger() - } else { - p.success() - }) + .color(if notice.error { p.danger } else { p.success }) .grow(1.0) .shrink() .truncate(), diff --git a/app/src/daemon.rs b/app/src/daemon.rs index 0bc065c..14c1bfb 100644 --- a/app/src/daemon.rs +++ b/app/src/daemon.rs @@ -30,6 +30,10 @@ pub fn log_file() -> Option { /// Find the record-daemon binary next to the app, in an installed `resources` folder, or in a /// development checkout. +/// +/// A checkout can hold both `target/debug` and `target/release`, one of them stale; the newest +/// build wins, so the app talks to the daemon that was built last ("cargo build" then "cargo +/// run", rather than a release build from another week). pub fn find_binary() -> Option { let name = if cfg!(windows) { "record-daemon.exe" @@ -49,34 +53,39 @@ pub fn find_binary() -> Option { return Some(resources); } } - let mut dir = exe_dir.to_path_buf(); - for _ in 0..6 { - for suffix in ["record-daemon/target/release", "record-daemon/target/debug"] { - let candidate = dir.join(suffix).join(name); - if candidate.exists() { - return Some(candidate); - } - } - if !dir.pop() { - break; - } + if let Some(found) = newest_build(exe_dir, name) { + return Some(found); } } - let cwd = std::env::current_dir().ok()?; - let mut dir = cwd.as_path(); - for _ in 0..5 { - for suffix in ["record-daemon/target/release", "record-daemon/target/debug"] { - let candidate = dir.join(suffix).join(name); - if candidate.exists() { - return Some(candidate); - } + newest_build(&std::env::current_dir().ok()?, name) +} + +/// The most recently built daemon in a checkout, walking up from `start`. +fn newest_build(start: &Path, name: &str) -> Option { + let mut dir = start.to_path_buf(); + for _ in 0..6 { + let candidates = [ + dir.join("record-daemon/target/release").join(name), + dir.join("record-daemon/target/debug").join(name), + ]; + if let Some(found) = candidates + .into_iter() + .filter_map(|path| { + let modified = std::fs::metadata(&path).ok()?.modified().ok()?; + Some((modified, path)) + }) + .max_by_key(|(modified, _)| *modified) + .map(|(_, path)| path) + { + return Some(found); + } + if !dir.pop() { + break; } - dir = dir.parent()?; } None } - /// Start the daemon detached, so it outlives the app. pub fn spawn() -> Result { let binary = find_binary().ok_or_else(|| { diff --git a/app/src/main.rs b/app/src/main.rs index 7312191..f3c6839 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -14,6 +14,7 @@ mod os; mod portraits; mod screens; mod settings; +mod theme; mod ui; use rune::AppSpec; @@ -27,8 +28,9 @@ fn main() { probe(); return; } - // A second launch hands its arguments to the first and exits. - if !rune::single_instance(os::APP_ID) { + // A second launch hands its arguments to the first and exits; the tooling modes + // (`--snap`, `--tree`, that sort of thing) render or print and exit, so they never forward. + if !app::headless() && !rune::single_instance(os::APP_ID) { return; } os::register_notifications(); @@ -60,7 +62,7 @@ fn main() { }), ("daemon", |app| { app.screen = app::Screen::Settings; - app.settings_tab = app::SettingsTab::Daemon; + app.settings_tab = app::SettingsTab::Advanced; }), ("review", |app| app::demo_review(app)), ("details", |app| { diff --git a/app/src/screens/history.rs b/app/src/screens/history.rs index 7a4fe03..451c3ce 100644 --- a/app/src/screens/history.rs +++ b/app/src/screens/history.rs @@ -27,8 +27,8 @@ fn filter_label(filter: Option) -> &'static str { fn outcome_color(p: Paint, outcome: Outcome) -> Color { match outcome { - Outcome::Victory => p.success(), - Outcome::Defeat => p.danger(), + Outcome::Victory => p.success, + Outcome::Defeat => p.danger, Outcome::Terminated => p.muted(), } } @@ -183,11 +183,7 @@ fn game_card(app: &App, p: Paint, game: &Timeline, index: usize) -> El { ), ); if let Some(lp) = &lp { - let lp_ink = if lp.delta >= 0 { - p.success() - } else { - p.danger() - }; + let lp_ink = if lp.delta >= 0 { p.success } else { p.danger }; banner = banner.child( text(format!( "{} LP · {}", diff --git a/app/src/screens/review.rs b/app/src/screens/review.rs index 22677c2..cfd2e7c 100644 --- a/app/src/screens/review.rs +++ b/app/src/screens/review.rs @@ -145,8 +145,8 @@ pub fn view(app: &App, p: Paint) -> El { }; let outcome = game.outcome(); let ink = match outcome { - Outcome::Victory => p.success(), - Outcome::Defeat => p.danger(), + Outcome::Victory => p.success, + Outcome::Defeat => p.danger, Outcome::Terminated => p.muted(), }; @@ -397,9 +397,9 @@ fn scrubber(app: &App, p: Paint, game: &Timeline) -> El { let x = (clip.start / duration).clamp(0.0, 1.0) as f32 * width; let end = (clip.end / duration).clamp(0.0, 1.0) as f32 * width; let ink = match clip.kind { - highlight::Kind::Death => p.danger(), + highlight::Kind::Death => p.danger, highlight::Kind::Assist => p.muted(), - _ => p.success(), + _ => p.success, }; bar = bar.child( el().key(format!("clip-{index}")) @@ -435,8 +435,8 @@ fn scrubber(app: &App, p: Paint, game: &Timeline) -> El { .and_then(|name| highlight::involvement(event, name)); let ink = match event.event_type.as_str() { "kill" => match involvement { - Some(highlight::Kind::Death) => p.danger(), - Some(_) => p.success(), + Some(highlight::Kind::Death) => p.danger, + Some(_) => p.success, None => p.muted().alpha(0.75), }, "objective" | "lp_change" => p.accent, @@ -494,10 +494,10 @@ fn highlights(app: &App, p: Paint, game: &Timeline) -> El { let video = game.video_path().map(|path| path.display().to_string()); let name = format!("{}-highlight-{}", game.recording_id, index); let kind_ink = match clip.kind { - highlight::Kind::Death => p.danger(), + highlight::Kind::Death => p.danger, highlight::Kind::Assist => p.muted(), highlight::Kind::MultiKill | highlight::Kind::TradeKill => p.accent, - highlight::Kind::Kill => p.success(), + highlight::Kind::Kill => p.success, }; let mut actions = row().key("actions").gap(6.0).center_y().child(ui::button( p, diff --git a/app/src/screens/settings.rs b/app/src/screens/settings.rs index b532488..5f99f14 100644 --- a/app/src/screens/settings.rs +++ b/app/src/screens/settings.rs @@ -1,4 +1,4 @@ -//! Settings: general, recording, audio and daemon tabs, saved through `updateSettings`. +//! Settings: a quick tab of everyday choices, then recording, audio and the daemon. use rune::tok::{radius, space, type_}; use rune::{El, TextEdit, col, row, text}; @@ -141,25 +141,13 @@ const SOURCE_TYPES: [&str; 3] = ["Application", "Output device", "Input device"] /// A row with a single-line field on the right. fn text_row( p: Paint, - kit: &rune_kit::Kit, key: &'static str, label: &str, hint: &str, edit: &TextEdit, to: fn(&mut App) -> &mut TextEdit, ) -> El { - ui::setting_row_with( - p, - key, - label, - hint, - rune_kit::TextInput::new(edit) - .label(label) - .on_edit(to) - .width(Some(220.0)) - .on_change(|app: &mut App, _| app.form.dirty = true) - .build(kit), - ) + ui::setting_row_with(p, key, label, hint, ui::text_field(p, key, label, edit, to)) } #[allow(clippy::too_many_arguments)] // the columns of a settings row @@ -199,10 +187,10 @@ fn select_row( fn tabs(p: Paint, app: &App) -> El { let mut row = row().key("tabs").gap(4.0).center_y(); for (tab, label) in [ - (SettingsTab::General, "General"), + (SettingsTab::Quick, "Quick"), (SettingsTab::Recording, "Recording"), (SettingsTab::Audio, "Audio"), - (SettingsTab::Daemon, "Advanced"), + (SettingsTab::Advanced, "Advanced"), ] { row = row.child(ui::tab( p, @@ -221,19 +209,20 @@ fn tabs(p: Paint, app: &App) -> El { pub fn view(app: &App, p: Paint) -> El { let kit = rune_kit::Kit::neutral(app.dark); let mut layer = Vec::new(); + let (save_text, save_ink) = save_label(app, p); // The scroll bar gets its own strip at the end, clear of the cards. let content = col() .key("tab-content") .w_full() - .gap(space::S6) + .gap(20.0) .pb(space::S6) .pr(14.0); let content = match app.settings_tab { - SettingsTab::General => general(app, p, &kit, &mut layer, content), + SettingsTab::Quick => quick(app, p, &kit, &mut layer, content), SettingsTab::Recording => recording(app, p, &kit, &mut layer, content), SettingsTab::Audio => audio(app, p, &kit, &mut layer, content), - SettingsTab::Daemon => daemon_tab(app, p, &kit, &mut layer, content), + SettingsTab::Advanced => advanced(app, p, &kit, &mut layer, content), }; let body = col() @@ -261,10 +250,10 @@ pub fn view(app: &App, p: Paint) -> El { .center_y() .child( row().key("save-state").mr(4.0).center_y().child( - text(save_label(app)) + text(save_text) .key("save-label") .font(body(11.5)) - .color(p.muted()), + .color(save_ink), ), ) .child(ui::button(p, "revert", "Revert", false, |app: &mut App| { @@ -306,15 +295,15 @@ pub fn view(app: &App, p: Paint) -> El { rune_kit::root(body, layer) } -fn save_label(app: &App) -> String { +fn save_label(app: &App, p: Paint) -> (String, rune::Color) { if !app.connected { - "Daemon offline".into() + ("Daemon offline".into(), p.danger) } else if app.form.dirty { - "Unsaved changes".into() + ("Unsaved changes".into(), p.warning) } else if app.form.loaded { - "Saved".into() + ("Saved".into(), p.muted()) } else { - "Loading".into() + ("Loading".into(), p.muted()) } } @@ -328,24 +317,55 @@ fn save_footer(p: Paint, _app: &App) -> El { ) } -fn general(_app: &App, p: Paint, _kit: &rune_kit::Kit, _layer: &mut Vec, content: El) -> El { +/// The settings most people touch: how a clip looks, whether it has sound, and where it lands. +fn quick(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: El) -> El { + let v = &app.form.draft.video; + let a = &app.form.draft.audio; content + .child( + text("Recording starts on its own when a game begins, and the game shows up here when it ends.") + .key("quick-note") + .font(body(12.5)) + .color(p.muted()), + ) .child(ui::card( p, Some("Recording"), vec![ - text("Recording starts by itself when a game begins and stops when the game ends.") - .key("auto-note") - .font(body(12.5)) - .color(p.muted()) - .px(space::S3) - .py(space::S3), - text("A finished game shows up in the match history on its own.") - .key("history-note") - .font(body(12.5)) - .color(p.muted()) - .px(space::S3) - .pb(space::S3), + select_row( + p, + kit, + layer, + "quality", + "Quality", + v.quality.detail(), + &QUALITIES, + Some(v.quality.label().to_string()), + app, + ), + ui::toggle_row( + p, + "quick-audio", + "Record game audio", + a.enabled && a.capture_game, + |app: &mut App| { + let on = !(app.form.draft.audio.enabled && app.form.draft.audio.capture_game); + app.form.draft.audio.enabled = on; + app.form.draft.audio.capture_game = on; + app.form.dirty = true; + }, + ), + ui::toggle_row( + p, + "quick-mic", + "Record my microphone", + a.capture_mic, + |app: &mut App| { + app.form.draft.audio.capture_mic ^= true; + app.form.dirty = true; + }, + ), + mic_row(p, kit, layer, app, "quick-mic-device"), ], )) .child(ui::card( @@ -355,7 +375,7 @@ fn general(_app: &App, p: Paint, _kit: &rune_kit::Kit, _layer: &mut Vec, con p, "autostart", "Launch League Recorder at startup", - _app.autostart, + app.autostart, |app: &mut App| { let next = !app.autostart; match crate::os::set_autostart(next) { @@ -372,31 +392,49 @@ fn general(_app: &App, p: Paint, _kit: &rune_kit::Kit, _layer: &mut Vec, con }, )], )) - .child( - row() - .key("general-actions") - .gap(8.0) - .center_y() - .child(ui::button( + .child(ui::card( + p, + Some("Files"), + vec![ + ui::setting_row( p, - "open-recordings", - "Open recordings folder", - false, - |_app: &mut App| { - crate::daemon::open_folder(&crate::daemon::recordings_dir()); - }, - )) - .child(ui::button( - p, - "reset-settings", - "Reset to defaults", - false, - |app: &mut App| { - app.daemon.reset_settings(); - }, - )), - ) + "recordings-folder", + "Recordings folder", + text(short_path(&app.form.draft.output.path)) + .key("path") + .font(body(12.0)) + .color(p.muted()), + ), + row() + .key("files-actions") + .px(space::S3) + .pb(space::S2) + .child(ui::button( + p, + "open-recordings", + "Open folder", + false, + |_app: &mut App| { + crate::daemon::open_folder(&crate::daemon::recordings_dir()); + }, + )), + ], + )) } + +/// A path as it fits on a row: the last two segments, with the rest elided. +/// A path as it fits on a row: the last two segments, with the rest elided. +fn short_path(path: &str) -> String { + let sep = std::path::MAIN_SEPARATOR; + let normalized = path.replace(sep, "/"); + let parts: Vec<&str> = normalized.split('/').filter(|p| !p.is_empty()).collect(); + match parts.len() { + 0 => path.to_string(), + 1 | 2 => parts.join(&sep.to_string()), + n => format!("…{sep}{}{sep}{}", parts[n - 2], parts[n - 1]), + } +} + fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: El) -> El { let v = &app.form.draft.video; let o = &app.form.draft.output; @@ -443,7 +481,6 @@ fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, cont ), text_row( p, - kit, "frame-rate", "Frame rate", "Frames per second the daemon records", @@ -480,7 +517,6 @@ fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, cont vec![ text_row( p, - kit, "output-path", "Folder", "Where finished recordings are written", @@ -489,7 +525,6 @@ fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, cont ), text_row( p, - kit, "naming", "File name", "{date}, {time}, {champion} and {queue_type} are filled in", @@ -509,7 +544,6 @@ fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, cont ), text_row( p, - kit, "split-size", "Split at size", "Start a new file past this many MB (0 for never)", @@ -518,7 +552,6 @@ fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, cont ), text_row( p, - kit, "split-time", "Split at time", "Start a new file after this many minutes (0 for never)", @@ -538,59 +571,63 @@ fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, cont ))) } -fn audio(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: El) -> El { - let a = &app.form.draft.audio; - let mic_name = a +/// The microphone picker: the system default plus every input device the daemon reports. +fn mic_row(p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, app: &App, key: &'static str) -> El { + let devices = &app.devices.input_devices; + let current = app + .form + .draft + .audio .mic_device .as_ref() - .and_then(|id| app.devices.input_devices.iter().find(|d| &d.id == id)) + .and_then(|id| devices.iter().find(|d| &d.id == id)) .map(|d| d.name.clone()) - .or_else(|| { - app.devices - .input_devices - .iter() - .find(|d| d.is_default) - .map(|d| d.name.clone()) - }); - let mic_options: Vec = app - .devices - .input_devices - .iter() - .map(|d| d.name.clone()) - .collect(); - let mic_refs: Vec<&str> = mic_options.iter().map(String::as_str).collect(); - let open = app.form.open_select == Some("mic-device"); - let mic_select = Select::new("mic-device", &mic_refs) - .current(mic_name.as_deref()) + .unwrap_or_else(|| "System default".to_string()); + let mut options = vec!["System default".to_string()]; + options.extend(devices.iter().map(|d| d.name.clone())); + let names: Vec<&str> = options.iter().map(String::as_str).collect(); + let open = app.form.open_select == Some(key); + let select = Select::new(key, &names) + .current(Some(¤t)) .open(open) .label("Microphone") .width(220.0) - .on_toggle(|app: &mut App| { - app.form.open_select = if app.form.open_select == Some("mic-device") { + .on_toggle(move |app: &mut App| { + app.form.open_select = if app.form.open_select == Some(key) { None } else { - Some("mic-device") + Some(key) }; }) .on_close(|app: &mut App| app.form.open_select = None) .on_choose(|app: &mut App, value: &str, _: &mut rune::Tx| { - let id = app - .devices - .input_devices - .iter() - .find(|d| d.name == value) - .map(|d| d.id.clone()) - .unwrap_or_else(|| value.to_string()); - app.form.draft.audio.mic_device = Some(id); + app.form.draft.audio.mic_device = if value == "System default" { + None + } else { + app.devices + .input_devices + .iter() + .find(|d| d.name == value) + .map(|d| d.id.clone()) + }; app.form.dirty = true; app.form.open_select = None; }) .build(kit, layer); + let hint = match devices.iter().find(|d| d.is_default) { + _ if devices.is_empty() => "The recorder sees no microphone".to_string(), + Some(d) => format!("System default is {}", d.name), + None => "What the recorder captures when the microphone is on".to_string(), + }; + ui::setting_row_with(p, key, "Microphone", &hint, select) +} +fn audio(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: El) -> El { + let a = &app.form.draft.audio; let mut sources = col().key("sources").gap(2.0); if a.capture_apps.is_empty() { sources = sources.child( - text("No application sources yet.") + text("No applications yet. Add one below and its audio joins the clip.") .key("sources-empty") .font(body(12.0)) .color(p.muted()) @@ -670,47 +707,12 @@ fn audio(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: )), ); } - let source_type = app.form.source_type; + content .child(ui::card( p, - Some("Audio"), + Some("Levels"), vec![ - ui::toggle_row( - p, - "audio-enabled", - "Record audio", - a.enabled, - |app: &mut App| { - app.form.draft.audio.enabled ^= true; - app.form.dirty = true; - }, - ), - text_row( - p, - kit, - "audio-bitrate", - "Bitrate", - "Audio bitrate in kbps", - &app.form.bitrate, - |app| &mut app.form.bitrate, - ), - ], - )) - .child(ui::card( - p, - Some("Game audio"), - vec![ - ui::toggle_row( - p, - "capture-game", - "Capture desktop audio", - a.capture_game, - |app: &mut App| { - app.form.draft.audio.capture_game ^= true; - app.form.dirty = true; - }, - ), ui::setting_row( p, "game-volume", @@ -720,12 +722,42 @@ fn audio(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: "game-volume", "Game volume", a.game_volume, - |app: &mut App, value| { + |app, value| { app.form.draft.audio.game_volume = value; app.form.dirty = true; }, ), ), + ui::setting_row( + p, + "mic-volume", + "Microphone volume", + ui::slider( + p, + "mic-volume", + "Microphone volume", + a.mic_volume, + |app, value| { + app.form.draft.audio.mic_volume = value; + app.form.dirty = true; + }, + ), + ), + ui::setting_row( + p, + "apps-volume", + "Applications volume", + ui::slider( + p, + "apps-volume", + "Applications volume", + a.apps_volume, + |app, value| { + app.form.draft.audio.apps_volume = value; + app.form.dirty = true; + }, + ), + ), ], )) .child(ui::card( @@ -742,133 +774,117 @@ fn audio(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: app.form.dirty = true; }, ), - ui::setting_row_with( - p, - "mic-device", - "Device", - "The microphone the daemon records", - mic_select, - ), - ui::setting_row( - p, - "mic-volume", - "Microphone volume", - ui::slider( - p, - "mic-volume", - "Microphone volume", - a.mic_volume, - |app: &mut App, value| { - app.form.draft.audio.mic_volume = value; - app.form.dirty = true; - }, - ), - ), + mic_row(p, kit, layer, app, "mic-device"), ], )) .child(ui::card( p, Some("Applications"), - vec![ - ui::setting_row( - p, - "apps-volume", - "Application volume", - ui::slider( - p, - "apps-volume", - "Application volume", - a.apps_volume, - |app: &mut App, value| { - app.form.draft.audio.apps_volume = value; - app.form.dirty = true; - }, - ), - ), - sources, - text(format!( - "{} output devices are visible to the daemon.", - app.devices.output_devices.len() - )) - .key("output-devices") - .font(body(11.0)) - .color(p.muted()) - .ml(space::S3) - .pb(space::S2), - ], - )) - .child(ui::card( - p, - Some("Add an application"), - vec![ - text_row( - p, - kit, - "source-name", - "Name", - "What the source is called in this list", - &app.form.source_name, - |app| &mut app.form.source_name, - ), - text_row( - p, - kit, - "source-device", - "Process or device", - "Discord.exe for an application, or a device id", - &app.form.source_device, - |app| &mut app.form.source_device, - ), - select_row( - p, - kit, - layer, - "source-type", - "Type", - "Application audio, an output device or an input device", - &SOURCE_TYPES, - Some(source_type.label().to_string()), - app, - ), - row() - .key("add-source") - .px(space::S3) - .pb(space::S2) - .child(ui::button( - p, - "add-source-button", - "Add source", - true, - |app: &mut App| { - let name = app.form.source_name.text.trim().to_string(); - let device = app.form.source_device.text.trim().to_string(); - if name.is_empty() || device.is_empty() { - app.notify_error( - "An audio source needs a name and a process or device", - ); - return; - } - let source = AudioSource { - id: String::new(), - name, - source_type: app.form.source_type, - device, - enabled: true, - volume: None, - }; - match serde_json::to_value(&source) { - Ok(value) => app.daemon.add_audio_source(value), - Err(e) => { - app.notify_error(format!("audio source did not serialize: {e}")) - } - } - }, - )), - ], + vec![sources, add_source(app, p, kit, layer)], )) } -fn daemon_tab(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: El) -> El { +/// The row that adds an application source: a name, the process or device, and its kind. +fn add_source(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec) -> El { + let open = app.form.open_select == Some("source-type"); + let kind = Select::new("source-type", &SOURCE_TYPES) + .current(Some(app.form.source_type.label())) + .open(open) + .label("Type") + .width(170.0) + .on_toggle(|app: &mut App| { + app.form.open_select = if app.form.open_select == Some("source-type") { + None + } else { + Some("source-type") + }; + }) + .on_close(|app: &mut App| app.form.open_select = None) + .on_choose(|app: &mut App, value: &str, _: &mut rune::Tx| { + app.form.choose("source-type", value); + app.form.open_select = None; + }) + .build(kit, layer); + col() + .key("add-source") + .gap(space::S2) + .px(space::S3) + .pb(space::S3) + .child( + text("Add an application") + .key("add-title") + .font(type_::SECTION) + .color(p.muted()), + ) + .child( + row() + .key("add-fields") + .gap(space::S3) + .center_y() + .child(labelled_field( + p, + "Name", + "source-name", + &app.form.source_name, + |app| &mut app.form.source_name, + )) + .child(labelled_field( + p, + "Process or device", + "source-device", + &app.form.source_device, + |app| &mut app.form.source_device, + )) + .child(kind) + .child(ui::button( + p, + "add-source-button", + "Add", + true, + |app: &mut App| { + let name = app.form.source_name.text.trim().to_string(); + let device = app.form.source_device.text.trim().to_string(); + if name.is_empty() || device.is_empty() { + app.notify_error( + "An audio source needs a name and a process or device", + ); + return; + } + let source = AudioSource { + id: String::new(), + name, + source_type: app.form.source_type, + device, + enabled: true, + volume: None, + }; + match serde_json::to_value(&source) { + Ok(value) => app.daemon.add_audio_source(value), + Err(e) => { + app.notify_error(format!("audio source did not serialize: {e}")) + } + } + }, + )), + ) +} + +/// A field with its name above it, for a row of controls that has no room for setting labels. +fn labelled_field( + p: Paint, + label: &'static str, + key: &'static str, + edit: &TextEdit, + to: fn(&mut App) -> &mut TextEdit, +) -> El { + col() + .key(key) + .gap(space::S1) + .child(text(label).key("caption").font(body(11.0)).color(p.muted())) + .child(ui::text_field(p, key, label, edit, to)) +} + +fn advanced(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: El) -> El { let status_row = |key: &str, label: &'static str, value: String| { row() .key(key) @@ -919,7 +935,11 @@ fn daemon_tab(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, con .map(|id| id.to_string()) .unwrap_or_else(|| "none".into()), ), - status_row("daemon-log", "Log file", crate::daemon::log_path()), + status_row( + "daemon-log", + "Log file", + short_path(&crate::daemon::log_path()), + ), ], )) .child(ui::card( @@ -928,7 +948,6 @@ fn daemon_tab(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, con vec![ text_row( p, - kit, "poll-interval", "Client poll interval", "How often the recorder checks the League Client, in milliseconds", @@ -948,6 +967,29 @@ fn daemon_tab(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, con ), ], )) + .child(ui::card( + p, + Some("Audio"), + vec![ + status_row( + "audio-devices", + "Devices", + format!( + "{} in · {} out", + app.devices.input_devices.len(), + app.devices.output_devices.len() + ), + ), + text_row( + p, + "audio-bitrate", + "Bitrate", + "Audio bitrate in kbps", + &app.form.bitrate, + |app| &mut app.form.bitrate, + ), + ], + )) .child( row() .key("daemon-actions") @@ -979,6 +1021,15 @@ fn daemon_tab(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, con |app: &mut App| { app.daemon.shutdown(); }, + )) + .child(ui::button( + p, + "reset-settings", + "Reset to defaults", + false, + |app: &mut App| { + app.daemon.reset_settings(); + }, )), ) .child(ui::card( diff --git a/app/src/theme.rs b/app/src/theme.rs new file mode 100644 index 0000000..976f052 --- /dev/null +++ b/app/src/theme.rs @@ -0,0 +1,155 @@ +//! The app's palette. +//! +//! One shell colour scheme: near-black surfaces tinted blue, brass for what the user acts on, +//! and mint or coral only for results. Champion art and victory banners are the loudest things +//! on screen, so the chrome around them stays quiet. +//! +//! Every colour is OKLCH, written as lightness, chroma and hue. The two appearances share the +//! hues and swap lightness, so a control keeps its identity in light mode. + +use rune::tok::Theme; +use rune::{Color, oklch, oklcha}; + +/// Hues, one per role. Neutrals take a touch of the shell's blue; text keeps the same cast. +const SHELL: f32 = 268.0; +const BRASS: f32 = 84.0; +const MINT: f32 = 157.0; +const CORAL: f32 = 22.0; + +pub struct Palette { + /// Surfaces and ink: what rune's widgets read. + pub theme: Theme, + /// The window chrome: title bar and status bar, a step above or below the page. + pub chrome: Color, + /// A field's well, sunk into the card it sits in. + pub field: Color, + /// What the user acts on: primary buttons, sliders, the selected tab. + pub accent: Color, + /// Ink for text on an accent fill. + pub accent_ink: Color, + pub success: Color, + pub danger: Color, + pub warning: Color, +} + +pub fn palette(dark: bool) -> Palette { + if dark { + Palette { + theme: Theme { + bg: oklch(0.168, 0.012, SHELL), + card: oklch(0.212, 0.014, SHELL), + card_border: oklcha(1.0, 0.0, 0.0, 0.055), + text: oklch(0.962, 0.003, SHELL), + text_secondary: oklch(0.734, 0.012, SHELL), + separator: oklcha(1.0, 0.0, 0.0, 0.05), + control_track: oklcha(1.0, 0.0, 0.0, 0.045), + control_off: oklcha(1.0, 0.0, 0.0, 0.09), + indicator: oklch(0.44, 0.01, SHELL), + knob: oklch(0.985, 0.0, 0.0), + hover: oklcha(1.0, 0.0, 0.0, 0.018), + press: oklcha(1.0, 0.0, 0.0, 0.035), + shadow: oklcha(0.0, 0.0, 0.0, 0.45), + shadow_soft: oklcha(0.0, 0.0, 0.0, 0.3), + edge: oklcha(0.0, 0.0, 0.0, 0.32), + knob_shadow: oklcha(0.0, 0.0, 0.0, 0.32), + }, + chrome: oklch(0.192, 0.013, SHELL), + field: oklch(0.178, 0.012, SHELL), + accent: oklch(0.818, 0.116, BRASS), + accent_ink: oklch(0.245, 0.035, BRASS), + success: oklch(0.782, 0.132, MINT), + danger: oklch(0.706, 0.152, CORAL), + warning: oklch(0.812, 0.115, 72.0), + } + } else { + Palette { + theme: Theme { + bg: oklch(0.966, 0.004, SHELL), + card: oklch(1.0, 0.0, 0.0), + card_border: oklcha(0.2, 0.02, SHELL, 0.1), + text: oklch(0.235, 0.012, SHELL), + text_secondary: oklch(0.515, 0.013, SHELL), + separator: oklcha(0.2, 0.02, SHELL, 0.12), + control_track: oklcha(0.2, 0.01, SHELL, 0.13), + control_off: oklcha(0.2, 0.01, SHELL, 0.22), + indicator: oklch(1.0, 0.0, 0.0), + knob: oklch(1.0, 0.0, 0.0), + hover: oklcha(0.2, 0.01, SHELL, 0.06), + press: oklcha(0.2, 0.01, SHELL, 0.11), + shadow: oklcha(0.2, 0.02, SHELL, 0.16), + shadow_soft: oklcha(0.2, 0.02, SHELL, 0.07), + edge: oklcha(0.2, 0.02, SHELL, 0.2), + knob_shadow: oklcha(0.2, 0.02, SHELL, 0.3), + }, + chrome: oklch(0.988, 0.003, SHELL), + field: oklch(0.955, 0.004, SHELL), + accent: oklch(0.535, 0.104, 74.0), + accent_ink: oklch(1.0, 0.0, 0.0), + success: oklch(0.512, 0.122, MINT), + danger: oklch(0.55, 0.165, CORAL), + warning: oklch(0.52, 0.112, 68.0), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Relative luminance of a linear-light colour, as WCAG defines contrast. + fn luminance(c: Color) -> f64 { + 0.2126 * c.r as f64 + 0.7152 * c.g as f64 + 0.0722 * c.b as f64 + } + + fn contrast(a: Color, b: Color) -> f64 { + let (x, y) = (luminance(a), luminance(b)); + let (hi, lo) = if x > y { (x, y) } else { (y, x) }; + (hi + 0.05) / (lo + 0.05) + } + + /// Every colour that carries text keeps WCAG AA (4.5:1) on the surfaces it is drawn on, + /// in both appearances. The palette is tuned by hand, so this is the guard that a nudge + /// to one number did not make a label unreadable. + #[test] + fn ink_reads_on_the_surfaces() { + for dark in [true, false] { + let p = palette(dark); + let mode = if dark { "dark" } else { "light" }; + for (name, ink) in [ + ("text", p.theme.text), + ("text_secondary", p.theme.text_secondary), + ("accent", p.accent), + ("success", p.success), + ("danger", p.danger), + ("warning", p.warning), + ] { + for (surface, bg) in [ + ("bg", p.theme.bg), + ("card", p.theme.card), + ("chrome", p.chrome), + ] { + let ratio = contrast(ink, bg); + assert!( + ratio >= 4.5, + "{mode}: {name} on {surface} is {ratio:.2}:1, under 4.5:1" + ); + } + } + let ratio = contrast(p.accent, p.accent_ink); + assert!( + ratio >= 4.5, + "{mode}: accent_ink on accent is {ratio:.2}:1, under 4.5:1" + ); + } + } + + /// A field's well has to be visible inside the card it sits in. + #[test] + fn the_field_well_stands_out_from_the_card() { + for dark in [true, false] { + let p = palette(dark); + let ratio = contrast(p.field, p.theme.card); + assert!(ratio >= 1.04, "{dark}: field vs card is {ratio:.2}:1"); + } + } +} diff --git a/app/src/ui.rs b/app/src/ui.rs index f076a6c..6ac0251 100644 --- a/app/src/ui.rs +++ b/app/src/ui.rs @@ -1,6 +1,6 @@ //! Shared colours and small controls for the screens. -use rune::tok::{self, Theme, motion, radius, space, type_}; +use rune::tok::{Theme, motion, radius, space, type_}; use rune::{Color, Drag, El, Phase, TextStyle, col, el, row, text}; use crate::app::App; @@ -9,17 +9,28 @@ use crate::app::App; pub struct Paint { pub t: Theme, pub accent: Color, - pub dark: bool, + pub accent_ink: Color, + pub success: Color, + pub danger: Color, + pub warning: Color, + pub chrome: Color, + pub field: Color, pub hair: f32, pub scale: f32, } impl Paint { pub fn new(dark: bool, scale: f32) -> Self { + let palette = crate::theme::palette(dark); Self { - t: tok::theme(dark), - accent: tok::accent(0, dark), - dark, + t: palette.theme, + accent: palette.accent, + accent_ink: palette.accent_ink, + success: palette.success, + danger: palette.danger, + warning: palette.warning, + chrome: palette.chrome, + field: palette.field, hair: 1.0 / scale, scale, } @@ -28,14 +39,6 @@ impl Paint { pub fn muted(&self) -> Color { self.t.text_secondary } - - pub fn danger(&self) -> Color { - tok::accent(2, self.dark) - } - - pub fn success(&self) -> Color { - tok::accent(4, self.dark) - } } pub fn heading(size: f32) -> TextStyle { @@ -272,6 +275,46 @@ pub fn slider( ) } +/// A one-line text field for a settings row. The text sits at the row's end edge, like every +/// other control, so values start and end on one line down the card. +pub fn text_field( + p: Paint, + key: &'static str, + label: &str, + edit: &rune::TextEdit, + to: fn(&mut App) -> &mut rune::TextEdit, +) -> El { + let focused = edit.focused; + row() + .key(key) + .h(28.0) + .w(220.0) + .px(8.0) + .center_y() + .radius(radius::MD) + .fill(p.field) + .border( + p.hair, + if focused { + p.accent.alpha(0.7) + } else { + p.t.card_border + }, + ) + .child( + rune::text_input(edit, type_::BODY) + .key("input") + .single_line() + .w_full() + .label(label) + .color(p.t.text) + .align(rune::Align::End) + .caret(p.t.text, p.accent.alpha(0.3)) + .on_edit(to) + .on_input_change(|app: &mut App, _| app.form.dirty = true), + ) +} + /// A pill tab; the selected one fills with the accent. pub fn tab( p: Paint, @@ -285,15 +328,23 @@ pub fn tab( .label(label) .checked(selected) .focusable(true) - .px(14.0) + .px(18.0) .h(28.0) .center() .radius(radius::MD) .fill(if selected { - p.accent.alpha(0.16) + p.accent.alpha(0.22) } else { Color::TRANSPARENT }) + .border( + p.hair, + if selected { + p.accent.alpha(0.32) + } else { + Color::TRANSPARENT + }, + ) .hover(p.t.hover) .press(p.t.press) .on_press(on_press) @@ -334,7 +385,7 @@ pub fn button( text(label) .key("label") .font(type_::LABEL) - .color(if primary { Color::WHITE } else { p.t.text }) + .color(if primary { p.accent_ink } else { p.t.text }) .a11y_hidden(), ) }