browser_window_core/application/
c.rs1use super::{ApplicationExt, ApplicationSettings};
5
6use crate::{
7 error::*,
8 prelude::*
9};
10
11use std::{
12 os::raw::{c_char, c_int, c_void},
13 ptr,
14 time::Duration
15};
16
17
18
19#[derive(Clone,Copy)]
20pub struct ApplicationImpl {
21 pub(in crate) inner: *mut cbw_Application
22}
23
24impl ApplicationExt for ApplicationImpl {
25
26 fn assert_correct_thread( &self ) {
27 unsafe { cbw_Application_assertCorrectThread( self.inner ) }
28 }
29
30 fn dispatch( &self, work: unsafe fn(ApplicationImpl, *mut ()), _data: *mut () ) -> bool {
31 let data = Box::new( DispatchData {
32 func: work,
33 data: _data
34 } );
35
36 let data_ptr = Box::into_raw( data );
37
38 unsafe { cbw_Application_dispatch( self.inner, Some( invocation_handler ), data_ptr as _ ) != 0 }
39 }
40
41 fn dispatch_delayed( &self, work: unsafe fn(ApplicationImpl, *mut ()), _data: *mut (), delay: Duration ) -> bool {
42 let data = Box::new( DispatchData {
43 func: work,
44 data: _data
45 } );
46
47 let data_ptr = Box::into_raw( data );
48
49 unsafe { cbw_Application_dispatchDelayed( self.inner, Some( invocation_handler ), data_ptr as _, delay.as_millis() as _ ) != 0 }
50 }
51
52 fn exit( &self, exit_code: i32 ) {
53 unsafe { cbw_Application_exit( self.inner, exit_code as _ ) }
54 }
55
56 fn exit_threadsafe( self: &Self, exit_code: i32 ) {
57 unsafe { cbw_Application_exitAsync( self.inner, exit_code ) }
58 }
59
60 fn finish( &self ) {
61 unsafe { cbw_Application_finish( self.inner ) }
62 }
63
64 fn initialize( argc: c_int, argv: *mut *mut c_char, _settings: &ApplicationSettings ) -> CbwResult<Self> {
65
66 let exec_path: &str = match _settings.engine_seperate_executable_path.as_ref() {
67 None => "",
68 Some(path) => path.to_str().unwrap()
69 };
70
71 let c_settings = cbw_ApplicationSettings {
72 engine_seperate_executable_path: exec_path.into(),
73 resource_dir: _settings.resource_dir.as_ref().unwrap_or(&"".to_owned()).as_str().into()
74 };
75
76 let mut c_handle: *mut cbw_Application = ptr::null_mut();
77 let c_err = unsafe { cbw_Application_initialize( &mut c_handle, argc, argv, &c_settings ) };
78 if c_err.code != 0 {
79 return Err( c_err.into() )
80 }
81
82 Ok(Self {
83 inner: c_handle
84 })
85 }
86
87 fn mark_as_done(&self) {
88 unsafe { cbw_Application_markAsDone(self.inner) };
89 }
90
91 fn run( &self, on_ready: unsafe fn( ApplicationImpl, *mut () ), _data: *mut () ) -> i32 {
92 let data = Box::new( DispatchData {
93 func: on_ready,
94 data: _data
95 } );
96
97 let data_ptr = Box::into_raw( data );
98
99 unsafe { cbw_Application_run( self.inner, Some( invocation_handler ), data_ptr as _ ) }
101 }
102}
103
104
105
106struct DispatchData {
107 func: unsafe fn( ApplicationImpl, *mut () ),
108 data: *mut ()
109}
110
111unsafe extern "C" fn invocation_handler( _handle: *mut cbw_Application, _data: *mut c_void ) {
112
113 let data_ptr = _data as *mut DispatchData;
114 let data = Box::from_raw( data_ptr );
115 let handle = ApplicationImpl { inner: _handle };
116
117 (data.func)( handle, data.data );
118}