GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: add reload command which updates workspace configs + inner gaps
Lars Bergeraba1378158e7
diff --git a/packages/watcher/src/main.rs b/packages/watcher/src/main.rsindex 1aadfb24..46f3d1e1 100644--- a/packages/watcher/src/main.rs+++ b/packages/watcher/src/main.rs@@ -2,6 +2,6 @@ use wm::user_config::UserConfig;#[tokio::main]async fn main() {- let config = UserConfig::read(None).await.unwrap();+ let config = UserConfig::new(None).await.unwrap();println!("config: {:?}", config);}diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rsindex 5e2fd945..d1a8e3c8 100644--- a/packages/wm/src/app_command.rs+++ b/packages/wm/src/app_command.rs@@ -8,7 +8,8 @@ use uuid::Uuid;use crate::{common::{- commands::shell_exec, Direction, LengthValue, ResizeDimension,+ commands::{reload_config, shell_exec},+ Direction, LengthValue, ResizeDimension,},containers::{commands::{focus_in_direction, toggle_tiling_direction},@@ -480,7 +481,7 @@ impl InvokeCommand {state.add_container_to_redraw(root_container.into());Ok(())}- InvokeCommand::WmReloadConfig => todo!(),+ InvokeCommand::WmReloadConfig => reload_config(state, config),InvokeCommand::WmToggleFocusState => todo!(),}}diff --git a/packages/wm/src/common/commands/mod.rs b/packages/wm/src/common/commands/mod.rsindex eddc5626..6ef7dd15 100644--- a/packages/wm/src/common/commands/mod.rs+++ b/packages/wm/src/common/commands/mod.rs@@ -1,5 +1,7 @@+mod reload_config;mod shell_exec;mod sync_native_focus;+pub use reload_config::*;pub use shell_exec::*;pub use sync_native_focus::*;diff --git a/packages/wm/src/common/commands/reload_config.rs b/packages/wm/src/common/commands/reload_config.rsnew file mode 100644index 00000000..55461d37--- /dev/null+++ b/packages/wm/src/common/commands/reload_config.rs@@ -0,0 +1,81 @@+use anyhow::Context;+use tracing::{info, warn};++use crate::{+ containers::traits::{CommonGetters, TilingSizeGetters},+ user_config::UserConfig,+ wm_event::WmEvent,+ wm_state::WmState,+};++pub fn reload_config(+ state: &mut WmState,+ config: &mut UserConfig,+) -> anyhow::Result<()> {+ info!("Config reloaded.");++ // Re-evaluate user config file and set its values in state.+ let handle = tokio::runtime::Handle::current();+ handle.block_on(config.reload())?;++ // TODO: Run window rules on all windows.++ // Update configs of active workspaces.+ let workspaces = state.workspaces();+ for workspace in &workspaces {+ let workspace_config = config+ .value+ .workspaces+ .iter()+ .find(|config| config.name == workspace.config().name);++ match workspace_config {+ Some(workspace_config) => {+ workspace.set_config(workspace_config.clone());+ }+ // When the workspace config is not found, the current name of the+ // workspace has been removed. So, we reassign the first suitable+ // workspace config to the workspace.+ None => {+ let monitor = workspace.monitor().context("No monitor.")?;+ let inactive_config =+ config.workspace_config_for_monitor(&monitor, &workspaces);++ if let Some(inactive_config) = inactive_config {+ workspace.set_config(inactive_config.clone());+ } else {+ warn!(+ "Unable to update workspace config. No available workspace configs."+ );+ }+ }+ }+ }++ // Update inner gaps of tiling containers.+ for container in state+ .root_container+ .self_and_descendants()+ .into_iter()+ .filter_map(|c| c.as_tiling_container().ok())+ {+ container.set_inner_gap(config.value.gaps.inner_gap.clone());+ }++ // Redraw full container tree.+ let root_container = state.root_container.clone();+ state.add_container_to_redraw(root_container.into());++ // Emit the updated config.+ state.emit_event(WmEvent::UserConfigChanged {+ config_path: config+ .path+ .to_str()+ .context("Invalid config path.")?+ .to_string(),+ config_string: config.value_str.clone(),+ parsed_config: config.value.clone(),+ });++ Ok(())+}diff --git a/packages/wm/src/containers/split_container.rs b/packages/wm/src/containers/split_container.rsindex e8a44961..8fcf799f 100644--- a/packages/wm/src/containers/split_container.rs+++ b/packages/wm/src/containers/split_container.rs@@ -82,10 +82,6 @@ impl SplitContainer {Self(Rc::new(RefCell::new(split)))}- pub fn inner_gap(&self) -> LengthValue {- self.0.borrow().inner_gap.clone()- }-pub fn to_dto(&self) -> anyhow::Result<SplitContainerDto> {let children = self.children()diff --git a/packages/wm/src/containers/traits/tiling_size_getters.rs b/packages/wm/src/containers/traits/tiling_size_getters.rsindex 45efa44e..51430bff 100644--- a/packages/wm/src/containers/traits/tiling_size_getters.rs+++ b/packages/wm/src/containers/traits/tiling_size_getters.rs@@ -1,6 +1,6 @@use enum_dispatch::enum_dispatch;-use crate::containers::TilingContainer;+use crate::{common::LengthValue, containers::TilingContainer};use super::CommonGetters;@@ -9,6 +9,10 @@ pub trait TilingSizeGetters: CommonGetters {fn tiling_size(&self) -> f32;fn set_tiling_size(&self, tiling_size: f32) -> ();++ fn inner_gap(&self) -> LengthValue;++ fn set_inner_gap(&self, inner_gap: LengthValue);}/// Implements the `TilingSizeGetters` trait for a given struct.@@ -26,6 +30,14 @@ macro_rules! impl_tiling_size_getters {fn set_tiling_size(&self, tiling_size: f32) -> () {self.0.borrow_mut().tiling_size = tiling_size;}++ fn inner_gap(&self) -> LengthValue {+ self.0.borrow().inner_gap.clone()+ }++ fn set_inner_gap(&self, inner_gap: LengthValue) -> () {+ self.0.borrow_mut().inner_gap = inner_gap;+ }}};}diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rsindex 69477956..1dc53575 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -76,7 +76,7 @@ async fn start_wm(let _single_instance = Platform::new_single_instance()?;// Parse and validate user config.- let mut config = UserConfig::read(config_path).await?;+ let mut config = UserConfig::new(config_path).await?;// Start watcher process for restoring hidden windows on crash.start_watcher_process()?;diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rsindex 8202e325..6be98c49 100644--- a/packages/wm/src/user_config.rs+++ b/packages/wm/src/user_config.rs@@ -18,20 +18,42 @@ const SAMPLE_CONFIG: &str =pub struct UserConfig {pub changes_rx: mpsc::UnboundedReceiver<()>,pub changes_tx: mpsc::UnboundedSender<()>,- pub config_path: PathBuf,+ pub path: PathBuf,pub value: ParsedConfig,+ pub value_str: String,}impl UserConfig {- /// Read and validate the user config from the given path.- pub async fn read(config_path: Option<PathBuf>) -> Result<Self> {+ /// Creates an instance of `UserConfig`. Reads and validates the user+ /// config from the given path.+ ///+ /// Creates a new config file from sample if it doesn't exist.+ pub async fn new(config_path: Option<PathBuf>) -> anyhow::Result<Self> {+ let (changes_tx, changes_rx) = mpsc::unbounded_channel::<()>();+let default_config_path = home::home_dir().context("Unable to get home directory.")?.join(".glzr/glazewm/config.yaml");let config_path = config_path.unwrap_or(default_config_path);- // Create new config file from sample if it doesn't exist.+ let (value, value_str) = Self::read(&config_path).await?;++ Ok(Self {+ changes_rx,+ changes_tx,+ path: config_path,+ value,+ value_str,+ })+ }++ /// Reads and validates the user config from the given path.+ ///+ /// Creates a new config file from sample if it doesn't exist.+ async fn read(+ config_path: &PathBuf,+ ) -> anyhow::Result<(ParsedConfig, String)> {if !config_path.exists() {Self::create_sample(config_path.clone()).await?;}@@ -42,19 +64,12 @@ impl UserConfig {// TODO: Improve error formatting of serde_yaml errors. Something// similar to https://github.com/AlexanderThaller/format_serde_error- let parsed_config = serde_yaml::from_str(&config_str)?;-- let (changes_tx, changes_rx) = mpsc::unbounded_channel::<()>();+ let config_value = serde_yaml::from_str(&config_str)?;- Ok(Self {- changes_rx,- changes_tx,- config_path,- value: parsed_config,- })+ Ok((config_value, config_str))}- /// Initialize a new config file from the sample config resource.+ /// Initializes a new config file from the sample config resource.async fn create_sample(config_path: PathBuf) -> Result<()> {let parent_dir =config_path.parent().context("Invalid config path.")?;@@ -72,8 +87,11 @@ impl UserConfig {Ok(())}- async fn refresh(&self) -> Result<()> {- // TODO+ pub async fn reload(&mut self) -> anyhow::Result<()> {+ let (value, value_str) = Self::read(&self.path).await?;+ self.value = value;+ self.value_str = value_str;+Ok(())}diff --git a/packages/wm/src/windows/tiling_window.rs b/packages/wm/src/windows/tiling_window.rsindex ad8ccf5a..d4ec0662 100644--- a/packages/wm/src/windows/tiling_window.rs+++ b/packages/wm/src/windows/tiling_window.rs@@ -96,10 +96,6 @@ impl TilingWindow {Self(Rc::new(RefCell::new(window)))}- pub fn inner_gap(&self) -> LengthValue {- self.0.borrow().inner_gap.clone()- }-pub fn to_non_tiling(&self,state: WindowState,diff --git a/packages/wm/src/workspaces/workspace.rs b/packages/wm/src/workspaces/workspace.rsindex 097b898a..1192a98a 100644--- a/packages/wm/src/workspaces/workspace.rs+++ b/packages/wm/src/workspaces/workspace.rs@@ -84,6 +84,11 @@ impl Workspace {self.0.borrow().config.clone()}+ /// Update the underlying config for the workspace.+ pub fn set_config(&self, config: WorkspaceConfig) -> () {+ self.0.borrow_mut().config = config;+ }+/// Whether the workspace is currently displayed by the parent monitor.pub fn is_displayed(&self) -> bool {selfdiff --git a/resources/sample-config.yaml b/resources/sample-config.yamlindex 67e9b4ec..23dc26d1 100644--- a/resources/sample-config.yaml+++ b/resources/sample-config.yaml@@ -164,6 +164,10 @@ keybindings:- commands: ['wm-reload-config']bindings: ['Alt+Shift+R']+ # Redraw all windows.+ - commands: ['wm-redraw']+ bindings: ['Alt+Shift+W']+# Launch CMD terminal. Alternatively `shell_exec --command wt` or# `shell_exec --command %ProgramFiles%/Git/git-bash.exe` to start Windows# Terminal and Git Bash respectively.