browser_window/core/window/
c.rs

1//! This module implements the `Window` trait with the corresponding function
2//! definitions found in the C code base of `browser-window-c`. All functions
3//! are basically wrapping the FFI provided by crate `browser-window-c`.
4
5use std::{os::raw::c_char, ptr};
6
7use super::{WindowExt, WindowOptions};
8use crate::{core::application::ApplicationImpl, prelude::*};
9
10#[derive(Clone)]
11pub struct WindowImpl {
12	pub(crate) inner: *mut cbw_Window,
13}
14
15impl WindowImpl {
16	pub fn new(
17		app: ApplicationImpl, parent: Self, title: &str, width: Option<u32>, height: Option<u32>,
18		options: &WindowOptions,
19	) -> Self {
20		let str_slice: cbw_CStrSlice = title.into();
21
22		let w = match width {
23			None => -1i32,
24			Some(x) => x as i32,
25		};
26		let h = match height {
27			None => -1i32,
28			Some(x) => x as i32,
29		};
30
31		let handle = unsafe { cbw_Window_new(app.inner, parent.inner, str_slice, w, h, options) };
32
33		// Return
34		Self { inner: handle }
35	}
36}
37
38impl Default for WindowImpl {
39	fn default() -> Self {
40		Self {
41			inner: ptr::null_mut(),
42		}
43	}
44}
45
46impl WindowExt for WindowImpl {
47	fn app(&self) -> ApplicationImpl {
48		ApplicationImpl {
49			inner: unsafe { (*self.inner).app },
50		}
51	}
52
53	fn close(&self) { unsafe { cbw_Window_close(self.inner) } }
54
55	fn free(&self) { unsafe { cbw_Window_free(self.inner) } }
56
57	fn content_dimensions(&self) -> Dims2D {
58		unsafe { cbw_Window_getContentDimensions(self.inner) }
59	}
60
61	fn opacity(&self) -> u8 { unsafe { cbw_Window_getOpacity(self.inner) } }
62
63	fn position(&self) -> Pos2D { unsafe { cbw_Window_getPosition(self.inner) } }
64
65	fn title(&self) -> String {
66		// First obtain string size
67		let mut buf: *mut c_char = ptr::null_mut();
68		let buf_len = unsafe { cbw_Window_getTitle(self.inner, &mut buf) };
69
70		let slice = cbw_StrSlice {
71			data: buf,
72			len: buf_len,
73		};
74
75		unsafe { cbw_string_freeCstr(buf) };
76
77		// Convert to String
78		slice.into()
79	}
80
81	fn window_dimensions(&self) -> Dims2D { unsafe { cbw_Window_getWindowDimensions(self.inner) } }
82
83	fn hide(&self) { unsafe { cbw_Window_hide(self.inner) } }
84
85	fn set_content_dimensions(&self, dimensions: Dims2D) {
86		unsafe { cbw_Window_setContentDimensions(self.inner, dimensions) }
87	}
88
89	fn set_opacity(&self, opacity: u8) { unsafe { cbw_Window_setOpacity(self.inner, opacity) } }
90
91	fn set_position(&self, position: Pos2D) {
92		unsafe { cbw_Window_setPosition(self.inner, position) }
93	}
94
95	fn set_title(&self, title: &str) {
96		let slice: cbw_CStrSlice = title.into();
97		unsafe { cbw_Window_setTitle(self.inner, slice) };
98	}
99
100	fn set_user_data(&self, user_data: *mut ()) {
101		unsafe {
102			(*self.inner).user_data = user_data as _;
103		}
104	}
105
106	fn set_window_dimensions(&self, dimensions: Dims2D) {
107		unsafe { cbw_Window_setWindowDimensions(self.inner, dimensions) }
108	}
109
110	fn show(&self) { unsafe { cbw_Window_show(self.inner) } }
111}