Skip to content

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

feat: create `ConfigReader` struct to simplify listening to config changes

Lars Bergerf3852e7b80ea
diff --git a/packages/wm/src/common/platform/event_listener.rs b/packages/wm/src/common/platform/event_listener.rs
index f52c60ce..8c10e42d 100644
--- a/packages/wm/src/common/platform/event_listener.rs
+++ b/packages/wm/src/common/platform/event_listener.rs
@@ -28,25 +28,24 @@ pub enum PlatformEvent {
pub struct EventListener {
config: Arc<Mutex<UserConfig>>,
- config_changes_rx: UnboundedReceiver<UserConfig>,
pub event_rx: UnboundedReceiver<PlatformEvent>,
event_window: EventWindow,
}
impl EventListener {
/// Starts listening for platform events.
- pub async fn start(
- config: Arc<Mutex<UserConfig>>,
- config_changes_rx: UnboundedReceiver<UserConfig>,
- ) -> Result<Self> {
+ pub async fn start(config: Arc<Mutex<UserConfig>>) -> Result<Self> {
let (event_tx, event_rx) = mpsc::unbounded_channel();
let event_window = EventWindow::new(event_tx);
Ok(Self {
config,
- config_changes_rx,
event_rx,
event_window,
})
}
+
+ pub fn update(&self) {
+ todo!()
+ }
}
diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rs
index f7521d7a..a671a406 100644
--- a/packages/wm/src/common/platform/platform.rs
+++ b/packages/wm/src/common/platform/platform.rs
@@ -1,6 +1,6 @@
use std::sync::Arc;
-use tokio::sync::{mpsc::UnboundedReceiver, Mutex};
+use tokio::sync::Mutex;
use windows::Win32::UI::WindowsAndMessaging::GetForegroundWindow;
use crate::user_config::UserConfig;
@@ -39,8 +39,7 @@ impl Platform {
pub async fn new_event_listener(
config: Arc<Mutex<UserConfig>>,
- config_changes_rx: UnboundedReceiver<UserConfig>,
) -> anyhow::Result<EventListener> {
- EventListener::start(config, config_changes_rx).await
+ EventListener::start(config).await
}
}
diff --git a/packages/wm/src/containers/commands/mod.rs b/packages/wm/src/containers/commands/mod.rs
new file mode 100644
index 00000000..b7a42113
--- /dev/null
+++ b/packages/wm/src/containers/commands/mod.rs
@@ -0,0 +1,3 @@
+mod redraw_command;
+
+pub use redraw_command::*;
diff --git a/packages/wm/src/containers/commands/redraw_command.rs b/packages/wm/src/containers/commands/redraw_command.rs
new file mode 100644
index 00000000..051ac0b9
--- /dev/null
+++ b/packages/wm/src/containers/commands/redraw_command.rs
@@ -0,0 +1,7 @@
+use crate::wm_state::WmState;
+
+pub struct RedrawCommand;
+
+pub fn redraw_handler(cmd: RedrawCommand, state: &mut WmState) {
+ todo!()
+}
diff --git a/packages/wm/src/containers/mod.rs b/packages/wm/src/containers/mod.rs
index a72cf07e..2b2773d8 100644
--- a/packages/wm/src/containers/mod.rs
+++ b/packages/wm/src/containers/mod.rs
@@ -1,3 +1,4 @@
+pub mod commands;
mod common_container;
mod container_ref;
mod container_type;
diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rs
index 74f74427..27829457 100644
--- a/packages/wm/src/main.rs
+++ b/packages/wm/src/main.rs
@@ -12,7 +12,7 @@ use tokio::{
};
use tracing::{info, level_filters::LevelFilter};
use tracing_subscriber::EnvFilter;
-use user_config::UserConfig;
+use user_config::ConfigReader;
use wm::WindowManager;
use crate::{
@@ -52,20 +52,22 @@ async fn main() {
async fn start_wm(config_path: Option<String>) -> Result<()> {
// Parse and validate user config.
- let config = Arc::new(Mutex::new(UserConfig::read(config_path).await?));
- let (config_changes_tx, config_changes_rx) =
- mpsc::unbounded_channel::<UserConfig>();
+ let mut config_reader = ConfigReader::read(config_path).await?;
let mut ipc_server = IpcServer::start().await?;
// Start watcher process for restoring hidden windows on crash.
start_watcher_process()?;
- let mut wm = WindowManager::start(config.clone(), config_changes_tx)?;
+ let mut wm = WindowManager::start(
+ config_reader.config(),
+ config_reader.changes_tx.clone(),
+ )
+ .await?;
// Start listening for platform events.
let mut event_listener =
- Platform::new_event_listener(config, config_changes_rx).await?;
+ Platform::new_event_listener(config_reader.config()).await?;
loop {
let wm_state = wm.state.clone();
@@ -73,22 +75,6 @@ async fn start_wm(config_path: Option<String>) -> Result<()> {
tokio::select! {
Some(event) = event_listener.event_rx.recv() => {
info!("Received platform event: {:?}", event);
- wm_state.lock().await.add_monitor();
- wm_state.lock().await.add_monitor();
- wm_state.lock().await.add_monitor();
-
- let root = RootContainerRef::new();
- let leaf = RootContainerRef::new();
- root.insert_child(0, ContainerRef::RootContainer(leaf.clone()));
-
- let leaf_deep = RootContainerRef::new();
- leaf.insert_child(0, ContainerRef::RootContainer(leaf_deep.clone()));
-
- // println!("aaaa {:?}", root);
- // println!("aaaa {:?}", leaf);
- // println!("aaaa {:?}", leaf_deep);
- // println!("aaaa {:?}", leaf_deep.grandparent());
-
wm.process_event(event).await
},
Some(wm_event) = wm.event_rx.recv() => {
@@ -103,6 +89,10 @@ async fn start_wm(config_path: Option<String>) -> Result<()> {
info!("Received WM command via IPC: {:?}", wm_command);
wm.process_command(wm_command).await
},
+ Some(config) = config_reader.changes_rx.recv() => {
+ info!("Received user config update: {:?}", config);
+ event_listener.update();
+ },
}
}
}
diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rs
index 3b120715..46c98f12 100644
--- a/packages/wm/src/user_config.rs
+++ b/packages/wm/src/user_config.rs
@@ -1,8 +1,11 @@
-use std::path::PathBuf;
+use std::{path::PathBuf, sync::Arc};
use anyhow::{Context, Result};
use serde::Deserialize;
-use tokio::fs;
+use tokio::{
+ fs,
+ sync::{mpsc, Mutex},
+};
use crate::{
common::{LengthValue, RectDelta},
@@ -74,7 +77,7 @@ pub enum WindowAnimations {
#[derive(Debug, Deserialize)]
pub struct FocusBorder {
- /// Whether the default transparent border be used.
+ /// Whether to use a custom border color.
pub enabled: bool,
/// Border color of the window.
@@ -117,10 +120,18 @@ pub struct UserConfig {
pub workspaces: Vec<WorkspaceConfig>,
}
+#[derive(Debug)]
+pub struct ConfigReader {
+ pub config: Arc<Mutex<UserConfig>>,
+ pub changes_rx: mpsc::UnboundedReceiver<UserConfig>,
+ pub changes_tx: mpsc::UnboundedSender<UserConfig>,
+}
+
const SAMPLE_CONFIG: &str =
include_str!("../../../resources/sample-config.yaml");
-impl UserConfig {
+impl ConfigReader {
+ /// Read and validate the user config from the given path.
pub async fn read(config_path: Option<String>) -> Result<Self> {
let default_config_path = home::home_dir()
.context("Unable to get home directory.")?
@@ -133,7 +144,7 @@ impl UserConfig {
// Create new config file from sample if it doesn't exist.
if !config_path.exists() {
- Self::create_from_sample(config_path.clone()).await?;
+ Self::create_sample(config_path.clone()).await?;
}
let config_str = fs::read_to_string(&config_path)
@@ -141,10 +152,18 @@ impl UserConfig {
.context("Unable to read config file.")?;
let parsed_config = serde_yaml::from_str(&config_str)?;
- Ok(parsed_config)
+
+ let (changes_tx, changes_rx) = mpsc::unbounded_channel::<UserConfig>();
+
+ Ok(Self {
+ config: Arc::new(Mutex::new(parsed_config)),
+ changes_rx,
+ changes_tx,
+ })
}
- async fn create_from_sample(config_path: PathBuf) -> Result<()> {
+ /// Initialize 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.")?;
@@ -160,4 +179,13 @@ impl UserConfig {
Ok(())
}
+
+ pub fn config(&self) -> Arc<Mutex<UserConfig>> {
+ self.config.clone()
+ }
+
+ // TODO: Maybe this should be on the impl of `UserConfig` instead.
+ async fn refresh(&self) -> Result<()> {
+ Ok(())
+ }
}