1
0
mirror of https://github.com/4yn/slidershim.git synced 2024-11-27 23:10:49 +01:00

persistency

This commit is contained in:
4yn 2022-01-29 04:23:16 +08:00
parent 05ff89b923
commit a44220fed1
7 changed files with 139 additions and 26 deletions

21
src-tauri/Cargo.lock generated
View File

@ -682,6 +682,15 @@ dependencies = [
"subtle",
]
[[package]]
name = "directories"
version = "4.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs"
version = "1.0.5"
@ -703,6 +712,17 @@ dependencies = [
"dirs-sys-next",
]
[[package]]
name = "dirs-sys"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
dependencies = [
"libc",
"redox_users 0.4.0",
"winapi",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
@ -2743,6 +2763,7 @@ checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5"
name = "slidershim"
version = "0.1.0"
dependencies = [
"directories",
"palette",
"rusb",
"serde",

View File

@ -21,6 +21,7 @@ tauri = { version = "1.0.0-beta.8", features = ["api-all", "system-tray"] }
rusb = "0.9.0"
palette = "0.6.0"
winapi = "0.3.9"
directories = "4.0.1"
[features]
default = [ "custom-protocol" ]

View File

@ -2,7 +2,7 @@ extern crate slidershim;
use std::io;
use slidershim::slider_io::{config::Config, manager::Manager};
use slidershim::slider_io::{Config, Manager};
fn main() {
let config = Config::from_str(
@ -12,7 +12,8 @@ fn main() {
"ledMode": "reactive-8",
"keyboardSensitivity": 50
}"#,
);
)
.unwrap();
let manager = Manager::new(config);

View File

@ -7,6 +7,8 @@
mod slider_io;
use std::sync::{Arc, Mutex};
use tauri::{
AppHandle, CustomMenuItem, Event, Manager, Runtime, SystemTray, SystemTrayEvent, SystemTrayMenu,
};
@ -24,6 +26,12 @@ fn quit_app() {
}
fn main() {
let config = Arc::new(Mutex::new(Some(slider_io::Config::default())));
{
println!("Saving");
config.lock().unwrap().as_ref().unwrap().save();
}
tauri::Builder::default()
.system_tray(
SystemTray::new().with_menu(
@ -54,16 +62,34 @@ fn main() {
},
_ => {}
})
.setup(|app| {
app.listen_global("setConfig", |event| {
let payload = event.payload().unwrap();
println!("Setting config to {}", payload);
.setup(move |app| {
let app_handle = app.handle();
let config_clone = Arc::clone(&config);
app.listen_global("heartbeat", move |e| {
let config_handle = config_clone.lock().unwrap();
println!("Heartbeat {}", config_handle.as_ref().unwrap().raw.as_str());
app_handle
.emit_all(
"showConfig",
Some(config_handle.as_ref().unwrap().raw.as_str().to_string()),
)
.unwrap();
});
let handle = app.handle();
let config_clone = Arc::clone(&config);
app.listen_global("setConfig", move |event| {
let payload = event.payload().unwrap();
println!("Setting config to {}", payload);
if let Some(new_config) = slider_io::Config::from_str(payload) {
let mut config_handle = config_clone.lock().unwrap();
config_handle.replace(new_config);
config_handle.as_ref().unwrap().save();
}
});
let app_handle = app.handle();
app.listen_global("hide", move |_| {
hide_window(&handle);
hide_window(&app_handle);
});
app.listen_global("quit", |_| {

View File

@ -1,5 +1,7 @@
use directories::ProjectDirs;
use std::{convert::TryFrom, fs, path::PathBuf};
use serde_json::Value;
use std::convert::TryFrom;
#[derive(Debug, Clone)]
pub enum DeviceMode {
@ -59,12 +61,12 @@ pub struct Config {
}
impl Config {
pub fn from_str(s: &str) -> Config {
let v: Value = serde_json::from_str(s).unwrap();
pub fn from_str(s: &str) -> Option<Config> {
let v: Value = serde_json::from_str(s).ok()?;
Config {
Some(Config {
raw: s.to_string(),
device_mode: match v["deviceMode"].as_str().unwrap() {
device_mode: match v["deviceMode"].as_str()? {
"none" => DeviceMode::None,
"tasoller-one" => DeviceMode::TasollerOne,
"tasoller-two" => DeviceMode::TasollerTwo,
@ -77,34 +79,34 @@ impl Config {
"none" => OutputMode::None,
"kb-32-tasoller" => OutputMode::Keyboard {
layout: KeyboardLayout::Tasoller,
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64().unwrap()).unwrap(),
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64()?).ok()?,
},
"kb-32-yuancon" => OutputMode::Keyboard {
layout: KeyboardLayout::Yuancon,
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64().unwrap()).unwrap(),
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64()?).ok()?,
},
"kb-6-deemo" => OutputMode::Keyboard {
layout: KeyboardLayout::Deemo,
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64().unwrap()).unwrap(),
sensitivity: u8::try_from(v["keyboardSensitivity"].as_i64()?).ok()?,
},
"websocket" => OutputMode::Websocket {
url: v["outputWebsocketUrl"].to_string(),
},
_ => panic!("Invalid output mode"),
},
led_mode: match v["ledMode"].as_str().unwrap() {
led_mode: match v["ledMode"].as_str()? {
"none" => LedMode::None,
"reactive-4" => LedMode::Reactive {
layout: ReactiveLayout::Four,
sensitivity: u8::try_from(v["ledSensitivity"].as_i64().unwrap()).unwrap(),
sensitivity: u8::try_from(v["ledSensitivity"].as_i64()?).ok()?,
},
"reactive-8" => LedMode::Reactive {
layout: ReactiveLayout::Eight,
sensitivity: u8::try_from(v["ledSensitivity"].as_i64().unwrap()).unwrap(),
sensitivity: u8::try_from(v["ledSensitivity"].as_i64()?).ok()?,
},
"reactive-16" => LedMode::Reactive {
layout: ReactiveLayout::Sixteen,
sensitivity: u8::try_from(v["ledSensitivity"].as_i64().unwrap()).unwrap(),
sensitivity: u8::try_from(v["ledSensitivity"].as_i64()?).ok()?,
},
"attract" => LedMode::Attract,
"test" => LedMode::Test,
@ -113,6 +115,56 @@ impl Config {
},
_ => panic!("Invalid led mode"),
},
})
}
fn factory() -> Self {
Self::from_str(
r#"{
"deviceMode": "none",
"outputMode": "none",
"ledMode": "none",
"keyboardSensitivity": 20,
"outputWebsocketUrl": "localhost:3000",
"ledSensitivity": 20,
"ledWebsocketUrl": "localhost:3001"
}"#,
)
.unwrap()
}
fn get_saved_path() -> Option<Box<PathBuf>> {
let project_dir = ProjectDirs::from("me", "imp.ress", "slidershim").unwrap();
let config_dir = project_dir.config_dir();
fs::create_dir_all(config_dir);
let config_path = config_dir.join("config.json");
return Some(Box::new(config_path));
}
fn load_saved() -> Option<Self> {
let config_path = Self::get_saved_path()?;
if !config_path.exists() {
return None;
}
println!("Found saved");
let mut saved_data = fs::read_to_string(config_path.as_path()).ok()?;
println!("Loaded saved {}", saved_data);
return Self::from_str(saved_data.as_str());
}
pub fn default() -> Self {
Self::load_saved()
.or_else(|| Some(Self::factory()))
.unwrap()
}
pub fn save(&self) -> Option<()> {
let config_path = Self::get_saved_path()?;
println!("Saving to {:?}", config_path);
fs::write(config_path.as_path(), self.raw.as_str()).unwrap();
Some(())
}
}

View File

@ -1,4 +1,4 @@
pub mod config;
mod config;
mod controller_state;
mod worker;
@ -9,4 +9,7 @@ mod device;
mod led;
mod output;
pub mod manager;
mod manager;
pub use config::Config;
pub use manager::Manager;

View File

@ -13,9 +13,14 @@
let ledSensitivity = 20;
let ledWebsocketUrl = "http://localhost:3001";
let debugstr = "";
onMount(async () => {
// console.log(emit, listen);
await listen("showConfig", (event) => {
const payload: any = event.payload;
console.log("heartbeat", event);
debugstr = event.payload;
const payload: any = JSON.parse(event.payload as any);
deviceMode = payload.deviceMode;
outputMode = payload.outputMode;
ledMode = payload.ledMode;
@ -24,6 +29,7 @@
ledSensitivity = payload.ledSensitivity;
ledWebsocketUrl = payload.ledWebsocketUrl;
});
await emit("heartbeat", "");
});
async function setConfig() {
@ -59,6 +65,9 @@
slidershim
</div>
</div>
<!-- <div>
{debugstr}
</div> -->
<div class="row">
<Preview />
</div>
@ -145,7 +154,7 @@
min="1"
max="255"
step="1"
bind:value={keyboardSensitivity}
bind:value={ledSensitivity}
/>
</div>
</div>
@ -157,7 +166,7 @@
min="1"
max="255"
step="1"
bind:value={keyboardSensitivity}
bind:value={ledSensitivity}
/>
</div>
</div>