Skip to content

Commit

Permalink
added window width changing to cascade
Browse files Browse the repository at this point in the history
  • Loading branch information
laudominik committed Sep 10, 2024
1 parent ad31a37 commit 1a59c18
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Window manager written in Rust. Heavily inspired by DWM. The goal is to support
* auto cascade tiling ✔
* fullscreen window
* floating window
* wallpaper
* wallpaper
* spawning some scripts with shell on startup ✔
* top bar
* keybindings ✔
Expand Down
106 changes: 74 additions & 32 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::process::Command;
use std::sync::Arc;

use x11::keysym::{XK_Return, XK_j, XK_k, XK_space, XK_c};
use x11::xlib::{Mod1Mask, Mod3Mask, Mod4Mask, ShiftMask};
use x11::keysym::{XK_Return, XK_c, XK_h, XK_j, XK_k, XK_l, XK_space};
use x11::xlib::{Mod1Mask, Mod3Mask, Mod4Mask, ShiftMask, XDisplayHeight, XDisplayWidth};

use crate::state::{self, Keybinding, KEYBINDINGS};
use crate::style::{ColorScheme, ColorSchemes, Style};
use crate::wm;
use crate::{active_workspace, active_workspace_wins, wm};

macro_rules! set_spaces {
($state:expr, [ $($tag:expr),* ]) => {{
$(
$state.workspaces.push(wm::Space {
tag: $tag,
windows: Vec::new()
windows: Vec::new(),
custom: None
});
)*
}};
Expand Down Expand Up @@ -75,34 +76,68 @@ const MODKEY_SHIFT: u32 = MODKEY | ShiftMask;

/* your private config goes here */
pub fn make(state: &mut state::State){
set_spaces!(state, ["一", "二", "三", "四"]);

set_keybinding!(
modkey: MODKEY,
callback: |_| {spawn_with_shell!("alacritty");},
key: XK_Return
);

set_keybinding!(
modkey: MODKEY,
callback: |state| {state.focus_next();},
key: XK_j
);

set_keybinding!(
modkey: MODKEY,
callback: |state| {state.focus_previous();},
key: XK_k
);

set_keybinding!(
modkey: MODKEY_SHIFT,
callback: |state| {state.close_active();},
key: XK_c
);

spawn_with_shell!("nitrogen", ["--restore"]);
spawn_with_shell!("picom");

/* keybindings */
{
set_keybinding!(
modkey: MODKEY,
callback: |_| {spawn_with_shell!("alacritty");},
key: XK_Return
);

set_keybinding!(
modkey: MODKEY,
callback: |state| {state.focus_next();},
key: XK_j
);

set_keybinding!(
modkey: MODKEY,
callback: |state| {state.focus_previous();},
key: XK_k
);

set_keybinding!(
modkey: MODKEY,
callback: |state| {separator_modify(state, 10)},
key: XK_l
);

set_keybinding!(
modkey: MODKEY,
callback: |state| {separator_modify(state, -10)},
key: XK_h
);

set_keybinding!(
modkey: MODKEY_SHIFT,
callback: |state| {state.close_active();},
key: XK_c
);
}

/* startup apps */
{
spawn_with_shell!("nitrogen", ["--restore"]);
spawn_with_shell!("picom");

}

/* default workspaces config */
{
set_spaces!(state, ["一", "二", "三", "四"]);
let screen_width = unsafe{XDisplayWidth(state.dpy, state.screen) as u32};

for space in state.workspaces.iter_mut() {
space.custom = Some(CustomData {
separator: screen_width/2
});
}
}
}

pub struct CustomData {
pub separator: u32 /* used by cascade_autotiling */
}

impl state::State<'_> {
Expand All @@ -111,3 +146,10 @@ impl state::State<'_> {
self.cascade_autotiling();
}
}

fn separator_modify(state: &mut state::State, modifier: i32) {
if let Some(custom ) = &mut active_workspace!(state).custom {
custom.separator = (custom.separator as i32 + modifier).clamp(10, 1760) as u32;
state.retile();
}
}
2 changes: 1 addition & 1 deletion src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ffi::CString;
use std::ptr::null;
use std::mem;

use crate::config::{STYLE};
use crate::config::{CustomData, STYLE};
use crate::wm;
use crate::style::{self};
use crate::state::{Active, Cursor, State, KEYBINDINGS};
Expand Down
6 changes: 3 additions & 3 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use x11::xlib::{self, Window};

use crate::{style::{ColorSchemesXft}, wm};
use crate::{config::CustomData, style::ColorSchemesXft, wm};

pub type Cursor = Cursor_<xlib::Cursor>;

Expand All @@ -19,12 +19,12 @@ pub struct State<'a> {
pub dpy: &'a mut xlib::Display,
pub workspaces: Vec<wm::Space<'a>>,
pub colors : ColorSchemesXft,
pub active: Active
pub active: Active,
}

pub struct Active {
pub workspace: usize,
pub window: Window
pub window: Window,
}

pub static mut KEYBINDINGS : Vec<Keybinding> = Vec::new();
Expand Down
23 changes: 18 additions & 5 deletions src/wm.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
use x11::xlib::{CWBorderWidth, CurrentTime, False, NoEventMask, RevertToNone, RevertToPointerRoot, Window, XConfigureWindow, XDestroyWindow, XDisplayHeight, XDisplayWidth, XEvent, XMapWindow, XMoveResizeWindow, XSendEvent, XSetInputFocus, XSetWindowBorder, XSync, XWindowChanges};
use std::{mem, process::exit};

use crate::{config::STYLE, state};
use crate::{config::{CustomData, STYLE}, state};

pub struct Space<'a> {
pub tag: &'a str,
pub windows: Vec<Window>
pub windows: Vec<Window>,
pub custom: Option<CustomData> /* custom config for active workspace*/
}

pub struct _Tile {
pub coords: (i32, i32),
pub size: (u32, u32)
}

#[macro_export]
macro_rules! active_workspace {
($state: expr) => {
$state.workspaces[$state.active.workspace]
};
}

#[macro_export]
macro_rules! active_workspace_wins {
($state: expr) => {
Expand Down Expand Up @@ -80,10 +88,15 @@ impl state::State<'_> {
));
return;
}

let mut middle = screen_width / 2;
if let Some(custom) = &active_workspace!(self).custom {
middle = custom.separator;
}

latest_window.do_map(self, (
useless_gap as i32, useless_gap as i32,
screen_width / 2 - useless_gap * 2 - border * 2, screen_height - useless_gap * 2 - border * 2
middle - useless_gap * 2 - border * 2, screen_height - useless_gap * 2 - border * 2
));

let len_rest = active_workspace_wins!(self).len() - 1;
Expand All @@ -93,8 +106,8 @@ impl state::State<'_> {
let start_y = increment * i as u32 + useless_gap;

active_workspace_wins!(self)[i].do_map(self, (
(useless_gap / 2 + screen_width / 2) as i32, start_y as i32,
screen_width / 2 - useless_gap * 2 - border * 2, increment - useless_gap * 2 - border * 2
(useless_gap / 2 + middle) as i32, start_y as i32,
(screen_width - middle) - useless_gap * 2 - border * 2, increment - useless_gap * 2 - border * 2
));
}
}
Expand Down

0 comments on commit 1a59c18

Please sign in to comment.