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
use crate::cx::*;

#[derive(Clone)]
pub struct Window {
    pub window_id: Option<usize>,
    pub create_inner_size: Option<Vec2>,
    pub create_position: Option<Vec2>,
    pub create_title: String,
}

impl Window {
    pub fn new(_cx: &mut Cx) -> Self {
        Self {
            window_id: None,
            create_inner_size: None,
            create_position: None,
            create_title: "Makepad".to_string()
        }
    }
    
    pub fn begin_window(&mut self, cx: &mut Cx) {
        // if we are not at ground level for viewports,
        if self.window_id.is_none() {
            let new_window = CxWindow {
                window_state: CxWindowState::Create {
                    title: self.create_title.clone(),
                    inner_size: if let Some(inner_size) = self.create_inner_size {
                        inner_size
                    }
                    else {
                        cx.get_default_window_size()
                    },
                    position: self.create_position,
                },
                ..Default::default()
            };
            let window_id;
            if cx.windows_free.len() != 0 {
                window_id = cx.windows_free.pop().unwrap();
                cx.windows[window_id] = new_window
            }
            else {
                window_id = cx.windows.len();
                cx.windows.push(new_window);
            }
            self.window_id = Some(window_id);
        }
        let window_id = self.window_id.unwrap();
        cx.windows[window_id].main_pass_id = None;
        cx.window_stack.push(window_id);
        
    }
    
    pub fn get_inner_size(&mut self, cx: &mut Cx) -> Vec2 {
        if let Some(window_id) = self.window_id {
            return cx.windows[window_id].get_inner_size()
        }
        return Vec2::default();
    }
    
    pub fn get_position(&mut self, cx: &mut Cx) -> Option<Vec2> {
        if let Some(window_id) = self.window_id {
            return cx.windows[window_id].get_position()
        }
        return None
    }
    
        
    pub fn set_position(&mut self, cx: &mut Cx, pos:Vec2) {
        if let Some(window_id) = self.window_id {
            return cx.windows[window_id].window_set_position = Some(pos);
        }
    }
    
    pub fn handle_window(&mut self, _cx: &mut Cx, _event: &mut Event) -> bool {
        false
    }
    
    pub fn redraw_window_area(&mut self, cx: &mut Cx) {
        if let Some(window_id) = self.window_id {
            if let Some(pass_id) = cx.windows[window_id].main_pass_id {
                cx.redraw_pass_and_sub_passes(pass_id);
            }
        }
    }
    
    pub fn end_window(&mut self, cx: &mut Cx) -> Area {
        cx.window_stack.pop();
        Area::Empty
    }
    
    pub fn minimize_window(&mut self, cx: &mut Cx) {
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_command = CxWindowCmd::Minimize;
        }
    }
    
    pub fn maximize_window(&mut self, cx: &mut Cx) {
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_command = CxWindowCmd::Maximize;
        }
    }
    
    pub fn is_fullscreen(&mut self, cx: &mut Cx) -> bool {
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_geom.is_fullscreen
        }
        else {
            false
        }
    }
    
    pub fn vr_is_presenting(&mut self, cx: &mut Cx) -> bool {
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_geom.vr_is_presenting
        }
        else {
            false
        }
    }
    
    pub fn vr_start_presenting(&mut self, cx: &mut Cx){
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_command = CxWindowCmd::VrStartPresenting;
        }
    }
    
    pub fn vr_stop_presenting(&mut self, cx: &mut Cx){
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_command = CxWindowCmd::VrStopPresenting;
        }
    }
    
    pub fn is_topmost(&mut self, cx: &mut Cx) -> bool {
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_geom.is_topmost
        }
        else {
            false
        }
    }
    
    pub fn set_topmost(&mut self, cx: &mut Cx, topmost: bool) {
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_topmost = Some(topmost);
        }
    }
    
    pub fn restore_window(&mut self, cx: &mut Cx) {
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_command = CxWindowCmd::Restore;
        }
    }
    
    pub fn close_window(&mut self, cx: &mut Cx) {
        if let Some(window_id) = self.window_id {
            cx.windows[window_id].window_state = CxWindowState::Close;
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct WindowGeom {
    pub dpi_factor: f32,
    pub vr_is_presenting: bool,
    pub is_fullscreen: bool,
    pub is_topmost: bool,
    pub position: Vec2,
    pub inner_size: Vec2,
    pub outer_size: Vec2,
}

#[derive(Clone)]
pub enum CxWindowState {
    Create {title: String, inner_size: Vec2, position: Option<Vec2>},
    Created,
    Close,
    Closed
}

#[derive(Clone)]
pub enum CxWindowCmd {
    None,
    Restore,
    Maximize,
    Minimize,
    VrStartPresenting,
    VrStopPresenting
}

impl Default for CxWindowCmd {
    fn default() -> Self {CxWindowCmd::None}
}

impl Default for CxWindowState {
    fn default() -> Self {CxWindowState::Closed}
}

#[derive(Clone, Default)]
pub struct CxWindow {
    pub window_state: CxWindowState,
    pub window_command: CxWindowCmd,
    pub window_set_position: Option<Vec2>,
    pub window_topmost: Option<bool>,
    pub window_geom: WindowGeom,
    pub main_pass_id: Option<usize>,
}

impl CxWindow {
    pub fn get_inner_size(&mut self) -> Vec2 {
        match &self.window_state {
            CxWindowState::Create {inner_size, ..} => *inner_size,
            CxWindowState::Created => self.window_geom.inner_size,
            _ => Vec2::default()
        }
    }
    
    pub fn get_position(&mut self) -> Option<Vec2> {
        match &self.window_state {
            CxWindowState::Create {position, ..} => *position,
            CxWindowState::Created => Some(self.window_geom.position),
            _ => None
        }
    }
    
    pub fn get_dpi_factor(&mut self) -> Option<f32> {
        match &self.window_state {
            CxWindowState::Created => Some(self.window_geom.dpi_factor),
            _ => None
        }
    }
}