summaryrefslogtreecommitdiff
path: root/src/helpers.rs
blob: 819719bf769b4c5eab035db738a22823356c97b1 (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
99
100
101
102
103
104
105
//! Helpers for writing configuration

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

/// Syntax sugar for creating [`Keybind`](`crate::keys::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`](`crate::buttons::ButtonBind`)s. See also: [`crate::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.
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);
    }
}

/// Send the kill signal to the currently focused client
pub fn kill_client(wm: &mut WM<'_>) {
    if let Some(pos) = wm.clients.focused_pos() {
        wm.clients.kill_client(&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:?}");
    }
}