save and restore window size and position

This commit is contained in:
Max Bradbury 2021-04-09 18:46:23 +01:00
parent 3e01249ecf
commit 4fd83be754
2 changed files with 74 additions and 3 deletions

View File

@ -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 <max@tinybird.info>"]
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"

View File

@ -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<Config> {
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: &gtk::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: &gtk::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() {