From 4fd83be754bb0b999f95ea06079b3ebd2ddbfc28 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Fri, 9 Apr 2021 18:46:23 +0100 Subject: [PATCH] save and restore window size and position --- Cargo.toml | 6 +++-- src/main.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0ce7955..896dc81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,11 @@ [package] name = "lull" description = "a looping sound player for generating atmospheric soundscapes" -version = "1.0.2" +version = "1.0.3" authors = ["Max Bradbury "] repository = "https://tinybird.dev/max/lull" license = "MIT" edition = "2018" -crate_type = "bin" [dependencies] dirs = "^3.0.1" @@ -14,3 +13,6 @@ gdk = "^0.13.2" gio = "^0" gtk = "^0" rodio = "^0.11.0" +serde = "^1.0.125" +serde_derive = "^1.0.125" +toml = "^0.5.8" diff --git a/src/main.rs b/src/main.rs index 99aa811..6e816a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,14 +4,69 @@ use gio::prelude::*; use gtk::prelude::*; use gtk::Orientation; use rodio::{Sink, Source}; +use serde_derive::{Serialize, Deserialize}; use std::env::args; use std::fs::File; -use std::io::BufReader; +use std::io::{BufReader, Write, Read}; use std::path::PathBuf; use std::process::Command; const SPACING: i32 = 16; +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] +struct Config { + position: (i32, i32), + size: (i32, i32), +} + +fn save_config(config: Config) { + let mut config_path = dirs::config_dir() + .expect("Couldn't find user config directory"); + + config_path.push("ruin/lull"); + + if !config_path.exists() { + std::fs::create_dir_all(&config_path) + .expect("Couldn't create lull config directory"); + } + + let toml = toml::to_string(&config) + .expect("Couldn't convert config to toml"); + + config_path.push("lull.toml"); + + let mut config_file = File::create(config_path) + .expect("Couldn't create config file"); + + print!("{}", toml); + + config_file.write(&toml.into_bytes()) + .expect("Couldn't write config file"); +} + +fn load_config() -> Option { + let mut config_path = dirs::config_dir() + .expect("Couldn't find user config directory"); + + config_path.push("ruin/lull/lull.toml"); + + let file = File::open(config_path); + + if file.is_err() { + return None; + } + + let mut toml = String::new(); + + file.unwrap().read_to_string(&mut toml) + .expect("Couldn't read config file"); + + let config = toml::from_str(&toml) + .expect("Couldn't parse config"); + + Some(config) +} + #[cfg(target_os = "windows")] fn file_manager() -> &'static str { "explorer" @@ -57,6 +112,11 @@ fn build_ui(application: >k::Application) { window.set_position(gtk::WindowPosition::Center); window.set_default_size(256, 128); + if let Some(config) = load_config() { + window.move_(config.position.0, config.position.1); + window.resize(config.size.0, config.size.1); + } + let button_manage_sounds = gtk::Button::with_label("manage sounds"); button_manage_sounds.connect_clicked(|_| { @@ -153,6 +213,15 @@ fn build_ui(application: >k::Application) { } window.show_all(); + + window.connect_delete_event(|window, _event| { + save_config(Config { + position: window.get_position(), + size: window.get_size() + }); + + Inhibit(false) + }); } fn main() {