diff --git a/src/config.rs b/src/config.rs index d9c2bff..eb82120 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,11 +1,12 @@ -use serde::{Deserialize, Serialize}; -use std::sync::Arc; +#![allow(dead_code)] -#[derive(Debug, Serialize, Deserialize)] +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] pub struct XcrabConfig { - pub border_color: Option, - pub border_size: Option, - pub gap_width: Option, + border_color: Option, + border_size: Option, + gap_width: Option, } impl Default for XcrabConfig { @@ -18,11 +19,21 @@ impl Default for XcrabConfig { } } -pub fn load_file() -> Arc { - Arc::new(load_file_inner().unwrap_or_default()) +impl XcrabConfig { + pub fn border_color(&self) -> u32 { + self.border_color.unwrap_or(0xff_00_00) + } + + pub fn border_size(&self) -> u16 { + self.border_size.unwrap_or(5) + } + + pub fn gap_width(&self) -> u16 { + self.gap_width.unwrap_or(10) + } } -fn load_file_inner() -> Result { +pub fn load_file() -> Result { let contents = std::fs::read_to_string("~/.config/xcrab/config.toml")?; let config: XcrabConfig = toml::from_str(&contents)?; diff --git a/src/main.rs b/src/main.rs index 2adab4a..d237879 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,8 +19,8 @@ mod config; mod x11; use x11::client::XcrabClient; -const BORDER_WIDTH: u16 = 5; -const GAP_WIDTH: u16 = 10; +// const BORDER_WIDTH: u16 = 5; +// const GAP_WIDTH: u16 = 10; #[non_exhaustive] pub enum XcrabError { @@ -66,7 +66,8 @@ impl Debug for XcrabError { #[tokio::main] async fn main() -> Result<(), XcrabError> { - let conf = config::load_file(); + let xcrab_conf = config::load_file().unwrap(); + dbg!(xcrab_conf); // connect to the x server let mut conn = AsyncDisplayConnection::create_async(None, None).await?; @@ -93,9 +94,9 @@ async fn main() -> Result<(), XcrabError> { if !attrs.override_redirect && attrs.map_state == MapState::Viewable { clients.insert( win, - XcrabClient::new(win, &mut conn, clients.len() + 1).await?, + XcrabClient::new(win, &mut conn, clients.len() + 1, xcrab_conf).await?, ); - x11::client::calculate_geometry(&mut clients, &mut conn).await?; + x11::client::calculate_geometry(&mut clients, &mut conn, xcrab_conf).await?; } } @@ -110,9 +111,9 @@ async fn main() -> Result<(), XcrabError> { clients.insert( win, - XcrabClient::new(win, &mut conn, clients.len() + 1).await?, + XcrabClient::new(win, &mut conn, clients.len() + 1, xcrab_conf).await?, ); - x11::client::calculate_geometry(&mut clients, &mut conn).await?; + x11::client::calculate_geometry(&mut clients, &mut conn, xcrab_conf).await?; } Event::ConfigureRequest(ev) => { // cope from `ev` to `params` diff --git a/src/x11/client.rs b/src/x11/client.rs index e39337f..a4d15d4 100644 --- a/src/x11/client.rs +++ b/src/x11/client.rs @@ -1,3 +1,4 @@ +use crate::config::XcrabConfig; use breadx::prelude::{AsyncDisplayXprotoExt, SetMode}; use breadx::{AsyncDisplay, ConfigureWindowParameters, Window}; use std::collections::HashMap; @@ -21,6 +22,7 @@ impl XcrabClient { window: Window, dpy: &mut Dpy, position: usize, + conf: XcrabConfig, ) -> Result { let geometry = window.geometry_immediate_async(dpy).await?; @@ -32,8 +34,8 @@ impl XcrabClient { geometry.y, geometry.width, geometry.height, - crate::BORDER_WIDTH, - 0xff_00_00, + conf.border_size(), + conf.border_color(), 0x00_00_00, ) .await?; @@ -58,6 +60,7 @@ impl XcrabClient { pub async fn calculate_geometry( windows: &mut HashMap, dpy: &mut Dpy, + conf: XcrabConfig, ) -> Result<(), crate::XcrabError> { let root = dpy.default_root(); let root_geometry = root.geometry_immediate_async(dpy).await?; @@ -65,7 +68,7 @@ pub async fn calculate_geometry( // let gap_width = crate::GAP_WIDTH as usize * (window_count + 1); let width_per_window = root_geometry.width as usize / window_count; - let border_width = crate::BORDER_WIDTH as usize * 2; + let border_width = conf.border_size() as usize * 2; for (window, xcrab_client) in windows { let xcrab_geometry = XcrabGeometry {