diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 60451749..1a1944d7 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -331,7 +331,7 @@ pub enum SocketMessage { ToggleWorkspaceLayer, // Configuration // ReloadConfiguration, - // ReplaceConfiguration(#[serde_as(as = "ResolvedPathBuf")] PathBuf), + ReplaceConfiguration(#[serde_as(as = "ResolvedPathBuf")] PathBuf), ReloadStaticConfiguration(#[serde_as(as = "ResolvedPathBuf")] PathBuf), // WatchConfiguration(bool), // CompleteConfiguration, diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index e43bfdcc..7e4a3746 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -40,6 +40,7 @@ use crate::window::RuleDebug; use crate::window::Window; use crate::window::WindowInfo; use crate::window_manager::WindowManager; +use crate::window_manager_event_listener; use crate::workspace::WorkspaceLayer; use crate::workspace::WorkspaceWindowLocation; use color_eyre::eyre; @@ -1629,6 +1630,36 @@ impl WindowManager { let mut display_index_preferences = DISPLAY_INDEX_PREFERENCES.write(); display_index_preferences.insert(index_preference, display.clone()); } + SocketMessage::ReplaceConfiguration(ref config) => { + // Check that this is a valid static config file first + if StaticConfig::read(config).is_ok() { + // Clear workspace rules; these will need to be replaced + WORKSPACE_MATCHING_RULES.lock().clear(); + // Pause so that restored windows come to the foreground from all workspaces + self.is_paused = true; + // Bring all windows to the foreground + self.restore_all_windows(false)?; + + // Create a new wm from the config path + let mut wm = StaticConfig::preload( + config, + window_manager_event_listener::event_rx(), + self.command_listener.try_clone().ok(), + &self.run_loop.0, + )?; + + // Initialize the new wm + wm.init()?; + + wm.restore_all_windows(true)?; + + // This is equivalent to StaticConfig::postload for this use case + StaticConfig::reload(config, &mut wm)?; + + // Set self to the new wm instance + *self = wm; + } + } } notify_subscribers( Notification { diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 51d96779..40ef2dce 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -582,6 +582,13 @@ struct LoadResize { path: PathBuf, } +#[derive(Parser)] +struct ReplaceConfiguration { + /// Static configuration JSON file from which the configuration should be loaded + #[clap(value_parser = replace_env_in_path)] + path: PathBuf, +} + #[derive(Parser)] struct EagerFocus { /// Case-sensitive exe identifier @@ -955,9 +962,9 @@ enum SubCommand { Manage, /// Unmanage a window that was forcibly managed Unmanage, - // /// Replace the configuration of a running instance of komorebi from a static configuration file - // #[clap(arg_required_else_help = true)] - // ReplaceConfiguration(ReplaceConfiguration), + /// Replace the configuration of a running instance of komorebi from a static configuration file + #[clap(arg_required_else_help = true)] + ReplaceConfiguration(ReplaceConfiguration), // /// Reload legacy komorebi.ahk or komorebi.ps1 configurations (if they exist) // ReloadConfiguration, // /// Enable or disable watching of legacy komorebi.ahk or komorebi.ps1 configurations (if they exist) @@ -1816,6 +1823,9 @@ fn main() -> eyre::Result<()> { arg.display, ))?; } + SubCommand::ReplaceConfiguration(arg) => { + send_message(&SocketMessage::ReplaceConfiguration(arg.path))?; + } } Ok(())