xonecas.io
8447
Co

conveyor / files

conveyor / templates / library / widgets / root.rs

2.2 KB
1//! The whole window: the app draws this widget and nothing else. A title bar, the shared
2//! "project"'s own canvas (its desktop), and the taskbar on the edge picked for it.
3use super::api::*;
4use super::{taskbar::{self, Edge}, titlebar};
5use rune::{El, KeyEvent, Tx, col};
6
7pub const WIDGET: Widget = Widget { title: "Window", size: (0.0, 0.0), new: |_| Box::new(Root) };
8
9pub struct Root;
10
11impl View for Root {
12    fn view(&self, cx: &Cx) -> El {
13        let (t, w, z) = (taskbar::THICK, taskbar::WIDE, Some(0.0));
14        // The bar goes after the canvas, in the room left for it, so its dock menu draws above the canvas.
15        let (body, bar) = match taskbar::edge(cx) {
16            Edge::Top => (col().pt(t), col().pin(z, z, z, None).h(t)),
17            Edge::Bottom => (col().pb(t), col().pin(z, None, z, z).h(t)),
18            Edge::Left => (col().pl(w), col().pin(z, z, None, z).w(w)),
19            Edge::Right => (col().pr(w), col().pin(None, z, z, z).w(w)),
20        };
21        let body = body.w_full().grow(1.0).min_h(0.0)
22            .child(col().w_full().h_full().min_w(0.0).min_h(0.0).child(cx.child("canvas", &desktop(cx))))
23            .child(bar.child(cx.child("taskbar", "taskbar")));
24        let bar = format!("{}.titlebar", cx.id);
25        // Focusable with a key handler, so Ctrl+O reaches the title bar from any control but a text field.
26        // The title bar goes last, pinned to the top, so its Widgets dropdown draws above the canvas.
27        col().w_full().h_full().pt(36.0).focusable(true)
28            .on_key(move |h: &mut HostState, k: &KeyEvent, tx: &mut Tx| titlebar::menu_key(h, &bar, k, tx))
29            .child(body)
30            .child(col().absolute(0.0, 0.0).w_full().h(36.0).child(cx.child("titlebar", "titlebar")))
31    }
32}
33
34/// The key of the shared "project"'s canvas under root. The widgets project (or none picked) keeps
35/// the original "canvas".
36pub fn desktop(cx: &Cx) -> String {
37    match cx.shared("project") {
38        Some(p) if p != "widgets" => format!("canvas-{p}"),
39        _ => "canvas".into(),
40    }
41}
42
43/// The project whose desktop holds instance `id` (`root.canvas-<slug>.<key>`): the widgets project on `root.canvas`.
44pub fn project_of(id: &str) -> &str {
45    id.rsplit('.').nth(1).and_then(|d| d.strip_prefix("canvas-")).unwrap_or("widgets")
46}