maruzzella 0.1.2

GTK4 desktop shell prototype in Rust with persisted layouts and plugin-backed views.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use std::collections::HashSet;
use std::rc::Rc;

use gtk::gio;
use gtk::prelude::*;
use gtk::{
    Box as GtkBox, Button, DropDown, Entry, EventControllerMotion, Fixed, Image, Label,
    Orientation, Overlay, PopoverMenuBar,
};

use crate::app::ShellChrome;
use crate::commands::CommandRegistry;
use crate::spec::{
    command_name, menu_action_ref, MenuItemSpec, ShellSpec, ToolbarDisplayMode, ToolbarItemSpec,
};
use crate::theme;
use maruzzella_sdk::attach_text_tooltip;

struct IconButtonTooltip {
    button: Button,
    label: Label,
}

pub struct TopBar {
    pub root: GtkBox,
    pub search: Entry,
    tooltips: Vec<IconButtonTooltip>,
}

impl TopBar {
    pub fn install_tooltip_overlay(&self, app_overlay: &Overlay) {
        if self.tooltips.is_empty() {
            return;
        }

        let fixed = Fixed::new();
        fixed.set_can_target(false);
        fixed.set_overflow(gtk::Overflow::Visible);

        for tooltip in &self.tooltips {
            tooltip.label.set_visible(false);
            tooltip.label.set_can_target(false);
            fixed.put(&tooltip.label, 0.0, 0.0);
        }

        app_overlay.add_overlay(&fixed);

        for tooltip in &self.tooltips {
            let button = tooltip.button.clone();
            let label = tooltip.label.clone();
            let fixed_ref = fixed.clone();

            let hover = EventControllerMotion::new();
            let label_enter = label.clone();
            let button_enter = button.clone();
            let fixed_enter = fixed_ref.clone();
            hover.connect_enter(move |_, _, _| {
                if let Some((bx, by)) = button_enter.translate_coordinates(&fixed_enter, 0.0, 0.0) {
                    let bw = button_enter.width() as f64;
                    let bh = button_enter.height() as f64;
                    let lw = label_enter.preferred_size().1.width() as f64;
                    let x = bx + (bw - lw) / 2.0;
                    let y = by + bh + 4.0;
                    fixed_enter.move_(&label_enter, x, y);
                }
                label_enter.set_visible(true);
            });
            let label_leave = label.clone();
            hover.connect_leave(move |_| {
                label_leave.set_visible(false);
            });
            button.add_controller(hover);
        }
    }
}

pub fn build(
    spec: &ShellSpec,
    chrome: ShellChrome,
    registry: Option<&CommandRegistry>,
) -> Option<TopBar> {
    if !chrome.show_menu_bar && !chrome.show_toolbar && !chrome.show_search {
        return None;
    }

    let root = GtkBox::new(Orientation::Vertical, 0);
    root.add_css_class("topbar-shell");
    root.add_css_class(&theme::surface_css_class(&spec.topbar_appearance_id));

    let search = Entry::new();
    search.set_placeholder_text(Some(&spec.search_placeholder));
    search.add_css_class(&theme::input_css_class(&spec.search_input_appearance_id));
    let mut tooltips = Vec::new();

    if chrome.show_menu_bar {
        let masthead = GtkBox::new(Orientation::Horizontal, 12);
        masthead.add_css_class("topbar-masthead");
        masthead.add_css_class(&theme::surface_css_class(&spec.topbar_appearance_id));

        let menu_model = build_menu_model(spec);
        let menu_bar = PopoverMenuBar::from_model(Some(&menu_model));
        menu_bar.add_css_class("menu-bar");
        menu_bar.add_css_class(&theme::surface_css_class(&spec.menu_appearance_id));
        menu_bar.set_hexpand(true);
        masthead.append(&menu_bar);
        root.append(&masthead);
    }

    if chrome.show_toolbar || chrome.show_search {
        let toolbar = GtkBox::new(Orientation::Horizontal, 12);
        toolbar.add_css_class("studio-toolbar");
        toolbar.add_css_class(&theme::surface_css_class(&spec.toolbar_appearance_id));

        if chrome.show_search {
            let search_cluster = GtkBox::new(Orientation::Horizontal, 0);
            search_cluster.add_css_class("toolbar-search-cluster");
            search_cluster.set_hexpand(true);
            search.add_css_class("toolbar-search");
            search.set_hexpand(true);
            search_cluster.append(&search);
            toolbar.append(&search_cluster);
        }

        if chrome.show_toolbar {
            if !chrome.show_search {
                let spacer = GtkBox::new(Orientation::Horizontal, 0);
                spacer.set_hexpand(true);
                toolbar.append(&spacer);
            }

            let actions_group = GtkBox::new(Orientation::Horizontal, 8);
            actions_group.add_css_class("toolbar-actions");
            for item in spec.toolbar_items.iter().filter(|item| !item.secondary) {
                actions_group.append(&action_bar_item_widget(item, &mut tooltips, registry));
            }
            toolbar.append(&actions_group);

            let utility_group = GtkBox::new(Orientation::Horizontal, 6);
            utility_group.add_css_class("toolbar-utility-group");
            for item in spec.toolbar_items.iter().filter(|item| item.secondary) {
                utility_group.append(&action_bar_item_widget(item, &mut tooltips, registry));
            }
            toolbar.append(&utility_group);
        }

        root.append(&toolbar);
    }

    Some(TopBar {
        root,
        search,
        tooltips,
    })
}

fn action_bar_item_widget(
    item: &ToolbarItemSpec,
    tooltips: &mut Vec<IconButtonTooltip>,
    registry: Option<&CommandRegistry>,
) -> gtk::Widget {
    let action_ref = menu_action_ref(&item.id);
    match item.display_mode {
        ToolbarDisplayMode::IconOnly => {
            let icon_name = item.icon_name.as_deref().unwrap_or_else(|| {
                panic!("toolbar item '{}' is IconOnly but has no icon", item.id)
            });
            let tooltip = item.label.as_deref().unwrap_or(&item.id);
            icon_button(
                icon_name,
                &action_ref,
                tooltip,
                &item.appearance_id,
                tooltips,
            )
            .upcast()
        }
        ToolbarDisplayMode::IconAndText => {
            let label = item.label.as_deref().unwrap_or(&item.id);
            let icon_name = item
                .icon_name
                .as_deref()
                .unwrap_or("applications-system-symbolic");
            toolbar_button(icon_name, label, &action_ref, &item.appearance_id).upcast()
        }
        ToolbarDisplayMode::TextOnly => {
            let label = item.label.as_deref().unwrap_or(&item.id);
            text_button(label, &action_ref, &item.appearance_id).upcast()
        }
        ToolbarDisplayMode::Dropdown => dropdown_item(item, registry).upcast(),
    }
}

pub fn standalone_toolbar_item_button(item: &ToolbarItemSpec) -> Button {
    match item.display_mode {
        ToolbarDisplayMode::IconOnly => {
            let icon_name = item
                .icon_name
                .as_deref()
                .unwrap_or("applications-system-symbolic");
            let tooltip = item.label.as_deref().unwrap_or(&item.id);
            standalone_icon_button(icon_name, tooltip, &item.appearance_id)
        }
        ToolbarDisplayMode::IconAndText => {
            let label = item.label.as_deref().unwrap_or(&item.id);
            let icon_name = item
                .icon_name
                .as_deref()
                .unwrap_or("applications-system-symbolic");
            standalone_toolbar_button(icon_name, label, &item.appearance_id)
        }
        ToolbarDisplayMode::TextOnly | ToolbarDisplayMode::Dropdown => {
            let label = item.label.as_deref().unwrap_or(&item.id);
            standalone_text_button(label, &item.appearance_id)
        }
    }
}

fn dropdown_item(item: &ToolbarItemSpec, registry: Option<&CommandRegistry>) -> DropDown {
    let labels = if item.options.is_empty() {
        vec![item.label.clone().unwrap_or_else(|| item.id.clone())]
    } else {
        item.options
            .iter()
            .map(|option| option.label.clone())
            .collect::<Vec<_>>()
    };
    let label_refs = labels.iter().map(String::as_str).collect::<Vec<_>>();
    let dropdown = DropDown::from_strings(&label_refs);
    dropdown.add_css_class("toolbar-dropdown");
    dropdown.set_width_request(176);
    if let Some(label) = &item.label {
        dropdown.set_tooltip_text(Some(label));
    }
    let selected_index = item
        .selected_index
        .min(item.options.len().saturating_sub(1) as u32);
    dropdown.set_selected(selected_index);

    if item.options.is_empty() {
        dropdown.set_sensitive(false);
        return dropdown;
    }

    if registry
        .map(|registry| !registry.is_enabled(&item.command_id))
        .unwrap_or(false)
    {
        dropdown.set_sensitive(false);
        return dropdown;
    }

    if let Some(handler) = registry.and_then(|registry| registry.handler_for(&item.command_id)) {
        let options = item.options.clone();
        dropdown.connect_selected_notify(move |dropdown| {
            let selected = dropdown.selected();
            if selected == gtk::INVALID_LIST_POSITION {
                return;
            }
            if let Some(option) = options.get(selected as usize) {
                handler(&option.payload);
            }
        });
    } else {
        dropdown.set_sensitive(false);
    }

    dropdown
}

fn toolbar_button(icon_name: &str, label: &str, action_name: &str, appearance_id: &str) -> Button {
    let button = Button::new();
    button.add_css_class("toolbar-button");
    button.add_css_class(&theme::button_css_class(appearance_id));
    button.set_action_name(Some(action_name));

    let content = GtkBox::new(Orientation::Horizontal, 6);
    let icon = Image::from_icon_name(icon_name);
    icon.set_icon_size(gtk::IconSize::Normal);
    content.append(&icon);
    let text = Label::new(Some(label));
    text.add_css_class("toolbar-button-label");
    text.add_css_class(&theme::text_css_class("body"));
    content.append(&text);
    button.set_child(Some(&content));
    button
}

fn text_button(label: &str, action_name: &str, appearance_id: &str) -> Button {
    let button = Button::new();
    button.add_css_class("toolbar-button");
    button.add_css_class(&theme::button_css_class(appearance_id));
    button.set_action_name(Some(action_name));
    let text = Label::new(Some(label));
    text.add_css_class("toolbar-button-label");
    text.add_css_class(&theme::text_css_class("body"));
    button.set_child(Some(&text));
    button
}

fn standalone_toolbar_button(icon_name: &str, label: &str, appearance_id: &str) -> Button {
    let button = Button::new();
    button.add_css_class("toolbar-button");
    button.add_css_class(&theme::button_css_class(appearance_id));

    let content = GtkBox::new(Orientation::Horizontal, 6);
    let icon = Image::from_icon_name(icon_name);
    icon.set_icon_size(gtk::IconSize::Normal);
    content.append(&icon);
    let text = Label::new(Some(label));
    text.add_css_class("toolbar-button-label");
    text.add_css_class(&theme::text_css_class("body"));
    content.append(&text);
    button.set_child(Some(&content));
    button
}

fn standalone_text_button(label: &str, appearance_id: &str) -> Button {
    let button = Button::new();
    button.add_css_class("toolbar-button");
    button.add_css_class(&theme::button_css_class(appearance_id));
    let text = Label::new(Some(label));
    text.add_css_class("toolbar-button-label");
    text.add_css_class(&theme::text_css_class("body"));
    button.set_child(Some(&text));
    button
}

fn icon_button(
    icon_name: &str,
    action_name: &str,
    tooltip: &str,
    appearance_id: &str,
    tooltips: &mut Vec<IconButtonTooltip>,
) -> Button {
    let button = Button::new();
    button.add_css_class("toolbar-button");
    button.add_css_class("toolbar-icon-button");
    button.add_css_class(&theme::button_css_class(appearance_id));
    button.set_action_name(Some(action_name));

    let icon = Image::from_icon_name(icon_name);
    icon.set_icon_size(gtk::IconSize::Normal);
    button.set_child(Some(&icon));

    let tip_label = Label::new(Some(tooltip));
    tip_label.add_css_class("maruzzella-tooltip-label");
    tip_label.add_css_class(&theme::text_css_class("meta"));

    tooltips.push(IconButtonTooltip {
        button: button.clone(),
        label: tip_label,
    });

    button
}

fn standalone_icon_button(icon_name: &str, tooltip: &str, appearance_id: &str) -> Button {
    let button = Button::new();
    button.add_css_class("toolbar-button");
    button.add_css_class("toolbar-icon-button");
    button.add_css_class(&theme::button_css_class(appearance_id));

    let icon = Image::from_icon_name(icon_name);
    icon.set_icon_size(gtk::IconSize::Normal);
    button.set_child(Some(&icon));

    attach_text_tooltip(&button, tooltip);

    button
}

fn build_menu_model(spec: &ShellSpec) -> gio::Menu {
    let menu = gio::Menu::new();
    for root in &spec.menu_roots {
        let submenu = submenu(
            &spec
                .menu_items
                .iter()
                .filter(|item| item.root_id == root.id)
                .cloned()
                .collect::<Vec<_>>(),
        );
        menu.append_submenu(Some(&root.label), &submenu);
    }
    menu
}

fn submenu(items: &[MenuItemSpec]) -> gio::Menu {
    let submenu = gio::Menu::new();
    let mut section = gio::Menu::new();
    for item in items {
        if item.command_id.is_empty() {
            if section.n_items() > 0 {
                submenu.append_section(None, &section);
                section = gio::Menu::new();
            }
            continue;
        }
        section.append(Some(&item.label), Some(&menu_action_ref(&item.id)));
    }
    if section.n_items() > 0 {
        submenu.append_section(None, &section);
    }
    submenu
}

pub fn install_actions(
    window: &gtk::ApplicationWindow,
    spec: &ShellSpec,
    registry: &CommandRegistry,
) -> Vec<String> {
    let mut installed = Vec::new();
    let action_bindings = Rc::new(action_bindings(spec, registry));
    for (action_name, action_id) in action_bindings.iter() {
        let simple = gio::SimpleAction::new(action_name, None);
        simple.set_enabled(registry.is_enabled(action_id));
        let handler = registry.handler_for(action_id);
        let title = action_id.clone();
        let window_for_activate = window.clone();
        let registry = registry.clone();
        let action_bindings = Rc::clone(&action_bindings);
        simple.connect_activate(move |_, _| {
            if let Some(handler) = handler.as_ref() {
                handler(&[]);
            } else {
                eprintln!("unhandled command: {title}");
            }
            refresh_action_enabled(&window_for_activate, &registry, &action_bindings);
        });
        window.add_action(&simple);
        installed.push(action_name.clone());
    }
    installed
}

fn action_bindings(spec: &ShellSpec, registry: &CommandRegistry) -> Vec<(String, String)> {
    let mut seen = HashSet::new();
    let mut bindings = Vec::new();
    for action_id in spec
        .commands
        .iter()
        .map(|command| command.id.as_str())
        .chain(spec.menu_items.iter().map(|item| item.id.as_str()))
        .chain(spec.toolbar_items.iter().map(|item| item.id.as_str()))
    {
        let action_name = command_name(action_id);
        if seen.insert(action_name.clone()) && registry.handler_for(action_id).is_some() {
            bindings.push((action_name, action_id.to_string()));
        }
    }
    bindings
}

fn refresh_action_enabled(
    window: &gtk::ApplicationWindow,
    registry: &CommandRegistry,
    action_bindings: &[(String, String)],
) {
    for (action_name, action_id) in action_bindings {
        if let Some(action) = window
            .lookup_action(action_name)
            .and_then(|action| action.downcast::<gio::SimpleAction>().ok())
        {
            action.set_enabled(registry.is_enabled(action_id));
        }
    }
}