//! Shared colours and small controls for the screens. use rune::tok::{Theme, motion, radius, space, type_}; use rune::{Color, Drag, El, Phase, TextStyle, col, el, row, text}; use crate::app::App; #[derive(Clone, Copy)] pub struct Paint { pub t: Theme, pub accent: Color, 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: 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, } } pub fn muted(&self) -> Color { self.t.text_secondary } } pub fn heading(size: f32) -> TextStyle { TextStyle::new(size, 600.0) } pub fn body(size: f32) -> TextStyle { TextStyle::new(size, 400.0) } /// A titled card of rows with hairlines between them. pub fn card(p: Paint, title: Option<&str>, rows: Vec) -> El { card_action(p, title, None, rows) } /// [`card`] with a control in its header, such as the button that adds to the list below. pub fn card_action(p: Paint, title: Option<&str>, action: Option, rows: Vec) -> El { let count = rows.len(); let mut inner = col() .key("card") .padding(space::S1) .fill(p.t.card) .radius(radius::LG) .border(p.hair, p.t.card_border) .shadow(1.0, 2.0, p.t.shadow) .shadow(8.0, 24.0, p.t.shadow_soft); for (i, r) in rows.into_iter().enumerate() { inner = inner.child(r); if i + 1 < count { inner = inner.child( el().key(format!("sep{i}")) .h(p.hair) .ml(space::S3) .mr(space::S3) .fill(p.t.separator) .a11y_hidden(), ); } } let mut section = col().gap(space::S2); if let Some(title) = title { let mut header = row() .key("header") .w_full() .center_y() .justify_between() .child( text(title) .key("header-label") .font(type_::SECTION) .color(p.t.text_secondary) .ml(space::S3), ); if let Some(action) = action { header = header.child(action); } section = section.child(header); } section.child(inner) } /// The 44-point row every setting sits in: a label on the left, a control on the right. pub fn setting_row(p: Paint, key: &str, label: &str, control: El) -> El { row() .key(key) .h(44.0) .px(space::S3) .center_y() .justify_between() .radius(radius::MD) .label(label) .child( text(label) .key("label") .font(type_::BODY) .color(p.t.text) .a11y_hidden(), ) .child(control) } /// A row with a subtitle under the label. pub fn setting_row_with(p: Paint, key: &str, label: &str, subtitle: &str, control: El) -> El { row() .key(key) .h(52.0) .px(space::S3) .center_y() .justify_between() .radius(radius::MD) .label(label) .child( col() .key("labels") .gap(1.0) .child( text(label) .key("label") .font(type_::BODY) .color(p.t.text) .a11y_hidden(), ) .child( text(subtitle) .key("subtitle") .font(body(11.5)) .color(p.t.text_secondary) .a11y_hidden(), ), ) .child(control) } /// A 38 × 22 switch with a 18-point knob. pub fn switch(p: Paint, key: &str, on: bool) -> El { let (w, h, k) = (38.0, 22.0, 18.0); let inset = (h - k) / 2.0; el().key(key) .role("switch") .size(w, h) .radius(h / 2.0) .fill(if on { p.accent } else { p.t.control_off }) .motion(motion::SNAPPY) .child( el().key("knob") .size(k, k) .absolute(if on { w - k - inset } else { inset }, inset) .radius(k / 2.0) .fill(p.t.knob) .shadow(1.0, 3.0, p.t.knob_shadow) .shadow(0.0, 0.5, p.t.edge) .a11y_hidden(), ) } /// A labelled switch row. The whole row is the target and the keyboard stop. pub fn toggle_row( p: Paint, key: &str, label: &str, on: bool, flip: impl Fn(&mut App) + Send + Sync + 'static, ) -> El { setting_row( p, key, label, switch(p, &format!("{key}-switch"), on).a11y_hidden(), ) .role("switch") .checked(on) .focusable(true) .hover(p.t.hover) .press(p.t.press) .on_press(flip) } /// A 0-100 slider with its value spelled out on the right. pub fn slider( p: Paint, key: impl Into, label: &'static str, value: u32, on_change: impl Fn(&mut App, u32) + Send + Sync + 'static, ) -> El { slider_width(p, key, label, 180.0, value, on_change) } /// [`slider`] at a given width, for the player bar where 180 points is too much. pub fn slider_width( p: Paint, key: impl Into, label: &'static str, width: f32, value: u32, on_change: impl Fn(&mut App, u32) + Send + Sync + 'static, ) -> El { let key = key.into(); let (w, h, k, bar) = (width, 22.0, 18.0, 4.0); let travel = w - k; let v = (value.min(100) as f32 / 100.0).clamp(0.0, 1.0); let to_value = move |x: f32| (((x - k / 2.0) / travel).clamp(0.0, 1.0) * 100.0).round() as u32; let on_change = std::sync::Arc::new(on_change); let drag_change = on_change.clone(); let step_change = on_change; row() .key(format!("{key}-control")) .gap(space::S3) .center_y() .child( el().key(key) .role("slider") .label(label) .range_value(f64::from(value.min(100)), 0.0, 100.0) .range_step(5.0) .a11y_value(format!("{value}%")) .on_step(move |s: &mut App, steps, _| { let next = (i64::from(value) + i64::from(steps) * 5).clamp(0, 100) as u32; step_change(s, next); }) .size(w, h) .motion(motion::SNAPPY) .on_drag(move |s: &mut App, d: &mut Drag, tx| match d.phase { Phase::Down => { d.data = v; let knob = k / 2.0 + v * travel; if (d.x - knob).abs() > k / 2.0 + 2.0 { let next = to_value(d.x); drag_change(s, next); } } Phase::Move => { let knob = k / 2.0 + d.data * travel; let grab = if (d.start_x - knob).abs() <= k / 2.0 + 2.0 { d.start_x - knob } else { 0.0 }; let next = to_value(d.x - grab); drag_change(s, next); tx.instant(); } Phase::Up => {} }) .child( el().key("track") .size(w, bar) .absolute(0.0, (h - bar) / 2.0) .radius(bar / 2.0) .fill(p.t.control_off) .a11y_hidden(), ) .child( el().key("fill") .size(k / 2.0 + v * travel, bar) .absolute(0.0, (h - bar) / 2.0) .radius(bar / 2.0) .fill(p.accent) .a11y_hidden(), ) .child( el().key("knob") .size(k, k) .absolute(v * travel, (h - k) / 2.0) .radius(k / 2.0) .fill(p.t.knob) .shadow(1.0, 3.0, p.t.knob_shadow) .shadow(0.0, 0.5, p.t.edge) .a11y_hidden(), ), ) .child( text(format!("{value}%")) .key("value") .w(40.0) .font(type_::MONO_CAPTION) .color(p.t.text_secondary) .align(rune::Align::End) .a11y_hidden(), ) } /// 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 { text_field_width(p, key, label, 220.0, rune::Align::End, edit, to, |app| { app.form.dirty = true; }) } /// [`text_field`] at a given width and text alignment; a dialog field is wide and starts at its /// left edge, where typing begins. #[allow(clippy::too_many_arguments)] // the columns of a field: size, alignment and what typing does pub fn text_field_width( p: Paint, key: &'static str, label: &str, width: f32, align: rune::Align, edit: &rune::TextEdit, to: fn(&mut App) -> &mut rune::TextEdit, on_input: fn(&mut App), ) -> El { let focused = edit.focused; row() .key(key) .h(28.0) .w(width) .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(align) .caret(p.t.text, p.accent.alpha(0.3)) .on_edit(to) .on_input_change(move |app: &mut App, _| on_input(app)), ) } /// A pill tab; the selected one fills with the accent. pub fn tab( p: Paint, key: &str, label: &str, selected: bool, on_press: impl Fn(&mut App) + Send + Sync + 'static, ) -> El { el().key(key) .role("tab") .label(label) .checked(selected) .focusable(true) .px(18.0) .h(28.0) .center() .radius(radius::MD) .fill(if selected { 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) .child( text(label) .key("label") .font(type_::LABEL) .color(if selected { p.accent } else { p.t.text_secondary }) .a11y_hidden(), ) } /// A small pill button. pub fn button( p: Paint, key: &str, label: &str, primary: bool, on_press: impl Fn(&mut App) + Send + Sync + 'static, ) -> El { el().key(key) .role("button") .label(label) .focusable(true) .h(28.0) .px(14.0) .center() .radius(radius::MD) .fill(if primary { p.accent } else { p.t.control_off }) .hover(p.t.hover) .press(p.t.press) .on_press(on_press) .child( text(label) .key("label") .font(type_::LABEL) .color(if primary { p.accent_ink } else { p.t.text }) .a11y_hidden(), ) } /// One transport control: an icon, its accessible label, round like the bar it sits in. pub fn media_button( p: Paint, key: &str, icon: &'static rune::Icon, label: &str, primary: bool, on_press: impl Fn(&mut App) + Send + Sync + 'static, ) -> El { let ink = if primary { p.accent_ink } else { p.t.text }; el().key(key) .role("button") .label(label) .focusable(true) .size(32.0, 32.0) .center() .radius(radius::MD) .fill(if primary { p.accent } else { p.t.control_off }) .hover(p.t.hover) .press(p.t.press) .on_press(on_press) .child(rune::icon(icon, 16.0).color(ink).a11y_hidden()) } /// A small square button holding one glyph, for the corner of a card or a dialog. pub fn icon_button( p: Paint, key: &str, glyph: &str, label: &str, primary: bool, on_press: impl Fn(&mut App) + Send + Sync + 'static, ) -> El { el().key(key) .role("button") .label(label) .focusable(true) .size(24.0, 24.0) .center() .radius(radius::MD) .fill(if primary { p.accent.alpha(0.14) } else { Color::TRANSPARENT }) .border( p.hair, if primary { p.accent.alpha(0.32) } else { Color::TRANSPARENT }, ) .hover(p.t.hover) .press(p.t.press) .on_press(on_press) .child( text(glyph) .key("glyph") .font(body(if primary { 16.0 } else { 15.0 })) .color(if primary { p.accent } else { p.muted() }) .a11y_hidden(), ) } /// A primary button with nothing to do yet, for a dialog whose fields are still empty. pub fn button_muted(p: Paint, key: &str, label: &str) -> El { el().key(key) .role("button") .label(label) .disabled(true) .h(28.0) .px(14.0) .center() .radius(radius::MD) .fill(p.accent.alpha(0.28)) .child( text(label) .key("label") .font(type_::LABEL) .color(p.accent_ink.alpha(0.55)) .a11y_hidden(), ) } /// A process's icon as Windows draws it for the executable, with its initial until the icon is /// decoded and for processes whose executable has none. pub fn process_icon(app: &App, p: Paint, process: &str, size: f32) -> El { let radius = (size * 0.22).max(2.0); let initial = process .trim() .chars() .next() .map(|c| c.to_ascii_uppercase().to_string()) .unwrap_or_else(|| "?".to_string()); let image = app .processes .get(&crate::process::normalize(process)) .and_then(|found| found.path.as_deref()) .and_then(|path| { app.icons .as_ref() .map(|icons| icons.app_icon_scaled(path, size, p.scale)) }); let tile = el() .key("process-icon") .size(size, size) .radius(radius) .fill(p.t.control_off) .a11y_hidden(); match image { Some(image) => tile.child( el().key("process-image") .w_full() .h_full() .radius(radius) .pixels(image.pixels()), ), None => tile.child( text(initial) .key("initial") .font(body(size * 0.52)) .color(p.muted()) .center(), ), } } /// Whether a process is up: a chip beside its name in the list. pub fn process_state(p: Paint, running: bool, width: f32) -> El { let color = if running { p.success } else { p.muted() }; row() .key("process-state") .w(width) .h(20.0) .px(8.0) .gap(6.0) .center_y() .radius(radius::MD) .fill(color.alpha(0.12)) .child( el().key("state-dot") .size(6.0, 6.0) .radius(3.0) .fill(color) .a11y_hidden(), ) .child( text(if running { "Running" } else { "Not found" }) .key("state-label") .font(body(10.5)) .color(color), ) } /// A coloured dot used for status. pub fn dot(color: Color) -> El { el().key("dot") .size(8.0, 8.0) .radius(4.0) .fill(color) .a11y_hidden() } /// A square champion portrait at `size` points: the Data Dragon file once it is on disk, /// initials on a colour derived from the name until then (and offline). pub fn champion_portrait(app: &App, p: Paint, champion: Option<&str>, size: f32) -> El { let name = champion.unwrap_or_default(); let hue = name .bytes() .fold(2166136261u32, |hash, byte| { hash.wrapping_mul(16777619) ^ byte as u32 }) .rem_euclid(360) as f32; let (fill, ink) = if name.is_empty() { (p.t.control_off, p.muted()) } else { (rune::oklch(0.36, 0.075, hue), rune::oklch(0.94, 0.02, hue)) }; let radius = (size * 0.14).max(2.0); let tile = el() .key("portrait") .size(size, size) .radius(radius) .fill(fill) .border(p.hair, ink.alpha(0.18)) .a11y_hidden(); match app .portraits .as_ref() .and_then(|portraits| portraits.image(name, size, p.scale)) { Some(image) => tile.child( el().key("portrait-image") .w_full() .h_full() .radius(radius) .pixels(image.pixels()), ), None => tile.child( text(initials(name)) .font(heading(size * 0.33)) .color(ink) .center(), ), } } /// Up to two letters from the champion's name: `Kai'Sa` is KS, `Jinx` is J. fn initials(name: &str) -> String { let letters: Vec = name .split(|c: char| !c.is_alphanumeric()) .filter(|word| !word.is_empty()) .filter_map(|word| word.chars().next()) .take(2) .collect(); if letters.is_empty() { "?".into() } else { letters.into_iter().collect() } } /// A scroll viewport's scroll bar: a thumb in a strip of its own at the viewport's end edge, /// shown whenever the content overflows, and dragged to scroll. /// /// The screen lays the thumb where the viewport's strip is (the content leaves it room), so it /// never draws over a card. `follow` takes where the viewport is, `thumb` draws it. pub struct ScrollBar { area: rune::Live<[f32; 4]>, thumb: rune::Live<[f32; 4]>, last: std::rc::Rc>, } impl Default for ScrollBar { fn default() -> Self { Self::new() } } impl ScrollBar { pub fn new() -> Self { Self { area: rune::Live::new([0.0; 4]), thumb: rune::Live::new([0.0; 4]), last: std::rc::Rc::new(std::cell::Cell::new(rune::ScrollPos::default())), } } /// Where the viewport is scrolled to, as of the last frame that reported it. /// /// A screen can read this to load more rows as the end of a long list comes into view. pub fn position(&self) -> rune::ScrollPos { self.last.get() } /// Make this viewport's scrolling move the thumb, and its key one a drag can scroll to. pub fn follow(&self, key: &'static str, viewport: El) -> El { let reported = self.area.clone(); let area = self.area.clone(); let thumb = self.thumb.clone(); let layout_thumb = self.thumb.clone(); let last = self.last.clone(); let layout_last = self.last.clone(); viewport .on_layout_tx(move |_app: &mut App, rect, tx| { reported.set(rect); place(&layout_thumb, rect, layout_last.get()); tx.live_only(); }) .on_scroll_offset(move |at: rune::ScrollPos| { last.set(at); place(&thumb, area.get(), at); }) .key(key) } /// The thumb, drawn over the window. `key` must differ per bar on one screen. pub fn thumb(&self, p: Paint, key: &'static str, viewport: &'static str) -> El { let last = self.last.clone(); let area = self.area.clone(); el().key(key) .absolute(0.0, 0.0) .size(10.0, 0.0) .radius(5.0) .fill(p.t.text_secondary.alpha(0.45)) .hover(p.t.text_secondary.alpha(0.8)) .live_rect(&self.thumb) .a11y_hidden() .on_drag_any(move |d: &mut rune::Drag, tx: &mut rune::Tx| { tx.live_only(); let at = last.get(); let max = at.max[1]; let [_, _, _, area_h] = area.get(); let travel = (area_h - d.h).max(1.0); if max <= 0.5 || area_h <= 1.0 { return; } match d.phase { rune::Phase::Down => d.data = at.offset[1], rune::Phase::Move => { let y = (d.data + d.dy * max / travel).clamp(0.0, max); tx.scroll_to(viewport, 0.0, y); } rune::Phase::Up => {} } }) } } /// Put the thumb where `at` says the viewport is, inside `area`'s window rect. fn place(thumb: &rune::Live<[f32; 4]>, [x, y, w, h]: [f32; 4], at: rune::ScrollPos) { let content = at.viewport[1] + at.max[1]; if h <= 1.0 || w <= 1.0 || at.max[1] <= 0.5 || content <= 0.0 { thumb.set([0.0, 0.0, 0.0, 0.0]); return; } let height = (h * at.viewport[1] / content).clamp(28.0, h); let progress = (at.offset[1] / at.max[1]).clamp(0.0, 1.0); thumb.set([x + w - 12.0, y + progress * (h - height), 10.0, height]); }