use adw::prelude::*;
use gtk::{gio, glib};
use crate::components::Emit;
use crate::shortcuts::{
ACTION_ABOUT, ACTION_RELOAD, ACTION_REVERT, ACTION_SAVE, ACTION_SAVE_AS, ACTION_SHORTCUTS,
};
pub(crate) struct PresetBar {
pub(crate) root: adw::HeaderBar,
pub(crate) revert: gtk::Button,
title: adw::WindowTitle,
dropdown: gtk::DropDown,
dropdown_handler: glib::SignalHandlerId,
save: gtk::Button,
save_as: gtk::Button,
}
pub(crate) fn build(on_preset_change: Emit<String>) -> PresetBar {
let title = adw::WindowTitle::new("FluxFrame", "");
let header = adw::HeaderBar::builder().title_widget(&title).build();
let model = gtk::StringList::new(&[]);
let dropdown = gtk::DropDown::builder()
.model(&model)
.tooltip_text("Switch Active Preset")
.sensitive(false)
.build();
dropdown.update_property(&[gtk::accessible::Property::Label("Preset")]);
let dropdown_handler = dropdown.connect_selected_notify(move |dd| {
let model = dd
.model()
.and_then(|m| m.downcast::<gtk::StringList>().ok());
if let Some(name) = model.and_then(|m| m.string(dd.selected())) {
on_preset_change(name.to_string());
}
});
header.pack_start(&dropdown);
let save = action_button("document-save-symbolic", "Save", ACTION_SAVE);
let save_as = action_button("document-save-as-symbolic", "Save As…", ACTION_SAVE_AS);
let revert = action_button(
"document-revert-symbolic",
"Discard Unsaved Changes",
ACTION_REVERT,
);
let save_group = gtk::Box::new(gtk::Orientation::Horizontal, 0);
save_group.add_css_class("linked");
save_group.append(&save);
save_group.append(&save_as);
save_group.append(&revert);
header.pack_start(&save_group);
header.pack_end(&main_menu_button());
PresetBar {
root: header,
revert,
title,
dropdown,
dropdown_handler,
save,
save_as,
}
}
fn action_button(icon: &str, label: &str, action: &str) -> gtk::Button {
let button = gtk::Button::from_icon_name(icon);
button.set_tooltip_text(Some(label));
button.update_property(&[gtk::accessible::Property::Label(label)]);
button.set_action_name(Some(action));
button
}
fn main_menu_button() -> gtk::MenuButton {
let menu = gio::Menu::new();
let config_section = gio::Menu::new();
config_section.append(Some("_Discard Unsaved Changes"), Some(ACTION_REVERT));
config_section.append(Some("_Reload Configuration"), Some(ACTION_RELOAD));
menu.append_section(None, &config_section);
let app_section = gio::Menu::new();
app_section.append(Some("_Keyboard Shortcuts"), Some(ACTION_SHORTCUTS));
app_section.append(Some("_About FluxFrame"), Some(ACTION_ABOUT));
menu.append_section(None, &app_section);
let button = gtk::MenuButton::builder()
.icon_name("open-menu-symbolic")
.tooltip_text("Main Menu")
.primary(true)
.menu_model(&menu)
.build();
button.update_property(&[gtk::accessible::Property::Label("Main Menu")]);
button
}
impl PresetBar {
pub(crate) fn set_state(
&self,
connected: bool,
is_dirty: bool,
config_path: Option<&std::path::Path>,
active_preset: Option<&str>,
) {
self.dropdown.set_sensitive(connected);
let (save_tooltip, save_as_tooltip) = match config_path {
Some(p) => (
format!("Save to {}", p.display()),
format!("Save as New Preset in {}", p.display()),
),
None => (
"Save Unavailable: the Daemon Has No Writable Config".to_string(),
"Save As Unavailable: the Daemon Has No Writable Config".to_string(),
),
};
self.save.set_tooltip_text(Some(&save_tooltip));
self.save_as.set_tooltip_text(Some(&save_as_tooltip));
let subtitle = match (is_dirty, active_preset) {
(true, Some(name)) => format!("{name} — Unsaved changes"),
(true, None) => "Unsaved changes".into(),
(false, Some(name)) => name.to_string(),
(false, None) => String::new(),
};
self.title.set_subtitle(&subtitle);
}
pub(crate) fn set_presets(&self, names: &[String], active: Option<&str>) {
let model = gtk::StringList::new(&names.iter().map(String::as_str).collect::<Vec<_>>());
self.dropdown.block_signal(&self.dropdown_handler);
self.dropdown.set_model(Some(&model));
if let Some(idx) = active.and_then(|a| names.iter().position(|n| n == a)) {
#[allow(
clippy::cast_possible_truncation,
reason = "preset list length is far below u32::MAX"
)]
self.dropdown.set_selected(idx as u32);
}
self.dropdown.unblock_signal(&self.dropdown_handler);
}
}