Use config

This commit is contained in:
Yash Karandikar 2022-06-26 12:23:38 +05:30
parent 87997cf9f1
commit 1224e69a49
3 changed files with 34 additions and 19 deletions

View file

@ -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<u32>,
pub border_size: Option<u16>,
pub gap_width: Option<u16>,
border_color: Option<u32>,
border_size: Option<u16>,
gap_width: Option<u16>,
}
impl Default for XcrabConfig {
@ -18,11 +19,21 @@ impl Default for XcrabConfig {
}
}
pub fn load_file() -> Arc<XcrabConfig> {
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<XcrabConfig, crate::XcrabError> {
pub fn load_file() -> Result<XcrabConfig, crate::XcrabError> {
let contents = std::fs::read_to_string("~/.config/xcrab/config.toml")?;
let config: XcrabConfig = toml::from_str(&contents)?;

View file

@ -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`

View file

@ -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<Self, crate::XcrabError> {
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<Dpy: AsyncDisplay + ?Sized>(
windows: &mut HashMap<Window, XcrabClient>,
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<Dpy: AsyncDisplay + ?Sized>(
// 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 {