summaryrefslogtreecommitdiff
path: root/src/clients/monitors.rs
blob: ba597721feb28f7663ae65101f829a57a37d3316 (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
use xcb::xinerama::ScreenInfo;

use super::{Client, Tag};

/// Info stored for each monitor
#[derive(Debug)]
pub struct MonitorInfo {
    /// How clients should be filtered by tag
    pub focused_tag: TagFocus,

    /// The previously focused tag, for going back-and-forth
    pub last_focused_tag: TagFocus,

    /// The monitor's geometry
    pub screen_info: MonitorGeometry,
}

/// How clients are currently being filtered by tag
#[derive(Debug, Clone, Copy)]
pub enum TagFocus {
    /// Only show clients with the given tag
    Tag(Tag),

    /// Show all clients
    All,
}
impl TagFocus {
    /// Check if a client with the given tag should be displayed when using this filter
    pub const fn matches(&self, tag: Tag) -> bool {
        match self {
            Self::Tag(x) => *x == tag,
            Self::All => true,
        }
    }

    /// Get the tag that new clients should be assigned when using this filter
    pub const fn create_tag(&self) -> Tag {
        match self {
            Self::Tag(x) => *x,
            Self::All => 1,
        }
    }
}

impl MonitorInfo {
    pub fn iter_visible_tiling<'a>(
        &'a self,
        i: impl IntoIterator<Item = &'a mut Client>,
    ) -> impl Iterator<Item = &'a mut Client> {
        i.into_iter()
            .filter(|c| self.focused_tag.matches(c.tag))
            .filter(|c| c.tiled())
    }
}

impl Default for MonitorInfo {
    fn default() -> Self {
        Self {
            screen_info: MonitorGeometry {
                x_org: 0,
                y_org: 0,
                width: 0,
                height: 0,
            },
            focused_tag: TagFocus::Tag(1),
            last_focused_tag: TagFocus::Tag(1),
        }
    }
}

/// Info on the monitor's geometry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MonitorGeometry {
    pub x_org: i16,
    pub y_org: i16,
    pub width: u16,
    pub height: u16,
}
impl MonitorGeometry {
    #[allow(clippy::cast_possible_wrap)]
    pub(crate) fn contains(self, x: i16, y: i16) -> bool {
        (self.x_org..self.x_org + self.width as i16).contains(&x)
            && (self.y_org..self.y_org + self.height as i16).contains(&y)
    }
}

impl From<ScreenInfo> for MonitorGeometry {
    fn from(value: ScreenInfo) -> Self {
        Self {
            x_org: value.x_org,
            y_org: value.y_org,
            width: value.width,
            height: value.height,
        }
    }
}