8447
Co
conveyor / files
conveyor / templates / library / widgets / titlebar.rs
5.9 KB
1//! The window's title bar: drag to move it, a ◆ Widgets button (or Ctrl+O) that drops down a search of the library, the 2//! running indicator centered, and its buttons: set or clear the project's background image, tile or float the canvas's cards, minimize, maximize, close. 3use super::{api::*, canvas, look, ui, widget_menu::WidgetMenu}; 4use rune::{Align, El, FileDialogOptions, KeyEvent, Keybind, Shape, Side, Tx, col, el, row, text}; 5 6pub const WIDGET: Widget = Widget { title: "Title bar", size: (0.0, 36.0), new: |_| Box::new(TitleBar { open: false }) }; 7 8/// `open`: the Widgets dropdown shows. 9pub struct TitleBar { pub open: bool } 10 11fn button(t: &look::Look, key: &'static str, label: &'static str, glyph: &'static str, f: impl Fn(&mut HostState, &mut Tx) + 'static) -> El { 12 row().key(key).w(46.0).h_full().center().role("button").label(label).hover(t.hover).on_press_tx(f).child(text(glyph).font(t.body).color(t.text)) 13} 14 15impl View for TitleBar { 16 fn view(&self, cx: &Cx) -> El { 17 let t = look::of(cx); 18 // Equal-growth sides (flex-basis 0, same padding) keep the indicator at the bar's true center. 19 let mut bar = row().w_full().h_full().center_y().fill(t.card).window_drag() 20 .child(row().grow(1.0).h_full().center_y().gap(10.0).pl(14.0) 21 .child(text("Conveyor").font(t.title).color(t.text)) 22 .child(widgets_button(cx, self.open))) 23 .child(cx.child("running", "running")) 24 .child(row().grow(1.0).h_full().pl(14.0).child(el().grow(1.0)) 25 .child(background_button(cx)) 26 .child(layout_button(cx)) 27 .child(button(&t, "min", "Minimize", "_", |_, tx| tx.minimize())) 28 .child(button(&t, "max", "Maximize", "□", |_, tx| tx.toggle_maximized())) 29 .child(button(&t, "close", "Close", "×", |_, tx| tx.close_window()))); 30 if self.open { 31 // A click outside (the button included) dismisses it, as Escape does. 32 let id = cx.id.to_owned(); 33 bar = bar.child(ui::popover(&t, col().key("widgets-menu").w(380.0).max_h(560.0)) 34 .anchored_fit("widgets", Side::Bottom, Align::Start, 4.0) 35 .modal(true, move |h: &mut HostState| if let Some(b) = get::<TitleBar>(h, &id) { b.open = false }) 36 .child(cx.child("widget_menu", "menu"))); 37 } 38 bar 39 } 40} 41 42/// The Widgets button, an icon: opens the dropdown with an empty, focused search, highlighted while it shows. 43fn widgets_button(cx: &Cx, open: bool) -> El { 44 let (t, id) = (look::of(cx), cx.id.to_owned()); 45 let b = row().key("widgets").w(26.0).h(26.0).center().radius(6.0).role("button").label("Widgets") 46 .help(format!("Add a widget to this canvas ({})", open_key().label())).hover(t.hover) 47 .on_press_tx(move |h: &mut HostState, _: &mut Tx| toggle(h, &id)) 48 .child(text("◆").font(t.label).color(if open { t.accent } else { t.text_secondary })); 49 if open { b.fill(t.press) } else { b } 50} 51 52/// The shortcut that opens and closes the Widgets dropdown: Ctrl+O (⌘O on macOS). 53fn open_key() -> Keybind { Keybind::new("mod+o") } 54 55/// Opens the Widgets dropdown of title bar `id` with an empty, focused search, or closes it. 56fn toggle(h: &mut HostState, id: &str) { 57 let Some(b) = get::<TitleBar>(h, id) else { return }; 58 b.open = !b.open; 59 if b.open && let Some(m) = get::<WidgetMenu>(h, &format!("{id}.menu")) { m.reset() } 60} 61 62/// Toggles title bar `id`'s dropdown when `k` is its shortcut, and says whether it was. 63pub fn menu_key(h: &mut HostState, id: &str, k: &KeyEvent, tx: &Tx) -> bool { 64 let hit = open_key().matches_tx(k, tx); 65 if hit { toggle(h, id) } 66 hit 67} 68 69/// Toggles canvases between floating and tiled cards; its icon shows the layout a press switches to. 70fn layout_button(cx: &Cx) -> El { 71 let t = look::of(cx); 72 let tiled = canvas::tiled(cx); 73 let c = t.text; 74 let rect = |s: Shape, x0: f32, y0: f32, x1: f32, y1: f32| s.stroke(vec![[x0, y0], [x1, y0], [x1, y1], [x0, y1], [x0, y0]], 1.3, c); 75 let icon = if tiled { rect(rect(Shape::new(), 1.0, 1.0, 10.0, 8.0), 5.0, 5.0, 13.0, 13.0) } 76 else { rect(rect(rect(Shape::new(), 1.0, 1.0, 6.0, 13.0), 8.0, 1.0, 13.0, 6.0), 8.0, 8.0, 13.0, 13.0) }; 77 row().key("layout").w(46.0).h_full().center().role("button").label(if tiled { "Float windows" } else { "Tile windows" }) 78 .help(if tiled { "Float the windows" } else { "Tile the windows" }).hover(t.hover) 79 .on_press_tx(move |h: &mut HostState, _: &mut Tx| share(h, canvas::LAYOUT, if tiled { "float" } else { "tile" })) 80 .child(el().w(14.0).h(14.0).shape(icon)) 81} 82 83/// Picks an image for the shared "project"'s background, or clears the one it has; lit while one is set. 84fn background_button(cx: &Cx) -> El { 85 let t = look::of(cx); 86 let key = canvas::background(cx.shared("project").unwrap_or("widgets")); 87 let set = cx.shared(&key).is_some_and(|p| !p.is_empty()); 88 let c = if set { t.accent } else { t.text }; 89 let icon = Shape::new().stroke(vec![[1.0, 2.0], [13.0, 2.0], [13.0, 12.0], [1.0, 12.0], [1.0, 2.0]], 1.3, c) 90 .stroke(vec![[1.5, 11.0], [5.5, 6.5], [8.5, 9.5], [10.0, 8.0], [12.5, 10.5]], 1.3, c); 91 row().key("background").w(46.0).h_full().center().role("button").label(if set { "Clear background" } else { "Set background" }) 92 .help(if set { "Clear this project's background" } else { "Pick a background image for this project" }).hover(t.hover) 93 .on_press_tx(move |h: &mut HostState, tx: &mut Tx| { 94 if set { return share(h, &key, "") } 95 let key = key.clone(); 96 let options = FileDialogOptions { title: "Background image".into(), ..Default::default() }.filter("Images", &["png", "jpg", "jpeg", "svg"]); 97 tx.open_file(options, move |h: &mut HostState, result, _| if let Ok(Some(path)) = result { share(h, &key, &path.to_string_lossy()) }); 98 }) 99 .child(el().w(14.0).h(14.0).shape(icon)) 100}