use std::cell::RefCell;
use std::collections::HashMap;
use gtk::prelude::*;
use prism_core::navbar::{BarItem, ScreenConfig};
use prism_core::{Color, NodeId, UiEvent};
thread_local! {
static CONFIGS: RefCell<HashMap<NodeId, ScreenConfig>> = RefCell::default();
}
pub(crate) fn set_config(id: NodeId, config: &str) -> bool {
let c = ScreenConfig::decode(config);
CONFIGS.with(|m| m.borrow_mut().insert(id, c.clone()) != Some(c))
}
pub(crate) fn forget(id: NodeId) {
CONFIGS.with(|m| m.borrow_mut().remove(&id));
}
fn face(item: &BarItem) -> String {
match prism_core::glyphs::GLYPHS
.iter()
.find(|(s, _)| *s == item.icon)
{
Some((_, g)) => (*g).to_string(),
None if item.title.is_empty() => prism_core::glyphs::glyph(&item.icon).to_string(),
None => item.title.clone(),
}
}
pub(crate) fn refresh(
header: >k::Box,
back: >k::Button,
title: >k::Label,
top: Option<NodeId>,
) {
let mut child = header.first_child();
while let Some(c) = child {
child = c.next_sibling();
if c.has_css_class("prism-bar-button") {
header.remove(&c);
}
}
let Some(top) = top else { return };
let config = CONFIGS
.with(|m| m.borrow().get(&top).cloned())
.unwrap_or_default();
let button = |index: usize, item: &BarItem| {
let b = gtk::Button::with_label(&face(item));
b.set_has_frame(false);
b.add_css_class("prism-bar-button");
b.set_tooltip_text(Some(item.spoken()));
crate::imp::set_accessible_string(&b, crate::imp::a11y_property::LABEL, item.spoken());
b.connect_clicked(move |_| crate::imp::dispatch(UiEvent::Change(top, index as f64)));
b
};
let mut after: gtk::Widget = back.clone().upcast();
for (i, item) in config.leading() {
let b = button(i, item);
header.insert_child_after(&b, Some(&after));
after = b.upcast();
}
let mut after: gtk::Widget = title.clone().upcast();
for (i, item) in config.trailing() {
let b = button(i, item);
header.insert_child_after(&b, Some(&after));
after = b.upcast();
}
}
pub(crate) fn tint_rules(id: NodeId, tabs: bool, tint: Option<Color>, dark: bool) -> String {
let Some(tint) = tint else {
return String::new();
};
let color = crate::decor::color(tint, dark);
if tabs {
format!(".p{id} > .prism-tabbar > button.toggle:checked{{color:{color};}}\n")
} else {
format!(".p{id} > .prism-header > button{{color:{color};}}\n")
}
}