app.rs 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use async_recursion::async_recursion;
  19. use chrono::{NaiveDate, NaiveDateTime};
  20. use darkfi::event_graph;
  21. use darkfi_serial::Encodable;
  22. use futures::{stream::FuturesUnordered, StreamExt};
  23. use smol::Task;
  24. use std::{
  25. sync::{Arc, Mutex as SyncMutex},
  26. thread,
  27. time::SystemTime,
  28. };
  29. use crate::{
  30. darkirc::{DarkIrcBackendPtr, Privmsg},
  31. error::Error,
  32. expr::Op,
  33. gfx::{GraphicsEventPublisherPtr, RenderApiPtr, Vertex},
  34. prop::{Property, PropertyBool, PropertyStr, PropertySubType, PropertyType, Role},
  35. scene::{
  36. CallArgType, MethodResponseFn, Pimpl, SceneGraph, SceneGraphPtr2, SceneNodeId,
  37. SceneNodeType, Slot,
  38. },
  39. text::TextShaperPtr,
  40. ui::{chatview, Button, ChatView, EditBox, Image, Mesh, RenderLayer, Stoppable, Text, Window},
  41. ExecutorPtr,
  42. };
  43. //fn print_type_of<T>(_: &T) {
  44. // println!("{}", std::any::type_name::<T>())
  45. //}
  46. #[cfg(target_os = "android")]
  47. const CHATDB_PATH: &str = "/data/data/darkfi.darkwallet/chatdb/";
  48. #[cfg(target_os = "linux")]
  49. const CHATDB_PATH: &str = "chatdb";
  50. #[cfg(target_os = "android")]
  51. const KING_PATH: &str = "king.png";
  52. #[cfg(target_os = "linux")]
  53. const KING_PATH: &str = "assets/king.png";
  54. const LIGHTMODE: bool = false;
  55. fn get_systime() -> u64 {
  56. match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
  57. Ok(n) => n.as_secs(),
  58. Err(_) => panic!("SystemTime before UNIX EPOCH!"),
  59. }
  60. }
  61. pub struct AsyncRuntime {
  62. signal: async_channel::Sender<()>,
  63. shutdown: async_channel::Receiver<()>,
  64. exec_threadpool: SyncMutex<Option<thread::JoinHandle<()>>>,
  65. ex: ExecutorPtr,
  66. tasks: SyncMutex<Vec<Task<()>>>,
  67. }
  68. impl AsyncRuntime {
  69. pub fn new(ex: ExecutorPtr) -> Self {
  70. let (signal, shutdown) = async_channel::unbounded::<()>();
  71. Self {
  72. signal,
  73. shutdown,
  74. exec_threadpool: SyncMutex::new(None),
  75. ex,
  76. tasks: SyncMutex::new(vec![]),
  77. }
  78. }
  79. pub fn start(&self) {
  80. let n_threads = thread::available_parallelism().unwrap().get();
  81. let shutdown = self.shutdown.clone();
  82. let ex = self.ex.clone();
  83. let exec_threadpool = thread::spawn(move || {
  84. easy_parallel::Parallel::new()
  85. // N executor threads
  86. .each(0..n_threads, |_| smol::future::block_on(ex.run(shutdown.recv())))
  87. .run();
  88. });
  89. *self.exec_threadpool.lock().unwrap() = Some(exec_threadpool);
  90. debug!(target: "async_runtime", "Started runtime");
  91. }
  92. pub fn push_task(&self, task: Task<()>) {
  93. self.tasks.lock().unwrap().push(task);
  94. }
  95. pub fn stop(&self) {
  96. // Go through event graph and call stop on everything
  97. // Depth first
  98. debug!(target: "app", "Stopping async runtime...");
  99. let tasks = std::mem::take(&mut *self.tasks.lock().unwrap());
  100. // Close all tasks
  101. smol::future::block_on(async {
  102. // Perform cleanup code
  103. // If not finished in certain amount of time, then just exit
  104. let futures = FuturesUnordered::new();
  105. for task in tasks {
  106. futures.push(task.cancel());
  107. }
  108. let _: Vec<_> = futures.collect().await;
  109. });
  110. if !self.signal.close() {
  111. error!(target: "app", "exec threadpool was already shutdown");
  112. }
  113. let exec_threadpool = std::mem::replace(&mut *self.exec_threadpool.lock().unwrap(), None);
  114. let exec_threadpool = exec_threadpool.expect("threadpool wasnt started");
  115. exec_threadpool.join().unwrap();
  116. debug!(target: "app", "Stopped app");
  117. }
  118. }
  119. pub type AppPtr = Arc<App>;
  120. pub struct App {
  121. sg: SceneGraphPtr2,
  122. ex: ExecutorPtr,
  123. render_api: RenderApiPtr,
  124. event_pub: GraphicsEventPublisherPtr,
  125. text_shaper: TextShaperPtr,
  126. darkirc_backend: DarkIrcBackendPtr,
  127. tasks: SyncMutex<Vec<Task<()>>>,
  128. }
  129. impl App {
  130. pub fn new(
  131. sg: SceneGraphPtr2,
  132. ex: ExecutorPtr,
  133. render_api: RenderApiPtr,
  134. event_pub: GraphicsEventPublisherPtr,
  135. text_shaper: TextShaperPtr,
  136. darkirc_backend: DarkIrcBackendPtr,
  137. ) -> Arc<Self> {
  138. Arc::new(Self {
  139. sg,
  140. ex,
  141. render_api,
  142. event_pub,
  143. text_shaper,
  144. darkirc_backend,
  145. tasks: SyncMutex::new(vec![]),
  146. })
  147. }
  148. pub async fn start(self: Arc<Self>) {
  149. debug!(target: "app", "App::start()");
  150. // Setup UI
  151. let mut sg = self.sg.lock().await;
  152. let window = sg.add_node("window", SceneNodeType::Window);
  153. let mut prop = Property::new("screen_size", PropertyType::Float32, PropertySubType::Pixel);
  154. prop.set_array_len(2);
  155. // Window not yet initialized so we can't set these.
  156. //prop.set_f32(Role::App, 0, screen_width);
  157. //prop.set_f32(Role::App, 1, screen_height);
  158. window.add_property(prop).unwrap();
  159. let mut prop = Property::new("scale", PropertyType::Float32, PropertySubType::Pixel);
  160. prop.set_defaults_f32(vec![1.]).unwrap();
  161. window.add_property(prop).unwrap();
  162. let window_id = window.id;
  163. // Create Window
  164. // Window::new(window, weak sg)
  165. drop(sg);
  166. let pimpl = Window::new(
  167. self.ex.clone(),
  168. self.sg.clone(),
  169. window_id,
  170. self.render_api.clone(),
  171. self.event_pub.clone(),
  172. )
  173. .await;
  174. // -> reads any props it needs
  175. // -> starts procs
  176. let mut sg = self.sg.lock().await;
  177. let node = sg.get_node_mut(window_id).unwrap();
  178. node.pimpl = pimpl;
  179. sg.link(window_id, SceneGraph::ROOT_ID).unwrap();
  180. // Testing
  181. let node = sg.get_node(window_id).unwrap();
  182. node.set_property_f32(Role::App, "scale", 2.).unwrap();
  183. drop(sg);
  184. self.make_me_a_schema_plox().await;
  185. debug!(target: "app", "Schema loaded");
  186. // Access drawable in window node and call draw()
  187. self.trigger_redraw().await;
  188. // Start the backend
  189. if let Err(err) = self.darkirc_backend.start(self.sg.clone(), self.ex.clone()).await {
  190. error!(target: "app", "backend error: {err}");
  191. }
  192. }
  193. pub fn stop(&self) {
  194. smol::future::block_on(async {
  195. self.async_stop().await;
  196. });
  197. }
  198. async fn async_stop(&self) {
  199. self.darkirc_backend.stop().await;
  200. let sg = self.sg.lock().await;
  201. let window_id = sg.lookup_node("/window").unwrap().id;
  202. self.stop_node(&sg, window_id).await;
  203. drop(sg);
  204. }
  205. #[async_recursion]
  206. async fn stop_node(&self, sg: &SceneGraph, node_id: SceneNodeId) {
  207. let node = sg.get_node(node_id).unwrap();
  208. for child_inf in node.get_children2() {
  209. self.stop_node(sg, child_inf.id).await;
  210. }
  211. match &node.pimpl {
  212. Pimpl::Window(win) => win.stop().await,
  213. Pimpl::RenderLayer(layer) => layer.stop().await,
  214. Pimpl::Mesh(mesh) => mesh.stop().await,
  215. Pimpl::Text(txt) => txt.stop().await,
  216. Pimpl::EditBox(ebox) => ebox.stop().await,
  217. Pimpl::ChatView(_) | Pimpl::Image(_) | Pimpl::Button(_) => {}
  218. _ => panic!("unhandled pimpl type"),
  219. };
  220. }
  221. async fn make_me_a_schema_plox(&self) {
  222. let mut tasks = vec![];
  223. // Create a layer called view
  224. let mut sg = self.sg.lock().await;
  225. let layer_node_id = create_layer(&mut sg, "view");
  226. // Customize our layer
  227. let node = sg.get_node(layer_node_id).unwrap();
  228. let prop = node.get_property("rect").unwrap();
  229. prop.set_f32(Role::App, 0, 0.).unwrap();
  230. prop.set_f32(Role::App, 1, 0.).unwrap();
  231. let code = vec![Op::LoadVar("w".to_string())];
  232. prop.set_expr(Role::App, 2, code).unwrap();
  233. let code = vec![Op::LoadVar("h".to_string())];
  234. prop.set_expr(Role::App, 3, code).unwrap();
  235. node.set_property_bool(Role::App, "is_visible", true).unwrap();
  236. // Setup the pimpl
  237. let node_id = node.id;
  238. drop(sg);
  239. let pimpl =
  240. RenderLayer::new(self.ex.clone(), self.sg.clone(), node_id, self.render_api.clone())
  241. .await;
  242. let mut sg = self.sg.lock().await;
  243. let node = sg.get_node_mut(node_id).unwrap();
  244. node.pimpl = pimpl;
  245. let window_id = sg.lookup_node("/window").unwrap().id;
  246. sg.link(node_id, window_id).unwrap();
  247. // Create a bg mesh
  248. let node_id = create_mesh(&mut sg, "bg");
  249. let node = sg.get_node_mut(node_id).unwrap();
  250. let prop = node.get_property("rect").unwrap();
  251. prop.set_f32(Role::App, 0, 0.).unwrap();
  252. prop.set_f32(Role::App, 1, 0.).unwrap();
  253. let code = vec![Op::LoadVar("w".to_string())];
  254. prop.set_expr(Role::App, 2, code).unwrap();
  255. let code = vec![Op::LoadVar("h".to_string())];
  256. prop.set_expr(Role::App, 3, code).unwrap();
  257. let c = if LIGHTMODE { 1. } else { 0. };
  258. // Setup the pimpl
  259. let node_id = node.id;
  260. let (x1, y1) = (0., 0.);
  261. let (x2, y2) = (1., 1.);
  262. let verts = vec![
  263. // top left
  264. Vertex { pos: [x1, y1], color: [c, c, c, 1.], uv: [0., 0.] },
  265. // top right
  266. Vertex { pos: [x2, y1], color: [c, c, c, 1.], uv: [1., 0.] },
  267. // bottom left
  268. Vertex { pos: [x1, y2], color: [c, c, c, 1.], uv: [0., 1.] },
  269. // bottom right
  270. Vertex { pos: [x2, y2], color: [c, c, c, 1.], uv: [1., 1.] },
  271. ];
  272. let indices = vec![0, 2, 1, 1, 2, 3];
  273. drop(sg);
  274. let pimpl = Mesh::new(
  275. self.ex.clone(),
  276. self.sg.clone(),
  277. node_id,
  278. self.render_api.clone(),
  279. verts,
  280. indices,
  281. )
  282. .await;
  283. let mut sg = self.sg.lock().await;
  284. let node = sg.get_node_mut(node_id).unwrap();
  285. node.pimpl = pimpl;
  286. sg.link(node_id, layer_node_id).unwrap();
  287. // Create button bg
  288. let node_id = create_mesh(&mut sg, "btnbg");
  289. let node = sg.get_node_mut(node_id).unwrap();
  290. let prop = node.get_property("rect").unwrap();
  291. let code = vec![Op::Sub((
  292. Box::new(Op::LoadVar("w".to_string())),
  293. Box::new(Op::ConstFloat32(220.)),
  294. ))];
  295. prop.set_expr(Role::App, 0, code).unwrap();
  296. prop.set_f32(Role::App, 1, 10.).unwrap();
  297. prop.set_f32(Role::App, 2, 200.).unwrap();
  298. prop.set_f32(Role::App, 3, 60.).unwrap();
  299. // Setup the pimpl
  300. let (x1, y1) = (0., 0.);
  301. let (x2, y2) = (1., 1.);
  302. let verts = if LIGHTMODE {
  303. vec![
  304. // top left
  305. Vertex { pos: [x1, y1], color: [1., 0., 0., 1.], uv: [0., 0.] },
  306. // top right
  307. Vertex { pos: [x2, y1], color: [1., 0., 0., 1.], uv: [1., 0.] },
  308. // bottom left
  309. Vertex { pos: [x1, y2], color: [1., 0., 0., 1.], uv: [0., 1.] },
  310. // bottom right
  311. Vertex { pos: [x2, y2], color: [1., 0., 0., 1.], uv: [1., 1.] },
  312. ]
  313. } else {
  314. vec![
  315. // top left
  316. Vertex { pos: [x1, y1], color: [1., 0., 0., 1.], uv: [0., 0.] },
  317. // top right
  318. Vertex { pos: [x2, y1], color: [1., 0., 1., 1.], uv: [1., 0.] },
  319. // bottom left
  320. Vertex { pos: [x1, y2], color: [0., 0., 1., 1.], uv: [0., 1.] },
  321. // bottom right
  322. Vertex { pos: [x2, y2], color: [1., 1., 0., 1.], uv: [1., 1.] },
  323. ]
  324. };
  325. let indices = vec![0, 2, 1, 1, 2, 3];
  326. drop(sg);
  327. let pimpl = Mesh::new(
  328. self.ex.clone(),
  329. self.sg.clone(),
  330. node_id,
  331. self.render_api.clone(),
  332. verts,
  333. indices,
  334. )
  335. .await;
  336. let mut sg = self.sg.lock().await;
  337. let node = sg.get_node_mut(node_id).unwrap();
  338. node.pimpl = pimpl;
  339. sg.link(node_id, layer_node_id).unwrap();
  340. // Create the button
  341. let node_id = create_button(&mut sg, "btn");
  342. let node = sg.get_node_mut(node_id).unwrap();
  343. node.set_property_bool(Role::App, "is_active", true).unwrap();
  344. let prop = node.get_property("rect").unwrap();
  345. let code = vec![Op::Sub((
  346. Box::new(Op::LoadVar("w".to_string())),
  347. Box::new(Op::ConstFloat32(220.)),
  348. ))];
  349. prop.set_expr(Role::App, 0, code).unwrap();
  350. prop.set_f32(Role::App, 1, 10.).unwrap();
  351. prop.set_f32(Role::App, 2, 200.).unwrap();
  352. prop.set_f32(Role::App, 3, 60.).unwrap();
  353. let (sender, btn_click_recvr) = async_channel::unbounded();
  354. let slot_click = Slot { name: "button_clicked".to_string(), notify: sender };
  355. node.register("click", slot_click).unwrap();
  356. drop(sg);
  357. let pimpl =
  358. Button::new(self.ex.clone(), self.sg.clone(), node_id, self.event_pub.clone()).await;
  359. let mut sg = self.sg.lock().await;
  360. let node = sg.get_node_mut(node_id).unwrap();
  361. node.pimpl = pimpl;
  362. sg.link(node_id, layer_node_id).unwrap();
  363. // Create another mesh
  364. let node_id = create_mesh(&mut sg, "box");
  365. let node = sg.get_node_mut(node_id).unwrap();
  366. let prop = node.get_property("rect").unwrap();
  367. prop.set_f32(Role::App, 0, 10.).unwrap();
  368. prop.set_f32(Role::App, 1, 10.).unwrap();
  369. prop.set_f32(Role::App, 2, 60.).unwrap();
  370. prop.set_f32(Role::App, 3, 60.).unwrap();
  371. // Setup the pimpl
  372. let (x1, y1) = (0., 0.);
  373. let (x2, y2) = (1., 1.);
  374. let verts = if LIGHTMODE {
  375. vec![
  376. // top left
  377. Vertex { pos: [x1, y1], color: [1., 0., 0., 1.], uv: [0., 0.] },
  378. // top right
  379. Vertex { pos: [x2, y1], color: [1., 0., 0., 1.], uv: [1., 0.] },
  380. // bottom left
  381. Vertex { pos: [x1, y2], color: [1., 0., 0., 1.], uv: [0., 1.] },
  382. // bottom right
  383. Vertex { pos: [x2, y2], color: [1., 0., 0., 1.], uv: [1., 1.] },
  384. ]
  385. } else {
  386. vec![
  387. // top left
  388. Vertex { pos: [x1, y1], color: [1., 0., 0., 1.], uv: [0., 0.] },
  389. // top right
  390. Vertex { pos: [x2, y1], color: [1., 0., 1., 1.], uv: [1., 0.] },
  391. // bottom left
  392. Vertex { pos: [x1, y2], color: [0., 0., 1., 1.], uv: [0., 1.] },
  393. // bottom right
  394. Vertex { pos: [x2, y2], color: [1., 1., 0., 1.], uv: [1., 1.] },
  395. ]
  396. };
  397. let indices = vec![0, 2, 1, 1, 2, 3];
  398. drop(sg);
  399. let pimpl = Mesh::new(
  400. self.ex.clone(),
  401. self.sg.clone(),
  402. node_id,
  403. self.render_api.clone(),
  404. verts,
  405. indices,
  406. )
  407. .await;
  408. let mut sg = self.sg.lock().await;
  409. let node = sg.get_node_mut(node_id).unwrap();
  410. node.pimpl = pimpl;
  411. sg.link(node_id, layer_node_id).unwrap();
  412. // Debugging tool
  413. let node_id = create_mesh(&mut sg, "debugtool");
  414. let node = sg.get_node_mut(node_id).unwrap();
  415. let prop = node.get_property("rect").unwrap();
  416. prop.set_f32(Role::App, 0, 0.).unwrap();
  417. let code =
  418. vec![Op::Div((Box::new(Op::LoadVar("h".to_string())), Box::new(Op::ConstFloat32(2.))))];
  419. prop.set_expr(Role::App, 1, code).unwrap();
  420. let code = vec![Op::LoadVar("w".to_string())];
  421. prop.set_expr(Role::App, 2, code).unwrap();
  422. prop.set_f32(Role::App, 3, 5.).unwrap();
  423. node.set_property_u32(Role::App, "z_index", 2).unwrap();
  424. // Setup the pimpl
  425. let (x1, y1) = (0., 0.);
  426. let (x2, y2) = (1., 1.);
  427. let verts = vec![
  428. // top left
  429. Vertex { pos: [x1, y1], color: [0., 1., 0., 1.], uv: [0., 0.] },
  430. // top right
  431. Vertex { pos: [x2, y1], color: [0., 1., 0., 1.], uv: [1., 0.] },
  432. // bottom left
  433. Vertex { pos: [x1, y2], color: [0., 1., 0., 1.], uv: [0., 1.] },
  434. // bottom right
  435. Vertex { pos: [x2, y2], color: [0., 1., 0., 1.], uv: [1., 1.] },
  436. ];
  437. let indices = vec![0, 2, 1, 1, 2, 3];
  438. drop(sg);
  439. let pimpl = Mesh::new(
  440. self.ex.clone(),
  441. self.sg.clone(),
  442. node_id,
  443. self.render_api.clone(),
  444. verts,
  445. indices,
  446. )
  447. .await;
  448. let mut sg = self.sg.lock().await;
  449. let node = sg.get_node_mut(node_id).unwrap();
  450. node.pimpl = pimpl;
  451. sg.link(node_id, layer_node_id).unwrap();
  452. // Debugging tool
  453. let node_id = create_mesh(&mut sg, "debugtool2");
  454. let node = sg.get_node_mut(node_id).unwrap();
  455. let prop = node.get_property("rect").unwrap();
  456. prop.set_f32(Role::App, 0, 0.).unwrap();
  457. let code = vec![Op::Sub((
  458. Box::new(Op::LoadVar("h".to_string())),
  459. Box::new(Op::ConstFloat32(200.)),
  460. ))];
  461. prop.set_expr(Role::App, 1, code).unwrap();
  462. let code = vec![Op::LoadVar("w".to_string())];
  463. prop.set_expr(Role::App, 2, code).unwrap();
  464. prop.set_f32(Role::App, 3, 5.).unwrap();
  465. node.set_property_u32(Role::App, "z_index", 2).unwrap();
  466. // Setup the pimpl
  467. let (x1, y1) = (0., 0.);
  468. let (x2, y2) = (1., 1.);
  469. let verts = vec![
  470. // top left
  471. Vertex { pos: [x1, y1], color: [0., 1., 0., 1.], uv: [0., 0.] },
  472. // top right
  473. Vertex { pos: [x2, y1], color: [0., 1., 0., 1.], uv: [1., 0.] },
  474. // bottom left
  475. Vertex { pos: [x1, y2], color: [0., 1., 0., 1.], uv: [0., 1.] },
  476. // bottom right
  477. Vertex { pos: [x2, y2], color: [0., 1., 0., 1.], uv: [1., 1.] },
  478. ];
  479. let indices = vec![0, 2, 1, 1, 2, 3];
  480. drop(sg);
  481. let pimpl = Mesh::new(
  482. self.ex.clone(),
  483. self.sg.clone(),
  484. node_id,
  485. self.render_api.clone(),
  486. verts,
  487. indices,
  488. )
  489. .await;
  490. let mut sg = self.sg.lock().await;
  491. let node = sg.get_node_mut(node_id).unwrap();
  492. node.pimpl = pimpl;
  493. sg.link(node_id, layer_node_id).unwrap();
  494. // Create KING GNU!
  495. let node_id = create_image(&mut sg, "king");
  496. let node = sg.get_node_mut(node_id).unwrap();
  497. let prop = node.get_property("rect").unwrap();
  498. prop.set_f32(Role::App, 0, 80.).unwrap();
  499. prop.set_f32(Role::App, 1, 10.).unwrap();
  500. prop.set_f32(Role::App, 2, 60.).unwrap();
  501. prop.set_f32(Role::App, 3, 60.).unwrap();
  502. node.set_property_str(Role::App, "path", KING_PATH).unwrap();
  503. // Setup the pimpl
  504. drop(sg);
  505. let pimpl =
  506. Image::new(self.ex.clone(), self.sg.clone(), node_id, self.render_api.clone()).await;
  507. let mut sg = self.sg.lock().await;
  508. let node = sg.get_node_mut(node_id).unwrap();
  509. node.pimpl = pimpl;
  510. sg.link(node_id, layer_node_id).unwrap();
  511. // Create some text
  512. let node_id = create_text(&mut sg, "label");
  513. let node = sg.get_node_mut(node_id).unwrap();
  514. let prop = node.get_property("rect").unwrap();
  515. prop.set_f32(Role::App, 0, 100.).unwrap();
  516. prop.set_f32(Role::App, 1, 100.).unwrap();
  517. prop.set_f32(Role::App, 2, 800.).unwrap();
  518. prop.set_f32(Role::App, 3, 200.).unwrap();
  519. node.set_property_f32(Role::App, "baseline", 40.).unwrap();
  520. node.set_property_f32(Role::App, "font_size", 60.).unwrap();
  521. node.set_property_str(Role::App, "text", "anon1🍆").unwrap();
  522. //node.set_property_str(Role::App, "text", "anon1").unwrap();
  523. let prop = node.get_property("text_color").unwrap();
  524. prop.set_f32(Role::App, 0, 0.).unwrap();
  525. prop.set_f32(Role::App, 1, 1.).unwrap();
  526. prop.set_f32(Role::App, 2, 0.).unwrap();
  527. prop.set_f32(Role::App, 3, 1.).unwrap();
  528. drop(sg);
  529. let pimpl = Text::new(
  530. self.ex.clone(),
  531. self.sg.clone(),
  532. node_id,
  533. self.render_api.clone(),
  534. self.text_shaper.clone(),
  535. )
  536. .await;
  537. let mut sg = self.sg.lock().await;
  538. let node = sg.get_node_mut(node_id).unwrap();
  539. node.pimpl = pimpl;
  540. sg.link(node_id, layer_node_id).unwrap();
  541. // Text edit
  542. let node_id = create_editbox(&mut sg, "editz");
  543. let node = sg.get_node(node_id).unwrap();
  544. node.set_property_bool(Role::App, "is_active", true).unwrap();
  545. let prop = node.get_property("rect").unwrap();
  546. prop.set_f32(Role::App, 0, 150.).unwrap();
  547. prop.set_f32(Role::App, 1, 150.).unwrap();
  548. prop.set_f32(Role::App, 2, 380.).unwrap();
  549. //let code = vec![Op::Sub((
  550. // Box::new(Op::LoadVar("h".to_string())),
  551. // Box::new(Op::ConstFloat32(60.)),
  552. //))];
  553. //prop.set_expr(Role::App, 1, code).unwrap();
  554. //let code = vec![Op::Sub((
  555. // Box::new(Op::LoadVar("w".to_string())),
  556. // Box::new(Op::ConstFloat32(120.)),
  557. //))];
  558. //prop.set_expr(Role::App, 2, code).unwrap();
  559. prop.set_f32(Role::App, 3, 60.).unwrap();
  560. node.set_property_f32(Role::App, "baseline", 40.).unwrap();
  561. node.set_property_f32(Role::App, "font_size", 20.).unwrap();
  562. node.set_property_f32(Role::App, "font_size", 40.).unwrap();
  563. node.set_property_str(Role::App, "text", "hello king!😁🍆jelly 🍆1234").unwrap();
  564. let prop = node.get_property("text_color").unwrap();
  565. if LIGHTMODE {
  566. prop.set_f32(Role::App, 0, 0.).unwrap();
  567. prop.set_f32(Role::App, 1, 0.).unwrap();
  568. prop.set_f32(Role::App, 2, 0.).unwrap();
  569. prop.set_f32(Role::App, 3, 1.).unwrap();
  570. } else {
  571. prop.set_f32(Role::App, 0, 1.).unwrap();
  572. prop.set_f32(Role::App, 1, 1.).unwrap();
  573. prop.set_f32(Role::App, 2, 1.).unwrap();
  574. prop.set_f32(Role::App, 3, 1.).unwrap();
  575. }
  576. let prop = node.get_property("cursor_color").unwrap();
  577. prop.set_f32(Role::App, 0, 1.).unwrap();
  578. prop.set_f32(Role::App, 1, 0.5).unwrap();
  579. prop.set_f32(Role::App, 2, 0.5).unwrap();
  580. prop.set_f32(Role::App, 3, 1.).unwrap();
  581. let prop = node.get_property("hi_bg_color").unwrap();
  582. if LIGHTMODE {
  583. prop.set_f32(Role::App, 0, 0.5).unwrap();
  584. prop.set_f32(Role::App, 1, 0.5).unwrap();
  585. prop.set_f32(Role::App, 2, 0.5).unwrap();
  586. prop.set_f32(Role::App, 3, 1.).unwrap();
  587. } else {
  588. prop.set_f32(Role::App, 0, 1.).unwrap();
  589. prop.set_f32(Role::App, 1, 1.).unwrap();
  590. prop.set_f32(Role::App, 2, 1.).unwrap();
  591. prop.set_f32(Role::App, 3, 0.5).unwrap();
  592. }
  593. let prop = node.get_property("selected").unwrap();
  594. prop.set_null(Role::App, 0).unwrap();
  595. prop.set_null(Role::App, 1).unwrap();
  596. node.set_property_u32(Role::App, "z_index", 1).unwrap();
  597. //node.set_property_bool(Role::App, "debug", true).unwrap();
  598. let editbox_text = PropertyStr::wrap(node, Role::App, "text", 0).unwrap();
  599. let editbox_focus = PropertyBool::wrap(node, Role::App, "is_focused", 0).unwrap();
  600. let darkirc_backend = self.darkirc_backend.clone();
  601. let task = self.ex.spawn(async move {
  602. while let Ok(_) = btn_click_recvr.recv().await {
  603. let text = editbox_text.get();
  604. editbox_text.prop().unset(Role::App, 0).unwrap();
  605. // Clicking outside the editbox makes it lose focus
  606. // So lets focus it again
  607. editbox_focus.set(true);
  608. debug!(target: "app", "sending text {text}");
  609. let privmsg =
  610. Privmsg { channel: "#random".to_string(), nick: "king".to_string(), msg: text };
  611. darkirc_backend.send(privmsg).await;
  612. }
  613. });
  614. tasks.push(task);
  615. drop(sg);
  616. let pimpl = EditBox::new(
  617. self.ex.clone(),
  618. self.sg.clone(),
  619. node_id,
  620. self.render_api.clone(),
  621. self.event_pub.clone(),
  622. self.text_shaper.clone(),
  623. )
  624. .await;
  625. let mut sg = self.sg.lock().await;
  626. let node = sg.get_node_mut(node_id).unwrap();
  627. node.pimpl = pimpl;
  628. sg.link(node_id, layer_node_id).unwrap();
  629. // ChatView
  630. let (node_id, recvr) = create_chatview(&mut sg, "chatty");
  631. let node = sg.get_node(node_id).unwrap();
  632. let prop = node.get_property("rect").unwrap();
  633. prop.set_f32(Role::App, 0, 0.).unwrap();
  634. let code =
  635. vec![Op::Div((Box::new(Op::LoadVar("h".to_string())), Box::new(Op::ConstFloat32(2.))))];
  636. prop.set_expr(Role::App, 1, code).unwrap();
  637. let code = vec![Op::LoadVar("w".to_string())];
  638. prop.set_expr(Role::App, 2, code).unwrap();
  639. let code = vec![Op::Sub((
  640. Box::new(Op::Div((
  641. Box::new(Op::LoadVar("h".to_string())),
  642. Box::new(Op::ConstFloat32(2.)),
  643. ))),
  644. Box::new(Op::ConstFloat32(200.)),
  645. ))];
  646. prop.set_expr(Role::App, 3, code).unwrap();
  647. node.set_property_f32(Role::App, "font_size", 20.).unwrap();
  648. node.set_property_f32(Role::App, "line_height", 30.).unwrap();
  649. node.set_property_f32(Role::App, "baseline", 20.).unwrap();
  650. node.set_property_u32(Role::App, "z_index", 1).unwrap();
  651. //node.set_property_bool(Role::App, "debug", true).unwrap();
  652. let prop = node.get_property("timestamp_color").unwrap();
  653. prop.set_f32(Role::App, 0, 0.5).unwrap();
  654. prop.set_f32(Role::App, 1, 0.5).unwrap();
  655. prop.set_f32(Role::App, 2, 0.5).unwrap();
  656. prop.set_f32(Role::App, 3, 0.5).unwrap();
  657. let prop = node.get_property("text_color").unwrap();
  658. if LIGHTMODE {
  659. prop.set_f32(Role::App, 0, 0.).unwrap();
  660. prop.set_f32(Role::App, 1, 0.).unwrap();
  661. prop.set_f32(Role::App, 2, 0.).unwrap();
  662. prop.set_f32(Role::App, 3, 1.).unwrap();
  663. } else {
  664. prop.set_f32(Role::App, 0, 1.).unwrap();
  665. prop.set_f32(Role::App, 1, 1.).unwrap();
  666. prop.set_f32(Role::App, 2, 1.).unwrap();
  667. prop.set_f32(Role::App, 3, 1.).unwrap();
  668. }
  669. let prop = node.get_property("nick_colors").unwrap();
  670. #[rustfmt::skip]
  671. let nick_colors = [
  672. 0.00, 0.94, 1.00, 1.,
  673. 0.36, 1.00, 0.69, 1.,
  674. 0.29, 1.00, 0.45, 1.,
  675. 0.00, 0.73, 0.38, 1.,
  676. 0.21, 0.67, 0.67, 1.,
  677. 0.56, 0.61, 1.00, 1.,
  678. 0.84, 0.48, 1.00, 1.,
  679. 1.00, 0.61, 0.94, 1.,
  680. 1.00, 0.36, 0.48, 1.,
  681. 1.00, 0.30, 0.00, 1.
  682. ];
  683. for c in nick_colors {
  684. prop.push_f32(Role::App, c).unwrap();
  685. }
  686. drop(sg);
  687. let db = sled::open(CHATDB_PATH).expect("cannot open sleddb");
  688. let chat_tree = db.open_tree(b"chat").unwrap();
  689. //if chat_tree.is_empty() {
  690. // populate_tree(&chat_tree);
  691. //}
  692. debug!(target: "app", "db has {} lines", chat_tree.len());
  693. let pimpl = ChatView::new(
  694. self.ex.clone(),
  695. self.sg.clone(),
  696. node_id,
  697. self.render_api.clone(),
  698. self.event_pub.clone(),
  699. self.text_shaper.clone(),
  700. chat_tree,
  701. recvr,
  702. )
  703. .await;
  704. let mut sg = self.sg.lock().await;
  705. let node = sg.get_node_mut(node_id).unwrap();
  706. node.pimpl = pimpl;
  707. sg.link(node_id, layer_node_id).unwrap();
  708. // On android lets scale the UI up
  709. // TODO: add support for fractional scaling
  710. // This also affects mouse/touch input since coords need to be accurately translated
  711. // Also we need to think about nesting of layers.
  712. //let window_node = sg.get_node_mut(window_id).unwrap();
  713. //win_node.set_property_f32(Role::App, "scale", 1.6).unwrap();
  714. *self.tasks.lock().unwrap() = tasks;
  715. }
  716. async fn trigger_redraw(&self) {
  717. let sg = self.sg.lock().await;
  718. let window_node = sg.lookup_node("/window").expect("no window attached!");
  719. match &window_node.pimpl {
  720. Pimpl::Window(win) => win.draw(&sg).await,
  721. _ => panic!("wrong pimpl"),
  722. }
  723. }
  724. }
  725. impl Drop for App {
  726. fn drop(&mut self) {
  727. debug!(target: "app", "Dropping app");
  728. // This hangs
  729. //self.stop();
  730. }
  731. }
  732. // Just for testing
  733. fn populate_tree(tree: &sled::Tree) {
  734. let chat_txt = include_str!("../chat.txt");
  735. for line in chat_txt.lines() {
  736. let parts: Vec<&str> = line.splitn(3, ' ').collect();
  737. assert_eq!(parts.len(), 3);
  738. let time_parts: Vec<&str> = parts[0].splitn(2, ':').collect();
  739. let (hour, min) = (time_parts[0], time_parts[1]);
  740. let hour = hour.parse::<u32>().unwrap();
  741. let min = min.parse::<u32>().unwrap();
  742. let dt: NaiveDateTime =
  743. NaiveDate::from_ymd_opt(2024, 8, 6).unwrap().and_hms_opt(hour, min, 0).unwrap();
  744. let timest = dt.and_utc().timestamp() as u64;
  745. let message_id = [0u8; 32];
  746. let nick = parts[1].to_string();
  747. let text = parts[2].to_string();
  748. // serial order is important here
  749. let timest = timest.to_be_bytes();
  750. assert_eq!(timest.len(), 8);
  751. let mut key = [0u8; 8 + 32];
  752. key[..8].clone_from_slice(&timest);
  753. let msg = chatview::ChatMsg { nick, text };
  754. let mut val = vec![];
  755. msg.encode(&mut val).unwrap();
  756. tree.insert(&key, val).unwrap();
  757. }
  758. // O(n)
  759. debug!(target: "app", "populated db with {} lines", tree.len());
  760. }
  761. pub fn create_layer(sg: &mut SceneGraph, name: &str) -> SceneNodeId {
  762. debug!(target: "app", "create_layer({name})");
  763. let node = sg.add_node(name, SceneNodeType::RenderLayer);
  764. let prop = Property::new("is_visible", PropertyType::Bool, PropertySubType::Null);
  765. node.add_property(prop).unwrap();
  766. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  767. prop.set_array_len(4);
  768. prop.allow_exprs();
  769. node.add_property(prop).unwrap();
  770. node.id
  771. }
  772. pub fn create_mesh(sg: &mut SceneGraph, name: &str) -> SceneNodeId {
  773. debug!(target: "app", "create_mesh({name})");
  774. let node = sg.add_node(name, SceneNodeType::RenderMesh);
  775. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  776. prop.set_array_len(4);
  777. prop.allow_exprs();
  778. node.add_property(prop).unwrap();
  779. let prop = Property::new("z_index", PropertyType::Uint32, PropertySubType::Null);
  780. node.add_property(prop).unwrap();
  781. node.id
  782. }
  783. pub fn create_button(sg: &mut SceneGraph, name: &str) -> SceneNodeId {
  784. debug!(target: "app", "create_button({name})");
  785. let node = sg.add_node(name, SceneNodeType::Button);
  786. let mut prop = Property::new("is_active", PropertyType::Bool, PropertySubType::Null);
  787. prop.set_ui_text("Is Active", "An active Button can be clicked");
  788. node.add_property(prop).unwrap();
  789. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  790. prop.set_array_len(4);
  791. prop.allow_exprs();
  792. node.add_property(prop).unwrap();
  793. node.add_signal("click", "Button clicked event", vec![]).unwrap();
  794. node.id
  795. }
  796. pub fn create_image(sg: &mut SceneGraph, name: &str) -> SceneNodeId {
  797. debug!(target: "app", "create_image({name})");
  798. let node = sg.add_node(name, SceneNodeType::RenderMesh);
  799. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  800. prop.set_array_len(4);
  801. prop.allow_exprs();
  802. node.add_property(prop).unwrap();
  803. let prop = Property::new("z_index", PropertyType::Uint32, PropertySubType::Null);
  804. node.add_property(prop).unwrap();
  805. let prop = Property::new("path", PropertyType::Str, PropertySubType::Null);
  806. node.add_property(prop).unwrap();
  807. node.id
  808. }
  809. fn create_text(sg: &mut SceneGraph, name: &str) -> SceneNodeId {
  810. debug!(target: "app", "create_text({name})");
  811. let node = sg.add_node(name, SceneNodeType::RenderText);
  812. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  813. prop.set_array_len(4);
  814. prop.allow_exprs();
  815. node.add_property(prop).unwrap();
  816. let prop = Property::new("baseline", PropertyType::Float32, PropertySubType::Pixel);
  817. node.add_property(prop).unwrap();
  818. let prop = Property::new("font_size", PropertyType::Float32, PropertySubType::Pixel);
  819. node.add_property(prop).unwrap();
  820. let prop = Property::new("text", PropertyType::Str, PropertySubType::Null);
  821. node.add_property(prop).unwrap();
  822. let mut prop = Property::new("text_color", PropertyType::Float32, PropertySubType::Color);
  823. prop.set_array_len(4);
  824. prop.set_range_f32(0., 1.);
  825. node.add_property(prop).unwrap();
  826. let prop = Property::new("z_index", PropertyType::Uint32, PropertySubType::Null);
  827. node.add_property(prop).unwrap();
  828. let prop = Property::new("debug", PropertyType::Bool, PropertySubType::Null);
  829. node.add_property(prop).unwrap();
  830. node.id
  831. }
  832. fn create_editbox(sg: &mut SceneGraph, name: &str) -> SceneNodeId {
  833. debug!(target: "app", "create_editbox({name})");
  834. let node = sg.add_node(name, SceneNodeType::EditBox);
  835. let mut prop = Property::new("is_active", PropertyType::Bool, PropertySubType::Null);
  836. prop.set_ui_text("Is Active", "An active EditBox can be focused");
  837. node.add_property(prop).unwrap();
  838. let mut prop = Property::new("is_focused", PropertyType::Bool, PropertySubType::Null);
  839. prop.set_ui_text("Is Focused", "A focused EditBox receives input");
  840. node.add_property(prop).unwrap();
  841. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  842. prop.set_array_len(4);
  843. prop.allow_exprs();
  844. node.add_property(prop).unwrap();
  845. let mut prop = Property::new("baseline", PropertyType::Float32, PropertySubType::Pixel);
  846. node.add_property(prop).unwrap();
  847. let mut prop = Property::new("scroll", PropertyType::Float32, PropertySubType::Pixel);
  848. prop.set_range_f32(0., f32::MAX);
  849. node.add_property(prop).unwrap();
  850. let mut prop = Property::new("cursor_pos", PropertyType::Uint32, PropertySubType::Pixel);
  851. prop.set_range_u32(0, u32::MAX);
  852. node.add_property(prop).unwrap();
  853. let mut prop = Property::new("font_size", PropertyType::Float32, PropertySubType::Pixel);
  854. node.add_property(prop).unwrap();
  855. let mut prop = Property::new("text", PropertyType::Str, PropertySubType::Null);
  856. node.add_property(prop).unwrap();
  857. let mut prop = Property::new("text_color", PropertyType::Float32, PropertySubType::Color);
  858. prop.set_array_len(4);
  859. prop.set_range_f32(0., 1.);
  860. node.add_property(prop).unwrap();
  861. let mut prop = Property::new("cursor_color", PropertyType::Float32, PropertySubType::Color);
  862. prop.set_array_len(4);
  863. prop.set_range_f32(0., 1.);
  864. node.add_property(prop).unwrap();
  865. let mut prop = Property::new("hi_bg_color", PropertyType::Float32, PropertySubType::Color);
  866. prop.set_array_len(4);
  867. prop.set_range_f32(0., 1.);
  868. node.add_property(prop).unwrap();
  869. let mut prop = Property::new("selected", PropertyType::Uint32, PropertySubType::Color);
  870. prop.set_array_len(2);
  871. prop.allow_null_values();
  872. node.add_property(prop).unwrap();
  873. let mut prop = Property::new("z_index", PropertyType::Uint32, PropertySubType::Null);
  874. node.add_property(prop).unwrap();
  875. let mut prop = Property::new("debug", PropertyType::Bool, PropertySubType::Null);
  876. node.add_property(prop).unwrap();
  877. node.id
  878. }
  879. fn create_chatview(
  880. sg: &mut SceneGraph,
  881. name: &str,
  882. ) -> (SceneNodeId, async_channel::Receiver<Vec<u8>>) {
  883. debug!(target: "app", "create_chatview({name})");
  884. let node = sg.add_node(name, SceneNodeType::ChatView);
  885. let mut prop = Property::new("rect", PropertyType::Float32, PropertySubType::Pixel);
  886. prop.set_array_len(4);
  887. prop.allow_exprs();
  888. node.add_property(prop).unwrap();
  889. let mut prop = Property::new("scroll", PropertyType::Float32, PropertySubType::Null);
  890. prop.set_ui_text("Scroll", "Scroll up from the bottom");
  891. prop.set_range_f32(0., f32::MAX);
  892. node.add_property(prop).unwrap();
  893. let prop = Property::new("font_size", PropertyType::Float32, PropertySubType::Pixel);
  894. node.add_property(prop).unwrap();
  895. let prop = Property::new("line_height", PropertyType::Float32, PropertySubType::Pixel);
  896. node.add_property(prop).unwrap();
  897. let mut prop = Property::new("timestamp_color", PropertyType::Float32, PropertySubType::Color);
  898. prop.set_array_len(4);
  899. prop.set_range_f32(0., 1.);
  900. node.add_property(prop).unwrap();
  901. let mut prop = Property::new("text_color", PropertyType::Float32, PropertySubType::Color);
  902. prop.set_array_len(4);
  903. prop.set_range_f32(0., 1.);
  904. node.add_property(prop).unwrap();
  905. let mut prop = Property::new("nick_colors", PropertyType::Float32, PropertySubType::Pixel);
  906. prop.set_unbounded();
  907. prop.set_range_f32(0., 1.);
  908. node.add_property(prop).unwrap();
  909. let prop = Property::new("baseline", PropertyType::Float32, PropertySubType::Pixel);
  910. node.add_property(prop).unwrap();
  911. let mut prop = Property::new("z_index", PropertyType::Uint32, PropertySubType::Null);
  912. node.add_property(prop).unwrap();
  913. let mut prop = Property::new("debug", PropertyType::Bool, PropertySubType::Null);
  914. node.add_property(prop).unwrap();
  915. let mut prop =
  916. Property::new("mouse_scroll_start_accel", PropertyType::Float32, PropertySubType::Pixel);
  917. prop.set_ui_text("Mouse Scroll Start Acceleration", "Initial acceperation when scrolling");
  918. prop.set_defaults_f32(vec![4.]).unwrap();
  919. node.add_property(prop).unwrap();
  920. let mut prop =
  921. Property::new("mouse_scroll_decel", PropertyType::Float32, PropertySubType::Pixel);
  922. prop.set_ui_text(
  923. "Mouse Scroll Deceleration",
  924. "Deceleration factor for mouse scroll acceleration",
  925. );
  926. prop.set_range_f32(0., 1.);
  927. prop.set_defaults_f32(vec![0.5]).unwrap();
  928. node.add_property(prop).unwrap();
  929. let mut prop =
  930. Property::new("mouse_scroll_resist", PropertyType::Float32, PropertySubType::Pixel);
  931. prop.set_ui_text("Mouse Scroll Resistance", "How quickly scrolling speed is dampened");
  932. prop.set_range_f32(0., 1.);
  933. prop.set_defaults_f32(vec![0.9]).unwrap();
  934. node.add_property(prop).unwrap();
  935. let (sender, recvr) = async_channel::unbounded::<Vec<u8>>();
  936. let method = move |data: Vec<u8>, response_fn: MethodResponseFn| {
  937. if sender.try_send(data).is_err() {
  938. response_fn(Err(Error::ChannelClosed));
  939. } else {
  940. response_fn(Ok(vec![]));
  941. }
  942. };
  943. node.add_method(
  944. "insert_line",
  945. vec![
  946. ("timestamp", "Timestamp", CallArgType::Uint64),
  947. ("id", "Message ID", CallArgType::Hash),
  948. ("nick", "Nickname", CallArgType::Str),
  949. ("text", "Text", CallArgType::Str),
  950. ],
  951. vec![],
  952. Box::new(method),
  953. )
  954. .unwrap();
  955. (node.id, recvr)
  956. }