makepad_render/
window.rs

1use crate::cx::*;
2
3#[derive(Clone)]
4pub struct Window {
5    pub window_id: Option<usize>,
6    pub create_inner_size: Option<Vec2>,
7    pub create_position: Option<Vec2>,
8    pub create_title: String,
9}
10
11impl Window {
12    pub fn new(_cx: &mut Cx) -> Self {
13        Self {
14            window_id: None,
15            create_inner_size: None,
16            create_position: None,
17            create_title: "Makepad".to_string()
18        }
19    }
20    
21    pub fn begin_window(&mut self, cx: &mut Cx) {
22        // if we are not at ground level for viewports,
23        if self.window_id.is_none() {
24            let new_window = CxWindow {
25                window_state: CxWindowState::Create {
26                    title: self.create_title.clone(),
27                    inner_size: if let Some(inner_size) = self.create_inner_size {
28                        inner_size
29                    }
30                    else {
31                        cx.get_default_window_size()
32                    },
33                    position: self.create_position,
34                },
35                ..Default::default()
36            };
37            let window_id;
38            if cx.windows_free.len() != 0 {
39                window_id = cx.windows_free.pop().unwrap();
40                cx.windows[window_id] = new_window
41            }
42            else {
43                window_id = cx.windows.len();
44                cx.windows.push(new_window);
45            }
46            self.window_id = Some(window_id);
47        }
48        let window_id = self.window_id.unwrap();
49        cx.windows[window_id].main_pass_id = None;
50        cx.window_stack.push(window_id);
51        
52    }
53    
54    pub fn get_inner_size(&mut self, cx: &mut Cx) -> Vec2 {
55        if let Some(window_id) = self.window_id {
56            return cx.windows[window_id].get_inner_size()
57        }
58        return Vec2::default();
59    }
60    
61    pub fn get_position(&mut self, cx: &mut Cx) -> Option<Vec2> {
62        if let Some(window_id) = self.window_id {
63            return cx.windows[window_id].get_position()
64        }
65        return None
66    }
67    
68        
69    pub fn set_position(&mut self, cx: &mut Cx, pos:Vec2) {
70        if let Some(window_id) = self.window_id {
71            return cx.windows[window_id].window_set_position = Some(pos);
72        }
73    }
74    
75    pub fn handle_window(&mut self, _cx: &mut Cx, _event: &mut Event) -> bool {
76        false
77    }
78    
79    pub fn redraw_window_area(&mut self, cx: &mut Cx) {
80        if let Some(window_id) = self.window_id {
81            if let Some(pass_id) = cx.windows[window_id].main_pass_id {
82                cx.redraw_pass_and_sub_passes(pass_id);
83            }
84        }
85    }
86    
87    pub fn end_window(&mut self, cx: &mut Cx) -> Area {
88        cx.window_stack.pop();
89        Area::Empty
90    }
91    
92    pub fn minimize_window(&mut self, cx: &mut Cx) {
93        if let Some(window_id) = self.window_id {
94            cx.windows[window_id].window_command = CxWindowCmd::Minimize;
95        }
96    }
97    
98    pub fn maximize_window(&mut self, cx: &mut Cx) {
99        if let Some(window_id) = self.window_id {
100            cx.windows[window_id].window_command = CxWindowCmd::Maximize;
101        }
102    }
103    
104    pub fn is_fullscreen(&mut self, cx: &mut Cx) -> bool {
105        if let Some(window_id) = self.window_id {
106            cx.windows[window_id].window_geom.is_fullscreen
107        }
108        else {
109            false
110        }
111    }
112    
113    pub fn vr_is_presenting(&mut self, cx: &mut Cx) -> bool {
114        if let Some(window_id) = self.window_id {
115            cx.windows[window_id].window_geom.vr_is_presenting
116        }
117        else {
118            false
119        }
120    }
121    
122    pub fn vr_start_presenting(&mut self, cx: &mut Cx){
123        if let Some(window_id) = self.window_id {
124            cx.windows[window_id].window_command = CxWindowCmd::VrStartPresenting;
125        }
126    }
127    
128    pub fn vr_stop_presenting(&mut self, cx: &mut Cx){
129        if let Some(window_id) = self.window_id {
130            cx.windows[window_id].window_command = CxWindowCmd::VrStopPresenting;
131        }
132    }
133    
134    pub fn is_topmost(&mut self, cx: &mut Cx) -> bool {
135        if let Some(window_id) = self.window_id {
136            cx.windows[window_id].window_geom.is_topmost
137        }
138        else {
139            false
140        }
141    }
142    
143    pub fn set_topmost(&mut self, cx: &mut Cx, topmost: bool) {
144        if let Some(window_id) = self.window_id {
145            cx.windows[window_id].window_topmost = Some(topmost);
146        }
147    }
148    
149    pub fn restore_window(&mut self, cx: &mut Cx) {
150        if let Some(window_id) = self.window_id {
151            cx.windows[window_id].window_command = CxWindowCmd::Restore;
152        }
153    }
154    
155    pub fn close_window(&mut self, cx: &mut Cx) {
156        if let Some(window_id) = self.window_id {
157            cx.windows[window_id].window_state = CxWindowState::Close;
158        }
159    }
160}
161
162#[derive(Clone, Debug, Default, PartialEq)]
163pub struct WindowGeom {
164    pub dpi_factor: f32,
165    pub vr_is_presenting: bool,
166    pub is_fullscreen: bool,
167    pub is_topmost: bool,
168    pub position: Vec2,
169    pub inner_size: Vec2,
170    pub outer_size: Vec2,
171}
172
173#[derive(Clone)]
174pub enum CxWindowState {
175    Create {title: String, inner_size: Vec2, position: Option<Vec2>},
176    Created,
177    Close,
178    Closed
179}
180
181#[derive(Clone)]
182pub enum CxWindowCmd {
183    None,
184    Restore,
185    Maximize,
186    Minimize,
187    VrStartPresenting,
188    VrStopPresenting
189}
190
191impl Default for CxWindowCmd {
192    fn default() -> Self {CxWindowCmd::None}
193}
194
195impl Default for CxWindowState {
196    fn default() -> Self {CxWindowState::Closed}
197}
198
199#[derive(Clone, Default)]
200pub struct CxWindow {
201    pub window_state: CxWindowState,
202    pub window_command: CxWindowCmd,
203    pub window_set_position: Option<Vec2>,
204    pub window_topmost: Option<bool>,
205    pub window_geom: WindowGeom,
206    pub main_pass_id: Option<usize>,
207}
208
209impl CxWindow {
210    pub fn get_inner_size(&mut self) -> Vec2 {
211        match &self.window_state {
212            CxWindowState::Create {inner_size, ..} => *inner_size,
213            CxWindowState::Created => self.window_geom.inner_size,
214            _ => Vec2::default()
215        }
216    }
217    
218    pub fn get_position(&mut self) -> Option<Vec2> {
219        match &self.window_state {
220            CxWindowState::Create {position, ..} => *position,
221            CxWindowState::Created => Some(self.window_geom.position),
222            _ => None
223        }
224    }
225    
226    pub fn get_dpi_factor(&mut self) -> Option<f32> {
227        match &self.window_state {
228            CxWindowState::Created => Some(self.window_geom.dpi_factor),
229            _ => None
230        }
231    }
232}