windowing_qt 0.17.5

Implementation of windowing_api using QT Framework
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use crate::platform::qt::qt_wrapper::{
    FFIElementState, FFIEvent, FFIKeyModifiers, FFIMouseButton, FFIPosition, FFIScrollDelta,
    QString, QWindow,
};
use crate::{FUISystemError, Icon};
use std::ffi::{CStr, c_char, c_void};
use windowing_api::{
    CursorShape, Edge, ElementState, Event, KeyModifiers, Keycode, MouseButton, Position,
    ScrollDelta, TranslucentEffect, WindowFrameType,
};

///
/// Represents a window in the underlying windowing system.
///
pub struct Window {
    qwindow: QWindow,
}

impl Window {
    ///
    /// Creates a window as a child of the given parent window.
    ///
    pub fn new(parent: Option<&mut Window>) -> Result<Self, FUISystemError> {
        let qwindow = QWindow::new(parent.map(|p| &mut p.qwindow))?;
        Ok(Self { qwindow })
    }

    ///
    /// Sets the window's title.
    ///
    pub fn set_title(&mut self, title: &str) -> Result<(), FUISystemError> {
        let title = QString::from_str(title)?;
        self.qwindow.set_title(&title);
        Ok(())
    }

    ///
    /// Sets the window's icon.
    ///
    pub fn set_icon(&mut self, icon: &Icon) -> Result<(), FUISystemError> {
        self.qwindow.set_icon(&icon.qicon);
        Ok(())
    }

    ///
    /// Sets if window should stay on top.
    ///
    pub fn set_stay_on_top(&mut self, stay_on_top: bool) -> Result<(), FUISystemError> {
        self.qwindow.set_stay_on_top(stay_on_top);
        Ok(())
    }

    ///
    /// Sets if window should be transparent for input.
    ///
    pub fn set_transparent_for_input(
        &mut self,
        transparent_for_input: bool,
    ) -> Result<(), FUISystemError> {
        self.qwindow
            .set_transparent_for_input(transparent_for_input);
        Ok(())
    }

    ///
    /// Sets window frame type.
    ///
    pub fn set_frame_type(&mut self, frame_type: WindowFrameType) -> Result<(), FUISystemError> {
        let frame_type = match frame_type {
            WindowFrameType::Frameless => 0,
            WindowFrameType::Normal => 1,
        };
        self.qwindow.set_frame_type(frame_type);
        Ok(())
    }

    ///
    /// Makes the window popup window.
    ///
    pub fn set_popup_window(&mut self) {
        self.qwindow.set_popup_window();
    }

    ///
    /// Sets if window should have an alpha channel for translucent regions.
    ///
    pub fn set_translucent_background(
        &mut self,
        translucent_effect: TranslucentEffect,
    ) -> Result<(), FUISystemError> {
        let translucent_effect = match translucent_effect {
            TranslucentEffect::None => 0,
            TranslucentEffect::Transparent => 1,
            TranslucentEffect::Blur => 2,
        };
        self.qwindow.set_translucent_background(translucent_effect);
        Ok(())
    }

    ///
    /// Sets the visibility of the window.
    ///
    pub fn set_visible(&mut self, visible: bool) -> Result<(), FUISystemError> {
        self.qwindow.set_visible(visible);
        Ok(())
    }

    ///
    /// Gets window position (excluding it's window frame).
    ///
    pub fn get_position(&mut self) -> (i32, i32) {
        self.qwindow.get_position()
    }

    ///
    /// Sets window position (excluding it's window frame).
    ///
    pub fn set_position(&mut self, x: i32, y: i32) {
        self.qwindow.set_position(x, y)
    }

    ///
    /// Gets window position (including it's window frame).
    ///
    pub fn get_frame_position(&mut self) -> (i32, i32) {
        self.qwindow.get_frame_position()
    }

    ///
    /// Sets window position (including it's window frame).
    ///
    pub fn set_frame_position(&mut self, x: i32, y: i32) {
        self.qwindow.set_frame_position(x, y)
    }

    ///
    /// Get window width, excluding any window frame.
    ///
    pub fn get_width(&mut self) -> i32 {
        self.qwindow.get_width()
    }

    ///
    /// Get window height, excluding any window frame.
    ///
    pub fn get_height(&mut self) -> i32 {
        self.qwindow.get_height()
    }

    ///
    /// Resize window, excluding any window frame.
    ///
    pub fn resize(&mut self, width: i32, height: i32) {
        self.qwindow.resize(width, height);
    }

    ///
    /// Sets minimum window size, excluding any window frame.
    ///
    pub fn set_minimum_size(&mut self, width: i32, height: i32) {
        self.qwindow.set_minimum_size(width, height);
    }

    ///
    /// Sets the cursor shape for this window.
    ///
    pub fn set_cursor(&mut self, cursor_shape: CursorShape) {
        let cursor_shape = match cursor_shape {
            CursorShape::ArrowCursor => 0,
            CursorShape::UpArrowCursor => 1,
            CursorShape::CrossCursor => 2,
            CursorShape::WaitCursor => 3,
            CursorShape::IBeamCursor => 4,
            CursorShape::SizeVerCursor => 5,
            CursorShape::SizeHorCursor => 6,
            CursorShape::SizeBDiagCursor => 7,
            CursorShape::SizeFDiagCursor => 8,
            CursorShape::SizeAllCursor => 9,
            CursorShape::BlankCursor => 10,
            CursorShape::SplitVCursor => 11,
            CursorShape::SplitHCursor => 12,
            CursorShape::PointingHandCursor => 13,
            CursorShape::ForbiddenCursor => 14,
            CursorShape::OpenHandCursor => 17,
            CursorShape::ClosedHandCursor => 18,
            CursorShape::WhatsThisCursor => 15,
            CursorShape::BusyCursor => 16,
            CursorShape::DragMoveCursor => 20,
            CursorShape::DragCopyCursor => 19,
            CursorShape::DragLinkCursor => 21,
        };
        self.qwindow.set_cursor_shape(cursor_shape);
    }

    ///
    /// Start a system-specific move operation.
    /// Returns true if the operation was supported by the system.
    ///
    pub fn start_system_move(&mut self) -> bool {
        self.qwindow.start_system_move()
    }

    ///
    /// Start a system-specific resize operation.
    /// Returns true if the operation was supported by the system.
    ///
    pub fn start_system_resize(&mut self, edges: Edge) -> bool {
        self.qwindow.start_system_resize(edges.bits())
    }

    ///
    /// Marks the entire window as dirty and schedules a repaint.
    /// Subsequent calls to this function before the next paint event will get ignored.
    ///
    pub fn update(&mut self) {
        self.qwindow.update();
    }

    pub fn on_event<F: 'static + FnMut(Event) -> bool>(&mut self, mut callback: F) {
        self.qwindow.on_event(move |ffi_event: &FFIEvent| {
            if let Some(event) = convert_event(ffi_event) {
                callback(event)
            } else {
                false
            }
        });
    }

    ///
    /// OpenGL.
    ///
    /// Sets the callback that is called whenever the window contents needs to be repainted.
    /// The OpenGL context of the window is already made current.
    ///
    pub fn on_paint_gl<F: 'static + FnMut()>(&mut self, callback: F) {
        self.qwindow.on_paint_gl(callback);
    }

    pub fn get_opengl_proc_address(&self, proc_name: &str) -> Result<*mut c_void, FUISystemError> {
        let context = self.qwindow.get_context()?;
        context.get_proc_address(proc_name)
    }

    pub fn get_default_framebuffer_id(&self) -> u32 {
        self.qwindow.get_default_framebuffer_object()
    }
}

fn convert_event(ffi_event: &FFIEvent) -> Option<Event> {
    match ffi_event {
        FFIEvent::MouseEnter => Some(Event::MouseEnter),
        FFIEvent::MouseLeave => Some(Event::MouseLeave),
        FFIEvent::MouseButton { state, button } => Some(Event::MouseButton {
            state: convert_element_state(state),
            button: convert_mouse_button(button),
        }),
        FFIEvent::MouseMove { position } => Some(Event::MouseMove {
            position: convert_position(position),
        }),
        FFIEvent::ScrollWheel { delta } => Some(Event::ScrollWheel {
            delta: convert_delta(delta),
        }),
        FFIEvent::KeyEvent {
            state,
            is_repeat,
            keycode,
            modifiers,
            text,
        } => Some(Event::KeyEvent {
            state: convert_element_state(state),
            is_repeat: *is_repeat,
            keycode: convert_keycode(*keycode, modifiers),
            modifiers: convert_modifiers(*keycode, modifiers),
            text: convert_text(*text),
        }),
        FFIEvent::Resize { width, height } => Some(Event::Resize {
            width: *width,
            height: *height,
        }),
    }
}

fn convert_element_state(state: &FFIElementState) -> ElementState {
    match state {
        FFIElementState::Pressed => ElementState::Pressed,
        FFIElementState::Released => ElementState::Released,
    }
}

fn convert_mouse_button(button: &FFIMouseButton) -> MouseButton {
    match button {
        FFIMouseButton::Left => MouseButton::Left,
        FFIMouseButton::Right => MouseButton::Right,
        FFIMouseButton::Middle => MouseButton::Middle,
        FFIMouseButton::Other(code) => MouseButton::Other(*code),
    }
}

fn convert_position(position: &FFIPosition) -> Position {
    Position {
        x: position.x,
        y: position.y,
    }
}

fn convert_delta(delta: &FFIScrollDelta) -> ScrollDelta {
    match delta {
        FFIScrollDelta::LineDelta(x, y) => ScrollDelta::LineDelta(*x, *y),
        FFIScrollDelta::PixelDelta(x, y) => ScrollDelta::PixelDelta(*x, *y),
    }
}

fn convert_keycode(keycode: i32, _modifiers: &FFIKeyModifiers) -> Option<Keycode> {
    match keycode {
        // function row
        0x01000000 => Some(Keycode::Esc),
        0x01000030 => Some(Keycode::F1),
        0x01000031 => Some(Keycode::F2),
        0x01000032 => Some(Keycode::F3),
        0x01000033 => Some(Keycode::F4),
        0x01000034 => Some(Keycode::F5),
        0x01000035 => Some(Keycode::F6),
        0x01000036 => Some(Keycode::F7),
        0x01000037 => Some(Keycode::F8),
        0x01000038 => Some(Keycode::F9),
        0x01000039 => Some(Keycode::F10),
        0x0100003a => Some(Keycode::F11),
        0x0100003b => Some(Keycode::F12),
        0x0100003c => Some(Keycode::F13),
        0x0100003d => Some(Keycode::F14),
        0x0100003e => Some(Keycode::F15),
        0x0100003f => Some(Keycode::F16),
        0x01000040 => Some(Keycode::F17),
        0x01000041 => Some(Keycode::F18),
        0x01000042 => Some(Keycode::F19),
        0x01000043 => Some(Keycode::F20),
        0x01000009 => Some(Keycode::PrintScreen),
        0x01000026 => Some(Keycode::ScrollLock),
        0x01000008 => Some(Keycode::Pause),

        // navigation
        0x01000006 => Some(Keycode::Insert),
        0x01000007 => Some(Keycode::Delete),
        0x01000010 => Some(Keycode::Home),
        0x01000011 => Some(Keycode::End),
        0x01000016 => Some(Keycode::PageUp),
        0x01000017 => Some(Keycode::PageDown),

        // arrows
        0x01000012 => Some(Keycode::Left),
        0x01000014 => Some(Keycode::Right),
        0x01000013 => Some(Keycode::Up),
        0x01000015 => Some(Keycode::Down),

        // modifiers
        0x01000020 => Some(Keycode::Shift),
        0x01000021 => Some(Keycode::Ctrl),
        0x01000022 => Some(Keycode::Win),
        0x01000023 => Some(Keycode::Alt),
        0x01001103 => Some(Keycode::AltGr),

        // special
        0x01000055 => Some(Keycode::Menu),
        0x01000003 => Some(Keycode::Backspace),
        0x01000001 => Some(Keycode::Tab),
        0x01000024 => Some(Keycode::CapsLock),
        0x01000004 => Some(Keycode::Return),
        0x01000005 => Some(Keycode::Enter),
        0x00000020 => Some(Keycode::Space),

        // symbols
        0x00000060 => Some(Keycode::QuoteLeft),
        0x0000002d => Some(Keycode::Minus),
        0x0000003d => Some(Keycode::Equal),
        0x0000002a => Some(Keycode::Asterisk),
        0x0000002b => Some(Keycode::Plus),
        0x0000002e => Some(Keycode::Period),
        0x0000002f => Some(Keycode::Slash),
        0x0000005b => Some(Keycode::BracketLeft),
        0x0000005d => Some(Keycode::BracketRight),
        0x0000005c => Some(Keycode::Backslash),
        0x0000003b => Some(Keycode::Semicolon),
        0x00000022 => Some(Keycode::Quote),
        0x0000002c => Some(Keycode::Comma),

        // letters
        0x00000041 => Some(Keycode::KeyA),
        0x00000042 => Some(Keycode::KeyB),
        0x00000043 => Some(Keycode::KeyC),
        0x00000044 => Some(Keycode::KeyD),
        0x00000045 => Some(Keycode::KeyE),
        0x00000046 => Some(Keycode::KeyF),
        0x00000047 => Some(Keycode::KeyG),
        0x00000048 => Some(Keycode::KeyH),
        0x00000049 => Some(Keycode::KeyI),
        0x0000004a => Some(Keycode::KeyJ),
        0x0000004b => Some(Keycode::KeyK),
        0x0000004c => Some(Keycode::KeyL),
        0x0000004d => Some(Keycode::KeyM),
        0x0000004e => Some(Keycode::KeyN),
        0x0000004f => Some(Keycode::KeyO),
        0x00000050 => Some(Keycode::KeyP),
        0x00000051 => Some(Keycode::KeyQ),
        0x00000052 => Some(Keycode::KeyR),
        0x00000053 => Some(Keycode::KeyS),
        0x00000054 => Some(Keycode::KeyT),
        0x00000055 => Some(Keycode::KeyU),
        0x00000056 => Some(Keycode::KeyV),
        0x00000057 => Some(Keycode::KeyW),
        0x00000058 => Some(Keycode::KeyX),
        0x00000059 => Some(Keycode::KeyY),
        0x0000005a => Some(Keycode::KeyZ),

        // digits
        0x00000030 => Some(Keycode::Key0),
        0x00000031 => Some(Keycode::Key1),
        0x00000032 => Some(Keycode::Key2),
        0x00000033 => Some(Keycode::Key3),
        0x00000034 => Some(Keycode::Key4),
        0x00000035 => Some(Keycode::Key5),
        0x00000036 => Some(Keycode::Key6),
        0x00000037 => Some(Keycode::Key7),
        0x00000038 => Some(Keycode::Key8),
        0x00000039 => Some(Keycode::Key9),

        // numeric keyboard
        0x01000025 => Some(Keycode::NumLock),

        _ => None,
    }
}

fn convert_modifiers(keycode: i32, modifiers: &FFIKeyModifiers) -> KeyModifiers {
    KeyModifiers {
        alt: modifiers.alt,
        ctrl: modifiers.ctrl,
        shift: modifiers.shift,
        win: modifiers.win,
        keypad: modifiers.keypad,
        right: keycode == 0x01001103,
    }
}

fn convert_text(text: *const c_char) -> Option<String> {
    if text.is_null() {
        None
    } else {
        let text = unsafe { CStr::from_ptr(text) };
        Some(text.to_str().unwrap().to_owned())
    }
}