1use crate::app::App; 2use crate::ui::theme::{self, pal}; 3use rune::{Color, El, TextEdit, col, el, row, text}; 4 5#[derive(Clone, Copy, PartialEq)] 6pub enum Kind { 7 Normal, 8 Primary, 9 Danger, 10 Ghost, 11} 12 13pub fn lbl(s: &str) -> El { 14 text(s).font(theme::SMALL).color(pal().faint) 15} 16 17pub fn mono(s: impl Into<String>, color: Color) -> El { 18 text(s).font(theme::SMALL).color(color) 19} 20 21fn pressable(e: El, label: &str) -> El { 22 e.role("button") 23 .label(label) 24 .focusable(true) 25 .cursor(rune::Cursor::Pointer) 26 .hover(pal().hover) 27} 28 29fn base(label: &str, kind: Kind) -> El { 30 let p = pal(); 31 let (fill, ink) = match kind { 32 Kind::Primary => (p.accent, p.accent_fg), 33 Kind::Normal => (p.raised, p.fg), 34 Kind::Danger => (p.raised, p.danger), 35 Kind::Ghost => (Color::TRANSPARENT, p.muted), 36 }; 37 row() 38 .h(theme::CONTROL) 39 .px(8.0) 40 .radius(4.0) 41 .fill(fill) 42 .center_y() 43 .child(mono(label, ink).truncate_middle().min_w(0.0)) 44} 45 46pub fn btn(label: &str, kind: Kind, on: Option<impl Fn(&mut App) + 'static>) -> El { 47 match on { 48 Some(f) => pressable(base(label, kind), label).on_press(f), 49 None => base(label, kind).opacity(0.45), 50 } 51} 52 53pub fn btn_tx(label: &str, kind: Kind, on: impl Fn(&mut App, &mut rune::Tx) + 'static) -> El { 54 pressable(base(label, kind), label).on_press_tx(on) 55} 56 57pub fn link(label: &str, on: impl Fn(&mut App) + 'static) -> El { 58 pressable(mono(label, pal().muted), label) 59 .radius(4.0) 60 .on_press(on) 61} 62 63pub fn tab(label: &str, on: bool) -> El { 64 let p = pal(); 65 let r = row() 66 .h_full() 67 .px(12.0) 68 .gap(8.0) 69 .center_y() 70 .child(mono(label, if on { p.strong } else { p.muted })); 71 if on { 72 r.fill(p.raised) 73 .border_sides([2.0, 0.0, 0.0, 0.0], p.accent) 74 } else { 75 r.hover(p.hover) 76 } 77} 78 79pub fn ph(left: El, right: El) -> El { 80 row() 81 .h(32.0) 82 .px(12.0) 83 .fill(pal().surface) 84 .center_y() 85 .justify_between() 86 .child(left) 87 .child(right) 88} 89 90pub fn section(title: &str, body: El) -> El { 91 col() 92 .gap(4.0) 93 .child(lbl(title).px(12.0)) 94 .child(body.fill(pal().surface).radius(4.0).clip_rounded(4.0)) 95} 96 97pub fn hd(left: &str, right: &str) -> El { 98 row() 99 .px(12.0) 100 .pt(12.0) 101 .pb(4.0) 102 .gap(8.0) 103 .justify_between() 104 .child(lbl(left).truncate().min_w(0.0)) 105 .child(lbl(right)) 106} 107 108pub fn item(on: bool, content: El) -> El { 109 let p = pal(); 110 let r = row() 111 .px(12.0) 112 .py(2.0) 113 .gap(6.0) 114 .center_y() 115 .min_w(0.0) 116 .focusable(true) 117 .child(content.grow(1.0).min_w(0.0)); 118 if on { 119 r.fill(p.soft).border_sides([0.0, 0.0, 0.0, 2.0], p.accent) 120 } else { 121 r.hover(p.hover) 122 } 123} 124 125pub fn seg<T: Copy + PartialEq + 'static>( 126 opts: &[(T, &str)], 127 cur: T, 128 on: impl Fn(&mut App, T) + Clone + 'static, 129) -> El { 130 let p = pal(); 131 row() 132 .h(theme::CONTROL) 133 .radius(4.0) 134 .border(1.0, p.line) 135 .clip_rounded(4.0) 136 .children(opts.iter().map(|&(v, l)| { 137 let sel = v == cur; 138 let on = on.clone(); 139 row() 140 .h_full() 141 .px(8.0) 142 .center_y() 143 .fill(if sel { p.accent } else { Color::TRANSPARENT }) 144 .role("radio") 145 .label(l) 146 .checked(sel) 147 .focusable(true) 148 .cursor(rune::Cursor::Pointer) 149 .on_press(move |s: &mut App| on(s, v)) 150 .child(mono(l, if sel { p.accent_fg } else { p.fg })) 151 })) 152} 153 154pub fn stat(value: &str, label: &str, color: Color) -> El { 155 col() 156 .pl(16.0) 157 .pr(24.0) 158 .h_full() 159 .justify_center() 160 .child(text(value).font(theme::TITLE).color(color)) 161 .child(mono(label, pal().muted)) 162} 163 164pub fn bar(pct: f32, color: Color) -> El { 165 el().h(3.0) 166 .w_full() 167 .fill(pal().raised) 168 .child(el().h(3.0).w_frac(pct.clamp(0.0, 1.0)).fill(color)) 169} 170 171pub fn input( 172 edit: &TextEdit, 173 placeholder: &str, 174 access: impl for<'a> Fn(&'a mut App) -> &'a mut TextEdit + 'static, 175) -> rune_kit::TextInput { 176 rune_kit::TextInput::new(edit) 177 .placeholder(placeholder) 178 .text_style(theme::BODY) 179 .height(theme::CONTROL) 180 .on_edit(access) 181}