libwmctl/model/
position.rs

1use crate::WmCtlError;
2use std::{convert, fmt};
3
4/// Position provides a number of pre-defined positions on the screen to quickly and easily
5/// move the window to taking into account borders and taskbars automatically.
6#[derive(Debug, Clone, PartialEq)]
7pub enum Position {
8    Center,
9    Left,
10    Right,
11    Top,
12    Bottom,
13    TopLeft,
14    TopRight,
15    BottomLeft,
16    BottomRight,
17    LeftCenter,
18    RightCenter,
19    TopCenter,
20    BottomCenter,
21    Static(i32, i32),
22}
23
24// Implement format! support
25impl fmt::Display for Position {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        match self {
28            _ => write!(f, "{}", format!("{:?}", self).to_lowercase()),
29        }
30    }
31}
32
33// Convert from &str to Postiion
34impl convert::TryFrom<&str> for Position {
35    type Error = WmCtlError;
36
37    fn try_from(val: &str) -> Result<Self, Self::Error> {
38        match val.to_lowercase().as_ref() {
39            "center" => Ok(Position::Center),
40            "left" => Ok(Position::Left),
41            "right" => Ok(Position::Right),
42            "top" => Ok(Position::Top),
43            "bottom" => Ok(Position::Bottom),
44            "top-left" => Ok(Position::TopLeft),
45            "top-right" => Ok(Position::TopRight),
46            "bottom-left" => Ok(Position::BottomLeft),
47            "bottom-right" => Ok(Position::BottomRight),
48            "left-center" => Ok(Position::LeftCenter),
49            "right-center" => Ok(Position::RightCenter),
50            "top-center" => Ok(Position::TopCenter),
51            "bottom-center" => Ok(Position::BottomCenter),
52            _ => Err(WmCtlError::InvalidWinPosition(val.to_string()).into()),
53        }
54    }
55}
56
57// Convert from String to Postiion
58impl convert::TryFrom<String> for Position {
59    type Error = WmCtlError;
60
61    fn try_from(val: String) -> Result<Self, Self::Error> {
62        Position::try_from(val.as_str())
63    }
64}