summaryrefslogtreecommitdiff
path: root/src/helpers.rs
blob: cd9fd1837eb6a80f1c354d4d14504e8f78cf90c7 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! 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);
}

/// Shift the currently focused window down the stack
pub fn shift_down(wm: &mut WM<'_>) {
    wm.clients.shift_focused_client(&wm.conn, true);
}

/// Shift the currently focused window up the stack
pub fn shift_up(wm: &mut WM<'_>) {
    wm.clients.shift_focused_client(&wm.conn, false);
}

/// Shift the currently focused window to the top of the stack
pub fn shift_top(wm: &mut WM<'_>) {
    wm.clients.shift_focused_to_top(&wm.conn);
}

/// 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:?}");
    }
}