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
pub mod def;
use self::def::{NSTDDisplay, NSTDWindowPosition, NSTDWindowSize};
use crate::{
    core::{
        def::{NSTDBool, NSTDErrorCode},
        str::NSTDStr,
    },
    events::{NSTDEventLoop, NSTDWindowID},
    image::NSTDImage,
};
use winit::{
    dpi::{PhysicalPosition, PhysicalSize},
    window::{Icon, Window},
};
#[cfg(target_os = "windows")]
use winit::{
    platform::windows::{WindowBuilderExtWindows, WindowExtWindows},
    window::WindowBuilder,
};

/// Represents a window.
pub type NSTDWindow = *mut Window;

/// Creates a new window.
/// Parameters:
///     `const NSTDEventLoop event_loop` - The event loop to attach to the window.
/// Returns: `NSTDWindow window` - The new window, null on error.
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_create(event_loop: NSTDEventLoop) -> NSTDWindow {
    if let Ok(window) = Window::new(&*event_loop) {
        return Box::into_raw(Box::new(window));
    }
    std::ptr::null_mut()
}

/// Creates a child window with `parent` being the parent window.
/// NOTE: This is only functional on Windows targets and will always return a null window handle on
/// any other platform.
/// Parameters:
///     `const NSTDEventLoop event_loop` - The event loop to attach to the window.
///     `const NSTDWindow parent` - The parent window.
/// Returns: `NSTDWindow child` - The new child window.
#[cfg_attr(feature = "clib", no_mangle)]
#[cfg_attr(not(target_os = "windows"), allow(unused_variables))]
pub unsafe extern "C" fn nstd_gui_window_create_child(
    event_loop: NSTDEventLoop,
    parent: NSTDWindow,
) -> NSTDWindow {
    #[cfg(target_os = "windows")]
    {
        let parent = (*parent).hwnd().cast();
        let window = WindowBuilder::new().with_parent_window(parent);
        if let Ok(window) = window.build(&*event_loop) {
            return Box::into_raw(Box::new(window));
        }
        std::ptr::null_mut()
    }
    #[cfg(not(target_os = "windows"))]
    std::ptr::null_mut()
}

/// Requests the window to be drawn.
/// Parameters:
///     `const NSTDWindow window` - The window.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_request_redraw(window: NSTDWindow) {
    (*window).request_redraw();
}

/// Gets a window's scale factor.
/// Parameters:
///     `const NSTDWindow window` - The window.
/// Returns: `NSTDFloat64 factor` - The scale factor of the window.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_get_scale_factor(window: NSTDWindow) -> f64 {
    (*window).scale_factor()
}

/// Sets a window's position.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDWindowPosition pos` - The new position.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_position(window: NSTDWindow, pos: NSTDWindowPosition) {
    (*window).set_outer_position(PhysicalPosition::new(pos.x, pos.y));
}

/// Gets a window's position.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `NSTDWindowPosition *const pos` - Returns as the position.
/// Returns: `NSTDErrorCode errc` - Nonzero on error.
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_get_position(
    window: NSTDWindow,
    pos: *mut NSTDWindowPosition,
) -> NSTDErrorCode {
    if let Ok(outer_position) = (*window).outer_position() {
        let pos = &mut *pos;
        pos.x = outer_position.x;
        pos.y = outer_position.y;
        return 0;
    }
    1
}

/// Gets a window's client position.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `NSTDWindowPosition *const pos` - Returns as the position.
/// Returns: `NSTDErrorCode errc` - Nonzero on error.
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_get_client_position(
    window: NSTDWindow,
    pos: *mut NSTDWindowPosition,
) -> NSTDErrorCode {
    if let Ok(inner_position) = (*window).inner_position() {
        let pos = &mut *pos;
        pos.x = inner_position.x;
        pos.y = inner_position.y;
        return 0;
    }
    1
}

/// Gets a window's size.
/// Parameters:
///     `const NSTDWindow window` - The window.
/// Returns: `NSTDWindowSize size` - The size of the window.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_get_size(window: NSTDWindow) -> NSTDWindowSize {
    let size = (*window).outer_size();
    NSTDWindowSize::new(size.width, size.height)
}

/// Sets a window's client size.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDWindowSize size` - An array of 2 `NSTDInt32`s.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_client_size(window: NSTDWindow, size: NSTDWindowSize) {
    (*window).set_inner_size(PhysicalSize::new(size.width, size.height));
}

/// Gets a window's client size.
/// Parameters:
///     `const NSTDWindow window` - The window.
/// Returns: `NSTDWindowSize size` - The size of the window's client area.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_get_client_size(window: NSTDWindow) -> NSTDWindowSize {
    let size = (*window).inner_size();
    NSTDWindowSize::new(size.width, size.height)
}

/// Sets a window's client min size.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDWindowSize *const size` - An array of 2 `NSTDUInt32`s, null for no min.
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_client_min_size(
    window: NSTDWindow,
    size: *const NSTDWindowSize,
) {
    if !size.is_null() {
        let size = &*size;
        (*window).set_min_inner_size(Some(PhysicalSize::new(size.width, size.height)));
    } else {
        (*window).set_min_inner_size::<PhysicalSize<u32>>(None);
    }
}

/// Sets a window's client max size.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDWindowSize *const size` - An array of 2 `NSTDUInt32`s, null for no max.
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_client_max_size(
    window: NSTDWindow,
    size: *const NSTDWindowSize,
) {
    if !size.is_null() {
        let size = &*size;
        (*window).set_max_inner_size(Some(PhysicalSize::new(size.width, size.height)));
    } else {
        (*window).set_max_inner_size::<PhysicalSize<u32>>(None);
    }
}

/// Sets a window's title.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDStr *const title` - The new window title.
/// Returns: `NSTDErrorCode errc` - Nonzero on error.
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_title(
    window: NSTDWindow,
    title: &NSTDStr,
) -> NSTDErrorCode {
    if let Ok(title) = std::str::from_utf8(title.bytes.as_byte_slice()) {
        (*window).set_title(title);
        return 0;
    }
    1
}

/// Sets a window's visibility.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDBool visible` - Whether to show or hide the window.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_visible(window: NSTDWindow, visible: NSTDBool) {
    (*window).set_visible(visible.into());
}

/// Sets whether the window is resizable or not.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDBool resizable` - Whether the window should be resizable or not.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_resizable(window: NSTDWindow, resizable: NSTDBool) {
    (*window).set_resizable(resizable.into());
}

/// Sets the window's minimization mode.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDBool minimized` - Whether the window should be minimized or not.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_minimized(window: NSTDWindow, minimized: NSTDBool) {
    (*window).set_minimized(minimized.into());
}

/// Sets the window's maximization mode.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDBool maximized` - Whether the window should be maximized or not.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_maximized(window: NSTDWindow, maximized: NSTDBool) {
    (*window).set_maximized(maximized.into());
}

/// Checks if the window is maximized.
/// Parameters:
///     `const NSTDWindow window` - The window.
/// Returns: `NSTDBool maximized` - Nonzero if the window is maximized.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_is_maximized(window: NSTDWindow) -> NSTDBool {
    (*window).is_maximized().into()
}

/// Sets a window's icon image.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDImage *const img` - The icon image, null for default.
/// Returns: `NSTDErrorCode errc` - Nonzero on error.
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_icon(
    window: NSTDWindow,
    img: *const NSTDImage,
) -> NSTDErrorCode {
    let icon = match !img.is_null() {
        true => {
            const RGBA_COMPONENTS: u32 = 4;
            let size = (RGBA_COMPONENTS * (*img).width * (*img).height) as usize;
            let raw = std::slice::from_raw_parts((*img).raw, size);
            match Icon::from_rgba(raw.to_vec(), (*img).width, (*img).height) {
                Ok(icon) => Some(icon),
                _ => return 1,
            }
        }
        false => None,
    };
    (*window).set_window_icon(icon);
    0
}

/// Turn window decorations on or off.
/// Parameters:
///     `const NSTDWindow window` - The window.
///     `const NSTDBool decorations` - Whether to allow window decorations or not.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_set_decorations(
    window: NSTDWindow,
    decorations: NSTDBool,
) {
    (*window).set_decorations(decorations.into());
}

/// Gets the window's ID.
/// Parameters:
///     `const NSTDWindow window` - The window.
/// Returns: `NSTDWindowID window_id` - The window ID.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_get_id(window: NSTDWindow) -> NSTDWindowID {
    Box::into_raw(Box::new((*window).id()))
}

/// Gets the display that the given window resides in.
/// Parameters:
///     `const NSTDWindow window` - The window.
/// Returns: `NSTDDisplay display` - The display that the window is in.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_get_display(window: NSTDWindow) -> NSTDDisplay {
    if let Some(handle) = (*window).current_monitor() {
        return Box::into_raw(Box::new(handle));
    }
    std::ptr::null_mut()
}

/// Closes a window.
/// Parameters:
///     `NSTDWindow *const window` - Pointer to the window.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_close(window: *mut NSTDWindow) {
    Box::from_raw(*window);
    *window = std::ptr::null_mut();
}

/// Compares two window IDs.
/// Parameters:
///     `const NSTDWindowID id1` - A window ID.
///     `const NSTDWindowID id2` - Another window ID.
/// Returns: `NSTDBool are_same` - 1 if the two IDs refer to the same window, 0 otherwise.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_id_compare(
    id1: NSTDWindowID,
    id2: NSTDWindowID,
) -> NSTDBool {
    (*id1 == *id2).into()
}

/// Frees a window ID.
/// Parameters:
///     `NSTDWindowID *const window_id` - Pointer to the window ID.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_window_id_free(window_id: *mut NSTDWindowID) {
    Box::from_raw(*window_id);
    *window_id = std::ptr::null_mut();
}

/// Returns a display's size.
/// Parameters:
///     `const NSTDDisplay display` - The display.
/// Returns: `NSTDWindowSize size` - The size of the display.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_display_get_size(display: NSTDDisplay) -> NSTDWindowSize {
    let size = (*display).size();
    NSTDWindowSize::new(size.width, size.height)
}

/// Returns the display's scale factor.
/// Parameters:
///     `const NSTDDisplay display` - The display.
/// Returns: `NSTDFloat64 scale_factor` - The scale factor of the display.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_display_get_scale_factor(display: NSTDDisplay) -> f64 {
    (*display).scale_factor()
}

/// Frees a display handle.
/// Parameters:
///     `NSTDDisplay *const display` - Pointer to the display handle.
#[inline]
#[cfg_attr(feature = "clib", no_mangle)]
pub unsafe extern "C" fn nstd_gui_display_free(display: *mut NSTDDisplay) {
    Box::from_raw(*display);
    *display = std::ptr::null_mut();
}