use std::collections::BTreeMap;
use std::path::PathBuf;
use fluxframe_core::{EffectSchema, OutputInfo, SetPath, SubchainKind};
use serde_json::{Map, Value};
#[derive(Debug, Default, Clone)]
pub(crate) struct EffectInventory {
pub(crate) sections: BTreeMap<SubchainKind, Vec<EffectSchema>>,
pub(crate) build_features: Vec<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ConnectionStatus {
Connecting,
Connected,
Disconnected,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PresetSwitch {
AlreadyActive,
NeedsConfirmation,
Proceed,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ChainEdit {
Append(String),
Remove(usize),
MoveUp(usize),
MoveDown(usize),
}
impl ChainEdit {
pub(crate) fn apply(self, chain: &mut Vec<String>) -> bool {
match self {
Self::Append(effect) => chain.push(effect),
Self::Remove(index) if index < chain.len() => {
chain.remove(index);
}
Self::MoveUp(index) if index > 0 && index < chain.len() => {
chain.swap(index - 1, index);
}
Self::MoveDown(index) if index + 1 < chain.len() => {
chain.swap(index, index + 1);
}
Self::Remove(_) | Self::MoveUp(_) | Self::MoveDown(_) => return false,
}
true
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ConfigSync {
KeepBaseline,
ResyncBaseline,
}
#[derive(Debug, Clone)]
pub(crate) struct LostSession {
active_preset: Option<String>,
active_config: Value,
baseline_config: Value,
}
#[derive(Debug)]
pub(crate) struct AppState {
pub(crate) socket_path: PathBuf,
pub(crate) status: ConnectionStatus,
pub(crate) presets: Vec<String>,
pub(crate) active_preset: Option<String>,
pub(crate) inventory: EffectInventory,
pub(crate) active_config: Value,
pub(crate) baseline_config: Value,
pub(crate) config_path: Option<PathBuf>,
pub(crate) output: Option<OutputInfo>,
}
impl AppState {
#[must_use]
pub(crate) fn new(socket_path: PathBuf) -> Self {
Self {
socket_path,
status: ConnectionStatus::Connecting,
presets: Vec::new(),
active_preset: None,
inventory: EffectInventory::default(),
active_config: Value::Null,
baseline_config: Value::Null,
config_path: None,
output: None,
}
}
#[must_use]
pub(crate) fn is_dirty(&self) -> bool {
self.active_config != self.baseline_config
}
#[must_use]
pub(crate) fn is_connected(&self) -> bool {
self.status == ConnectionStatus::Connected
}
#[must_use]
pub(crate) fn preset_switch(&self, name: &str) -> PresetSwitch {
if self.active_preset.as_deref() == Some(name) {
PresetSwitch::AlreadyActive
} else if self.is_dirty() {
PresetSwitch::NeedsConfirmation
} else {
PresetSwitch::Proceed
}
}
pub(crate) fn apply_config_snapshot(&mut self, data: Value, sync: ConfigSync) {
if sync == ConfigSync::ResyncBaseline {
self.baseline_config = data.clone();
}
self.active_config = data;
}
pub(crate) fn lost_session(&self) -> LostSession {
LostSession {
active_preset: self.active_preset.clone(),
active_config: self.active_config.clone(),
baseline_config: self.baseline_config.clone(),
}
}
pub(crate) fn apply_handshake(
&mut self,
active_preset: String,
config: Value,
lost: Option<&LostSession>,
) -> bool {
let dirty_before = lost.filter(|l| l.active_config != l.baseline_config);
let survived = dirty_before.filter(|l| {
l.active_preset.as_deref() == Some(active_preset.as_str()) && l.active_config == config
});
if let Some(l) = survived {
self.baseline_config = l.baseline_config.clone();
self.active_config = config;
} else {
self.apply_config_snapshot(config, ConfigSync::ResyncBaseline);
}
self.active_preset = Some(active_preset);
dirty_before.is_some() && survived.is_none()
}
pub(crate) fn mark_saved(&mut self) {
self.baseline_config = self.active_config.clone();
}
pub(crate) fn effects_for(&self, section: SubchainKind) -> &[EffectSchema] {
self.inventory
.sections
.get(§ion)
.map_or(&[][..], Vec::as_slice)
}
pub(crate) fn config_field(&self, path: &SetPath) -> &Value {
const NULL: Value = Value::Null;
self.active_config
.get(path.section.as_str())
.and_then(|s| s.get(&path.effect))
.and_then(|e| e.get(&path.field))
.unwrap_or(&NULL)
}
pub(crate) fn set_config_field(&mut self, path: &SetPath, value: Value) {
let keys = [path.section.as_str(), path.effect.as_str()];
let mut node = &mut self.active_config;
for key in keys {
node = object_mut(node)
.entry(key)
.or_insert_with(|| Value::Object(Map::new()));
}
object_mut(node).insert(path.field.clone(), value);
}
pub(crate) fn effect_enabled(&self, section: SubchainKind, effect: &str) -> bool {
self.active_config
.get(section.as_str())
.and_then(|s| s.get(effect))
.and_then(|e| e.get(fluxframe_core::EFFECT_ENABLED_KEY))
.and_then(Value::as_bool)
.unwrap_or(true)
}
pub(crate) fn set_effect_enabled(
&mut self,
section: SubchainKind,
effect: &str,
enabled: bool,
) {
let key = fluxframe_core::EFFECT_ENABLED_KEY;
if enabled {
let Some(section_obj) = self
.active_config
.get_mut(section.as_str())
.and_then(Value::as_object_mut)
else {
return;
};
let emptied = section_obj
.get_mut(effect)
.and_then(Value::as_object_mut)
.is_some_and(|table| {
table.remove(key);
table.is_empty()
});
if emptied {
section_obj.remove(effect);
}
} else {
let mut node = &mut self.active_config;
for segment in [section.as_str(), effect] {
node = object_mut(node)
.entry(segment)
.or_insert_with(|| Value::Object(Map::new()));
}
object_mut(node).insert(key.to_string(), Value::Bool(false));
}
}
pub(crate) fn chain_for(&self, section: SubchainKind) -> Vec<String> {
self.active_config
.get(section.as_str())
.and_then(|s| s.get("chain"))
.and_then(Value::as_array)
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default()
}
}
fn object_mut(node: &mut Value) -> &mut Map<String, Value> {
if !node.is_object() {
*node = Value::Object(Map::new());
}
match node {
Value::Object(map) => map,
_ => unreachable!("node coerced to object just above"),
}
}
#[cfg(test)]
pub(crate) fn daemon_config(preset_toml: &str) -> Value {
let preset: fluxframe_core::Preset = toml::from_str(preset_toml).expect("valid preset TOML");
serde_json::to_value(preset).expect("preset serialises")
}
#[cfg(test)]
mod tests {
use fluxframe_core::parse_set_path;
use super::*;
fn path(s: &str) -> SetPath {
parse_set_path(s).expect("valid test path")
}
fn fixture() -> AppState {
let mut s = AppState::new(PathBuf::from("/tmp/test.sock"));
s.active_config = daemon_config(
r#"
[background]
chain = ["blur", "vignette"]
[background.blur]
radius = 30
passes = 2
[background.vignette]
strength = 0.4
[foreground]
chain = []
"#,
);
s.baseline_config = s.active_config.clone();
s
}
#[test]
fn config_field_reads_daemon_get_config_shape() {
let s = fixture();
assert!(s.active_config["background"].get("per_effect").is_none());
assert_eq!(s.config_field(&path("background.blur.passes")), 2);
assert_eq!(s.config_field(&path("background.vignette.strength")), 0.4);
}
#[test]
fn config_field_returns_present_value() {
let s = fixture();
let v = s.config_field(&path("background.blur.radius"));
assert_eq!(v.as_i64(), Some(30));
}
#[test]
fn config_field_returns_null_for_missing_section() {
let s = fixture();
assert!(s.config_field(&path("post.auto_frame.threshold")).is_null());
}
#[test]
fn config_field_returns_null_for_missing_effect() {
let s = fixture();
assert!(s.config_field(&path("background.unknown.field")).is_null());
}
#[test]
fn config_field_returns_null_for_missing_field() {
let s = fixture();
assert!(
s.config_field(&path("background.blur.nonexistent"))
.is_null()
);
}
#[test]
fn chain_for_returns_names_in_order() {
let s = fixture();
assert_eq!(
s.chain_for(SubchainKind::Background),
vec!["blur".to_string(), "vignette".to_string()]
);
}
#[test]
fn chain_for_empty_section_returns_empty_vec() {
let s = fixture();
assert!(s.chain_for(SubchainKind::Foreground).is_empty());
}
#[test]
fn chain_for_missing_section_returns_empty_vec() {
let s = fixture();
assert!(s.chain_for(SubchainKind::Post).is_empty());
}
#[test]
fn chain_for_handles_non_array_chain_gracefully() {
let mut s = AppState::new(PathBuf::from("/tmp/test.sock"));
s.active_config = serde_json::json!({
"mask": { "chain": "not-an-array" }
});
assert!(s.chain_for(SubchainKind::Mask).is_empty());
}
#[test]
fn set_config_field_updates_existing_value_and_marks_dirty() {
let mut s = fixture();
s.set_config_field(&path("background.blur.radius"), serde_json::json!(12));
assert_eq!(s.config_field(&path("background.blur.radius")), 12);
assert!(s.is_dirty());
}
#[test]
fn set_config_field_creates_missing_and_replaces_non_object_nodes() {
let mut s = AppState::new(PathBuf::from("/tmp/test.sock"));
s.active_config = serde_json::json!({ "post": "garbage" });
s.set_config_field(&path("post.mirror.flip"), Value::Bool(true));
assert_eq!(
s.active_config,
serde_json::json!({ "post": { "mirror": { "flip": true } } })
);
}
#[test]
fn keep_baseline_snapshot_preserves_unsaved_edits() {
let mut s = fixture();
s.set_config_field(&path("background.blur.radius"), serde_json::json!(12));
let daemon_view = s.active_config.clone();
s.apply_config_snapshot(daemon_view, ConfigSync::KeepBaseline);
assert!(s.is_dirty(), "unsaved edit must stay dirty after refetch");
}
#[test]
fn resync_baseline_snapshot_clears_dirty() {
let mut s = fixture();
s.set_config_field(&path("background.blur.radius"), serde_json::json!(12));
s.apply_config_snapshot(
serde_json::json!({ "mask": {} }),
ConfigSync::ResyncBaseline,
);
assert!(!s.is_dirty());
assert_eq!(s.active_config, serde_json::json!({ "mask": {} }));
}
#[test]
fn preset_switch_to_active_preset_is_a_no_op_even_when_dirty() {
let mut s = fixture();
s.active_preset = Some("a".into());
s.set_config_field(&path("background.blur.radius"), serde_json::json!(12));
assert_eq!(s.preset_switch("a"), PresetSwitch::AlreadyActive);
assert_eq!(s.preset_switch("b"), PresetSwitch::NeedsConfirmation);
s.mark_saved();
assert_eq!(s.preset_switch("b"), PresetSwitch::Proceed);
}
fn chain(names: &[&str]) -> Vec<String> {
names.iter().map(|n| (*n).to_string()).collect()
}
#[test]
fn chain_edit_applies_in_range_edits() {
let mut c = chain(&["a", "b", "c"]);
assert!(ChainEdit::MoveUp(2).apply(&mut c));
assert_eq!(c, chain(&["a", "c", "b"]));
assert!(ChainEdit::MoveDown(0).apply(&mut c));
assert_eq!(c, chain(&["c", "a", "b"]));
assert!(ChainEdit::Remove(1).apply(&mut c));
assert_eq!(c, chain(&["c", "b"]));
assert!(ChainEdit::Append("d".into()).apply(&mut c));
assert_eq!(c, chain(&["c", "b", "d"]));
}
#[test]
fn chain_edit_rejects_boundary_and_out_of_range_edits() {
let mut c = chain(&["a", "b"]);
assert!(!ChainEdit::MoveUp(0).apply(&mut c));
assert!(!ChainEdit::MoveUp(5).apply(&mut c));
assert!(!ChainEdit::MoveDown(1).apply(&mut c));
assert!(!ChainEdit::Remove(2).apply(&mut c));
assert_eq!(c, chain(&["a", "b"]));
}
#[test]
fn effect_enabled_defaults_to_true_and_reads_the_flag() {
let mut s = fixture();
s.active_config = daemon_config(
r#"
[background]
chain = ["blur", "vignette"]
[background.blur]
enabled = false
radius = 30
"#,
);
assert!(!s.effect_enabled(SubchainKind::Background, "blur"));
assert!(s.effect_enabled(SubchainKind::Background, "vignette"));
assert!(s.effect_enabled(SubchainKind::Post, "mirror"));
}
#[test]
fn set_effect_enabled_matches_the_daemon_canonical_form() {
let mut s = fixture();
let bg = SubchainKind::Background;
let original = s.active_config.clone();
s.set_effect_enabled(bg, "color_fill", false);
assert_eq!(
s.active_config["background"],
daemon_config(
r#"
[background]
chain = ["blur", "vignette"]
[background.blur]
radius = 30
passes = 2
[background.vignette]
strength = 0.4
[background.color_fill]
enabled = false
"#
)["background"]
);
assert!(s.is_dirty());
s.set_effect_enabled(bg, "blur", false);
assert!(!s.effect_enabled(bg, "blur"));
s.set_effect_enabled(bg, "blur", true);
assert_eq!(s.config_field(&path("background.blur.radius")), 30);
assert!(
s.active_config["background"]["blur"]
.get("enabled")
.is_none()
);
s.set_effect_enabled(bg, "color_fill", true);
assert_eq!(s.active_config, original);
assert!(!s.is_dirty());
}
#[test]
fn first_handshake_is_a_clean_baseline() {
let mut s = AppState::new(PathBuf::from("/tmp/test.sock"));
let edits_lost = s.apply_handshake("a".into(), serde_json::json!({"mask": {}}), None);
assert!(!edits_lost);
assert!(!s.is_dirty());
assert_eq!(s.active_preset.as_deref(), Some("a"));
}
#[test]
fn reconnect_keeps_edits_the_daemon_still_holds() {
let mut s = fixture();
s.active_preset = Some("a".into());
s.set_config_field(&path("background.blur.radius"), serde_json::json!(12));
let lost = s.lost_session();
let daemon_view = s.active_config.clone();
let edits_lost = s.apply_handshake("a".into(), daemon_view, Some(&lost));
assert!(!edits_lost);
assert!(s.is_dirty(), "edits the daemon kept stay unsaved");
}
#[test]
fn reconnect_reports_edits_lost_to_a_daemon_restart() {
let mut s = fixture();
s.active_preset = Some("a".into());
let saved = s.baseline_config.clone();
s.set_config_field(&path("background.blur.radius"), serde_json::json!(12));
let lost = s.lost_session();
let edits_lost = s.apply_handshake("a".into(), saved.clone(), Some(&lost));
assert!(edits_lost);
assert!(!s.is_dirty());
assert_eq!(s.active_config, saved);
}
#[test]
fn reconnect_without_unsaved_edits_reports_nothing() {
let mut s = fixture();
s.active_preset = Some("a".into());
let lost = s.lost_session();
let edits_lost =
s.apply_handshake("b".into(), serde_json::json!({"mask": {}}), Some(&lost));
assert!(!edits_lost);
assert!(!s.is_dirty());
assert_eq!(s.active_preset.as_deref(), Some("b"));
}
#[test]
fn mark_saved_clears_dirty() {
let mut s = fixture();
s.set_config_field(&path("background.blur.radius"), serde_json::json!(12));
s.mark_saved();
assert!(!s.is_dirty());
}
}