8447
Co
conveyor / files
conveyor / templates / library / widgets / widget_menu.rs
5.7 KB
1//! The title bar's Widgets spotlight: type to filter every widget that can go on a canvas, or 2//! browse the list. Enter or a click adds the highlighted one to the shown project's canvas. 3use super::{api::*, look, canvas::Canvas, titlebar::TitleBar}; 4use rune::{El, Key, KeyEvent, ScrollSpec, TextEdit, Tx, col, row, text, text_input}; 5 6pub const WIDGET: Widget = Widget { title: "Widgets menu", size: (0.0, 0.0), new: |_| { let mut m = WidgetMenu::default(); m.reset(); Box::new(m) } }; 7 8/// `search`: what's typed; `active`: the highlighted row among the matches; `query`: the search 9/// `active` was picked for, so typing starts again from the best match. 10#[derive(Default)] 11pub struct WidgetMenu { pub search: TextEdit, active: usize, query: String } 12 13impl WidgetMenu { 14 /// Opened again: an empty, focused search with the first row highlighted. 15 pub fn reset(&mut self) { 16 (self.search, self.active, self.query) = (TextEdit::new(""), 0, String::new()); 17 self.search.focused = true; 18 } 19} 20 21impl View for WidgetMenu { 22 fn update(&mut self, _cx: &Cx) { 23 if self.search.text != self.query { (self.active, self.query) = (0, self.search.text.clone()) } 24 } 25 26 fn view(&self, cx: &Cx) -> El { 27 let t = look::of(cx); 28 let found = matches(cx, &self.search.text); 29 let active = self.active.min(found.len().saturating_sub(1)); 30 let field = text_input(&self.search, t.body).key("search").single_line().w_full().px(12.0).py(10.0) 31 .color(t.text).placeholder("Search widgets…", t.text_secondary).caret(t.accent, t.accent.alpha(0.3)) 32 .on_edit(edit(cx.id, |m: &mut WidgetMenu| &mut m.search)) 33 .on_input_key(keys(cx, found.clone())); 34 let mut list = col().w_full().gap(2.0).padding(6.0); 35 if found.is_empty() { list = list.child(text("No widget matches").font(t.label).color(t.text_secondary).px(10.0).py(8.0)) } 36 for (i, (name, w)) in found.iter().enumerate() { list = list.child(item(cx, name, w, i == active)) } 37 col().w_full().shrink().min_h(0.0) 38 .child(row().w_full().center_y().pl(12.0).child(text("⌕").font(t.body).color(t.text_secondary)).child(field)) 39 .child(row().w_full().h(1.0).fill(t.separator)) 40 .child(col().key("kinds").w_full().shrink().min_h(0.0).scroll(ScrollSpec { stick_bottom: false }).child(list)) 41 } 42} 43 44/// The kinds that can go on a canvas (layouts, size 0, fill a parent instead) whose title, file 45/// name or description holds every word typed; title matches first. 46fn matches(cx: &Cx, query: &str) -> Vec<(&'static str, &'static Widget)> { 47 let words: Vec<String> = query.split_whitespace().map(str::to_lowercase).collect(); 48 let mut found: Vec<_> = cx.kinds().iter().filter(|(_, w)| w.size.0 > 0.0 && w.size.1 > 0.0) 49 .map(|(name, w)| (*name, *w, w.title.to_lowercase(), format!("{name} {}", cx.about(name)).to_lowercase())) 50 .filter(|(_, _, title, rest)| words.iter().all(|q| title.contains(q.as_str()) || rest.contains(q.as_str()))) 51 .collect(); 52 found.sort_by_key(|(_, _, title, _)| !words.iter().all(|q| title.contains(q.as_str()))); 53 found.into_iter().map(|(name, w, ..)| (name, w)).collect() 54} 55 56/// Up and Down move the highlight, Enter adds the highlighted kind, Ctrl+O closes. Escape reaches the dropdown. 57fn keys(cx: &Cx, found: Vec<(&'static str, &'static Widget)>) -> impl Fn(&mut HostState, &KeyEvent, &mut Tx) -> bool + 'static { 58 let (id, canvas) = (cx.id.to_owned(), canvas_of(cx)); 59 move |h: &mut HostState, k: &KeyEvent, tx: &mut Tx| { 60 if super::titlebar::menu_key(h, id.rsplit_once('.').map_or("", |(p, _)| p), k, tx) { return true } 61 let Some(m) = get::<WidgetMenu>(h, &id) else { return false }; 62 let last = found.len().saturating_sub(1); 63 let at = m.active.min(last); 64 m.active = match k.key { 65 Key::Down => (at + 1).min(last), 66 Key::Up => at.saturating_sub(1), 67 Key::Enter => { if let Some((name, w)) = found.get(at) { add(h, &id, &canvas, name, w.size) } return true } 68 _ => return false, 69 }; 70 if let Some((name, _)) = found.get(m.active) { tx.scroll_into_view(format!("kind-{name}")) } 71 true 72 } 73} 74 75/// One kind: a glyph, its title and description, as a button that adds it. `active` is the row 76/// Enter adds. 77fn item(cx: &Cx, name: &'static str, w: &Widget, active: bool) -> El { 78 let t = look::of(cx); 79 let about = cx.about(name); 80 let r = row().key(format!("kind-{name}")).w_full().px(10.0).py(8.0).gap(10.0).radius(6.0) 81 .child(text("◆").font(t.label).color(t.accent)) 82 .child(col().grow(1.0).min_w(0.0).gap(2.0) 83 .child(text(w.title).font(t.body).color(t.text).truncate()) 84 .child(text(if about.is_empty() { format!("{name}.rs") } else { about.to_owned() }) 85 .font(t.label).color(t.text_secondary).w_full().truncate())); 86 let (id, canvas, size) = (cx.id.to_owned(), canvas_of(cx), w.size); 87 let r = r.role("button").label(format!("Add {}", w.title)).hover(t.hover) 88 .on_press_tx(move |h: &mut HostState, _: &mut Tx| add(h, &id, &canvas, name, size)); 89 if active { r.fill(t.press) } else { r } 90} 91 92/// The canvas root shows, beside the title bar that holds this menu. 93fn canvas_of(cx: &Cx) -> String { 94 let bar = cx.id.rsplit_once('.').map_or("", |(p, _)| p); 95 format!("{}.{}", bar.rsplit_once('.').map_or("root", |(p, _)| p), super::root::desktop(cx)) 96} 97 98/// Adds `name` to the canvas and closes the dropdown (the title bar, this instance's parent). 99fn add(h: &mut HostState, id: &str, canvas: &str, name: &'static str, size: (f32, f32)) { 100 if let Some(c) = get::<Canvas>(h, canvas) { c.add_free(name, size) } 101 if let Some(b) = get::<TitleBar>(h, id.rsplit_once('.').map_or("", |(p, _)| p)) { b.open = false } 102}