From c77a7a93cce6769a85e1c395d2f813e3f1f04665 Mon Sep 17 00:00:00 2001 From: LoipesMas <46327403+LoipesMas@users.noreply.github.com> Date: Sun, 19 Dec 2021 19:30:16 +0100 Subject: [PATCH] Add remember_window_position setting --- Cargo.lock | 1 + Cargo.toml | 2 +- src/settings/mod.rs | 5 ++++- src/settings/window_geometry.rs | 34 +++++++++++++++++++++++++++++++++ src/window/mod.rs | 14 ++++++++++++-- src/window/settings.rs | 2 ++ 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37ebb2d..19b4d74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2589,6 +2589,7 @@ dependencies = [ "percent-encoding", "raw-window-handle", "scopeguard", + "serde", "smithay-client-toolkit", "unicode-segmentation", "wayland-client", diff --git a/Cargo.toml b/Cargo.toml index 7b7af35..d7c0ebd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ dirs = "2" rand = "0.7" pin-project = "0.4.27" futures = "0.3.12" -glutin = { git = "https://github.com/neovide/glutin", branch = "new-keyboard-all" } +glutin = { git = "https://github.com/neovide/glutin", branch = "new-keyboard-all", features=["serde"] } winit = { git = "https://github.com/neovide/winit", branch = "new-keyboard-all" } gl = "0.14.0" swash = "0.1.4" diff --git a/src/settings/mod.rs b/src/settings/mod.rs index 7d0ac68..94ec412 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -9,7 +9,10 @@ use log::trace; use nvim_rs::Neovim; use parking_lot::RwLock; pub use rmpv::Value; -pub use window_geometry::{maybe_save_window_size, parse_window_geometry, DEFAULT_WINDOW_GEOMETRY}; +pub use window_geometry::{ + load_last_window_position, maybe_save_window_position, maybe_save_window_size, + parse_window_geometry, DEFAULT_WINDOW_GEOMETRY, +}; use crate::bridge::TxWrapper; use crate::error_handling::ResultPanicExplanation; diff --git a/src/settings/window_geometry.rs b/src/settings/window_geometry.rs index 6cddf67..7b51cdf 100644 --- a/src/settings/window_geometry.rs +++ b/src/settings/window_geometry.rs @@ -1,6 +1,7 @@ use crate::settings::SETTINGS; use crate::utils::Dimensions; use crate::window::WindowSettings; +use glutin::dpi::PhysicalPosition; use std::path::PathBuf; #[cfg(unix)] @@ -34,6 +35,25 @@ pub fn try_to_load_last_window_size() -> Result { } } +pub fn load_last_window_position() -> PhysicalPosition { + let settings_path = neovim_std_datapath(); + let json = std::fs::read_to_string(&settings_path).map_err(|e| e.to_string()); + if json.is_err() { + return PhysicalPosition::default(); + } + let json = json.unwrap(); + + let loaded_position: Result, _> = + serde_json::from_str(&json).map_err(|e| e.to_string()); + if loaded_position.is_err() { + return PhysicalPosition::default(); + } + let loaded_position = loaded_position.unwrap(); + log::debug!("Loaded Window Position: {:?}", loaded_position); + + loaded_position +} + pub fn maybe_save_window_size(grid_size: Option) { let settings = SETTINGS.get::(); let saved_window_size = if settings.remember_window_size { @@ -48,6 +68,20 @@ pub fn maybe_save_window_size(grid_size: Option) { std::fs::write(settings_path, json).unwrap(); } +pub fn maybe_save_window_position(position: Option>) { + let settings = SETTINGS.get::(); + let saved_window_position = if settings.remember_window_position { + position.unwrap_or_default() + } else { + PhysicalPosition::default() + }; + + let settings_path = neovim_std_datapath(); + let json = serde_json::to_string(&saved_window_position).unwrap(); + log::debug!("Saved Window Position: {}", json); + std::fs::write(settings_path, json).unwrap(); +} + pub fn parse_window_geometry(geometry: Option) -> Result { let saved_window_size = try_to_load_last_window_size().or::(Ok(DEFAULT_WINDOW_GEOMETRY)); diff --git a/src/window/mod.rs b/src/window/mod.rs index 08169ab..bacbccd 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -30,7 +30,9 @@ use crate::{ redraw_scheduler::REDRAW_SCHEDULER, renderer::Renderer, running_tracker::*, - settings::{maybe_save_window_size, SETTINGS}, + settings::{ + load_last_window_position, maybe_save_window_position, maybe_save_window_size, SETTINGS, + }, utils::Dimensions, }; use image::{load_from_memory, GenericImageView, Pixel}; @@ -270,7 +272,8 @@ pub fn create_window( .with_window_icon(Some(icon)) .with_maximized(cmd_line_settings.maximized) .with_transparent(true) - .with_decorations(!cmd_line_settings.frameless); + .with_decorations(!cmd_line_settings.frameless) + .with_position(load_last_window_position()); #[cfg(target_os = "linux")] let winit_window_builder = winit_window_builder @@ -323,6 +326,13 @@ pub fn create_window( event_loop.run(move |e, _window_target, control_flow| { if !RUNNING_TRACKER.is_running() { maybe_save_window_size(window_wrapper.saved_grid_size); + maybe_save_window_position( + window_wrapper + .windowed_context + .window() + .outer_position() + .ok(), + ); std::process::exit(0); } diff --git a/src/window/settings.rs b/src/window/settings.rs index cb799fa..ba0eaab 100644 --- a/src/window/settings.rs +++ b/src/window/settings.rs @@ -8,6 +8,7 @@ pub struct WindowSettings { pub fullscreen: bool, pub iso_layout: bool, pub remember_window_size: bool, + pub remember_window_position: bool, pub hide_mouse_when_typing: bool, } @@ -20,6 +21,7 @@ impl Default for WindowSettings { refresh_rate: 60, no_idle: SETTINGS.get::().no_idle, remember_window_size: false, + remember_window_position: false, hide_mouse_when_typing: false, } }