From c9d07378c031e2636042ac788debcc1a59152728 Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Sun, 26 Jun 2022 23:54:22 +0530 Subject: [PATCH] Put back the config code, thanks missing --- Cargo.lock | 7 +++++ Cargo.toml | 1 + src/main.rs | 17 +++++++---- src/x11/client.rs | 75 +++++++++++++++++++++++++++-------------------- 4 files changed, 64 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4161969..8566f72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -217,6 +217,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.126" @@ -583,6 +589,7 @@ name = "xcrab" version = "0.1.0" dependencies = [ "breadx", + "lazy_static", "serde", "tokio", "toml", diff --git a/Cargo.toml b/Cargo.toml index b877e05..e8dd28c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ breadx = { version = "2.0.0", features = ["async"] } tokio = { version = "1.19.2", features = ["full"] } toml = "0.5.9" serde = { version = "1.0.137", features = ["derive"]} +lazy_static = "1.4.0" diff --git a/src/main.rs b/src/main.rs index 56d107c..73eb329 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,16 @@ #![warn(clippy::pedantic)] -use std::fmt::{Display, Debug}; +use std::fmt::{Debug, Display}; use breadx::{ prelude::{AsyncDisplayXprotoExt, MapState}, - traits::DisplayBase, AsyncDisplayConnection, AsyncDisplayExt, BreadError, ConfigureWindowParameters, - Event, EventMask, + traits::DisplayBase, + AsyncDisplayConnection, AsyncDisplayExt, BreadError, ConfigureWindowParameters, Event, + EventMask, }; +use lazy_static::lazy_static; + mod config; mod x11; @@ -46,6 +49,10 @@ impl From for XcrabError { } } +lazy_static! { + pub static ref CONFIG: config::XcrabConfig = config::load_file().unwrap_or_default(); +} + impl Display for XcrabError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -55,7 +62,7 @@ impl Display for XcrabError { Self::Var(ve) => Display::fmt(&ve, f)?, Self::ClientDoesntExist => Display::fmt("client didn't exist", f)?, }; - + Ok(()) } } @@ -136,4 +143,4 @@ async fn main() -> Result<()> { _ => {} } } -} \ No newline at end of file +} diff --git a/src/x11/client.rs b/src/x11/client.rs index e762e1c..56ee72c 100644 --- a/src/x11/client.rs +++ b/src/x11/client.rs @@ -2,7 +2,7 @@ use breadx::prelude::{AsyncDisplayXprotoExt, SetMode}; use breadx::{AsyncDisplay, ConfigureWindowParameters, EventMask, Window}; use std::collections::HashMap; -use crate::{XcrabError, Result}; +use crate::{Result, XcrabError}; #[derive(Debug, Clone, Copy)] pub enum Direction { @@ -53,7 +53,10 @@ impl XcrabWindowManager { if let Some(focused) = self.focused { use Direction::*; - let focused_client = *self.clients.get(&focused).ok_or(XcrabError::ClientDoesntExist)?; + let focused_client = *self + .clients + .get(&focused) + .ok_or(XcrabError::ClientDoesntExist)?; let new_client = match direction { Up | Down => { @@ -80,7 +83,7 @@ impl XcrabWindowManager { } new_client - }, + } Left | Right => { if focused_client.width % 2 == 1 { for client in self.clients.values_mut() { @@ -107,7 +110,7 @@ impl XcrabWindowManager { new_client } }; - + self.clients.insert(win, new_client); self.update_client(conn, focused).await?; @@ -143,13 +146,16 @@ impl XcrabWindowManager { conn: &mut Dpy, win: Window, ) -> Result<()> { - let client = *self.clients.get(&win).ok_or(XcrabError::ClientDoesntExist)?; + let client = *self + .clients + .get(&win) + .ok_or(XcrabError::ClientDoesntExist)?; let root = conn.default_root(); let root_geo = root.geometry_immediate_async(conn).await?; let root_width: u32 = root_geo.width.into(); let root_height: u32 = root_geo.height.into(); - + let x = (client.x * root_width) / self.grid_width; let y = (client.y * root_height) / self.grid_height; let width = (client.width * root_width) / self.grid_width; @@ -157,21 +163,30 @@ impl XcrabWindowManager { let parent = win.query_tree_immediate_async(conn).await?.parent; - parent.configure_async(conn, ConfigureWindowParameters { - x: Some(x.try_into().unwrap()), - y: Some(y.try_into().unwrap()), - width: Some(width), - height: Some(height), - ..Default::default() - }).await?; + parent + .configure_async( + conn, + ConfigureWindowParameters { + x: Some(x.try_into().unwrap()), + y: Some(y.try_into().unwrap()), + width: Some(width), + height: Some(height), + ..Default::default() + }, + ) + .await?; - win.configure_async(conn, ConfigureWindowParameters { - x: Some(0), - y: Some(0), - width: Some(width), - height: Some(height), - ..Default::default() - }).await?; + win.configure_async( + conn, + ConfigureWindowParameters { + x: Some(0), + y: Some(0), + width: Some(width), + height: Some(height), + ..Default::default() + }, + ) + .await?; Ok(()) } @@ -180,7 +195,11 @@ impl XcrabWindowManager { self.clients.contains_key(&win) } - pub async fn remove_client(&mut self, conn: &mut Dpy, win: Window) -> Result<()> { + pub async fn remove_client( + &mut self, + conn: &mut Dpy, + win: Window, + ) -> Result<()> { // TODO: maybe an `unframe` method? let root = conn.default_root(); @@ -191,9 +210,7 @@ impl XcrabWindowManager { win.reparent_async(conn, root, 0, 0).await?; // no longer related to us, remove from save set - win - .change_save_set_async(conn, SetMode::Delete) - .await?; + win.change_save_set_async(conn, SetMode::Delete).await?; parent.free_async(conn).await?; @@ -203,11 +220,7 @@ impl XcrabWindowManager { } } -async fn frame( - conn: &mut Dpy, - win: Window, -) -> Result { - const BORDER_WIDTH: u16 = 3; +async fn frame(conn: &mut Dpy, win: Window) -> Result { const GAP_WIDTH: u16 = 10; let root = conn.default_root(); @@ -221,8 +234,8 @@ async fn frame( geometry.y, geometry.width, geometry.height, - 3, - 0xff_00_00, + crate::CONFIG.border_size(), + crate::CONFIG.border_color(), 0x00_00_00, ) .await?;