summaryrefslogtreecommitdiff
path: root/src/helpers.rs
blob: e67aecc961a7ba2d2a7d7bdbf945cdba8e8a02e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Helpers for writing configuration

use crate::{
    clients::{Tag, TagFocus},
    WM,
};
use std::{ffi::OsStr, process::Command};

/// Syntax sugar for creating [`Keybind`]s. This is helpful for writing [`crate::config::KEYBINDS`]
///
/// ```rust
/// /// Control + Shift + t prints "It Works!"
/// bind!(ModMask::CONTROL | ModMask::SHIFT + t -> &|_| {println!("It Works!")})
/// ```
#[macro_export]
macro_rules! bind {
    ($mod:expr , $key:ident -> $action:expr) => {
        $crate::keys::Keybind {
            modifiers: $mod,
            key: ::xkeysym::Keysym::$key,
            action: $action,
        }
    };
}

/// Syntax sugar for creating [`ButtonBind`]s. See also: [`self::bind`].
#[macro_export]
macro_rules! bind_btn {
    ($mod:expr , $key:ident -> $action:expr) => {
        $crate::buttons::ButtonBind {
            modifiers: $mod,
            button: ::xcb::x::ButtonIndex::$key,
            action: $action,
        }
    };
}

/// Execute the given command with arguments, disowning the process.
pub fn spawn<I, S>(cmd: &str, args: I)
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    if let Err(e) = Command::new(cmd).args(args).spawn() {
        eprintln!("error spawning {cmd}: {e:#?}");
    };
}

/// Move focus to the next window on the stack
pub fn focus_next(wm: &mut WM<'_>) {
    wm.clients.change_focus(&wm.conn, true);
}

/// Move focus to the next window on the stack
pub fn focus_prev(wm: &mut WM<'_>) {
    wm.clients.change_focus(&wm.conn, false);
}

/// Toggle floating status for the currently focused window
pub fn toggle_floating(wm: &mut WM<'_>) {
    if let Some(pos) = wm.clients.focused_pos() {
        wm.clients.toggle_floating(&wm.conn, pos);
    }
}

/// Toggle fullscreen status for the currently focused window
pub fn toggle_fullscreen(wm: &mut WM<'_>) {
    if let Some(pos) = wm.clients.focused_pos() {
        wm.clients.toggle_fullscreen(&wm.conn, pos);
    }
}

/// Set the focused tag for the currently selected monitor
pub fn set_tag_focus(wm: &mut WM<'_>, tag_focus: TagFocus) {
    wm.clients
        .set_mon_tag_focus(&wm.conn, wm.clients.focused_mon_idx(), tag_focus);
}

/// Set the tag for the currently selected client
pub fn set_tag(wm: &mut WM<'_>, tag: Tag) {
    if let Some(pos) = wm.clients.focused_pos() {
        wm.clients.set_client_tag(&wm.conn, pos, tag);
    }
}

/// Move the currently focused window with the mouse
pub fn mouse_move(wm: &mut WM<'_>) {
    if let Err(e) = wm.mouse_move() {
        println!("error when moving with mouse: {e:?}");
    }
}

/// Resize the currently focused window with the mouse
pub fn mouse_resize(wm: &mut WM<'_>) {
    if let Err(e) = wm.mouse_resize() {
        println!("error when resizing with mouse: {e:?}");
    }
}