save and restore window size and position
This commit is contained in:
parent
3e01249ecf
commit
4fd83be754
|
@ -1,12 +1,11 @@
|
||||||
[package]
|
[package]
|
||||||
name = "lull"
|
name = "lull"
|
||||||
description = "a looping sound player for generating atmospheric soundscapes"
|
description = "a looping sound player for generating atmospheric soundscapes"
|
||||||
version = "1.0.2"
|
version = "1.0.3"
|
||||||
authors = ["Max Bradbury <max@tinybird.info>"]
|
authors = ["Max Bradbury <max@tinybird.info>"]
|
||||||
repository = "https://tinybird.dev/max/lull"
|
repository = "https://tinybird.dev/max/lull"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
crate_type = "bin"
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
dirs = "^3.0.1"
|
dirs = "^3.0.1"
|
||||||
|
@ -14,3 +13,6 @@ gdk = "^0.13.2"
|
||||||
gio = "^0"
|
gio = "^0"
|
||||||
gtk = "^0"
|
gtk = "^0"
|
||||||
rodio = "^0.11.0"
|
rodio = "^0.11.0"
|
||||||
|
serde = "^1.0.125"
|
||||||
|
serde_derive = "^1.0.125"
|
||||||
|
toml = "^0.5.8"
|
||||||
|
|
71
src/main.rs
71
src/main.rs
|
@ -4,14 +4,69 @@ use gio::prelude::*;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::Orientation;
|
use gtk::Orientation;
|
||||||
use rodio::{Sink, Source};
|
use rodio::{Sink, Source};
|
||||||
|
use serde_derive::{Serialize, Deserialize};
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::{BufReader, Write, Read};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
const SPACING: i32 = 16;
|
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")]
|
#[cfg(target_os = "windows")]
|
||||||
fn file_manager() -> &'static str {
|
fn file_manager() -> &'static str {
|
||||||
"explorer"
|
"explorer"
|
||||||
|
@ -57,6 +112,11 @@ fn build_ui(application: >k::Application) {
|
||||||
window.set_position(gtk::WindowPosition::Center);
|
window.set_position(gtk::WindowPosition::Center);
|
||||||
window.set_default_size(256, 128);
|
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");
|
let button_manage_sounds = gtk::Button::with_label("manage sounds");
|
||||||
|
|
||||||
button_manage_sounds.connect_clicked(|_| {
|
button_manage_sounds.connect_clicked(|_| {
|
||||||
|
@ -153,6 +213,15 @@ fn build_ui(application: >k::Application) {
|
||||||
}
|
}
|
||||||
|
|
||||||
window.show_all();
|
window.show_all();
|
||||||
|
|
||||||
|
window.connect_delete_event(|window, _event| {
|
||||||
|
save_config(Config {
|
||||||
|
position: window.get_position(),
|
||||||
|
size: window.get_size()
|
||||||
|
});
|
||||||
|
|
||||||
|
Inhibit(false)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in New Issue