aboutsummaryrefslogtreecommitdiff
path: root/src/safe_x11/window.rs
blob: 24b5b62e912e5899b96645acefd2843e8673de8b (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
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
use x11::xlib;
use x11::xlib::{
    Window,
    XWindowAttributes,
    XGetWindowAttributes,
    XDefaultScreenOfDisplay,
    XMoveWindow,

    XSetInputFocus,
    XSetWindowBorder,

    IsViewable, RevertToParent, CurrentTime,
};

use std::ptr;
use std::mem::uninitialized;
use std::cmp::{max};

use safe_x11::{screen_ratio,get_color};

/// Representation of [`xlib::Window`](../../x11/xlib/type.Window.html)
pub struct XWindow {
    display: *mut xlib::Display,
    /// xlib::Window that wraps this struct.
    pub inner: Window,
}

fn fixed_with_ratio(x: i32, ratio: f32) -> i32 {
    let fx = x as f32;
    let fixed = fx * ratio;
    fixed as i32
}

impl XWindow {
    /// Generate a reference to the window in certain display. This will return
    /// `None` if the window is the root window.
    pub fn new(d: *mut xlib::Display, w: Window) -> Option<XWindow> {
        if w != 0 {
            Some(XWindow {
                display: d,
                inner: w,
            })
        } else {
            None
        }
    }

    /// Set the border width To the window.
    pub fn set_border_width(&self, size: u32) {
        unsafe { xlib::XSetWindowBorderWidth(self.display, self.inner, size) };
    }

    pub fn set_border_color<T: Into<Vec<u8>>>(&self, color: T) {
        let c = get_color(self.display, color);
        unsafe { XSetWindowBorder(self.display, self.inner, c); }
    }

    /// Raises a window.
    pub fn raise(&self) {
        unsafe { xlib::XRaiseWindow(self.display, self.inner); }
    }

    /// Register input events on some [`xlib::Window`](../../x11/xlib/type.Window.html)
    ///
    /// # Example
    ///
    /// if we want to handle when a pointer enter into a window rectangle, we
    /// should do something like:
    ///
    /// ```
    /// use x11::xlib;
    ///
    /// let display = open_display("").unwrap();
    /// // Raw window
    /// let window = 0xc00022;
    ///
    /// window.select_input(xlib::EnterWindowMask);
    /// ```
    pub fn select_input(&self, mask: i64) {
        unsafe { xlib::XSelectInput(self.display, self.inner, mask); }
    }

    /// Returns the window attributes from certain window.
    pub fn attributes(&self) -> XWindowAttributes {
        unsafe {
            let mut attrptr: XWindowAttributes = uninitialized();
            XGetWindowAttributes(self.display, self.inner, &mut attrptr);
            attrptr
        }
    }

    /// Moves the window given an offset from where it is.
    pub fn move_offset(&self, xoffset: i32, yoffset: i32) {
        unsafe {
            let mut attributes: XWindowAttributes = uninitialized();
            XGetWindowAttributes(self.display, self.inner, &mut attributes);
            XMoveWindow(self.display, self.inner,
                        attributes.x + xoffset,
                        attributes.y + yoffset);
        }
    }

    /// Moves the window to a particular coord in the screen.
    pub fn move_to(&self, x: i32, y: i32) -> Result<(), &str> {
        let screen = unsafe {
            let s = XDefaultScreenOfDisplay(self.display);
            ptr::read(s)
        };
        
        if 0 > x && x <= screen.width || 0 > y && y <= screen.height {
            Err("Cannot move the window outside the screen!")
        } else {
            unsafe { XMoveWindow(self.display, self.inner, x, y) };
            Ok(())
        }
    }
    
    /// Resizes the window a fixed amount within the height and width.
    pub fn resize(&self, w: i32, h: i32) {
        let ratio = screen_ratio(self.display);
        let attrs = self.attributes();
        unsafe {
            let ww: u32 = max(1, (attrs.width + fixed_with_ratio(w, ratio)) as u32);
            let wh: u32 = max(1, (attrs.height + fixed_with_ratio(h, ratio)) as u32);
            xlib::XResizeWindow(self.display, self.inner,
                                ww, wh);
        }
    }

    /// Raise the focus of the window and set a border.
    pub fn focus(&self) {
        let attrs = self.attributes();
        if attrs.map_state == IsViewable {
            unsafe {
                XSetInputFocus(self.display, self.inner,
                               RevertToParent, CurrentTime)
            };
        }
        self.raise();
        self.set_border_width(1);
        self.set_border_color("red");
    }

    /// Give a black border.
    pub fn unfocus(&self) {
        self.set_border_width(1);
        self.set_border_color("black");
    }
}