|
|
@ -12,12 +12,15 @@ pub const DEFAULT_WINDOW_GEOMETRY: Dimensions = Dimensions {
|
|
|
|
height: 50,
|
|
|
|
height: 50,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct PersistentWindowSettings {
|
|
|
|
pub enum PersistentWindowSettings {
|
|
|
|
#[serde(default)]
|
|
|
|
Maximized,
|
|
|
|
position: PhysicalPosition<i32>,
|
|
|
|
Windowed {
|
|
|
|
#[serde(default = "default_size")]
|
|
|
|
#[serde(default)]
|
|
|
|
size: Dimensions,
|
|
|
|
position: PhysicalPosition<i32>,
|
|
|
|
|
|
|
|
#[serde(default = "default_size")]
|
|
|
|
|
|
|
|
size: Dimensions,
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn default_size() -> Dimensions {
|
|
|
|
fn default_size() -> Dimensions {
|
|
|
@ -54,51 +57,47 @@ fn load_settings() -> Result<PersistentSettings, String> {
|
|
|
|
serde_json::from_str(&json).map_err(|e| e.to_string())
|
|
|
|
serde_json::from_str(&json).map_err(|e| e.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn try_to_load_last_window_size() -> Result<Dimensions, String> {
|
|
|
|
pub fn load_last_window_settings() -> Result<PersistentWindowSettings, String> {
|
|
|
|
let settings = load_settings()?;
|
|
|
|
let settings = load_settings()?;
|
|
|
|
let loaded_geometry = settings.window.size;
|
|
|
|
let mut loaded_settings = settings.window;
|
|
|
|
log::debug!("Loaded Window Size: {:?}", loaded_geometry);
|
|
|
|
log::debug!("Loaded window settings: {:?}", loaded_settings);
|
|
|
|
|
|
|
|
|
|
|
|
if loaded_geometry.width == 0 || loaded_geometry.height == 0 {
|
|
|
|
|
|
|
|
log::warn!("Invalid Saved Window Size. Reverting to default");
|
|
|
|
|
|
|
|
Ok(DEFAULT_WINDOW_GEOMETRY)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Ok(loaded_geometry)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn load_last_window_position() -> PhysicalPosition<i32> {
|
|
|
|
if let PersistentWindowSettings::Windowed { size, .. } = &mut loaded_settings {
|
|
|
|
let settings = load_settings();
|
|
|
|
if size.width == 0 || size.height == 0 {
|
|
|
|
if let Ok(settings) = settings {
|
|
|
|
*size = DEFAULT_WINDOW_GEOMETRY;
|
|
|
|
let loaded_position = settings.window.position;
|
|
|
|
}
|
|
|
|
log::debug!("Loaded Window Position: {:?}", loaded_position);
|
|
|
|
|
|
|
|
loaded_position
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
PhysicalPosition::default()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(loaded_settings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn save_window_geometry(
|
|
|
|
pub fn save_window_geometry(
|
|
|
|
|
|
|
|
maximized: bool,
|
|
|
|
grid_size: Option<Dimensions>,
|
|
|
|
grid_size: Option<Dimensions>,
|
|
|
|
position: Option<PhysicalPosition<i32>>,
|
|
|
|
position: Option<PhysicalPosition<i32>>,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
let window_settings = SETTINGS.get::<WindowSettings>();
|
|
|
|
let window_settings = SETTINGS.get::<WindowSettings>();
|
|
|
|
|
|
|
|
|
|
|
|
let settings = PersistentSettings {
|
|
|
|
let settings = PersistentSettings {
|
|
|
|
window: PersistentWindowSettings {
|
|
|
|
window: if maximized && window_settings.remember_window_size {
|
|
|
|
size: {
|
|
|
|
PersistentWindowSettings::Maximized
|
|
|
|
window_settings
|
|
|
|
} else {
|
|
|
|
.remember_window_size
|
|
|
|
PersistentWindowSettings::Windowed {
|
|
|
|
.then(|| grid_size)
|
|
|
|
size: {
|
|
|
|
.flatten()
|
|
|
|
window_settings
|
|
|
|
.unwrap_or(DEFAULT_WINDOW_GEOMETRY)
|
|
|
|
.remember_window_size
|
|
|
|
},
|
|
|
|
.then(|| grid_size)
|
|
|
|
position: {
|
|
|
|
.flatten()
|
|
|
|
window_settings
|
|
|
|
.unwrap_or(DEFAULT_WINDOW_GEOMETRY)
|
|
|
|
.remember_window_position
|
|
|
|
},
|
|
|
|
.then(|| position)
|
|
|
|
position: {
|
|
|
|
.flatten()
|
|
|
|
window_settings
|
|
|
|
.unwrap_or_default()
|
|
|
|
.remember_window_position
|
|
|
|
},
|
|
|
|
.then(|| position)
|
|
|
|
|
|
|
|
.flatten()
|
|
|
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -110,8 +109,16 @@ pub fn save_window_geometry(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_window_geometry(geometry: Option<String>) -> Result<Dimensions, String> {
|
|
|
|
pub fn parse_window_geometry(geometry: Option<String>) -> Result<Dimensions, String> {
|
|
|
|
let saved_window_size =
|
|
|
|
let saved_window_size = load_last_window_settings()
|
|
|
|
try_to_load_last_window_size().or::<String>(Ok(DEFAULT_WINDOW_GEOMETRY));
|
|
|
|
.and_then(|window_settings| {
|
|
|
|
|
|
|
|
if let PersistentWindowSettings::Windowed { size, .. } = window_settings {
|
|
|
|
|
|
|
|
Ok(size)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Err(String::from("Window was maximized"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.or::<String>(Ok(DEFAULT_WINDOW_GEOMETRY));
|
|
|
|
|
|
|
|
|
|
|
|
geometry.map_or(saved_window_size, |input| {
|
|
|
|
geometry.map_or(saved_window_size, |input| {
|
|
|
|
let invalid_parse_err = format!(
|
|
|
|
let invalid_parse_err = format!(
|
|
|
|
"Invalid geometry: {}\nValid format: <width>x<height>",
|
|
|
|
"Invalid geometry: {}\nValid format: <width>x<height>",
|
|
|
|