use std::str::FromStr;
use adw::prelude::*;
use fluxframe_core::{EffectMetadata, SubchainKind};
use relm4::Sender;
use crate::app::AppMsg;
use crate::components::param_row::{self, DispatchCtx};
use crate::debounce::Debouncer;
use crate::state::AppState;
pub(crate) const SECTION_COUNT: usize = SECTION_LABELS.len();
const SECTION_LABELS: [(&str, &str, &str); 4] = [
(
"mask",
"Mask chain",
"Effects applied to the segmentation mask before compositing.",
),
(
"background",
"Background chain",
"Effects applied to pixels behind the subject.",
),
(
"foreground",
"Foreground chain",
"Effects applied to the subject's pixels.",
),
(
"post",
"Post-composite chain",
"Mask-aware effects applied after the composite.",
),
];
pub(crate) fn build(
state: &AppState,
debouncer: &Debouncer,
sender: &Sender<AppMsg>,
) -> gtk::ScrolledWindow {
let page = adw::PreferencesPage::new();
for (section, title, description) in SECTION_LABELS {
let section_kind = SubchainKind::from_str(section)
.expect("SECTION_LABELS entries are valid SubchainKind names");
let group = adw::PreferencesGroup::builder()
.title(title)
.description(description)
.build();
let chain = state.chain_for(section);
let inventory_for_section = state
.inventory
.sections
.get(section)
.map_or(&[][..], Vec::as_slice);
group.set_header_suffix(Some(&add_effect_menu_button(
section_kind,
inventory_for_section,
sender,
)));
if chain.is_empty() {
group.add(&placeholder_row());
} else {
let chain_len = chain.len();
for (idx, effect_name) in chain.iter().enumerate() {
let Some(meta) = find_effect(inventory_for_section, effect_name) else {
group.add(&unknown_effect_row(effect_name));
continue;
};
let ctx = EffectExpanderCtx {
section: section_kind,
index: idx,
chain_len,
state,
debouncer,
sender,
};
group.add(&effect_expander(ctx, meta));
}
}
page.add(&group);
}
gtk::ScrolledWindow::builder()
.hscrollbar_policy(gtk::PolicyType::Never)
.vscrollbar_policy(gtk::PolicyType::Automatic)
.hexpand(true)
.vexpand(true)
.child(&page)
.build()
}
fn placeholder_row() -> adw::ActionRow {
adw::ActionRow::builder()
.title("(empty)")
.subtitle("No effects in this section.")
.build()
}
fn unknown_effect_row(name: &str) -> adw::ActionRow {
adw::ActionRow::builder()
.title(name.to_string())
.subtitle("Unknown effect — not in registry inventory.")
.build()
}
fn find_effect<'a>(inventory: &'a [EffectMetadata], name: &str) -> Option<&'a EffectMetadata> {
inventory.iter().find(|m| m.name == name)
}
#[derive(Clone, Copy)]
struct EffectExpanderCtx<'a> {
section: SubchainKind,
index: usize,
chain_len: usize,
state: &'a AppState,
debouncer: &'a Debouncer,
sender: &'a Sender<AppMsg>,
}
fn effect_expander(ctx: EffectExpanderCtx<'_>, meta: &EffectMetadata) -> adw::ExpanderRow {
let row = adw::ExpanderRow::builder()
.title(meta.name)
.subtitle(meta.help)
.build();
let up = chain_action_button(
"go-up-symbolic",
"Move up",
ctx.index > 0,
ctx.section,
ctx.index,
ctx.sender,
|section, index| AppMsg::MoveEffectUp { section, index },
);
row.add_suffix(&up);
let down = chain_action_button(
"go-down-symbolic",
"Move down",
ctx.index + 1 < ctx.chain_len,
ctx.section,
ctx.index,
ctx.sender,
|section, index| AppMsg::MoveEffectDown { section, index },
);
row.add_suffix(&down);
let remove = chain_action_button(
"edit-delete-symbolic",
"Remove from chain",
true,
ctx.section,
ctx.index,
ctx.sender,
|section, index| AppMsg::RemoveEffect { section, index },
);
remove.add_css_class("destructive-action");
row.add_suffix(&remove);
let section_str = ctx.section.as_str();
for desc in meta.params {
let path = format!("{section_str}.{}.{}", meta.name, desc.name);
let initial = ctx.state.config_field(section_str, meta.name, desc.name);
let dispatch = DispatchCtx {
debouncer: ctx.debouncer.clone(),
sender: ctx.sender.clone(),
};
let param = param_row::build(*desc, path, initial, dispatch);
row.add_row(¶m);
}
row
}
fn chain_action_button(
icon: &str,
tooltip: &str,
enabled: bool,
section: SubchainKind,
index: usize,
sender: &Sender<AppMsg>,
ctor: impl Fn(SubchainKind, usize) -> AppMsg + 'static,
) -> gtk::Button {
let button = gtk::Button::from_icon_name(icon);
button.set_valign(gtk::Align::Center);
button.set_tooltip_text(Some(tooltip));
button.set_sensitive(enabled);
let sender = sender.clone();
button.connect_clicked(move |_| {
let _ = sender.send(ctor(section, index));
});
button
}
fn add_effect_menu_button(
section: SubchainKind,
inventory: &[EffectMetadata],
sender: &Sender<AppMsg>,
) -> gtk::MenuButton {
let popover = gtk::Popover::new();
let list = gtk::Box::new(gtk::Orientation::Vertical, 4);
list.set_margin_top(6);
list.set_margin_bottom(6);
list.set_margin_start(6);
list.set_margin_end(6);
if inventory.is_empty() {
let lbl = gtk::Label::new(Some("(no effects available)"));
list.append(&lbl);
} else {
for meta in inventory {
let btn = gtk::Button::with_label(meta.name);
btn.set_has_frame(false);
let popover_for_btn = popover.clone();
let sender = sender.clone();
let effect = meta.name.to_string();
btn.connect_clicked(move |_| {
popover_for_btn.popdown();
let _ = sender.send(AppMsg::AddEffect {
section,
effect: effect.clone(),
});
});
list.append(&btn);
}
}
popover.set_child(Some(&list));
let menu = gtk::MenuButton::builder()
.icon_name("list-add-symbolic")
.tooltip_text("Add effect to chain")
.popover(&popover)
.valign(gtk::Align::Center)
.build();
menu.add_css_class("flat");
menu
}