fluxframe-gui 0.11.0

GTK4/libadwaita GUI client for the FluxFrame control socket.
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Single-page chain editor.
//!
//! Renders the active preset as one [`adw::PreferencesPage`] holding
//! four [`adw::PreferencesGroup`]s (mask / background / foreground /
//! post). Each group lists the effects currently in the chain as
//! [`adw::ExpanderRow`]s; expanding a row reveals one
//! [`crate::components::param_row`] per descriptor in the effect's
//! metadata.
//!
//! The chain page is rebuilt from scratch on preset switch, chain
//! edits and state refetch. Rebuilds are user-driven, not per-frame;
//! the caller carries expanded rows, scroll position and keyboard
//! focus across a rebuild via [`ChainPage::view_state`] /
//! [`ChainPage::restore`]. A refused parameter write resets only its
//! own widget via [`ChainPage::reset_param`]; an effect's enable switch
//! is updated in place via [`ChainPage::set_toggle`], without a rebuild.

use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;

use adw::prelude::*;
use fluxframe_core::{EffectSchema, SetPath, SubchainKind};
use gtk::glib;
use serde_json::Value;

use crate::components::Emit;
use crate::components::param_row::{self, DispatchCtx, ParamChange, ParamReset};
use crate::debounce::Debouncer;
use crate::state::{AppState, ChainEdit};

/// Cap on the add-effect popover height; longer inventories scroll.
const ADD_EFFECT_MENU_MAX_HEIGHT: i32 = 360;

/// User intent reported by the chain page.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum ChainEvent {
    /// Change the effect list of `section`.
    Edit {
        /// Sub-chain being edited.
        section: SubchainKind,
        /// Requested change.
        edit: ChainEdit,
    },
    /// A param row produced a new value.
    Param(ParamChange),
    /// The operator flipped an effect's enable switch.
    SetEnabled {
        /// Sub-chain of the effect.
        section: SubchainKind,
        /// Effect name.
        effect: String,
        /// New switch position.
        enabled: bool,
    },
}

/// Which per-effect suffix control a focus key refers to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum ChainButtonKind {
    Toggle,
    Up,
    Down,
    Remove,
}

/// Identity of a per-effect suffix control that survives a rebuild.
type ButtonKey = (SubchainKind, String, ChainButtonKind);

/// The enable switch of one effect row, with what [`ChainPage::set_toggle`]
/// needs to update it in place.
struct EffectToggle {
    section: SubchainKind,
    effect: String,
    switch: gtk::Switch,
    handler: glib::SignalHandlerId,
    row: adw::ExpanderRow,
    help: String,
}

/// A built chain page plus the handles needed to snapshot and
/// restore its view state across rebuilds.
pub(crate) struct ChainPage {
    /// Scroller wrapping the preferences page; attach it to the body
    /// container.
    pub(crate) root: gtk::ScrolledWindow,
    /// Effect expander rows keyed by (section, effect name).
    expanders: Vec<(SubchainKind, String, adw::ExpanderRow)>,
    /// Per-effect suffix controls, for focus carry-over.
    focusables: Vec<(ButtonKey, gtk::Widget)>,
    /// Enable switches of every effect row.
    toggles: Vec<EffectToggle>,
    /// Reset handles of every param row.
    resets: HashMap<SetPath, ParamReset>,
}

/// Transient view state of a chain page: which effect rows are
/// expanded, where the page is scrolled and which suffix button has
/// keyboard focus.
pub(crate) struct ChainViewState {
    expanded: HashSet<(SubchainKind, String)>,
    scroll: f64,
    focused: Option<ButtonKey>,
}

impl ChainPage {
    /// Snapshot expanded rows, the vertical scroll offset and the
    /// focused suffix button.
    pub(crate) fn view_state(&self) -> ChainViewState {
        let expanded = self
            .expanders
            .iter()
            .filter(|(_, _, row)| row.is_expanded())
            .map(|(section, name, _)| (*section, name.clone()))
            .collect();
        let focused = self
            .focusables
            .iter()
            .find(|(_, widget)| widget.has_focus())
            .map(|(key, _)| key.clone());
        ChainViewState {
            expanded,
            scroll: self.root.vadjustment().value(),
            focused,
        }
    }

    /// Put the widget of `path` back to `value` without emitting a
    /// change. No-op when the page has no row for `path`.
    pub(crate) fn reset_param(&self, path: &SetPath, value: &Value) {
        if let Some(reset) = self.resets.get(path) {
            reset(value);
        }
    }

    /// Show `effect` of `section` as `enabled` without emitting a
    /// change: switch position and row subtitle of every row with that
    /// name (a chain may list an effect twice).
    pub(crate) fn set_toggle(&self, section: SubchainKind, effect: &str, enabled: bool) {
        for toggle in self
            .toggles
            .iter()
            .filter(|t| t.section == section && t.effect == effect)
        {
            toggle.switch.block_signal(&toggle.handler);
            toggle.switch.set_active(enabled);
            toggle.switch.unblock_signal(&toggle.handler);
            toggle
                .row
                .set_subtitle(&effect_subtitle(&toggle.help, enabled));
        }
    }

    /// Re-expand rows present in `state`, restore the scroll offset and
    /// return keyboard focus to the same suffix button (or its row, when
    /// the button became insensitive, e.g. Move Up at the head).
    ///
    /// A freshly built page has no allocation yet, so the scroll offset
    /// is applied on the vertical adjustment's first `changed` emission
    /// with a non-zero page size; the handler then disconnects itself.
    /// Rows are expanded before the page is mapped, so the expander
    /// revealers open without animation and the first allocation
    /// already accounts for their content.
    pub(crate) fn restore(&self, state: &ChainViewState) {
        for (section, name, row) in &self.expanders {
            if state.expanded.contains(&(*section, name.clone())) {
                row.set_expanded(true);
            }
        }

        if let Some((section, name, kind)) = &state.focused {
            let button = self
                .focusables
                .iter()
                .find(|((s, n, k), _)| s == section && n == name && k == kind)
                .map(|(_, widget)| widget);
            if let Some(button) = button.filter(|b| b.is_sensitive()) {
                button.grab_focus();
            } else if let Some((_, _, row)) = self
                .expanders
                .iter()
                .find(|(s, n, _)| s == section && n == name)
            {
                row.grab_focus();
            }
        }

        let target = state.scroll;
        if target <= 0.0 {
            return;
        }
        let adjustment = self.root.vadjustment();
        if adjustment.page_size() > 0.0 {
            adjustment.set_value(target);
            return;
        }
        // The handler owns only its own id cell (no widget), so there
        // is no reference cycle; the id is taken exactly once.
        let handler: Rc<RefCell<Option<glib::SignalHandlerId>>> = Rc::new(RefCell::new(None));
        let handler_for_cb = Rc::clone(&handler);
        let id = adjustment.connect_changed(move |adj| {
            if adj.page_size() <= 0.0 {
                return;
            }
            adj.set_value(target.min(adj.upper() - adj.page_size()));
            if let Some(id) = handler_for_cb.borrow_mut().take() {
                adj.disconnect(id);
            }
        });
        *handler.borrow_mut() = Some(id);
    }
}

/// Display title + group description for one chain section.
fn section_labels(section: SubchainKind) -> (&'static str, &'static str) {
    match section {
        SubchainKind::Mask => (
            "Mask chain",
            "Effects applied to the segmentation mask before compositing.",
        ),
        SubchainKind::Background => (
            "Background chain",
            "Effects applied to pixels behind the subject.",
        ),
        SubchainKind::Foreground => (
            "Foreground chain",
            "Effects applied to the subject's pixels.",
        ),
        SubchainKind::Post => (
            "Post-composite chain",
            "Mask-aware effects applied after the composite.",
        ),
    }
}

/// Build the chain page from the current [`AppState`].
///
/// The returned [`ChainPage::root`] is a [`gtk::ScrolledWindow`]
/// wrapping the [`adw::PreferencesPage`] — callers attach it to the
/// body container. User intent is reported through `emit`; param rows
/// route their changes through `debouncer` first.
pub(crate) fn build(state: &AppState, debouncer: &Debouncer, emit: &Emit<ChainEvent>) -> ChainPage {
    let page = adw::PreferencesPage::new();
    let mut parts = PageParts::default();
    let param_emit: Emit<ParamChange> = {
        let emit = Emit::clone(emit);
        std::rc::Rc::new(move |change| emit(ChainEvent::Param(change)))
    };

    for section_kind in SubchainKind::ALL {
        let (title, description) = section_labels(section_kind);
        let group = adw::PreferencesGroup::builder()
            .title(title)
            .description(description)
            .build();
        // Add-effect MenuButton in the group header (suffix slot).
        let chain = state.chain_for(section_kind);
        let inventory_for_section = state.effects_for(section_kind);
        group.set_header_suffix(Some(&add_effect_menu_button(
            section_kind,
            inventory_for_section,
            emit,
        )));
        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 {
                    // The chain references an effect the registry no
                    // longer carries (e.g. a feature-gated effect on
                    // a slim daemon). Render a stub so the operator
                    // sees the divergence.
                    group.add(&unknown_effect_row(effect_name));
                    continue;
                };
                let ctx = EffectExpanderCtx {
                    section: section_kind,
                    index: idx,
                    chain_len,
                    state,
                    debouncer,
                    emit,
                    param_emit: &param_emit,
                };
                let row = effect_expander(ctx, meta, &mut parts);
                group.add(&row);
                parts.expanders.push((section_kind, meta.name.clone(), row));
            }
        }
        page.add(&group);
    }

    let root = gtk::ScrolledWindow::builder()
        .hscrollbar_policy(gtk::PolicyType::Never)
        .vscrollbar_policy(gtk::PolicyType::Automatic)
        .hexpand(true)
        .vexpand(true)
        .child(&page)
        .build();
    let PageParts {
        expanders,
        focusables,
        toggles,
        resets,
    } = parts;
    ChainPage {
        root,
        expanders,
        focusables,
        toggles,
        resets,
    }
}

/// Handles collected while building the page.
#[derive(Default)]
struct PageParts {
    expanders: Vec<(SubchainKind, String, adw::ExpanderRow)>,
    focusables: Vec<(ButtonKey, gtk::Widget)>,
    toggles: Vec<EffectToggle>,
    resets: HashMap<SetPath, ParamReset>,
}

/// Row subtitle of an effect: its help text, prefixed when disabled so
/// the state is carried by text, not by the switch position alone.
fn effect_subtitle(help: &str, enabled: bool) -> String {
    if enabled {
        help.to_string()
    } else {
        format!("Disabled — {help}")
    }
}

fn placeholder_row() -> adw::ActionRow {
    adw::ActionRow::builder()
        .title("No effects added")
        .subtitle("Use the + button to add one.")
        .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 [EffectSchema], name: &str) -> Option<&'a EffectSchema> {
    inventory.iter().find(|m| m.name == name)
}

/// Context bundled into [`effect_expander`] so the function does
/// not have to thread 6 unrelated params individually.
#[derive(Clone, Copy)]
struct EffectExpanderCtx<'a> {
    section: SubchainKind,
    index: usize,
    chain_len: usize,
    state: &'a AppState,
    debouncer: &'a Debouncer,
    emit: &'a Emit<ChainEvent>,
    param_emit: &'a Emit<ParamChange>,
}

fn effect_expander(
    ctx: EffectExpanderCtx<'_>,
    meta: &EffectSchema,
    parts: &mut PageParts,
) -> adw::ExpanderRow {
    let enabled = ctx.state.effect_enabled(ctx.section, &meta.name);
    let row = adw::ExpanderRow::builder()
        .title(meta.name.as_str())
        .subtitle(effect_subtitle(&meta.help, enabled))
        .build();

    // Enable switch. Its initial state is set by the builder, before the
    // handler is connected, so building or rebuilding a page never emits
    // a toggle.
    let switch = gtk::Switch::builder()
        .active(enabled)
        .valign(gtk::Align::Center)
        .tooltip_text("Enable Effect")
        .build();
    switch.update_property(&[gtk::accessible::Property::Label(&format!(
        "Enable {}",
        meta.name
    ))]);
    let handler = {
        let emit = Emit::clone(ctx.emit);
        let section = ctx.section;
        let effect = meta.name.clone();
        switch.connect_active_notify(move |switch| {
            emit(ChainEvent::SetEnabled {
                section,
                effect: effect.clone(),
                enabled: switch.is_active(),
            });
        })
    };
    row.add_suffix(&switch);
    parts.focusables.push((
        (ctx.section, meta.name.clone(), ChainButtonKind::Toggle),
        switch.clone().upcast(),
    ));
    parts.toggles.push(EffectToggle {
        section: ctx.section,
        effect: meta.name.clone(),
        switch,
        handler,
        row: row.clone(),
        help: meta.help.clone(),
    });

    // Suffix toolbar: ↑ ↓ ✕ — each emits a chain edit when clicked.
    // Disabled at the boundary positions so the user gets visual
    // feedback.
    let buttons = [
        (
            ChainButton {
                icon: "go-up-symbolic",
                tooltip: "Move Up",
                accessible_label: format!("Move {} Up", meta.name),
                enabled: ctx.index > 0,
            },
            ChainButtonKind::Up,
            ChainEdit::MoveUp(ctx.index),
        ),
        (
            ChainButton {
                icon: "go-down-symbolic",
                tooltip: "Move Down",
                accessible_label: format!("Move {} Down", meta.name),
                enabled: ctx.index + 1 < ctx.chain_len,
            },
            ChainButtonKind::Down,
            ChainEdit::MoveDown(ctx.index),
        ),
        (
            ChainButton {
                icon: "edit-delete-symbolic",
                tooltip: "Remove From Chain",
                accessible_label: format!("Remove {} From Chain", meta.name),
                enabled: true,
            },
            ChainButtonKind::Remove,
            ChainEdit::Remove(ctx.index),
        ),
    ];
    for (look, kind, edit) in buttons {
        let button = chain_action_button(look, ctx.section, edit, ctx.emit);
        if kind == ChainButtonKind::Remove {
            button.add_css_class("error");
        }
        row.add_suffix(&button);
        parts
            .focusables
            .push(((ctx.section, meta.name.clone(), kind), button.upcast()));
    }

    for desc in &meta.params {
        let path = SetPath {
            section: ctx.section,
            effect: meta.name.clone(),
            field: desc.name.clone(),
        };
        let initial = ctx.state.config_field(&path);
        let dispatch = DispatchCtx {
            debouncer: ctx.debouncer.clone(),
            emit: Emit::clone(ctx.param_emit),
        };
        let param = param_row::build(desc, path.clone(), initial, dispatch);
        row.add_row(&param.row);
        parts.resets.insert(path, param.reset);
    }
    row
}

/// Appearance of a per-effect suffix button.
struct ChainButton {
    icon: &'static str,
    tooltip: &'static str,
    /// Full accessible label naming the effect, so a screen reader
    /// distinguishes the buttons of different rows.
    accessible_label: String,
    enabled: bool,
}

/// Build one of the per-effect flat suffix buttons (↑/↓/✕) emitting
/// `chain_edit` for `section` when clicked.
fn chain_action_button(
    look: ChainButton,
    section: SubchainKind,
    chain_edit: ChainEdit,
    emit: &Emit<ChainEvent>,
) -> gtk::Button {
    let ChainButton {
        icon,
        tooltip,
        accessible_label,
        enabled,
    } = look;
    let button = gtk::Button::from_icon_name(icon);
    button.set_valign(gtk::Align::Center);
    button.set_tooltip_text(Some(tooltip));
    button.update_property(&[gtk::accessible::Property::Label(&accessible_label)]);
    button.add_css_class("flat");
    button.set_sensitive(enabled);
    let emit = Emit::clone(emit);
    button.connect_clicked(move |_| {
        emit(ChainEvent::Edit {
            section,
            edit: chain_edit.clone(),
        });
    });
    button
}

/// Build a `+` MenuButton listing every effect available in the
/// section's inventory. Clicking an item emits an append edit.
fn add_effect_menu_button(
    section: SubchainKind,
    inventory: &[EffectSchema],
    emit: &Emit<ChainEvent>,
) -> 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 emit = Emit::clone(emit);
            let effect = meta.name.clone();
            // The popover (transitively) owns this button, so hold it
            // weakly to avoid a reference cycle.
            btn.connect_clicked(glib::clone!(
                #[weak]
                popover,
                move |_| {
                    popover.popdown();
                    emit(ChainEvent::Edit {
                        section,
                        edit: ChainEdit::Append(effect.clone()),
                    });
                }
            ));
            list.append(&btn);
        }
    }
    // Long inventories scroll instead of growing past the window.
    let scroller = gtk::ScrolledWindow::builder()
        .hscrollbar_policy(gtk::PolicyType::Never)
        .propagate_natural_height(true)
        .propagate_natural_width(true)
        .max_content_height(ADD_EFFECT_MENU_MAX_HEIGHT)
        .child(&list)
        .build();
    popover.set_child(Some(&scroller));

    let (title, _) = section_labels(section);
    let menu = gtk::MenuButton::builder()
        .icon_name("list-add-symbolic")
        .tooltip_text("Add Effect to Chain")
        .popover(&popover)
        .valign(gtk::Align::Center)
        .build();
    menu.update_property(&[gtk::accessible::Property::Label(&format!(
        "Add Effect to {title}"
    ))]);
    menu.add_css_class("flat");
    menu
}