devela/ui/back/miniquad/window.rs
1// devela::ui::back::miniquad::window
2
3use crate::{Box, /*MiniquadRenderingBackend, */ String, Vec};
4#[cfg(feature = "std")]
5use ::miniquad::window::dropped_file_path;
6#[cfg(any(target_os = "windows", target_os = "linux"))]
7use ::miniquad::window::get_window_position;
8use ::miniquad::window::{
9 blocking_event_loop, cancel_quit, clipboard_get, clipboard_set, dpi_scale, dropped_file_bytes,
10 dropped_file_count, high_dpi, new_rendering_backend, order_quit, request_quit, schedule_update,
11 screen_size, set_cursor_grab, set_fullscreen, set_mouse_cursor, set_window_position,
12 set_window_size, show_keyboard, show_mouse,
13};
14use ::miniquad::{CursorIcon, RenderingBackend};
15
16/// A wrapper namespace over [`miniquad::window`] functions.
17pub struct MiniquadWindow;
18
19#[rustfmt::skip]
20impl MiniquadWindow {
21 /// Returns a new rendering backend.
22 ///
23 /// It's normally `GlContext`, or maybe `MetalContext` in macos.
24 pub fn new_rendering_backend() -> Box<dyn RenderingBackend> { new_rendering_backend() }
25
26 /* event loop */
27
28 /// Returns `true` if the event loop blocks until [`schedule_update`] is called.
29 ///
30 /// See also: `MiniquadService::`[`blocking_event_loop()`], and
31 /// `::miniquad::`[`blocking_event_loop`][blocking_event_loop].
32 ///
33 /// [`schedule_update`]: Self::schedule_update
34 /// [`set_blocking_event_loop()`]: crate::MiniquadService::set_blocking_event_loop
35 pub fn blocking_event_loop() -> bool { blocking_event_loop() }
36
37 /// Requests an immediate update, ensuring `update()` and `draw()` are called without waiting.
38 ///
39 /// This must be called from an implementor of [`MiniquadEventHandler`]
40 /// and requires [`conf.platform`]`.`[`blocking_event_loop`] to be enabled.
41 ///
42 /// This can significantly reduce CPU usage while waiting for events.
43 ///
44 /// It is recommended to call this at the end of `resize_event`
45 /// or after relevant mouse/keyboard input.
46 ///
47 /// [`MiniquadEventHandler`]: crate::MiniquadEventHandler
48 /// [`conf.platform`]: crate::MiniquadConf#structfield.platform
49 /// [`blocking_event_loop`]: crate::MiniquadPlatform#structfield.blocking_event_loop
50 pub fn schedule_update() { schedule_update(); }
51
52 /* clipboard */
53
54 /// Get current OS clipboard value.
55 pub fn clipboard_get() -> Option<String> { clipboard_get() }
56
57 /// Save value to OS clipboard
58 pub fn clipboard_set(data: &str) { clipboard_set(data); }
59
60 /* dropping files */
61
62 /// Returns the contents of a dropped file as a byte vector, if available.
63 ///
64 /// The `index` parameter specifies which dropped file to retrieve, starting from 0.
65 pub fn dropped_file_bytes(index: usize) -> Option<Vec<u8>> { dropped_file_bytes(index) }
66
67 /// Returns the number of files that have been dropped.
68 pub fn dropped_file_count() -> usize { dropped_file_count() }
69
70 /// Returns the file path of a dropped file, if available.
71 ///
72 /// The `index` parameter specifies which dropped file to retrieve, starting from 0.
73 #[cfg(feature = "std")]
74 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
75 pub fn dropped_file_path(index: usize) -> Option<crate::PathBuf> { dropped_file_path(index) }
76
77 /* dpi */
78
79 /// The dpi scaling factor (window pixels to framebuffer pixels).
80 ///
81 /// See: [High DPI Rendering][::miniquad::conf#high-dpi-rendering].
82 pub fn dpi_scale() -> f32 { dpi_scale() }
83
84 /// Returns `true` if `high_dpi` was requested and it's running in a high-dpi scenario.
85 pub fn high_dpi() -> bool { high_dpi() }
86
87 /* quit */
88
89 /// This function simply quits the application without giving the user a chance to intervene.
90 ///
91 /// Usually this might be called when the user clicks the ‘Ok’ button in a
92 /// ‘Really Quit?’ dialog box Window might not be actually closed right away
93 /// (exit(0) might not happen in the order_quit implmentation) and execution
94 /// might continue for some time after But the window is going to be
95 /// inevitably closed at some point.
96 pub fn quit() { order_quit(); }
97
98 /// Triggers the “quit_requested_event” event.
99 ///
100 /// If the event handler callback does nothing, the application will be quit as usual.
101 /// To prevent this, call [`cancel_quit`][Self::cancel_quit] from inside the event handler.
102 pub fn request_quit() { request_quit(); }
103
104 /// Cancels a pending quit request, no matter who it was requested.
105 ///
106 /// The only place where calling this function makes sense is from inside the
107 /// event handler callback when the “quit_requested_event” event has been received.
108 pub fn cancel_quit() { cancel_quit(); }
109
110 /* window shape */
111
112 /// The current framebuffer size in pixels.
113 ///
114 /// See [High DPI Rendering][::miniquad::conf#high-dpi-rendering] and
115 /// `::miniquad::`[`screen_size`][screen_size].
116 pub fn get_size() -> (f32, f32) { screen_size() }
117
118 /// Set the application’s window size.
119 ///
120 /// See `::miniquad::`[`set_window_size`][set_window_size].
121 pub fn set_size(width: u32, height: u32) { set_window_size(width, height); }
122
123 /// Get the position of the window.
124 ///
125 /// See `::miniquad::`[`get_window_position`][set_window_position].
126 #[cfg(any(target_os = "windows", target_os = "linux"))]
127 #[cfg_attr(feature = "nightly_doc", doc(cfg(any(target_os = "windows", target_os = "linux"))))]
128 pub fn get_position() -> (u32, u32) { get_window_position() }
129
130 /// Set the window position.
131 ///
132 /// See `::miniquad::`[`set_window_position`][set_window_position].
133 pub fn set_position(x: u32, y: u32) { set_window_position(x, y); }
134
135 /// Sets the full screen mode.
136 pub fn set_fullscreen(fullscreen: bool) { set_fullscreen(fullscreen); }
137
138 /* keyboard & mouse */
139
140 /// Show/hide onscreen keyboard. Only works on Android right now.
141 pub fn show_keyboard(shown: bool) { show_keyboard(shown); }
142
143 /// Show or hide the mouse cursor
144 pub fn show_mouse(shown: bool) { show_mouse(shown); }
145
146 /// Set the mouse cursor icon.
147 pub fn set_mouse_cursor(icon: CursorIcon) { set_mouse_cursor(icon); }
148
149 /// Capture mouse cursor to the current window On WASM this will automatically
150 /// hide cursor.
151 ///
152 /// On desktop this will bound cursor to windows border.
153 ///
154 /// NOTICE: on desktop cursor will not be automatically released after window
155 /// lost focus so `set_cursor_grab(false)` on window's focus lost is recommended.
156 //
157 // WAIT: implement window focus events
158 pub fn set_cursor_grab(grab: bool) { set_cursor_grab(grab); }
159}