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
use boxfnonce::SendBoxFnOnce;
use browser_window_ffi::*;
use futures_channel::oneshot;
use std::{
	error::Error,
	ffi::CStr,
	ops::Deref,
	os::raw::*,
	rc::Rc,
	sync::Arc
};

use crate::application::*;
use crate::common::*;

pub mod builder;

pub use builder::BrowserWindowBuilder;



/// A thread-unsafe handle to a browser window.

// A reference counter is held internally,

//     meaning that you can simply clone this handle without having to worry about memory leakage.

// If the user closes the window, this handle stays valid.

// Also, if you lose this handle, window destruction and cleanup is only done when the user actually closes it.

// So you don't have to worry about lifetimes and/or propper destruction of the window either.

#[derive(Clone)]
pub struct BrowserWindow {
	pub(in super) inner: Rc<BrowserWindowInner>
}

/// A thread-safe handle to a browser window.

/// It allows you to dispatch code to the GUI thread.

// It provides the same functionality as Browserwindow.

// However, each function is async: it runs on the GUI thread, and returns when it is done.

#[derive(Clone)]
pub struct BrowserWindowAsync {
	pub(in super) inner: Arc<BrowserWindowInnerAsync>
}

/// A handle to a browser window.

/// This can not be instantiated, but can be seen as an interface that is provided for by the BrowserWindow and BrowserWindowAsync 'handles'.

#[derive(Clone)]
pub struct BrowserWindowHandle {
	_ffi_handle: *mut bw_BrowserWindow
}
// BrowserWindowHandle is made to be Send and Sync, because it is used internally by BrowserWindowAsync as well.

unsafe impl Send for BrowserWindowHandle {}
unsafe impl Sync for BrowserWindowHandle {}

/// This structure holds a browser window handle.

/// The purpose of this structure is to invoke the FFI function to drop the browser window handle, when this struct is dropped naturally by Rust.

/// So by putting this struct in an Arc<...> or Rc<...>, you effectively have some sort garbage collection.

pub struct BrowserWindowInner {
	app: Arc<ApplicationInner>,	// Keep a reference to ApplicationInner so that the application handle doesn't get freed prematurely

	pub(in super) handle: BrowserWindowHandle
}

pub struct BrowserWindowInnerAsync {
	app: Arc<ApplicationInner>,	// Keep a reference to ApplicationInner so that the application handle doesn't get freed prematurely

	pub(in super) handle: BrowserWindowHandle
}



impl HasAppHandle for BrowserWindow {
	fn app_handle( &self ) -> ApplicationHandle {
		self.inner.app.handle.clone()
	}
}



impl Deref for BrowserWindow {
	type Target = BrowserWindowHandle;

	fn deref( &self ) -> &Self::Target {
		&self.inner.handle
	}
}



impl BrowserWindowAsync {

	/// Closes the browser.

	pub async fn close( self ) {
		self.dispatch(|bw| {
			bw.close()
		}).await;
	}

	/// Executes the given closure within the GUI thread

	///

	/// # Arguments

	/// * `func` - The closure to run on the GUI thread

	pub fn dispatch<'a,F,R>( &self, func: F ) -> BrowserWindowDispatchFuture<'a,R> where
		F: FnOnce( BrowserWindowHandle ) -> R + Send + 'a,
		R: Send
	{
		BrowserWindowDispatchFuture::new( self.inner.handle.clone(), func )
	}

	/// Executes the given javascript code, and returns the resulting output as a string when done.

	///

	/// # Arguments:

	/// * `js` - Javascript code

	pub async fn eval_js( &self, js: &str ) -> Result<String, Box<dyn Error + Send>> {

		let (tx, rx) = oneshot::channel::<Result<String, Box<dyn Error + Send>>>();

		self.dispatch(|bw| {

			// Executing the JavaScript on the GUI thread

			bw.eval_js( js, |_, result| {

				// Result is ready

				let _ = tx.send( result ).unwrap();
			} );
		}).await;

		// The result

		rx.await.unwrap()
	}

	/// Causes the browser to navigate to the given url.

	///

	/// # Arguments

	/// * `url` - The url to navigate to

	pub async fn navigate( &self, url: &str ) -> Result<(), Box<dyn Error + Send>> {
		*self.dispatch(|bw| {
			bw.navigate( url )
		}).await
	}
}

impl Deref for BrowserWindowAsync {
	type Target = BrowserWindowHandle;

	fn deref( &self ) -> &Self::Target {
		&self.inner.handle
	}
}



type BrowserWindowCallbackData<'a> = SendBoxFnOnce<'a,(BrowserWindowHandle, Result<String, Box<dyn Error + Send>>),()>;

/// The future that dispatches a closure on the GUI thread used by BrowserWindowAsync.

pub type BrowserWindowDispatchFuture<'a,R> = DispatchFuture<'a, BrowserWindowHandle, R>;



impl BrowserWindowHandle {

	/// Closes the browser.

	// The browser will be freed from memory when the last handle to it gets dropped.

	pub fn close( self ) {
		unsafe { bw_BrowserWindow_close( self._ffi_handle ); }
	}

	/// Executes the given javascript code, and returns the output via a callback.

	/// If you don't need the result, see "exec_js".

	///

	/// # Arguments:

	/// * `js` - The javascript code to execute.

	/// * `on_complete` - The 'callback'. This closure will be invoked, with the result provided as the first argument.

	///                   The result contains the output of the javascript code when it succeeded.

	///                   Otherwise the error explains the javascript exception.

	pub fn eval_js<'a,H>( &self, js: &str, on_complete: H ) where
		H: FnOnce( BrowserWindowHandle, Result<String, Box<dyn Error + Send>> ) + Send + 'a
	{
		let data_ptr = Box::into_raw( Box::new(
			BrowserWindowCallbackData::<'a>::new( on_complete )
		) );

		unsafe { bw_BrowserWindow_evalJs(
			self._ffi_handle,
			js.into(),
			ffi_eval_js_callback,
			data_ptr as _
		) };
	}

	/// Executes the given javascript code without waiting on it to finish.

	///

	/// # Arguments:

	/// * `js` - The javascript code

	pub fn exec_js( &self, js: &str ) {
		self.eval_js( js, |_,_|{} );
	}

	unsafe fn from_ptr( ptr: *mut bw_BrowserWindow ) -> Self {
		Self {
			_ffi_handle: ptr
		}
	}

	/// Causes the browser to navigate to the given url.

	///

	/// # Arguments

	/// * `url` - The url to navigate to

	pub fn navigate( &self, url: &str ) -> Result<(), Box<dyn Error + Send>> {
		let err = unsafe { bw_BrowserWindow_navigate( self._ffi_handle, url.into() ) };

		if err.code == 0 {
			return Ok(());
		}

		Err( Box::new( err ) )
	}
}

impl HasAppHandle for BrowserWindowHandle {
	fn app_handle( &self ) -> ApplicationHandle {
		ApplicationHandle {
			_ffi_handle: unsafe { bw_BrowserWindow_getApp( self._ffi_handle ) as _ }
		}
	}
}

impl Deref for BrowserWindowInner {
	type Target = BrowserWindowHandle;

	fn deref( &self ) -> &Self::Target {
		&self.handle
	}
}

impl Drop for BrowserWindowInner {
	fn drop( &mut self ) {
		unsafe { bw_BrowserWindow_drop( self.handle._ffi_handle ) }
	}
}

impl Drop for BrowserWindowInnerAsync {
	fn drop( &mut self ) {
		unsafe { bw_Application_dispatch( self.app.handle._ffi_handle, ffi_free_browser_window, self.handle._ffi_handle as _ ); }
	}
}



extern "C" fn ffi_free_browser_window( _app: *mut bw_Application, data: *mut c_void ) {
	unsafe { bw_BrowserWindow_drop( data as *mut bw_BrowserWindow ); }
}

extern "C" fn ffi_eval_js_callback( bw: *mut bw_BrowserWindow, cb_data: *mut c_void, result: *const c_char, error: *const bw_Err ) {

	let data_ptr = cb_data as *mut BrowserWindowCallbackData;
	let data = unsafe { Box::from_raw( data_ptr ) };

	// Construct a result value depending on whether the result or error parameters are set

	let result_val: Result<String, Box<dyn Error + Send>> = if error.is_null() {
		let result_str = unsafe { CStr::from_ptr( result ).to_string_lossy().to_owned().to_string() };
		Ok( result_str )
	}
	else {
		Err( Box::new( unsafe { (*error).clone() } ) )
	};

	// Construct a

	let handle = unsafe { BrowserWindowHandle::from_ptr( bw ) };

	data.call( handle, result_val );
}