logo
  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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! Simple data types and enums
use crate::{
    core::xconnection::{Atom, Xid},
    Result,
};

/// Output of a Layout function: the new position a window should take
pub type ResizeAction = (Xid, Option<Region>);

/// An X window ID
pub type WinId = u32;

/// A window type to be specified when creating a new window in the X server
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum WinType {
    /// A simple hidden stub window for facilitating other API calls
    CheckWin,
    /// A window that receives input only (not queryable)
    InputOnly,
    /// A regular window. The [Atom] passed should be a
    /// valid _NET_WM_WINDOW_TYPE (this is not enforced)
    InputOutput(Atom),
}

/// A relative position along the horizontal and vertical axes
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum RelativePosition {
    /// Left of the current position
    Left,
    /// Right of the current position
    Right,
    /// Above the current position
    Above,
    /// Below the current position
    Below,
}

/// An x,y coordinate pair
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Point {
    /// An absolute x coordinate relative to the root window
    pub x: u32,
    /// An absolute y coordinate relative to the root window
    pub y: u32,
}

impl Point {
    /// Create a new Point.
    pub fn new(x: u32, y: u32) -> Self {
        Self { x, y }
    }
}

impl Default for Point {
    fn default() -> Self {
        Self::new(0, 0)
    }
}

/* Argument enums */

/// Increment / decrement a value
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Change {
    /// increase the value
    More,
    /// decrease the value, possibly clamping
    Less,
}

/// X window border kind
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Border {
    /// window is urgent
    Urgent,
    /// window currently has focus
    Focused,
    /// window does not have focus
    Unfocused,
}

/// An X window / screen position: top left corner + extent
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub struct Region {
    /// The x-coordinate of the top left corner of this region
    pub x: u32,
    /// The y-coordinate of the top left corner of this region
    pub y: u32,
    /// The width of this region
    pub w: u32,
    /// The height of this region
    pub h: u32,
}

impl Default for Region {
    fn default() -> Self {
        Self::new(0, 0, 0, 0)
    }
}

impl Region {
    /// Create a new Region.
    pub fn new(x: u32, y: u32, w: u32, h: u32) -> Region {
        Region { x, y, w, h }
    }

    /// Destructure this Region into its component values (x, y, w, h).
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::Region;
    ///
    /// // In practice, this will be a region your code is receiving: not one you create
    /// let r = Region::new(10, 20, 30, 40);
    ///
    /// assert_eq!(r.values(), (10, 20, 30, 40));
    /// ```
    pub fn values(&self) -> (u32, u32, u32, u32) {
        (self.x, self.y, self.w, self.h)
    }

    /// Create a new [Region] with width equal to `factor` x `self.w`
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::Region;
    ///
    /// let r = Region::new(10, 20, 30, 40);
    ///
    /// assert_eq!(r.scale_w(1.5), Region::new(10, 20, 45, 40));
    /// assert_eq!(r.scale_w(0.5), Region::new(10, 20, 15, 40));
    /// ```
    pub fn scale_w(&self, factor: f64) -> Self {
        Self {
            w: (self.w as f64 * factor).floor() as u32,
            ..*self
        }
    }

    /// Create a new [Region] with height equal to `factor` x `self.h`
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::Region;
    ///
    /// let r = Region::new(10, 20, 30, 40);
    ///
    /// assert_eq!(r.scale_h(1.5), Region::new(10, 20, 30, 60));
    /// assert_eq!(r.scale_h(0.5), Region::new(10, 20, 30, 20));
    /// ```
    pub fn scale_h(&self, factor: f64) -> Self {
        Self {
            h: (self.h as f64 * factor).floor() as u32,
            ..*self
        }
    }

    /// Check whether this Region contains `other` as a sub-Region
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::Region;
    ///
    /// let r1 = Region::new(10, 10, 50, 50);
    /// let r2 = Region::new(0, 0, 100, 100);
    ///
    /// assert!(r2.contains(&r1));
    /// assert!(!r1.contains(&r2));
    /// ```
    pub fn contains(&self, other: &Region) -> bool {
        match other {
            Region { x, .. } if *x < self.x => false,
            Region { x, w, .. } if (*x + *w) > (self.x + self.w) => false,
            Region { y, .. } if *y < self.y => false,
            Region { y, h, .. } if (*y + *h) > (self.y + self.h) => false,
            _ => true,
        }
    }

    /// Check whether this Region contains `p`
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::{Point, Region};
    ///
    /// let r1 = Region::new(10, 10, 50, 50);
    ///
    /// assert!(r1.contains_point(&Point::new(30, 20)));
    /// assert!(!r1.contains_point(&Point::new(0, 0)));
    /// ```
    pub fn contains_point(&self, p: &Point) -> bool {
        (self.x..(self.x + self.w)).contains(&p.x) && (self.y..(self.y + self.h)).contains(&p.y)
    }

    /// Center this region inside of `enclosing`.
    ///
    /// # Errors
    /// Fails if this Region can not fit inside of `enclosing`
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::Region;
    ///
    /// let r1 = Region::new(10, 10, 50, 60);
    /// let r2 = Region::new(0, 0, 100, 100);
    ///
    /// let centered = r1.centered_in(&r2);
    /// assert!(centered.is_ok());
    /// assert_eq!(centered.unwrap(), Region::new(25, 20, 50, 60));
    ///
    /// let too_big = r2.centered_in(&r1);
    /// assert!(too_big.is_err());
    /// ```
    pub fn centered_in(&self, enclosing: &Region) -> Result<Self> {
        if !enclosing.contains(self) {
            return Err(perror!(
                "enclosing does not conatain self: {:?} {:?}",
                enclosing,
                self
            ));
        }

        Ok(Self {
            x: enclosing.x + ((enclosing.w - self.w) / 2),
            y: enclosing.y + ((enclosing.h - self.h) / 2),
            ..*self
        })
    }

    /// Split this `Region` into evenly sized rows.
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::Region;
    ///
    /// let r = Region::new(0, 0, 100, 100);
    ///
    /// let regions = r.as_rows(2);
    ///
    /// assert_eq!(regions.len(), 2);
    /// assert_eq!(regions[0], Region::new(0, 0, 100, 50));
    /// assert_eq!(regions[1], Region::new(0, 50, 100, 50));
    /// ```
    pub fn as_rows(&self, n_rows: u32) -> Vec<Region> {
        if n_rows <= 1 {
            return vec![*self];
        }
        let h = self.h / n_rows as u32;
        (0..n_rows)
            .map(|n| Region::new(self.x, (self.y + n as u32 * h) as u32, self.w, h))
            .collect()
    }

    /// Split this `Region` into evenly sized columns.
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::Region;
    ///
    /// let r = Region::new(0, 0, 100, 100);
    ///
    /// let regions = r.as_columns(2);
    ///
    /// assert_eq!(regions.len(), 2);
    /// assert_eq!(regions[0], Region::new(0, 0, 50, 100));
    /// assert_eq!(regions[1], Region::new(50, 0, 50, 100));
    /// ```
    pub fn as_columns(&self, n_columns: u32) -> Vec<Region> {
        if n_columns <= 1 {
            return vec![*self];
        }
        let w = self.w / n_columns as u32;
        (0..n_columns)
            .map(|n| Region::new((self.x + n as u32 * w) as u32, self.y, w, self.h))
            .collect()
    }

    /// Divides this region into two columns where the first has the given width.
    ///
    /// # Errors
    /// Fails if the requested split point is not contained within `self`
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::Region;
    ///
    /// let r = Region::new(10, 10, 50, 60);
    /// let (r1, r2) = r.split_at_width(30).unwrap();
    ///
    /// assert_eq!(r1, Region::new(10, 10, 30, 60));
    /// assert_eq!(r2, Region::new(40, 10, 20, 60));
    ///
    /// assert!(r.split_at_width(100).is_err());
    /// ```
    pub fn split_at_width(&self, new_width: u32) -> Result<(Self, Self)> {
        if new_width > self.w {
            Err(perror!(
                "Region split is out of range: {} >= {}",
                new_width,
                self.w
            ))
        } else {
            Ok((
                Self {
                    w: new_width,
                    ..*self
                },
                Self {
                    x: self.x + new_width,
                    w: self.w - new_width,
                    ..*self
                },
            ))
        }
    }

    /// Divides this region into two rows where the first has the given height.
    ///
    /// # Errors
    /// Fails if the requested split point is not contained within `self`
    ///
    /// # Examples
    ///
    /// ```
    /// use penrose::core::data_types::Region;
    ///
    /// let r = Region::new(10, 10, 50, 60);
    /// let (r1, r2) = r.split_at_height(40).unwrap();
    ///
    /// assert_eq!(r1, Region::new(10, 10, 50, 40));
    /// assert_eq!(r2, Region::new(10, 50, 50, 20));
    ///
    /// assert!(r.split_at_height(100).is_err());
    /// ```
    pub fn split_at_height(&self, new_height: u32) -> Result<(Self, Self)> {
        if new_height > self.h {
            Err(perror!(
                "Region split is out of range: {} >= {}",
                new_height,
                self.h
            ))
        } else {
            Ok((
                Self {
                    h: new_height,
                    ..*self
                },
                Self {
                    y: self.y + new_height,
                    h: self.h - new_height,
                    ..*self
                },
            ))
        }
    }
}