xonecas.io
8447
Co

conveyor / files

conveyor / templates / library / widgets / ui.rs

3.9 KB
1//! The controls every widget draws with, in the shared look: text buttons, the main action's
2//! accent button, icon buttons, text fields, dropdown cards and Markdown, so they all match.
3use super::{api::{Cx, HostState}, look::Look};
4use rune::{Color, El, TextEdit, TextStyle, Tx, row, text, text_input};
5use rune_kit::markdown::{Markdown, MarkdownStyle};
6
7/// A text button on the press fill. Dimmed and inert unless `enabled`.
8pub fn button(t: &Look, key: &str, label: &str, enabled: bool, press: impl Fn(&mut HostState, &mut Tx) + 'static) -> El {
9    base(t, key, label, enabled, t.press, t.hover, t.text, press)
10}
11
12/// The main action: a button in the accent.
13pub fn primary(t: &Look, key: &str, label: &str, enabled: bool, press: impl Fn(&mut HostState, &mut Tx) + 'static) -> El {
14    base(t, key, label, enabled, t.accent, t.accent.mix(t.on_accent, 0.15), t.on_accent, press)
15}
16
17#[allow(clippy::too_many_arguments)]
18fn base(t: &Look, key: &str, label: &str, enabled: bool, fill: Color, hover: Color, ink: Color, press: impl Fn(&mut HostState, &mut Tx) + 'static) -> El {
19    let b = row().key(key).min_h(28.0).px(12.0).center().radius(6.0).fill(fill).role("button").label(label).disabled(!enabled)
20        .child(text(label).font(t.label).color(ink));
21    if enabled { b.hover(hover).on_press_tx(press) } else { b.opacity(0.5) }
22}
23
24/// A glyph button for headers and rows (□, ×, +, ✎), its `label` as its name and tooltip.
25pub fn icon(t: &Look, key: &str, glyph: &str, label: &str, press: impl Fn(&mut HostState, &mut Tx) + 'static) -> El {
26    row().key(key).min_w(24.0).min_h(24.0).px(4.0).center().radius(6.0).role("button").label(label).help(label).hover(t.press)
27        .on_press_tx(press)
28        .child(text(glyph).font(t.label).color(t.text_secondary))
29}
30
31/// A text field bound to `edit` in `font`, on the background, with an accent caret.
32pub fn field(t: &Look, edit: &TextEdit, font: TextStyle, hint: &str) -> El {
33    text_input(edit, font).w_full().px(8.0).py(6.0).radius(6.0).fill(t.bg).color(t.text)
34        .placeholder(hint, t.text_secondary).caret(t.accent, t.accent.alpha(0.3))
35}
36
37/// The key a popover of instance `cx` anchors to, for the element keyed `key`. Rune looks an
38/// anchor up across the whole window, so two instances of a widget each need their own.
39pub fn anchor(cx: &Cx, key: &str) -> String {
40    format!("{key}@{}", cx.id)
41}
42
43/// A card floating over the rest (a dropdown, a hover card): glass, rounded, with a hairline.
44pub fn popover(t: &Look, e: El) -> El {
45    t.glass(e.radius(8.0)).border(1.0, t.card_border)
46}
47
48/// `src` as Markdown in `font` and `color`: headings in the look's font a step up, accent links,
49/// mono code on the background, filling the width.
50pub fn markdown(t: &Look, src: &str, font: TextStyle, color: Color) -> El {
51    let heading = |k: f32| TextStyle { size: font.size * k, weight: 600.0, line_height: 0.0, ..font };
52    let style = MarkdownStyle::neutral(t.dark).body_font(font).text(color).muted(t.text_secondary).link(t.accent)
53        .heading_font(1, heading(1.3)).heading_font(2, heading(1.15)).heading_font(3, heading(1.0))
54        .code_block_font(t.mono).code_block(t.bg, t.card_border).inline_code_bg(t.text.alpha(0.08))
55        .rule(t.separator).quote_rule(t.separator);
56    Markdown::new(src).render(&style).w_full()
57}
58
59/// A × that forgets or removes something on a second press: the first arms it, red, showing
60/// `armed_glyph`, and the caller's `press` sees both.
61pub fn armed(t: &Look, key: &str, label: &str, armed: bool, armed_glyph: &str, press: impl Fn(&mut HostState, &mut Tx) + 'static) -> El {
62    let b = row().key(key).min_w(24.0).min_h(24.0).px(4.0).center().radius(6.0).role("button").label(label)
63        .help(if armed { "Press again to confirm" } else { label }).on_press_tx(press);
64    if armed { b.fill(t.danger).child(text(armed_glyph).font(t.label).color(Color::WHITE)) }
65    else { b.hover(t.press).child(text("×").font(t.label).color(t.text_secondary)) }
66}