feat: save active layout

This commit is contained in:
galister
2024-06-04 20:43:59 +09:00
parent 698c447e34
commit 1eebde3944
5 changed files with 37 additions and 22 deletions

View File

@@ -73,7 +73,7 @@ where
let mut show_screens = app.session.config.show_screens.clone(); let mut show_screens = app.session.config.show_screens.clone();
if show_screens.is_empty() { if show_screens.is_empty() {
if let Some((_, s, _)) = data.screens.first() { if let Some((_, s, _)) = data.screens.first() {
show_screens.arc_ins(s.name.clone()); show_screens.arc_set(s.name.clone());
} }
} }

View File

@@ -6,7 +6,7 @@ use glam::{Affine3A, Vec2, Vec3, Vec3A, Vec3Swizzles};
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use crate::backend::common::{snap_upright, OverlaySelector}; use crate::backend::common::{snap_upright, OverlaySelector};
use crate::config::{save_state, AStrMapExt, GeneralConfig}; use crate::config::{AStrMapExt, GeneralConfig};
use crate::overlays::anchor::ANCHOR_NAME; use crate::overlays::anchor::ANCHOR_NAME;
use crate::state::AppState; use crate::state::AppState;
@@ -538,21 +538,17 @@ impl Pointer {
if let Some(grab_data) = self.interaction.grabbed.as_ref() { if let Some(grab_data) = self.interaction.grabbed.as_ref() {
if overlay.state.curvature != grab_data.old_curvature { if overlay.state.curvature != grab_data.old_curvature {
if let Some(val) = overlay.state.curvature { if let Some(val) = overlay.state.curvature {
config.curve_values.arc_ins(overlay.state.name.clone(), val); config.curve_values.arc_set(overlay.state.name.clone(), val);
} else { } else {
let ref_name = overlay.state.name.as_ref(); let ref_name = overlay.state.name.as_ref();
config.curve_values.arc_rm(ref_name); config.curve_values.arc_rm(ref_name);
} }
} }
config.transform_values.arc_ins( }
config.transform_values.arc_set(
overlay.state.name.clone(), overlay.state.name.clone(),
overlay.state.saved_transform.unwrap(), overlay.state.saved_transform.unwrap(),
); );
match save_state(config) {
Ok(_) => log::debug!("Saved state"),
Err(e) => log::error!("Failed to save state: {:?}", e),
}
}
} }
self.interaction.grabbed = None; self.interaction.grabbed = None;

View File

@@ -23,16 +23,15 @@ use serde::Serialize;
pub type AStrMap<V> = Vec<(Arc<str>, V)>; pub type AStrMap<V> = Vec<(Arc<str>, V)>;
pub trait AStrMapExt<V> { pub trait AStrMapExt<V> {
fn arc_ins(&mut self, key: Arc<str>, value: V) -> bool; fn arc_set(&mut self, key: Arc<str>, value: V) -> bool;
fn arc_get(&self, key: &str) -> Option<&V>; fn arc_get(&self, key: &str) -> Option<&V>;
fn arc_rm(&mut self, key: &str) -> Option<V>; fn arc_rm(&mut self, key: &str) -> Option<V>;
} }
impl<V> AStrMapExt<V> for AStrMap<V> { impl<V> AStrMapExt<V> for AStrMap<V> {
fn arc_ins(&mut self, key: Arc<str>, value: V) -> bool { fn arc_set(&mut self, key: Arc<str>, value: V) -> bool {
if self.iter().any(|(k, _)| k.as_ref().eq(key.as_ref())) { let index = self.iter().position(|(k, _)| k.as_ref().eq(key.as_ref()));
return false; index.map(|i| self.remove(i).1);
}
self.push((key, value)); self.push((key, value));
true true
} }
@@ -51,13 +50,13 @@ impl<V> AStrMapExt<V> for AStrMap<V> {
pub type AStrSet = Vec<Arc<str>>; pub type AStrSet = Vec<Arc<str>>;
pub trait AStrSetExt { pub trait AStrSetExt {
fn arc_ins(&mut self, value: Arc<str>) -> bool; fn arc_set(&mut self, value: Arc<str>) -> bool;
fn arc_get(&self, value: &str) -> bool; fn arc_get(&self, value: &str) -> bool;
fn arc_rm(&mut self, value: &str) -> bool; fn arc_rm(&mut self, value: &str) -> bool;
} }
impl AStrSetExt for AStrSet { impl AStrSetExt for AStrSet {
fn arc_ins(&mut self, value: Arc<str>) -> bool { fn arc_set(&mut self, value: Arc<str>) -> bool {
if self.iter().any(|v| v.as_ref().eq(value.as_ref())) { if self.iter().any(|v| v.as_ref().eq(value.as_ref())) {
return false; return false;
} }
@@ -416,7 +415,7 @@ fn get_state_path() -> PathBuf {
path path
} }
pub fn save_state(config: &GeneralConfig) -> anyhow::Result<()> { pub fn save_layout(config: &GeneralConfig) -> anyhow::Result<()> {
let conf = AutoState { let conf = AutoState {
show_screens: config.show_screens.clone(), show_screens: config.show_screens.clone(),
curve_values: config.curve_values.clone(), curve_values: config.curve_values.clone(),

View File

@@ -16,7 +16,7 @@ use crate::{
overlay::RelativeTo, overlay::RelativeTo,
task::{ColorChannel, SystemTask, TaskType}, task::{ColorChannel, SystemTask, TaskType},
}, },
config::{save_settings, save_state, AStrSetExt}, config::{save_layout, save_settings, AStrSetExt},
overlays::{ overlays::{
toast::{Toast, ToastTopic}, toast::{Toast, ToastTopic},
watch::WATCH_NAME, watch::WATCH_NAME,
@@ -59,6 +59,7 @@ pub enum SystemAction {
PlayspaceFixFloor, PlayspaceFixFloor,
RecalculateExtent, RecalculateExtent,
PersistConfig, PersistConfig,
PersistLayout,
} }
#[derive(Deserialize, Clone)] #[derive(Deserialize, Clone)]
@@ -418,6 +419,11 @@ fn run_system(action: &SystemAction, app: &mut AppState) {
log::error!("Failed to save config: {:?}", e); log::error!("Failed to save config: {:?}", e);
} }
} }
SystemAction::PersistLayout => {
if let Err(e) = save_layout(&app.session.config) {
log::error!("Failed to save layout: {:?}", e);
}
}
} }
} }
@@ -594,11 +600,11 @@ fn run_overlay(overlay: &OverlaySelector, action: &OverlayAction, app: &mut AppS
if !o.want_visible { if !o.want_visible {
state_dirty |= app.session.config.show_screens.arc_rm(o.name.as_ref()); state_dirty |= app.session.config.show_screens.arc_rm(o.name.as_ref());
} else if o.want_visible { } else if o.want_visible {
state_dirty |= app.session.config.show_screens.arc_ins(o.name.clone()); state_dirty |= app.session.config.show_screens.arc_set(o.name.clone());
} }
if state_dirty { if state_dirty {
match save_state(&app.session.config) { match save_layout(&app.session.config) {
Ok(_) => log::debug!("Saved state"), Ok(_) => log::debug!("Saved state"),
Err(e) => log::error!("Failed to save state: {:?}", e), Err(e) => log::error!("Failed to save state: {:?}", e),
} }

View File

@@ -591,4 +591,18 @@ elements:
click_down: click_down:
- type: System - type: System
action: PersistConfig action: PersistConfig
- type: Toast
message: Settings saved successfully.
- type: Button
rect: [30, 625, 250, 30]
font_size: 12
fg_color: "#ffffff"
bg_color: "#206060"
text: "Save Overlay Layout"
click_down:
- type: System
action: PersistLayout
- type: Toast
message: Saved. You will see this layout on next startup.