pebble_rust/pebble/
window.rs

1/*
2 * This file is part of pebble-rust.
3 * Copyright (c) 2019 RoccoDev
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19use crate::pebble::internal::{types, functions::interface};
20use crate::pebble::types::GColor;
21use crate::pebble::WindowPtr;
22use crate::pebble::layer::Layer;
23
24pub struct Window {
25    internal: *mut types::Window
26}
27
28#[derive(Copy, Clone)]
29pub struct WindowHandlers {
30    pub load: extern fn(WindowPtr),
31    pub unload: extern fn(WindowPtr),
32    pub appear: extern fn(WindowPtr),
33    pub disappear: extern fn(WindowPtr)
34}
35
36impl Window {
37    pub fn new() -> Window {
38        Window {
39            internal: interface::window_create()
40        }
41    }
42
43    pub fn from_raw(ptr: WindowPtr) -> Window {
44        Window {
45            internal: ptr
46        }
47    }
48
49    pub fn push(&self, animate: bool) {
50        interface::window_stack_push(self.internal, animate);
51    }
52
53    pub fn set_handlers(&self, handlers: WindowHandlers) {
54        let WindowHandlers {load, unload,
55            appear, disappear} = handlers;
56        let converted = types::WindowHandlers {
57            load, unload, appear, disappear
58        };
59
60        interface::window_set_window_handlers(self.internal, converted);
61    }
62
63    pub fn set_background_color(&self, color: GColor) {
64        interface::window_set_background_color(self.internal, color);
65    }
66
67    pub fn get_root_layer(&self) -> Layer {
68        let layer_ptr = interface::window_get_root_layer(self.internal);
69        Layer::from_raw(layer_ptr)
70    }
71
72    pub fn clean_exit(&self) {
73        interface::window_destroy(self.internal);
74    }
75}