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

use boxfnonce::SendBoxFnOnce;
use std::ffi::*;
use tokio::sync::oneshot;

use crate::application::{Application, ApplicationAsync, ApplicationHandle};
use crate::browser_window::{BrowserWindow, BrowserWindowHandle, BrowserWindowInner, BrowserWindowAsync};

use std::{
	mem,
	ops::Deref,
	ptr,
	rc::Rc,
	sync::Arc
};



/// The type of content to display in a browser window

pub enum Source {
	Url( String ),
	Html( String )
}

/// Used to create a BrowserWindow instance.

pub struct BrowserWindowBuilder {

	parent: Option<BrowserWindowHandle>,
	source: Source,
	title: Option<String>,
	width: Option<u32>,
	height: Option<u32>,
	handler: Option<Box<dyn FnMut(BrowserWindowHandle, &str, &[&str]) + Send>>,

	borders: bool,
	maximizable: bool,
	minimizable: bool,
	resizable: bool
}



impl BrowserWindowBuilder {

	/// Sets whether or not the window has borders

	/// Default is true.

	pub fn borders( mut self, value: bool ) -> Self {
		self.borders = value;	self
	}

	/// Configure a closure that can be invoked from within JavaScript.

	/// The closure's first parameter specifies a command name.

	/// The closure's second parameter specifies an array of string arguments.

	pub fn handler<H>( mut self, handler: H ) -> Self where
		H: FnMut(BrowserWindowHandle, &str, &[&str]) + Send + 'static
	{
		self.handler = Some( Box::new( handler ) );
		self
	}

	/// Sets whether or not the window has a maximize button on the title bar

	/// Default is true.

	pub fn maximizable( mut self, value: bool ) -> Self {
		self.maximizable = value;	self
	}

	/// Sets whether or not the window has a minimize button on the title bar

	/// Default is true

	pub fn minimizable( mut self, value: bool ) -> Self {
		self.minimizable = value;	self
	}

	/// Configure a parent window.

	/// When a parent window closes, this browser window will close as well.

	pub fn parent<B>( mut self, bw: &B ) -> Self where
		B: Deref<Target=BrowserWindowHandle>
	{
		self.parent = Some( (**bw).clone() );
		self
	}

	/// Creates an instance of a browser window builder.

	///

	/// # Arguments

	/// * `source` - The content that will be displayed in the browser window.

	pub fn new( source: Source ) -> Self {
		Self {
			parent: None,
			source: source,
			handler: None,
			title: None,
			width: None,
			height: None,

			borders: true,
			minimizable: true,
			maximizable: true,
			resizable: true
		}
	}

	/// Sets the title of the window

	///

	/// # Arguments

	/// * `title` - The text that will be displayed in the title bar

	pub fn title( mut self, title: String ) -> Self {
		self.title = Some( title );
		self
	}

	/// Sets the width that the browser window will be created with initially

	///

	/// # Arguments

	/// * `width` - Width in pixels

	pub fn width( mut self, width: u32 ) -> Self {
		self.width = Some( width );
		self
	}

	/// Sets the height that the browser window will be created with initially

	///

	/// # Arguments

	/// * `height` - Height in pixels

	pub fn height( mut self, height: u32 ) -> Self {
		self.height = Some( height );
		self
	}

	/// Sets whether or not the window will be resizable

	/// Default is true

	pub fn resizable( mut self, resizable: bool ) -> Self {
		self.resizable = resizable;	self
	}

	/// Creates the browser window.

	///

	/// # Arguments

	/// * `app` - An application handle that this browser window can spawn into

	/// * `on_created` - A callback closure that will be invoked when the browser window is created and ready.

	pub fn spawn<H>( self, app: &Application, on_created: H ) where
		H: FnOnce( BrowserWindow ) + Send + 'static
	{
		let app_handle = (*app.inner).clone();

		self._spawn( app_handle.clone(), move |inner_handle| {
			let bw = BrowserWindow {
				inner: Rc::new( BrowserWindowInner {
					app: app_handle,
					handle: inner_handle
				} )
			};

			on_created( bw );
		} );
	}

	/// Same as spawn, but asynchronous.

	/// Instead of providing a callback, the handle is simply returned.

	///

	/// # Arguments

	/// * `app` - An async application handle.

	pub async fn spawn_async( self, app: &ApplicationAsync ) -> BrowserWindowAsync {
		let (tx, rx) = oneshot::channel::<BrowserWindowHandle>();

		let app_handle = (*app.inner).clone();

		// We need to dispatch the spawning to the GUI thread because only there we can call GUI functionality

		app.dispatch(|app| {
			self._spawn(app, move |inner_handle| {
				let _ = tx.send( inner_handle );
			} );
		}).await;

		let inner_handle = rx.await.unwrap();

		BrowserWindowAsync {
			inner: Arc::new( BrowserWindowInner {
				app: app_handle,
				handle: inner_handle
			} )
		}
	}

	fn _spawn<H: 'static>( self, app: ApplicationHandle, on_created: H ) where
		H: FnOnce( BrowserWindowHandle ) + Send + 'static
	{
		match self {
			BrowserWindowBuilder { parent, source, title, width, height, handler, borders, minimizable, maximizable, resizable } => {

				// Parent

				let parent_handle = match parent {
					None => ptr::null(),
					Some( p ) => p._ffi_handle
				};

				// Source

				let csource = match &source {	// Use a reference, we want source to live until the end of the function because bw_BrowserWindowSource holds a reference to its internal string.

					Source::Url( url ) => { bw_BrowserWindowSource {
						data: url.as_str().into(),
						is_html: false
					} },
					Source::Html( html ) => { bw_BrowserWindowSource {
						data: html.as_str().into(),
						is_html: true
					} }
				};

				let title_ptr = match title.as_ref() {
					None => "Browser Window".into(),
					Some( t ) => t.as_str().into()
				};

				// Width and height use -1 as the 'use default' option within the c code

				let w: i32 = match width {
					Some(w) => w as i32,
					None => -1
				};
				let h: i32 = match height {
					Some(h) => h as i32,
					None => -1
				};

				let user_data = Box::into_raw( Box::new(
					BrowserWindowUserData {
						handler: match handler {
							None => Box::new(|_,_,_| {}),
							Some(f) => Box::new(f)
						}
					}
				) );
				let callback_data = Box::into_raw( Box::new(
					BrowserWindowCreationCallbackData::from( on_created )
				) );

				let window_options = bw_WindowOptions {
					maximizable: maximizable,
					minimizable: minimizable,
					resizable: resizable,
					closable: true,
					borders: borders
				};
				let other_options = bw_BrowserWindowOptions {
					dev_tools: true
				};

				unsafe { bw_BrowserWindow_new(
					app._ffi_handle.clone(),
					parent_handle,
					csource,
					title_ptr,
					w,
					h,
					&window_options,
					&other_options,
					ffi_window_invoke_handler,
					user_data as _,
					ffi_browser_window_created_callback,
					callback_data as _
				) };
			}
		}
	}
}

/// The data that is passed to the C FFI handler function

struct BrowserWindowUserData {
	handler: Box<dyn FnMut( BrowserWindowHandle, &str, &[&str])>
}

/// The data that is passed to the creation callback function

type BrowserWindowCreationCallbackData = SendBoxFnOnce<'static, ( BrowserWindowHandle, )>;



/// Takes the arguments received from the C FFI handler callback, and converts it to a vector of strings

fn args_to_vec<'a>( args: *const bw_CStrSlice, args_count: usize ) -> Vec<&'a str> {

	let mut vec = Vec::<&str>::with_capacity( args_count );

	for i in 0..args_count {
		let str_ref: &str = unsafe { (*args.offset(i as _)).into() };

		vec.push( str_ref );
	}

	vec
}

/// This external C function will be invoked by the underlying implementation browser-window when it is invoked in JavaScript

extern "C" fn ffi_window_invoke_handler( inner_handle: *mut bw_BrowserWindow, _command: bw_CStrSlice, _args: *const bw_CStrSlice, args_count: usize ) {

	unsafe {
		let data_ptr: *mut BrowserWindowUserData = mem::transmute( bw_BrowserWindow_getUserData( inner_handle ) );
		let data: &mut BrowserWindowUserData = &mut *data_ptr;

		match data {
			BrowserWindowUserData{ handler } => {
				let outer_handle = BrowserWindowHandle::from_ptr( inner_handle );

				let args = args_to_vec( _args, args_count );

				handler( outer_handle, _command.into(), &*args );
			}
		}
	}
}

// This external C function will be given as the callback to the bw_BrowserWindow_new function, to be invoked when the browser window has been created

extern "C" fn ffi_browser_window_created_callback( inner_handle: *mut bw_BrowserWindow, data: *mut c_void ) {

	unsafe {
		let data_ptr: *mut BrowserWindowCreationCallbackData = mem::transmute( data );
		let data = Box::from_raw( data_ptr );

		let outer_handle = BrowserWindowHandle::from_ptr( inner_handle );

		data.call( outer_handle );
	}
}