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
use super::Qt5WidgetGenerator;

use crate::{
	DataProcessor,
	EventListener,
	Widget,
};

use std::{
	ffi::c_void,
	sync::{
		Arc,
		RwLock,
		Weak,
	},
};

extern "C" {
	fn uniui_gui_application_create() -> *mut c_void;

	fn uniui_gui_application_start(
		native_app: *mut c_void,
		cb: extern "C" fn(*mut std::ffi::c_void),
		app: *mut c_void,
	);

	fn uniui_gui_widget_show(native_app: *mut c_void);
}


extern "C" fn callback(app: *mut std::ffi::c_void) {
	let app: &mut Qt5Application = unsafe { &mut *(app as *mut Qt5Application) };
	app.process_data();
	app.on_draw();
}

pub struct Qt5Application {
	native: *mut c_void,
	main_widget: Option<Box<dyn Widget>>,
	widget_generator: Qt5WidgetGenerator,
	data_processor_list: Vec<Arc<RwLock<dyn DataProcessor>>>,
}

impl Qt5Application {
	pub fn start(f: &dyn Fn(&mut Qt5Application)) -> Result<(), ()> {
		let mut app = Qt5Application::new();
		f(&mut app);

		app.layout();

		log::trace!("app started");
		unsafe {
			uniui_gui_application_start(
				app.native,
				callback,
				&mut app as *mut _ as *mut std::ffi::c_void,
			);
		};
		Ok(())
	}

	pub fn set_title(
		&self,
		_title: &str,
	) {
		log::error!("set_title is not implemented for Qt5. Yet.");
	}

	pub fn set_local_path(
		&mut self,
		_module_address: &str,
	) -> Result<(), ()> {
		log::error!("set_local_path is not implemented for Qt5. Yet.");
		return Ok(());
	}

	pub fn link_from_base(
		&self,
		link: &str,
	) -> String {
		log::error!("link_from_base is not implemented for Qt5. Yet.");
		return link.to_string();
	}

	pub fn set_main_widget<T: 'static + Widget>(
		&mut self,
		widget: T,
	) {
		self.main_widget = Some(Box::new(widget));
	}

	pub fn add_data_processor<T>(
		&mut self,
		processor: T,
	) -> Weak<RwLock<T>>
	where
		T: 'static + DataProcessor,
	{
		let holder = Arc::new(RwLock::new(processor));
		let result = Arc::downgrade(&holder);
		self.data_processor_list.push(holder);
		return result;
	}

	pub fn add_event_listener<T>(
		&self,
		_e: &dyn EventListener<T>,
	) {
	}

	pub fn get_env_var(
		&self,
		_var_name: &str,
	) -> Option<String> {
		None
	}

	pub fn replace_local_ref(
		&self,
		_: &str,
	) {
	}

	pub fn get_host(&self) -> String {
		return String::new();
	}

	fn new() -> Qt5Application {
		let native = unsafe { uniui_gui_application_create() };
		return Qt5Application {
			native,
			main_widget: None,
			widget_generator: Qt5WidgetGenerator {},
			data_processor_list: Vec::new(),
		};
	}

	fn layout(&mut self) {
		match self.main_widget.as_mut() {
			Some(widget) => {
				match widget.to_native(&mut self.widget_generator) {
					Ok(native_main_widget) => {
						unsafe {
							uniui_gui_widget_show(native_main_widget);
						};
					},
					Err(_) => log::error!("to_native failed"),
				}
			},
			None => log::error!("main widget not set"),
		}
	}

	fn on_draw(&mut self) {
		match &mut self.main_widget {
			Some(l) => l.draw(),
			None => log::error!("no layout"),
		}
	}

	fn process_data(&mut self) {
		let start = std::time::SystemTime::now();
		let msec_since_app_epoch = start
			.duration_since(std::time::UNIX_EPOCH)
			.expect("Time went backwards")
			.as_millis() as i64;

		// FIXME(#131) layout are not accessable during data processing of layout and
		// it's subcomponents
		self.main_widget = match self.main_widget.take() {
			Some(mut l) => {
				l.process_data(msec_since_app_epoch, self);
				Some(l)
			},
			None => None,
		};

		let mut data_processor_list = self.data_processor_list.clone();
		data_processor_list.iter_mut().for_each(|holder| {
			match holder.write() {
				Ok(mut item) => item.process_data(msec_since_app_epoch, self),
				Err(e) => log::error!("can't lock data processor e:{:?}", e),
			}
		});
	}
}