Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat: add and implement commands for enabling/disabling binding modes

Lars Bergerfee2e7d2d4eb
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index d1a8e3c8..e400cdfc 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -8,7 +8,9 @@ use uuid::Uuid;
use crate::{
common::{
- commands::{reload_config, shell_exec},
+ commands::{
+ disable_binding_mode, enable_binding_mode, reload_config, shell_exec,
+ },
Direction, LengthValue, ResizeDimension,
},
containers::{
@@ -473,8 +475,13 @@ impl InvokeCommand {
InvokeCommand::ToggleTilingDirection => {
toggle_tiling_direction(subject_container, state, config)
}
- InvokeCommand::WmDisableBindingMode { name } => todo!(),
- InvokeCommand::WmEnableBindingMode { name } => todo!(),
+ InvokeCommand::WmDisableBindingMode { name } => {
+ disable_binding_mode(name, state);
+ Ok(())
+ }
+ InvokeCommand::WmEnableBindingMode { name } => {
+ enable_binding_mode(name, state, config)
+ }
InvokeCommand::WmExit => todo!(),
InvokeCommand::WmRedraw => {
let root_container = state.root_container.clone();
diff --git a/packages/wm/src/common/commands/disable_binding_mode.rs b/packages/wm/src/common/commands/disable_binding_mode.rs
new file mode 100644
index 00000000..8043e065
--- /dev/null
+++ b/packages/wm/src/common/commands/disable_binding_mode.rs
@@ -0,0 +1,14 @@
+use crate::{wm_event::WmEvent, wm_state::WmState};
+
+pub fn disable_binding_mode(name: &str, state: &mut WmState) {
+ state.binding_modes = state
+ .binding_modes
+ .iter()
+ .filter(|config| config.name != name)
+ .cloned()
+ .collect::<Vec<_>>();
+
+ state.emit_event(WmEvent::BindingModesChanged {
+ active_binding_modes: state.binding_modes.clone(),
+ });
+}
diff --git a/packages/wm/src/common/commands/enable_binding_mode.rs b/packages/wm/src/common/commands/enable_binding_mode.rs
new file mode 100644
index 00000000..ae5a9880
--- /dev/null
+++ b/packages/wm/src/common/commands/enable_binding_mode.rs
@@ -0,0 +1,28 @@
+use anyhow::Context;
+
+use crate::{
+ user_config::UserConfig, wm_event::WmEvent, wm_state::WmState,
+};
+
+pub fn enable_binding_mode(
+ name: &str,
+ state: &mut WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
+ let binding_mode = config
+ .value
+ .binding_modes
+ .iter()
+ .find(|config| name == config.name)
+ .with_context(|| {
+ format!("No binding mode found with the name '{}'.", name)
+ })?;
+
+ state.binding_modes = vec![binding_mode.clone()];
+
+ state.emit_event(WmEvent::BindingModesChanged {
+ active_binding_modes: state.binding_modes.clone(),
+ });
+
+ Ok(())
+}
diff --git a/packages/wm/src/common/commands/mod.rs b/packages/wm/src/common/commands/mod.rs
index 6ef7dd15..1fb41d5d 100644
--- a/packages/wm/src/common/commands/mod.rs
+++ b/packages/wm/src/common/commands/mod.rs
@@ -1,7 +1,11 @@
+mod disable_binding_mode;
+mod enable_binding_mode;
mod reload_config;
mod shell_exec;
mod sync_native_focus;
+pub use disable_binding_mode::*;
+pub use enable_binding_mode::*;
pub use reload_config::*;
pub use shell_exec::*;
pub use sync_native_focus::*;
diff --git a/packages/wm/src/common/platform/event_listener.rs b/packages/wm/src/common/platform/event_listener.rs
index 3c1ee25b..02888392 100644
--- a/packages/wm/src/common/platform/event_listener.rs
+++ b/packages/wm/src/common/platform/event_listener.rs
@@ -47,7 +47,7 @@ impl EventListener {
let event_window = EventWindow::new(
event_tx,
- config.value.keybindings.clone(),
+ &config.value.keybindings,
config.value.general.focus_follows_cursor,
)?;
@@ -62,12 +62,16 @@ impl EventListener {
pub fn update(
&mut self,
config: &UserConfig,
- binding_modes: &Vec<&BindingModeConfig>,
+ binding_modes: &Vec<BindingModeConfig>,
) {
- // TODO: Modify keybindings based on active binding modes.
- self.event_window.update(
- config.value.keybindings.clone(),
- config.value.general.focus_follows_cursor,
- );
+ // Modify keybindings based on active binding modes.
+ let keybindings = match binding_modes.first() {
+ Some(binding_mode) => &binding_mode.keybindings,
+ None => &config.value.keybindings,
+ };
+
+ self
+ .event_window
+ .update(keybindings, config.value.general.focus_follows_cursor);
}
}
diff --git a/packages/wm/src/common/platform/event_window.rs b/packages/wm/src/common/platform/event_window.rs
index 2f58136e..fc9e8723 100644
--- a/packages/wm/src/common/platform/event_window.rs
+++ b/packages/wm/src/common/platform/event_window.rs
@@ -76,7 +76,7 @@ impl EventWindow {
/// ever be instantiated once in the application's lifetime.
pub fn new(
event_tx: mpsc::UnboundedSender<PlatformEvent>,
- keybindings: Vec<KeybindingConfig>,
+ keybindings: &Vec<KeybindingConfig>,
enable_mouse_events: bool,
) -> anyhow::Result<Self> {
let keyboard_hook = KeyboardHook::new(keybindings, event_tx.clone())?;
@@ -132,7 +132,7 @@ impl EventWindow {
pub fn update(
&mut self,
- keybindings: Vec<KeybindingConfig>,
+ keybindings: &Vec<KeybindingConfig>,
enable_mouse_events: bool,
) {
self.keyboard_hook.update(keybindings);
diff --git a/packages/wm/src/common/platform/keyboard_hook.rs b/packages/wm/src/common/platform/keyboard_hook.rs
index 9e474809..76a6bae9 100644
--- a/packages/wm/src/common/platform/keyboard_hook.rs
+++ b/packages/wm/src/common/platform/keyboard_hook.rs
@@ -58,7 +58,7 @@ pub struct KeyboardHook {
impl KeyboardHook {
/// Creates an instance of `KeyboardHook`.
pub fn new(
- keybindings: Vec<KeybindingConfig>,
+ keybindings: &Vec<KeybindingConfig>,
event_tx: mpsc::UnboundedSender<PlatformEvent>,
) -> anyhow::Result<Arc<Self>> {
let keyboard_hook = Arc::new(Self {
@@ -87,7 +87,7 @@ impl KeyboardHook {
Ok(())
}
- pub fn update(&self, keybindings: Vec<KeybindingConfig>) {
+ pub fn update(&self, keybindings: &Vec<KeybindingConfig>) {
*self.keybindings_by_trigger_key.lock().unwrap() =
Self::keybindings_by_trigger_key(keybindings);
}
@@ -99,11 +99,11 @@ impl KeyboardHook {
}
fn keybindings_by_trigger_key(
- keybindings: Vec<KeybindingConfig>,
+ keybindings: &Vec<KeybindingConfig>,
) -> HashMap<u16, Vec<ActiveKeybinding>> {
let mut keybinding_map = HashMap::new();
- for keybinding in &keybindings {
+ for keybinding in keybindings {
for binding in &keybinding.bindings {
let vk_codes = binding
.split("+")
diff --git a/packages/wm/src/ipc_server.rs b/packages/wm/src/ipc_server.rs
index 171d9ed1..57fcc91f 100644
--- a/packages/wm/src/ipc_server.rs
+++ b/packages/wm/src/ipc_server.rs
@@ -17,7 +17,7 @@ use crate::{
app_command::{AppCommand, QueryCommand, SubscribableEvent},
containers::{Container, WindowContainer},
monitors::Monitor,
- user_config::UserConfig,
+ user_config::{BindingModeConfig, UserConfig},
wm::WindowManager,
wm_event::WmEvent,
workspaces::Workspace,
@@ -44,7 +44,7 @@ pub struct ClientResponseMessage {
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum ClientResponseData {
- BindingModes(Vec<String>),
+ BindingModes(Vec<BindingModeConfig>),
Command(CommandData),
EventSubscribe(EventSubscribeData),
EventUnsubscribe,
diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rs
index 401d0e8b..27b69f4e 100644
--- a/packages/wm/src/main.rs
+++ b/packages/wm/src/main.rs
@@ -131,7 +131,7 @@ async fn start_wm(
) {
event_listener.update(
&config,
- &config.binding_mode_configs(&wm.state.binding_modes),
+ &wm.state.binding_modes,
);
}
diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rs
index fd1bdf8b..56fde4d2 100644
--- a/packages/wm/src/user_config.rs
+++ b/packages/wm/src/user_config.rs
@@ -134,19 +134,6 @@ impl UserConfig {
.or(inactive_configs.first())
.cloned()
}
-
- /// Gets the binding mode configs with the given names.
- pub fn binding_mode_configs(
- &self,
- names: &Vec<String>,
- ) -> Vec<&BindingModeConfig> {
- self
- .value
- .binding_modes
- .iter()
- .filter(|config| names.contains(&config.name))
- .collect()
- }
}
#[derive(Clone, Debug, Deserialize, Serialize)]
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index c623a43e..c59e0344 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -17,7 +17,7 @@ use crate::{
Container, RootContainer, WindowContainer,
},
monitors::{commands::add_monitor, Monitor},
- user_config::UserConfig,
+ user_config::{BindingModeConfig, UserConfig},
windows::{commands::manage_window, traits::WindowGetters, WindowState},
wm_event::WmEvent,
workspaces::Workspace,
@@ -48,8 +48,8 @@ pub struct WmState {
/// Used to decide whether to override incoming focus events.
pub unmanaged_or_minimized_timestamp: Option<Instant>,
- /// Names of any currently enabled binding modes.
- pub binding_modes: Vec<String>,
+ /// Configs of currently enabled binding modes.
+ pub binding_modes: Vec<BindingModeConfig>,
/// Sender for emitting WM-related events.
event_tx: mpsc::UnboundedSender<WmEvent>,