fluxframe-gui 0.10.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
//! Request bookkeeping and reply handling for the daemon session.
//!
//! [`apply_reply`] is the pure core of the AppModel's reply path: it
//! folds one daemon reply into [`AppState`] and returns the UI work the
//! caller has to perform ([`ReplyEffects`]). Keeping widgets out of it
//! makes every reply transition unit-testable without a display.

use std::collections::HashMap;

use fluxframe_core::protocol::Response;
use fluxframe_core::{SetPath, SubchainKind};
use serde_json::Value;

use crate::state::{AppState, ConfigSync};

/// Classification of an in-flight request, used to interpret its reply
/// without inspecting `data` shape heuristics.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum PendingKind {
    /// A `Command::Set` issued by a widget change. Rollback on `Err`
    /// works by leaving `active_config` untouched and resetting the
    /// one widget, so only the requested value is recorded.
    SetParam {
        /// Parameter the widget changed.
        path: SetPath,
        /// Value the daemon is being asked to apply.
        value: Value,
    },
    /// A `Command::GetConfig { path: None }` — the `Ok` payload is the
    /// full preset tree, installed according to the carried
    /// [`ConfigSync`].
    GetConfig(ConfigSync),
    /// A `Command::GetConfig { path: None }` keeping the baseline, sent
    /// after a toggle the GUI already mirrored locally. Its snapshot is
    /// installed without a page rebuild unless a chain changed shape.
    GetConfigQuiet,
    /// A `Command::SetEnabled` from an effect row's switch.
    SetEnabled {
        /// Sub-chain of the effect.
        section: SubchainKind,
        /// Effect name.
        effect: String,
        /// Requested flag.
        enabled: bool,
    },
    /// A `Command::SetPreset` switching to another preset. The clean
    /// baseline is refetched only once the daemon accepted the switch.
    SetPreset,
    /// A `Command::SetPreset` re-activating the active preset to drop
    /// unsaved edits: the daemon rebuilds it from its persisted copy,
    /// whereas `get_config` would echo the in-memory edits back.
    Revert,
    /// A `Command::Reload`. On `Ok` the preset list and the active
    /// preset are refetched, since the file on disk may have changed
    /// both.
    Reload,
    /// A `Command::ListPresets` — the `Ok` payload is the array of
    /// preset names.
    ListPresets,
    /// A `Command::CurrentPreset` — the `Ok` payload is a JSON string
    /// with the active preset name.
    CurrentPreset,
    /// A `Command::SavePreset` — on `Ok` the in-memory state becomes
    /// the baseline without another `GetConfig` round-trip.
    Save,
    /// A `Command::SavePresetAs` — on `Ok` the new name joins the preset
    /// list. The active preset does not change, so dirty is left as is.
    SaveAs {
        /// Name of the preset being created.
        new_name: String,
    },
    /// Anything else (SetChain, …). Tracked so their errors still
    /// reach the operator.
    Other,
}

/// In-flight requests keyed by their wire tag.
#[derive(Debug, Default)]
pub(crate) struct PendingRequests {
    next_tag: u64,
    kinds: HashMap<u64, PendingKind>,
}

impl PendingRequests {
    /// Allocate a fresh tag for a request of `kind`.
    pub(crate) fn register(&mut self, kind: PendingKind) -> u64 {
        let tag = self.next_tag;
        self.next_tag = self.next_tag.wrapping_add(1);
        self.kinds.insert(tag, kind);
        tag
    }

    /// Remove and return the kind registered for `tag`.
    pub(crate) fn take(&mut self, tag: u64) -> Option<PendingKind> {
        self.kinds.remove(&tag)
    }

    /// Forget every in-flight request (their replies will never arrive).
    /// Tags keep increasing, so a late reply cannot match a new request.
    pub(crate) fn clear(&mut self) {
        self.kinds.clear();
    }
}

/// Severity of a [`Notice`]; decides how long its toast stays up.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum NoticeKind {
    /// The daemon refused a request the operator cares about.
    Error,
    /// A request the operator triggered succeeded.
    Confirmation,
}

/// A message to surface to the operator.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Notice {
    /// Severity.
    pub(crate) kind: NoticeKind,
    /// One-line text.
    pub(crate) message: String,
}

/// UI work required after a reply was folded into the state. The
/// header is always refreshed by the caller, so it is not listed.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "one independent flag per follow-up the caller performs"
)]
pub(crate) struct ReplyEffects {
    /// Re-render the chain page from `active_config`.
    pub(crate) rebuild_chain: bool,
    /// Reset the widget of this parameter to its `active_config` value
    /// (the daemon refused the write).
    pub(crate) reset_param: Option<SetPath>,
    /// Show the enable switch of this effect as `active_config` says
    /// (after an accepted or refused `set_enabled`).
    pub(crate) sync_toggle: Option<(SubchainKind, String)>,
    /// Request the config without a page rebuild (baseline kept).
    pub(crate) refetch_config_quiet: bool,
    /// Re-populate the preset drop-down (list or active entry changed).
    pub(crate) presets_changed: bool,
    /// Request the active preset name and config as a clean baseline.
    pub(crate) refetch_active: bool,
    /// Request the preset list.
    pub(crate) refetch_presets: bool,
    /// Toast to show.
    pub(crate) notice: Option<Notice>,
}

/// Fold the reply to a request of `kind` (`None` for an unknown tag)
/// into `state` and report the UI work it requires.
pub(crate) fn apply_reply(
    state: &mut AppState,
    kind: Option<PendingKind>,
    response: Response,
) -> ReplyEffects {
    let mut effects = ReplyEffects::default();
    match response {
        Response::Ok { data } => match kind {
            Some(PendingKind::SetParam { path, value }) => {
                // Mirror the accepted write so rebuilds render it; it
                // is a delta against the persisted baseline.
                state.set_config_field(&path, value);
            }
            Some(PendingKind::CurrentPreset) => {
                if let Some(name) = data.as_str() {
                    state.active_preset = Some(name.to_string());
                    effects.presets_changed = true;
                }
            }
            Some(PendingKind::GetConfig(sync)) => {
                state.apply_config_snapshot(data, sync);
                effects.rebuild_chain = true;
            }
            Some(PendingKind::GetConfigQuiet) => {
                let shape = |state: &AppState| SubchainKind::ALL.map(|s| state.chain_for(s));
                let before = shape(state);
                state.apply_config_snapshot(data, ConfigSync::KeepBaseline);
                // Widgets already show this state; only a chain that
                // changed shape meanwhile needs new rows.
                effects.rebuild_chain = shape(state) != before;
            }
            Some(PendingKind::SetEnabled {
                section,
                effect,
                enabled,
            }) => {
                state.set_effect_enabled(section, &effect, enabled);
                effects.sync_toggle = Some((section, effect));
                // Confirm the mirror against the daemon's own view.
                effects.refetch_config_quiet = true;
            }
            Some(PendingKind::SetPreset) => effects.refetch_active = true,
            Some(PendingKind::Revert) => {
                effects.refetch_active = true;
                effects.notice = Some(confirmation("Unsaved changes discarded".into()));
            }
            Some(PendingKind::Reload) => {
                effects.refetch_active = true;
                effects.refetch_presets = true;
                effects.notice = Some(confirmation("Configuration reloaded".into()));
            }
            Some(PendingKind::ListPresets) => match crate::ipc::wire::parse_presets(&data) {
                Ok(names) => {
                    state.presets = names;
                    effects.presets_changed = true;
                }
                Err(e) => tracing::warn!(error = %e, "malformed list_presets reply"),
            },
            Some(PendingKind::Save) => {
                state.mark_saved();
                let message = match state.config_path.as_deref() {
                    Some(p) => format!("Saved to {}", p.display()),
                    None => "Saved".to_string(),
                };
                effects.notice = Some(confirmation(message));
            }
            Some(PendingKind::SaveAs { new_name }) => {
                effects.notice = Some(confirmation(format!("Saved as preset “{new_name}”")));
                if !state.presets.contains(&new_name) {
                    state.presets.push(new_name);
                    effects.presets_changed = true;
                }
            }
            // SetChain or an untracked tag — the follow-up GetConfig
            // refreshes the UI.
            Some(PendingKind::Other) | None => {}
        },
        Response::Err { error, hint } => {
            tracing::warn!(error = %error, hint = ?hint, "reply err");
            match kind {
                Some(PendingKind::SetParam { path, .. }) => {
                    // `active_config` still holds the previous value
                    // (we only commit on Ok), so resetting the widget
                    // from it snaps the control back.
                    effects.notice = Some(error_notice(&error, hint.as_deref()));
                    effects.reset_param = Some(path);
                }
                Some(PendingKind::SetEnabled {
                    section, effect, ..
                }) => {
                    // `active_config` is untouched, so syncing the switch
                    // from it flips it back.
                    effects.notice = Some(error_notice(&error, hint.as_deref()));
                    effects.sync_toggle = Some((section, effect));
                }
                Some(
                    PendingKind::Other
                    | PendingKind::Save
                    | PendingKind::SaveAs { .. }
                    | PendingKind::SetPreset
                    | PendingKind::Revert
                    | PendingKind::Reload,
                ) => {
                    // No rollback and no refetch: the daemon kept its
                    // state, so the dirty baseline must stay put. The
                    // operator still needs to know it refused.
                    effects.notice = Some(error_notice(&error, hint.as_deref()));
                }
                // Internal refetches and unknown tags — the warn above
                // is enough.
                Some(
                    PendingKind::CurrentPreset
                    | PendingKind::GetConfig(_)
                    | PendingKind::GetConfigQuiet
                    | PendingKind::ListPresets,
                )
                | None => {}
            }
        }
        // `Response` is `#[non_exhaustive]`; tolerate future variants.
        _ => tracing::warn!("unrecognised Response variant"),
    }
    effects
}

fn confirmation(message: String) -> Notice {
    Notice {
        kind: NoticeKind::Confirmation,
        message,
    }
}

/// One-line error text from the daemon's reason and optional hint.
/// Kept short — toasts wrap awkwardly past one line.
fn error_notice(error: &str, hint: Option<&str>) -> Notice {
    let message = match hint {
        Some(h) if !h.is_empty() => format!("{error} — {h}"),
        _ => error.to_string(),
    };
    Notice {
        kind: NoticeKind::Error,
        message,
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use fluxframe_core::parse_set_path;
    use serde_json::json;

    use super::*;

    fn state() -> AppState {
        let mut s = AppState::new(PathBuf::from("/tmp/test.sock"));
        s.presets = vec!["a".into(), "b".into()];
        s.active_preset = Some("a".into());
        s.apply_config_snapshot(
            crate::state::daemon_config(
                "[background]\nchain = [\"blur\"]\n\n[background.blur]\nradius = 30\n",
            ),
            ConfigSync::ResyncBaseline,
        );
        s
    }

    fn set_radius(v: i64) -> PendingKind {
        PendingKind::SetParam {
            path: parse_set_path("background.blur.radius").expect("valid"),
            value: json!(v),
        }
    }

    fn ok(data: Value) -> Response {
        Response::Ok { data }
    }

    fn err(error: &str, hint: Option<&str>) -> Response {
        Response::Err {
            error: error.into(),
            hint: hint.map(str::to_string),
        }
    }

    #[test]
    fn pending_requests_hand_out_unique_tags_and_forget_on_clear() {
        let mut p = PendingRequests::default();
        let t1 = p.register(PendingKind::Save);
        let t2 = p.register(PendingKind::Other);
        assert_ne!(t1, t2);
        assert_eq!(p.take(t1), Some(PendingKind::Save));
        assert_eq!(p.take(t1), None);
        p.clear();
        assert_eq!(p.take(t2), None);
        assert!(p.register(PendingKind::Other) > t2);
    }

    #[test]
    fn accepted_set_marks_dirty_without_rebuild() {
        let mut s = state();
        let fx = apply_reply(&mut s, Some(set_radius(12)), ok(Value::Null));
        assert!(s.is_dirty());
        assert_eq!(fx, ReplyEffects::default());
    }

    #[test]
    fn rejected_set_keeps_config_rebuilds_and_reports() {
        let mut s = state();
        let fx = apply_reply(
            &mut s,
            Some(set_radius(9999)),
            err("out of range", Some("max 256")),
        );
        assert!(!s.is_dirty());
        // Regression: a refusal must reset only the one widget; a full
        // page rebuild destroys a slider mid-drag.
        assert!(!fx.rebuild_chain);
        assert_eq!(
            fx.reset_param,
            Some(parse_set_path("background.blur.radius").expect("valid"))
        );
        assert_eq!(
            fx.notice,
            Some(Notice {
                kind: NoticeKind::Error,
                message: "out of range — max 256".into()
            })
        );
    }

    #[test]
    fn chain_refetch_keeps_unsaved_edits_dirty() {
        let mut s = state();
        apply_reply(&mut s, Some(set_radius(12)), ok(Value::Null));
        let daemon_view = s.active_config.clone();
        let fx = apply_reply(
            &mut s,
            Some(PendingKind::GetConfig(ConfigSync::KeepBaseline)),
            ok(daemon_view),
        );
        assert!(fx.rebuild_chain);
        assert!(s.is_dirty());
    }

    /// Regression: Revert used to resync the baseline to `get_config`,
    /// which echoes the daemon's in-memory edits back — the dirty
    /// marker vanished while the edits stayed live. The baseline may
    /// only move once the daemon has re-activated the persisted preset.
    #[test]
    fn revert_waits_for_daemon_before_touching_baseline() {
        let mut s = state();
        apply_reply(&mut s, Some(set_radius(12)), ok(Value::Null));
        let fx = apply_reply(&mut s, Some(PendingKind::Revert), ok(Value::Null));
        assert!(fx.refetch_active);
        assert!(
            s.is_dirty(),
            "baseline moves only with the refetched snapshot"
        );
    }

    /// Regression: a refused Reload / SetPreset / Revert used to be
    /// followed by an unconditional clean-baseline refetch, silently
    /// clearing the dirty marker over edits the daemon still holds.
    #[test]
    fn refused_preset_commands_keep_dirty_and_skip_refetch() {
        for kind in [
            PendingKind::Reload,
            PendingKind::SetPreset,
            PendingKind::Revert,
        ] {
            let mut s = state();
            apply_reply(&mut s, Some(set_radius(12)), ok(Value::Null));
            let fx = apply_reply(&mut s, Some(kind.clone()), err("no config path", None));
            assert!(!fx.refetch_active, "{kind:?}");
            assert!(!fx.refetch_presets, "{kind:?}");
            assert!(s.is_dirty(), "{kind:?}");
            assert!(fx.notice.is_some(), "{kind:?}");
        }
    }

    #[test]
    fn accepted_reload_refetches_presets_and_active_and_confirms() {
        let mut s = state();
        let fx = apply_reply(&mut s, Some(PendingKind::Reload), ok(Value::Null));
        assert!(fx.refetch_active);
        assert!(fx.refetch_presets);
        assert_eq!(fx.notice.map(|n| n.kind), Some(NoticeKind::Confirmation));
    }

    fn set_enabled(effect: &str, enabled: bool) -> PendingKind {
        PendingKind::SetEnabled {
            section: SubchainKind::Background,
            effect: effect.into(),
            enabled,
        }
    }

    /// An accepted toggle updates state and the switch in place and
    /// asks for a quiet refetch — no page rebuild, so an in-flight
    /// slider drag elsewhere is not destroyed.
    #[test]
    fn accepted_set_enabled_mirrors_locally_without_rebuild() {
        let mut s = state();
        let fx = apply_reply(&mut s, Some(set_enabled("blur", false)), ok(Value::Null));
        assert!(!s.effect_enabled(SubchainKind::Background, "blur"));
        assert!(s.is_dirty());
        assert!(!fx.rebuild_chain);
        assert!(fx.refetch_config_quiet);
        assert_eq!(
            fx.sync_toggle,
            Some((SubchainKind::Background, "blur".to_string()))
        );
    }

    #[test]
    fn refused_set_enabled_resyncs_the_switch_and_reports() {
        let mut s = state();
        let fx = apply_reply(
            &mut s,
            Some(set_enabled("blur", false)),
            err("no effect named 'blur'", None),
        );
        assert!(s.effect_enabled(SubchainKind::Background, "blur"));
        assert!(!s.is_dirty());
        assert!(!fx.refetch_config_quiet);
        assert_eq!(
            fx.sync_toggle,
            Some((SubchainKind::Background, "blur".to_string()))
        );
        assert_eq!(fx.notice.map(|n| n.kind), Some(NoticeKind::Error));
    }

    #[test]
    fn quiet_refetch_rebuilds_only_when_a_chain_changed_shape() {
        let mut s = state();
        let same_shape = crate::state::daemon_config(
            "[background]\nchain = [\"blur\"]\n\n[background.blur]\nenabled = false\nradius = 30\n",
        );
        let fx = apply_reply(&mut s, Some(PendingKind::GetConfigQuiet), ok(same_shape));
        assert!(!fx.rebuild_chain);
        assert!(s.is_dirty(), "quiet refetch keeps the baseline");

        let new_shape =
            crate::state::daemon_config("[background]\nchain = [\"blur\", \"vignette\"]\n");
        let fx = apply_reply(&mut s, Some(PendingKind::GetConfigQuiet), ok(new_shape));
        assert!(fx.rebuild_chain);
    }

    #[test]
    fn list_presets_replaces_names() {
        let mut s = state();
        let fx = apply_reply(
            &mut s,
            Some(PendingKind::ListPresets),
            ok(json!(["x", "y"])),
        );
        assert!(fx.presets_changed);
        assert_eq!(s.presets, vec!["x", "y"]);
    }

    #[test]
    fn save_clears_dirty_and_confirms_with_target() {
        let mut s = state();
        s.config_path = Some(PathBuf::from("/etc/ff.toml"));
        apply_reply(&mut s, Some(set_radius(12)), ok(Value::Null));
        let fx = apply_reply(&mut s, Some(PendingKind::Save), ok(Value::Null));
        assert!(!s.is_dirty());
        assert_eq!(
            fx.notice,
            Some(Notice {
                kind: NoticeKind::Confirmation,
                message: "Saved to /etc/ff.toml".into()
            })
        );
    }

    #[test]
    fn save_as_adds_new_preset_once() {
        let mut s = state();
        let fx = apply_reply(
            &mut s,
            Some(PendingKind::SaveAs {
                new_name: "c".into(),
            }),
            ok(Value::Null),
        );
        assert!(fx.presets_changed);
        assert_eq!(s.presets, vec!["a", "b", "c"]);
        let fx = apply_reply(
            &mut s,
            Some(PendingKind::SaveAs {
                new_name: "c".into(),
            }),
            ok(Value::Null),
        );
        assert!(!fx.presets_changed);
        assert_eq!(s.presets.len(), 3);
    }

    #[test]
    fn current_preset_updates_active_name() {
        let mut s = state();
        let fx = apply_reply(&mut s, Some(PendingKind::CurrentPreset), ok(json!("b")));
        assert_eq!(s.active_preset.as_deref(), Some("b"));
        assert!(fx.presets_changed);
    }

    #[test]
    fn internal_refetch_errors_and_unknown_tags_are_silent() {
        let mut s = state();
        for kind in [
            Some(PendingKind::CurrentPreset),
            Some(PendingKind::GetConfig(ConfigSync::ResyncBaseline)),
            Some(PendingKind::ListPresets),
            None,
        ] {
            assert_eq!(
                apply_reply(&mut s, kind, err("boom", None)),
                ReplyEffects::default()
            );
        }
    }

    #[test]
    fn user_request_errors_are_reported() {
        let mut s = state();
        for kind in [PendingKind::Other, PendingKind::Save, PendingKind::Revert] {
            let fx = apply_reply(&mut s, Some(kind), err("nope", Some("")));
            assert_eq!(fx.notice.map(|n| n.message), Some("nope".into()));
            assert!(!fx.rebuild_chain);
        }
    }
}