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
use crate::config::Config;
use crate::display_servers::DisplayServer;
use crate::models::Manager;
use crate::models::Mode;
use crate::models::WindowHandle;
use crate::utils::xkeysym_lookup::Button;
use crate::utils::xkeysym_lookup::ModMask;
use crate::{display_action::DisplayAction, models::FocusBehaviour};
use x11_dl::xlib;

impl<C: Config, SERVER: DisplayServer> Manager<C, SERVER> {
    pub fn mouse_combo_handler(
        &mut self,
        modmask: ModMask,
        button: Button,
        handle: WindowHandle,
        modifier: ModMask,
    ) -> bool {
        //look through the config and build a command if its defined in the config
        let act = build_action(self, modmask, button, handle, modifier);
        if let Some(act) = act {
            //save off the info about position of the window when we started to move/resize
            self.state
                .windows
                .iter_mut()
                .filter(|w| w.handle == handle)
                .for_each(|w| {
                    if w.floating() {
                        let offset = w.get_floating_offsets().unwrap_or_default();
                        w.start_loc = Some(offset);
                    } else {
                        let container = w.container_size.unwrap_or_default();
                        let normal = w.normal;
                        let floating = normal - container;
                        w.set_floating_offsets(Some(floating));
                        w.start_loc = Some(floating);
                        w.set_floating(true);
                    }
                });
            self.move_to_top(&handle);
            self.state.actions.push_back(act);
            return false;
        }

        true
    }
}

fn build_action<C: Config, SERVER: DisplayServer>(
    manager: &mut Manager<C, SERVER>,
    mod_mask: ModMask,
    button: Button,
    window: WindowHandle,
    modifier: ModMask,
) -> Option<DisplayAction> {
    match button {
        xlib::Button1 => {
            if mod_mask == modifier || mod_mask == (modifier | xlib::ShiftMask) {
                let _ = manager
                    .state
                    .windows
                    .iter()
                    .find(|w| w.handle == window && w.can_move())?;
                manager.state.mode = Mode::MovingWindow(window);
                return Some(DisplayAction::StartMovingWindow(window));
            }
            if manager.state.focus_manager.behaviour == FocusBehaviour::ClickTo {
                manager.focus_window(&window);
            }
            None
        }
        xlib::Button3 => {
            let _ = manager
                .state
                .windows
                .iter()
                .find(|w| w.handle == window && w.can_resize())?;
            manager.state.mode = Mode::ResizingWindow(window);
            Some(DisplayAction::StartResizingWindow(window))
        }
        _ => None,
    }
}