//! Shared colours and small controls for the screens. use rune::tok::{self, 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 dark: bool, pub hair: f32, } impl Paint { pub fn new(dark: bool, scale: f32) -> Self { Self { t: tok::theme(dark), accent: tok::accent(0, dark), dark, hair: 1.0 / scale, } } 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 { 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 { 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 { section = section.child( text(title) .key("header") .font(type_::SECTION) .color(p.t.text_secondary) .ml(space::S3), ); } 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: &'static str, label: &'static str, value: u32, on_change: impl Fn(&mut App, u32) + Send + Sync + 'static, ) -> El { let (w, h, k, bar) = (180.0, 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 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(14.0) .h(28.0) .center() .radius(radius::MD) .fill(if selected { p.accent.alpha(0.16) } 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 { Color::WHITE } else { p.t.text }) .a11y_hidden(), ) } /// 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() }