fluxframe-gui 0.8.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
//! Root relm4 [`Component`] gluing the IPC worker to the libadwaita
//! window.
//!
//! Owns the application state (`AppState`), reacts to `WorkerOutput`
//! messages, and forwards user intent (preset switching, reload,
//! preview) back to the worker.

use std::collections::HashMap;
use std::path::PathBuf;
use std::process::Command as ProcessCommand;

use adw::prelude::*;
use fluxframe_core::protocol::Command;
use gtk::glib;
use relm4::prelude::{Component, ComponentParts, ComponentSender};
use relm4::{Sender, WorkerController};

use crate::components::{chain_page, preset_bar, preview, status_page};
use crate::debounce::Debouncer;
use crate::ipc::{IpcWorker, WorkerInput, WorkerOutput};
use crate::state::{AppState, ConnectionStatus};

/// Dwell time for daemon-error toasts. 5 seconds is long enough for
/// the operator to read but short enough to avoid stacking when a
/// slider drags through several rejection cases.
const TOAST_TIMEOUT_SECS: u32 = 5;

// TODO(stage-15): pull `[output].device` from the daemon's `GetConfig`
// round-trip instead of hardcoding /dev/video10. Same path used by both
// the embedded preview and the detached gst-launch viewer.
const DEFAULT_PREVIEW_DEVICE: &str = "/dev/video10";

/// Messages the AppModel handles internally.
#[derive(Debug)]
pub enum AppMsg {
    /// IPC worker reply. Wrapped so the AppModel can pattern-match
    /// without depending on the worker's `Output` type directly.
    Ipc(WorkerOutput),
    /// User clicked the preset DropDown — switch to `name`.
    SetPreset(String),
    /// Keyboard shortcut `Ctrl+<digit>` — switch to the preset at the
    /// 1-indexed slot, or no-op if the slot is empty.
    SetPresetByIndex {
        /// 1-indexed slot in the loaded preset list. `NonZeroUsize`
        /// rules out the `slot - 1` underflow at the type level.
        slot: std::num::NonZeroUsize,
    },
    /// User clicked the Reload button.
    Reload,
    /// User clicked "Open preview" — spawn an external viewer.
    OpenPreview,
    /// User clicked Retry on the disconnected status page.
    Retry,
    /// A parameter widget produced a new value — dispatch as
    /// `Command::Set { path, value }` to the daemon.
    SetParam {
        /// Dot-path `<section>.<effect>.<field>`.
        path: String,
        /// New value as a JSON `Value`.
        value: serde_json::Value,
    },
    /// User added an effect to a section's chain (via the section's
    /// add MenuButton). The new effect appends to the end.
    AddEffect {
        /// Sub-chain identifier as the typed [`fluxframe_core::SubchainKind`]
        /// enum (no longer accepts arbitrary strings).
        section: fluxframe_core::SubchainKind,
        /// Effect registry name to append.
        effect: String,
    },
    /// User removed an effect (✕ button on the chain item).
    RemoveEffect {
        /// Sub-chain identifier as the typed [`fluxframe_core::SubchainKind`]
        /// enum (no longer accepts arbitrary strings).
        section: fluxframe_core::SubchainKind,
        /// Position in the chain (0-indexed) to remove.
        index: usize,
    },
    /// User moved an effect one slot toward the chain head (`↑` button).
    /// No-op at index 0.
    MoveEffectUp {
        /// Sub-chain identifier as the typed [`fluxframe_core::SubchainKind`]
        /// enum (no longer accepts arbitrary strings).
        section: fluxframe_core::SubchainKind,
        /// Position to move (0-indexed); must be > 0.
        index: usize,
    },
    /// User moved an effect one slot toward the chain tail (`↓` button).
    /// No-op at the last index.
    MoveEffectDown {
        /// Sub-chain identifier as the typed [`fluxframe_core::SubchainKind`]
        /// enum (no longer accepts arbitrary strings).
        section: fluxframe_core::SubchainKind,
        /// Position to move (0-indexed).
        index: usize,
    },
    /// User clicked Save — persist the current preset back into the
    /// daemon's TOML config file.
    Save,
    /// User clicked "Save as…" — open the name-prompt dialog.
    SaveAsPrompt,
    /// User confirmed the "Save as…" dialog with a non-empty name.
    SaveAs(String),
    /// User clicked Revert — refetch the active preset from the
    /// daemon, discarding any in-memory edits since the last sync.
    Revert,
}

/// Record of an in-flight `Command::Set` correlated by request tag.
///
/// `new` is the value the widget produced and the daemon is being
/// asked to apply; `previous` is the last-known-good fallback. Today
/// rollback is implemented by leaving `active_config` untouched on
/// `Err` and rebuilding the chain page — the widget re-reads
/// `active_config`. `previous` is kept on the struct so a future
/// targeted-widget rollback path (without a full rebuild) does not
/// need to re-derive it from the cache.
#[derive(Debug, Clone)]
struct PendingSet {
    path: String,
    new: serde_json::Value,
    #[allow(
        dead_code,
        reason = "reserved for future per-widget rollback (rebuild-from-active_config covers it today)"
    )]
    previous: serde_json::Value,
}

/// Classification of an in-flight request, used by `on_reply` to
/// dispatch on the reply payload without inspecting `data` shape
/// heuristics.
#[derive(Debug, Clone)]
enum PendingKind {
    /// A `Command::Set` issued by a widget change.
    SetParam(PendingSet),
    /// A `Command::GetConfig { path: None }` issued after Connected,
    /// Reload, or Revert — the `Ok` payload is the full preset tree.
    GetConfig,
    /// Like [`GetConfig`](Self::GetConfig), but emitted as the
    /// follow-up to a Revert — on `Ok` the AppModel re-syncs the
    /// baseline so the dirty marker clears.
    GetConfigForRevert,
    /// A `Command::CurrentPreset` issued after SetPreset or Reload —
    /// the `Ok` payload is a JSON string with the active preset name.
    CurrentPreset,
    /// A `Command::SavePreset` issued by the Save button — on `Ok`,
    /// baseline is re-synced from `active_config` so the dirty marker
    /// clears without another `GetConfig` round-trip.
    Save,
    /// A `Command::SavePresetAs` issued by the Save-as dialog — on
    /// `Ok`, the new preset name is added to the dropdown and the
    /// baseline is re-synced.
    SaveAs {
        /// New preset name; needed by the Ok handler to update the
        /// presets list and switch the dropdown.
        new_name: String,
    },
    /// Anything else we send (Reload, SetPreset, SetChain, …). Tracked
    /// for completeness so unknown-tag warnings stay meaningful.
    Other,
}

/// Root component.
pub struct AppModel {
    state: AppState,
    /// IPC worker handle. `take()`-replaced on Retry so a fresh
    /// connection can be opened without restarting the GUI.
    worker: Option<WorkerController<IpcWorker>>,
    /// Cloned input sender — needed by widget callbacks that have to
    /// re-enter the model after `update()` has returned. relm4
    /// `Sender` is `Clone`, unlike the `ComponentSender` itself.
    input_sender: Sender<AppMsg>,
    /// Monotonic request counter used to correlate worker replies.
    next_tag: u64,
    /// Widgets that need to live longer than `view!` — the preset
    /// bar's children are mutated in `update_view`.
    preset_bar: preset_bar::PresetBar,
    /// Body container — we swap between a `StatusPage` (disconnected)
    /// and the chain editor (connected).
    body_container: gtk::Box,
    /// Embedded live preview pane at the top of the window. Owns the
    /// preview GStreamer pipeline; dropping it tears the pipeline
    /// down. Kept as a field purely so its `Drop` runs at window
    /// close — the widget itself is already parented into `outer`.
    /// Underscore-prefixed so the compiler doesn't flag the
    /// never-read field.
    _preview: preview::Preview,
    /// Per-parameter debounce queue shared by all `param_row`
    /// instances on the chain page.
    debouncer: Debouncer,
    /// Toast overlay wrapping the window body. Used to surface daemon
    /// `Err` replies (e.g. parameter validation failures) without
    /// blocking the editor.
    toast_overlay: adw::ToastOverlay,
    /// Last-good values for each `<section>.<effect>.<field>` path.
    /// Updated when a `Set` succeeds; used to roll back the widget on
    /// failure. Keyed by the same dot-path the daemon accepts.
    last_known_good: HashMap<String, serde_json::Value>,
    /// In-flight requests indexed by their wire `tag`. Replies look up
    /// their pending kind here to decide how to interpret the payload
    /// (vs. fragile `data` shape heuristics).
    pending: HashMap<u64, PendingKind>,
}

impl Component for AppModel {
    type Init = PathBuf;
    type Input = AppMsg;
    type Output = ();
    type CommandOutput = ();
    type Root = adw::ApplicationWindow;
    type Widgets = ();

    fn init_root() -> Self::Root {
        // Only sizing + maximisation here; the close-request handler is
        // wired in `init()` once the `gtk::Paned` widget exists so we
        // can persist its split position alongside the window geometry.
        let geometry = crate::persistence::load();
        let window = adw::ApplicationWindow::builder()
            .title("FluxFrame")
            .default_width(geometry.width)
            .default_height(geometry.height)
            .build();
        if geometry.maximized {
            window.maximize();
        }
        window
    }

    fn init(
        socket_path: Self::Init,
        root: Self::Root,
        sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        // Spawn the IPC worker.
        let worker = IpcWorker::builder()
            .detach_worker(socket_path.clone())
            .forward(sender.input_sender(), AppMsg::Ipc);

        // Build the header bar with closures that re-enter via the
        // input sender.
        let bar_sender = sender.input_sender().clone();
        let reload_sender = sender.input_sender().clone();
        let preview_sender = sender.input_sender().clone();
        let save_sender = sender.input_sender().clone();
        let save_as_sender = sender.input_sender().clone();
        let revert_sender = sender.input_sender().clone();
        let bar = preset_bar::build(preset_bar::PresetBarCallbacks {
            on_preset_change: Box::new(move |name| {
                let _ = bar_sender.send(AppMsg::SetPreset(name));
            }),
            on_reload: Box::new(move || {
                let _ = reload_sender.send(AppMsg::Reload);
            }),
            on_preview: Box::new(move || {
                let _ = preview_sender.send(AppMsg::OpenPreview);
            }),
            on_save: Box::new(move || {
                let _ = save_sender.send(AppMsg::Save);
            }),
            on_save_as: Box::new(move || {
                let _ = save_as_sender.send(AppMsg::SaveAsPrompt);
            }),
            on_revert: Box::new(move || {
                let _ = revert_sender.send(AppMsg::Revert);
            }),
        });

        let body = gtk::Box::new(gtk::Orientation::Vertical, 0);
        body.set_hexpand(true);
        body.set_vexpand(true);

        // Initial body — a Connecting status. Replaced on
        // `Connected` / `Disconnected`.
        body.append(&connecting_page("Talking to the FluxFrame daemon."));

        let preview = preview::build(std::path::Path::new(DEFAULT_PREVIEW_DEVICE));

        // `gtk::Paned` owns the split between preview (top) and the
        // chain editor (bottom). The Paned takes the responsibility
        // for the vertical allocation away from the outer Box, which
        // means the Picture inside `preview.root` can use
        // `content_fit = Contain` + `can_shrink = true` without
        // fighting `gtk::Picture`'s aspect-ratio-derived natural
        // size — the Paned tells the Picture exactly how tall it gets,
        // and the Picture letterboxes inside that allocation.
        //
        // `resize_*_child = true` makes the divider track the parent
        // size proportionally on resize; `shrink_*_child = false`
        // stops the user from collapsing either half to zero by
        // dragging the handle to an extreme.
        let geometry = crate::persistence::load();
        let paned = gtk::Paned::builder()
            .orientation(gtk::Orientation::Vertical)
            .vexpand(true)
            .hexpand(true)
            .resize_start_child(true)
            .resize_end_child(true)
            .shrink_start_child(false)
            .shrink_end_child(false)
            .start_child(&preview.root)
            .end_child(&body)
            .position(geometry.resolved_preview_split())
            .build();

        let outer = gtk::Box::new(gtk::Orientation::Vertical, 0);
        outer.append(&bar.root);
        outer.append(&paned);

        // Persist window geometry + Paned split on close. `close-request`
        // fires before teardown so the window and paned are still
        // queryable. `paned` is captured by move into the closure;
        // the closure outlives `init()` because GTK holds the signal.
        let paned_for_close = paned.clone();
        root.connect_close_request(move |w| {
            let state = crate::persistence::WindowState {
                width: w.default_width(),
                height: w.default_height(),
                maximized: w.is_maximized(),
                preview_split: Some(paned_for_close.position()),
            };
            crate::persistence::save(&state);
            glib::Propagation::Proceed
        });

        // Wrap the whole window body in a ToastOverlay so daemon
        // errors can fly over the chain page without disturbing the
        // layout. The overlay child has to be set before the window
        // content is attached.
        let toast_overlay = adw::ToastOverlay::new();
        toast_overlay.set_child(Some(&outer));
        root.set_content(Some(&toast_overlay));

        // Global keyboard shortcuts (Ctrl+R reload, Ctrl+Q quit,
        // Ctrl+1..9 preset switch).
        crate::shortcuts::install(&root, sender.input_sender());

        let state = AppState::new(socket_path);
        let input_sender = sender.input_sender().clone();
        let model = Self {
            state,
            worker: Some(worker),
            input_sender,
            next_tag: 0,
            preset_bar: bar,
            body_container: body,
            _preview: preview,
            debouncer: Debouncer::new(),
            toast_overlay,
            last_known_good: HashMap::new(),
            pending: HashMap::new(),
        };
        ComponentParts { model, widgets: () }
    }

    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>, _root: &Self::Root) {
        match msg {
            AppMsg::Ipc(out) => self.on_ipc(out),
            AppMsg::SetPreset(name) => {
                self.send_kind(Command::SetPreset { name }, PendingKind::Other);
            }
            AppMsg::SetPresetByIndex { slot } => {
                // 1-indexed slot → list position (slot - 1). `NonZeroUsize`
                // makes the subtraction safe at the type level.
                let Some(name) = self.state.presets.get(slot.get() - 1).cloned() else {
                    tracing::debug!(slot = slot.get(), "no preset at slot");
                    return;
                };
                let _ = self.input_sender.send(AppMsg::SetPreset(name));
            }
            AppMsg::Reload => {
                self.send_kind(Command::Reload, PendingKind::Other);
                // After a successful reload the cached active_preset
                // / config may have drifted; refetch the state.
                self.send_kind(Command::CurrentPreset, PendingKind::CurrentPreset);
                self.send_kind(Command::GetConfig { path: None }, PendingKind::GetConfig);
            }
            AppMsg::OpenPreview => open_preview(),
            AppMsg::Retry => self.retry(),
            AppMsg::SetParam { path, value } => {
                self.send_set_param(path, value);
            }
            AppMsg::AddEffect { section, effect } => {
                self.mutate_chain(section, |chain| chain.push(effect));
            }
            AppMsg::RemoveEffect { section, index } => {
                self.mutate_chain(section, |chain| {
                    if index < chain.len() {
                        chain.remove(index);
                    }
                });
            }
            AppMsg::MoveEffectUp { section, index } => {
                if index == 0 {
                    return;
                }
                self.mutate_chain(section, |chain| {
                    if index < chain.len() {
                        chain.swap(index - 1, index);
                    }
                });
            }
            AppMsg::MoveEffectDown { section, index } => {
                self.mutate_chain(section, |chain| {
                    if index + 1 < chain.len() {
                        chain.swap(index, index + 1);
                    }
                });
            }
            AppMsg::Save => {
                self.send_kind(Command::SavePreset, PendingKind::Save);
            }
            AppMsg::SaveAsPrompt => {
                self.open_save_as_dialog();
            }
            AppMsg::SaveAs(name) => {
                self.send_kind(
                    Command::SavePresetAs { name: name.clone() },
                    PendingKind::SaveAs { new_name: name },
                );
            }
            AppMsg::Revert => {
                self.send_kind(
                    Command::GetConfig { path: None },
                    PendingKind::GetConfigForRevert,
                );
            }
        }
    }
}

impl AppModel {
    fn next_tag(&mut self) -> u64 {
        let tag = self.next_tag;
        self.next_tag = self.next_tag.wrapping_add(1);
        tag
    }

    /// Send a command to the worker and register its `tag` with a
    /// classification used by `on_reply` to dispatch on the reply.
    fn send_kind(&mut self, command: Command, kind: PendingKind) {
        if self.worker.is_none() {
            tracing::warn!("send_kind dropped — worker not connected");
            return;
        }
        let tag = self.next_tag();
        // Worker presence re-checked just above; safe to unwrap is justified.
        let worker = self.worker.as_ref().expect("worker presence checked above");
        self.pending.insert(tag, kind);
        let _ = worker.sender().send(WorkerInput::Send { tag, command });
    }

    /// Apply `mutate` to the chain array for `section`, dispatch a
    /// `Command::SetChain` with the result, then refetch the active
    /// config so widgets pick up the new chain shape (and any defaulting
    /// the daemon applied).
    fn mutate_chain(
        &mut self,
        section: fluxframe_core::SubchainKind,
        mutate: impl FnOnce(&mut Vec<String>),
    ) {
        let chain_str = section.as_str();
        let mut chain = self.state.chain_for(chain_str);
        mutate(&mut chain);
        self.send_kind(Command::SetChain { section, chain }, PendingKind::Other);
        // Refetch the active config so widgets pick up the new chain
        // shape (and any defaulting the daemon applied).
        self.send_kind(Command::GetConfig { path: None }, PendingKind::GetConfig);
    }

    /// Dispatch a parameter change as a `Command::Set` while capturing
    /// the prior value so the widget can be rolled back if the daemon
    /// rejects it.
    fn send_set_param(&mut self, path: String, value: serde_json::Value) {
        // Previous value preference order: the last-known-good cache
        // (only populated on accepted Sets) → the current active_config
        // entry → `Null`.
        let previous = self
            .last_known_good
            .get(&path)
            .cloned()
            .or_else(|| read_config_value(&self.state.active_config, &path))
            .unwrap_or(serde_json::Value::Null);
        let kind = PendingKind::SetParam(PendingSet {
            path: path.clone(),
            new: value.clone(),
            previous,
        });
        self.send_kind(Command::Set { path, value }, kind);
    }

    fn on_ipc(&mut self, out: WorkerOutput) {
        match out {
            WorkerOutput::Connected(initial) => {
                tracing::info!(presets = initial.presets.len(), "daemon handshake complete");
                self.state.status = ConnectionStatus::Connected;
                self.state.presets = initial.presets;
                self.state.active_preset = Some(initial.active_preset.clone());
                self.state.inventory = initial.inventory;
                // Synchronise both pointers — handshake is the clean
                // baseline, so dirty starts at false.
                self.state.baseline_config = initial.active_config.clone();
                self.state.active_config = initial.active_config;
                self.state.config_path = initial.config_path;

                preset_bar::set_presets(
                    &self.preset_bar,
                    &self.state.presets,
                    self.state.active_preset.as_deref(),
                );
                if let Some(active) = self.state.active_preset.as_deref() {
                    preset_bar::set_active_preset(&self.preset_bar, active);
                }
                self.refresh_dirty_indicator();
                self.rebuild_chain_page();
            }
            WorkerOutput::Disconnected { reason } => {
                tracing::warn!(reason = %reason, "daemon disconnected");
                self.state.status = ConnectionStatus::Disconnected(reason.clone());
                self.replace_body_with_status_page(&reason);
            }
            WorkerOutput::Reply { tag, response } => self.on_reply(tag, response),
        }
    }

    fn on_reply(&mut self, tag: u64, response: fluxframe_core::protocol::Response) {
        let kind = self.pending.remove(&tag);
        match response {
            fluxframe_core::protocol::Response::Ok { data } => {
                tracing::debug!(tag, ?data, "reply ok");
                match kind {
                    Some(PendingKind::SetParam(set)) => {
                        // Daemon accepted the write. Mirror it into
                        // active_config so subsequent rebuilds reflect
                        // the new value, and remember the value as the
                        // rollback target for future Sets on this path.
                        set_config_value(&mut self.state.active_config, &set.path, set.new.clone());
                        self.last_known_good.insert(set.path, set.new);
                        // Any successful Set creates a delta against
                        // the daemon's persisted baseline.
                        self.refresh_dirty_indicator();
                    }
                    Some(PendingKind::CurrentPreset) => {
                        if let Some(name) = data.as_str() {
                            self.state.active_preset = Some(name.to_string());
                            preset_bar::set_active_preset(&self.preset_bar, name);
                            self.refresh_dirty_indicator();
                        }
                    }
                    Some(PendingKind::GetConfig) => {
                        // Full preset tree — refresh and rebuild. A
                        // GetConfig that follows SetPreset / Reload
                        // pulls a fresh daemon snapshot, so the
                        // baseline tracks active_config and the
                        // dirty marker clears.
                        self.state.active_config = data.clone();
                        self.state.baseline_config = data;
                        self.refresh_dirty_indicator();
                        self.rebuild_chain_page();
                    }
                    Some(PendingKind::GetConfigForRevert) => {
                        // Revert pulls the canonical state, then
                        // discards the in-memory edits.
                        self.state.active_config = data.clone();
                        self.state.baseline_config = data;
                        self.last_known_good.clear();
                        self.refresh_dirty_indicator();
                        self.rebuild_chain_page();
                    }
                    Some(PendingKind::Save) => {
                        // Daemon persisted the active preset; the
                        // current in-memory state IS the new
                        // baseline.
                        self.state.baseline_config = self.state.active_config.clone();
                        self.refresh_dirty_indicator();
                    }
                    Some(PendingKind::SaveAs { new_name }) => {
                        // Daemon created the new preset block in
                        // TOML; mirror in the local preset list so
                        // the dropdown picks it up immediately. The
                        // active preset is unchanged (Save-as does
                        // NOT switch), so dirty stays as it was.
                        if !self.state.presets.iter().any(|p| p == &new_name) {
                            self.state.presets.push(new_name);
                            preset_bar::set_presets(
                                &self.preset_bar,
                                &self.state.presets,
                                self.state.active_preset.as_deref(),
                            );
                        }
                    }
                    Some(PendingKind::Other) | None => {
                        // Reload / SetPreset / SetChain or an
                        // untracked tag — nothing further to do; the
                        // follow-up CurrentPreset/GetConfig will
                        // refresh the UI.
                    }
                }
            }
            fluxframe_core::protocol::Response::Err { error, hint } => {
                tracing::warn!(tag, error = %error, hint = ?hint, "reply err");
                match kind {
                    Some(PendingKind::SetParam(_set)) => {
                        // Daemon rejected the write. Surface the error
                        // via a toast and rebuild the chain page so
                        // every widget snaps back to active_config
                        // (which still holds the previous value because
                        // we only commit on Ok above).
                        self.show_error_toast(&error, hint.as_deref());
                        self.rebuild_chain_page();
                    }
                    Some(
                        PendingKind::Other
                        | PendingKind::Save
                        | PendingKind::SaveAs { .. }
                        | PendingKind::GetConfigForRevert,
                    ) => {
                        // Reload / SetPreset / SetChain / Save /
                        // SaveAs / Revert: no widget rollback, but
                        // the user still deserves to know the daemon
                        // refused the request (e.g. "no writable
                        // config path" on Save under
                        // --no-default-config, or "preset already
                        // exists" on SaveAs).
                        self.show_error_toast(&error, hint.as_deref());
                    }
                    Some(PendingKind::CurrentPreset | PendingKind::GetConfig) | None => {
                        // Internal refetches and unknown tags
                        // — log only, the operator will see the warn.
                    }
                }
            }
            // `Response` is `#[non_exhaustive]`; tolerate future
            // variants without panicking the UI thread.
            _ => {
                tracing::warn!(tag, "unrecognised Response variant");
            }
        }
    }

    /// Push the AppState's dirty flag + writable config path into the
    /// preset bar so the Save / Save as / Revert buttons and the
    /// title-bar marker stay in sync with the in-memory delta.
    fn refresh_dirty_indicator(&self) {
        preset_bar::set_dirty_state(
            &self.preset_bar,
            self.state.is_dirty(),
            self.state.config_path.as_deref(),
            self.state.active_preset.as_deref(),
        );
    }

    /// Show a small modal asking the operator for a new preset name.
    /// On OK with a non-empty trimmed name, sends [`AppMsg::SaveAs`]
    /// back into the AppModel. On Cancel or empty input, no-op.
    ///
    /// Uses [`adw::AlertDialog`] (the GTK 4.10+ replacement for
    /// [`gtk::Dialog`]) so the dialog stays inside the application
    /// window as a transient overlay rather than spawning a separate
    /// OS-level window.
    fn open_save_as_dialog(&self) {
        let entry = gtk::Entry::builder()
            .placeholder_text("Preset name")
            .activates_default(true)
            .build();

        let dialog = adw::AlertDialog::new(
            Some("Save preset as…"),
            Some("Choose a name for the new preset."),
        );
        dialog.add_response("cancel", "Cancel");
        dialog.add_response("save", "Save");
        dialog.set_response_appearance("save", adw::ResponseAppearance::Suggested);
        dialog.set_default_response(Some("save"));
        dialog.set_close_response("cancel");
        dialog.set_extra_child(Some(&entry));

        // `activates_default = true` on the entry combined with
        // `set_default_response("save")` on the dialog makes Enter
        // commit the dialog without a separate signal handler.

        let entry_for_response = entry.clone();
        let sender = self.input_sender.clone();
        dialog.connect_response(None, move |_dlg, response| {
            if response == "save" {
                let name = entry_for_response.text().to_string().trim().to_string();
                if !name.is_empty() {
                    let _ = sender.send(AppMsg::SaveAs(name));
                }
            }
        });

        // `AlertDialog::present` walks up the widget tree to find
        // the toplevel; passing the headerbar (already parented into
        // the application window) is enough to anchor the dialog
        // transient over the right window — no need to plumb the
        // root reference through AppModel.
        dialog.present(Some(&self.preset_bar.root));
    }

    /// Surface a daemon error as a 5-second `AdwToast` overlay banner.
    ///
    /// Used by [`Self::on_reply`] for any [`PendingKind`] variant whose
    /// Err reply deserves operator attention. Internally builds a single
    /// formatted line via [`format_toast_error`] so error and hint share
    /// a consistent shape across rejection sources.
    fn show_error_toast(&self, error: &str, hint: Option<&str>) {
        let message = format_toast_error(error, hint);
        let toast = adw::Toast::builder()
            .title(&message)
            .timeout(TOAST_TIMEOUT_SECS)
            .build();
        self.toast_overlay.add_toast(toast);
    }

    /// Drop the current body content and re-render the chain editor
    /// from the active state. Called after Connected, SetPreset, and
    /// any successful refetch.
    fn rebuild_chain_page(&self) {
        tracing::debug!(
            sections = chain_page::SECTION_COUNT,
            preset = self.state.active_preset.as_deref().unwrap_or("(none)"),
            "rebuilt chain page"
        );
        clear_children(&self.body_container);
        // `chain_page::build` already returns a `ScrolledWindow` with
        // the right policy/expand flags — no need to wrap again.
        let page = chain_page::build(&self.state, &self.debouncer, &self.input_sender);
        self.body_container.append(&page);
    }

    fn replace_body_with_status_page(&self, reason: &str) {
        clear_children(&self.body_container);
        let socket = self.state.socket_path.clone();
        let retry_sender = self.input_sender.clone();
        let page = status_page::build(&socket, reason, move || {
            let _ = retry_sender.send(AppMsg::Retry);
        });
        self.body_container.append(&page);
    }

    fn retry(&mut self) {
        tracing::info!("retry: re-creating IPC worker");
        // Drop the old worker (its background thread tears down) and
        // create a fresh one.
        self.worker = None;
        let socket_path = self.state.socket_path.clone();
        let worker = IpcWorker::builder()
            .detach_worker(socket_path)
            .forward(&self.input_sender, AppMsg::Ipc);
        self.worker = Some(worker);
        self.state.status = ConnectionStatus::Connecting;
        clear_children(&self.body_container);
        self.body_container
            .append(&connecting_page("Re-establishing the daemon socket."));
    }
}

/// Remove every child from a `gtk::Box`. relm4 has no built-in
/// "replace contents" for raw widgets.
fn clear_children(container: &gtk::Box) {
    while let Some(child) = container.first_child() {
        container.remove(&child);
    }
}

/// Build a "Connecting…" status page with the supplied description.
/// Icon and title are constant; only the description differs between
/// the initial wait and the post-Retry wait.
fn connecting_page(description: &str) -> adw::StatusPage {
    adw::StatusPage::builder()
        .icon_name("content-loading-symbolic")
        .title("Connecting…")
        .description(description)
        .hexpand(true)
        .vexpand(true)
        .build()
}

/// Read the leaf value at `path` (`<section>.<effect>.<field>`) out of
/// the active config tree. Returns `None` if any segment is missing.
///
/// Mirrors the layout the daemon's `Set` command writes back: the path
/// is rewritten to `<section>.per_effect.<effect>.<field>` for the
/// lookup, because per-effect fields live under `per_effect`.
fn read_config_value(config: &serde_json::Value, path: &str) -> Option<serde_json::Value> {
    let mut parts = path.splitn(3, '.');
    let section = parts.next()?;
    let effect = parts.next()?;
    let field = parts.next()?;
    config
        .get(section)?
        .get("per_effect")?
        .get(effect)?
        .get(field)
        .cloned()
}

/// Write `value` at `<section>.per_effect.<effect>.<field>` inside the
/// active config tree, creating intermediate objects as needed. Used
/// to keep the cached config in sync when a `Set` is accepted by the
/// daemon so subsequent `rebuild_chain_page` calls render the new
/// value without a full `GetConfig` round-trip.
fn set_config_value(config: &mut serde_json::Value, path: &str, value: serde_json::Value) {
    let mut parts = path.splitn(3, '.');
    let (Some(section), Some(effect), Some(field)) = (parts.next(), parts.next(), parts.next())
    else {
        tracing::warn!(path, "set_config_value: malformed dot-path, skipped");
        return;
    };
    if !config.is_object() {
        *config = serde_json::Value::Object(serde_json::Map::new());
    }
    let root = config
        .as_object_mut()
        .expect("config coerced to object just above");
    let section_obj = root
        .entry(section.to_string())
        .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));
    if !section_obj.is_object() {
        *section_obj = serde_json::Value::Object(serde_json::Map::new());
    }
    let section_map = section_obj
        .as_object_mut()
        .expect("section coerced to object just above");
    let per_effect = section_map
        .entry("per_effect".to_string())
        .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));
    if !per_effect.is_object() {
        *per_effect = serde_json::Value::Object(serde_json::Map::new());
    }
    let per_effect_map = per_effect
        .as_object_mut()
        .expect("per_effect coerced to object just above");
    let effect_obj = per_effect_map
        .entry(effect.to_string())
        .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));
    if !effect_obj.is_object() {
        *effect_obj = serde_json::Value::Object(serde_json::Map::new());
    }
    let effect_map = effect_obj
        .as_object_mut()
        .expect("effect coerced to object just above");
    effect_map.insert(field.to_string(), value);
}

/// Compose a one-line toast title from the daemon's error reason and
/// optional hint. Kept short — toasts wrap awkwardly past one line.
fn format_toast_error(error: &str, hint: Option<&str>) -> String {
    match hint {
        Some(h) if !h.is_empty() => format!("{error} — {h}"),
        _ => error.to_string(),
    }
}

/// Spawn `gst-launch-1.0` on `/dev/video10` to show the daemon's
/// live output. Detached — closing the GUI does not kill the viewer
/// (use case: open preview once, keep tuning).
///
/// Failures are logged but not surfaced to the user via a dialog
/// because the operation is best-effort.
fn open_preview() {
    let mut cmd = ProcessCommand::new("gst-launch-1.0");
    let device_arg = format!("device={DEFAULT_PREVIEW_DEVICE}");
    cmd.args([
        "v4l2src",
        device_arg.as_str(),
        "!",
        "videoconvert",
        "!",
        "autovideosink",
    ]);
    match cmd.spawn() {
        Ok(child) => tracing::info!(pid = child.id(), "spawned gst-launch preview"),
        Err(e) => tracing::warn!(error = %e, "failed to spawn gst-launch"),
    }
}