8447
Co
conveyor / files
conveyor / templates / library / widgets / keychain.rs
6.5 KB
1//! Your keychain: every secret you keep (API keys, tokens, passwords) by name. Add one, edit 2//! its name or value, or remove it; values stay masked unless you show them. 3use super::{api::*, look, canvas, ui}; 4use rune::{El, ScrollSpec, TextEdit, Tx, col, el, row, text}; 5 6pub const WIDGET: Widget = Widget { title: "Keychain", size: (360.0, 420.0), new: |_| Box::new(Keychain::new()) }; 7 8/// Nothing is kept in the instance's folder: secrets live in the keychain alone. 9pub struct Keychain { 10 /// The keychain's names, read again each second. 11 names: Vec<String>, 12 listed_at: f64, 13 /// The form: a name, its value, and the name being edited (None while adding). 14 name: TextEdit, 15 value: TextEdit, 16 editing: Option<String>, 17 shown: bool, 18 /// The name whose × was pressed once: a second press removes it. 19 removing: Option<String>, 20} 21 22impl Keychain { 23 fn new() -> Self { 24 let mut k = Keychain { names: vec![], listed_at: 0.0, name: TextEdit::new(""), value: TextEdit::new(""), editing: None, shown: false, removing: None }; 25 k.reset(); 26 k 27 } 28 29 fn reset(&mut self) { 30 (self.name, self.value, self.editing, self.shown, self.listed_at) = (TextEdit::new(""), TextEdit::new("").secure(), None, false, 0.0); 31 } 32} 33 34impl View for Keychain { 35 fn update(&mut self, cx: &Cx) { 36 if cx.now - self.listed_at > 1.0 { (self.names, self.listed_at) = (cx.secrets(), cx.now) } 37 } 38 39 fn view(&self, cx: &Cx) -> El { 40 let t = look::of(cx); 41 let mut list = col().w_full().gap(2.0); 42 if self.names.is_empty() { list = list.child(text("No secrets yet. Add one below.").font(t.label).color(t.text_secondary)) } 43 for n in &self.names { list = list.child(self.item(cx, n)) } 44 col().w_full().h_full().gap(8.0) 45 .child(row().key("header").w_full().center_y().gap(8.0) 46 .child(text("Keychain").font(t.title).color(t.text)) 47 .child(el().grow(1.0)) 48 .child(text(format!("{} secrets", self.names.len())).font(t.label).color(t.text_secondary)) 49 .child(canvas::maximize(cx)) 50 .child(canvas::close(cx, "Close keychain"))) 51 .child(el().w_full().h(1.0).fill(t.separator)) 52 .child(col().key("secrets").w_full().grow(1.0).min_h(0.0).scroll(ScrollSpec { stick_bottom: false }).child(list)) 53 .child(el().w_full().h(1.0).fill(t.separator)) 54 .child(self.form(cx)) 55 } 56} 57 58impl Keychain { 59 /// One secret: its name and a mask, ✎ to edit it in the form, × twice to remove it. 60 fn item(&self, cx: &Cx, name: &str) -> El { 61 let t = look::of(cx); 62 let (id, id2, n, m) = (cx.id.to_owned(), cx.id.to_owned(), name.to_owned(), name.to_owned()); 63 let removing = self.removing.as_deref() == Some(name); 64 let editing = self.editing.as_deref() == Some(name); 65 row().key(format!("secret-{name}")).w_full().min_h(36.0).px(8.0).gap(8.0).center_y().radius(6.0) 66 .fill(if editing { t.press } else { t.card }).hover(t.hover) 67 .child(text(name).font(t.body).color(t.text).truncate().min_w(0.0).shrink()) 68 .child(el().grow(1.0)) 69 .child(text("••••••••").font(t.label).color(t.text_secondary)) 70 .child(ui::icon(&t, "edit", "✎", &format!("Edit {name}"), move |h: &mut HostState, _: &mut Tx| { 71 let value = Host::live(h).secret(&n).unwrap_or_default(); 72 let Some(k) = get::<Keychain>(h, &id) else { return }; 73 (k.name, k.value, k.editing, k.shown, k.removing) = (TextEdit::new(n.clone()), TextEdit::new(value).secure(), Some(n.clone()), false, None); 74 })) 75 .child(ui::armed(&t, "remove", &format!("Remove {name}"), removing, "Remove?", move |h: &mut HostState, _: &mut Tx| { 76 let Some(k) = get::<Keychain>(h, &id2) else { return }; 77 if k.removing.as_deref() != Some(m.as_str()) { k.removing = Some(m.clone()); return } 78 if k.editing.as_deref() == Some(m.as_str()) { k.reset() } 79 (k.removing, k.listed_at) = (None, 0.0); 80 set_secret(h, &m, ""); 81 })) 82 } 83 84 /// Name and value fields, 👁 to show the value, Save (or Enter), and Cancel while editing. 85 fn form(&self, cx: &Cx) -> El { 86 let t = look::of(cx); 87 let field = |e: &TextEdit, key: &str, hint: &str| ui::field(&t, e, t.body, hint).key(key).single_line().on_submit(save(cx.id)); 88 let ready = !self.name.text.trim().is_empty() && !self.value.text.is_empty(); 89 let mut actions = row().w_full().gap(8.0).center_y() 90 .child(text(if self.editing.is_some() { "Editing" } else { "New secret" }).font(t.label).color(t.text_secondary)) 91 .child(el().grow(1.0)); 92 if self.editing.is_some() { 93 actions = actions.child(ui::button(&t, "cancel", "Cancel", true, on(cx.id, |k: &mut Keychain, _| k.reset()))); 94 } 95 actions = actions.child(ui::primary(&t, "save", "Save", ready, save(cx.id))); 96 col().key("form").w_full().gap(6.0) 97 .child(field(&self.name, "name", "Name, such as openrouter").on_edit(edit(cx.id, |k: &mut Keychain| &mut k.name))) 98 .child(row().w_full().gap(6.0).center_y() 99 .child(row().grow(1.0).min_w(0.0).child(field(&self.value, "value", "Value").secure().revealed(self.shown) 100 .on_edit(edit(cx.id, |k: &mut Keychain| &mut k.value)))) 101 .child(row().key("show").min_w(28.0).min_h(28.0).center().radius(6.0).role("button").label(if self.shown { "Hide value" } else { "Show value" }) 102 .fill(if self.shown { t.press } else { t.card }).hover(t.hover) 103 .child(text("👁").font(t.label).color(if self.shown { t.text } else { t.text_secondary })) 104 .on_press_tx(on(cx.id, |k: &mut Keychain, _| { k.shown = !k.shown; k.value.set_revealed(k.shown) })))) 105 .child(actions) 106 } 107} 108 109/// Saves the form: the value under its name, and, when an edit renamed it, forgets the old name. 110fn save(id: &str) -> impl Fn(&mut HostState, &mut Tx) + 'static { 111 let id = id.to_owned(); 112 move |h: &mut HostState, _: &mut Tx| { 113 let Some(k) = get::<Keychain>(h, &id) else { return }; 114 let (name, value) = (k.name.text.trim().to_owned(), k.value.text.clone()); 115 if name.is_empty() || value.is_empty() { return } 116 let old = k.editing.clone().filter(|o| *o != name); 117 k.reset(); 118 set_secret(h, &name, &value); 119 if let Some(o) = old { set_secret(h, &o, "") } 120 } 121} 122