Просмотр исходного кода

darkwallet: image widget able to load images from APK assets

darkfi 2 лет назад
Родитель
Сommit
b31bff1728

+ 1 - 0
bin/darkwallet/Cargo.toml

@@ -60,6 +60,7 @@ package_name = "darkfi.darkwallet"
 label = "DarkWallet"
 res = "res"
 icon = "@mipmap/ic_launcher"
+assets = "assets"
 
 [[package.metadata.android.permission]]
 name = "android.permission.INTERNET"

+ 0 - 0
bin/darkwallet/king.png → bin/darkwallet/assets/king.png


+ 7 - 1
bin/darkwallet/src/app.rs

@@ -34,6 +34,11 @@ use crate::{
 //    println!("{}", std::any::type_name::<T>())
 //}
 
+#[cfg(target_os = "android")]
+const CHATDB_PATH: &str = "/data/data/darkfi.darkwallet/chatdb/";
+#[cfg(target_os = "android")]
+//const KING_PATH: &str = "/data/data/darkfi.darkwallet/assets/king.png";
+const KING_PATH: &str = "king.png";
 pub struct AsyncRuntime {
     signal: smol::channel::Sender<()>,
     shutdown: smol::channel::Receiver<()>,
@@ -166,6 +171,7 @@ impl App {
         drop(sg);
 
         self.make_me_a_schema_plox().await;
+        debug!(target: "app", "Schema loaded");
 
         // Access drawable in window node and call draw()
         self.trigger_redraw().await;
@@ -358,7 +364,7 @@ impl App {
         prop.set_f32(2, 60.).unwrap();
         prop.set_f32(3, 60.).unwrap();
 
-        node.set_property_str("path", "../../king.png").unwrap();
+        node.set_property_str("path", KING_PATH).unwrap();
 
         // Setup the pimpl
         drop(sg);

+ 14 - 2
bin/darkwallet/src/ui/image.rs

@@ -18,8 +18,9 @@
 
 //use async_lock::Mutex;
 use miniquad::{BufferId, TextureId};
+use image::ImageReader;
 use rand::{rngs::OsRng, Rng};
-use std::sync::{Arc, Mutex as SyncMutex, Weak};
+use std::{io::Cursor, sync::{Arc, Mutex as SyncMutex, Weak}};
 
 use crate::{
     gfx2::{DrawCall, DrawInstruction, DrawMesh, Rectangle, RenderApi, RenderApiPtr, Vertex},
@@ -104,8 +105,19 @@ impl Image {
 
     async fn load_texture(&self) -> TextureId {
         let path = self.path.get();
+
         // TODO we should NOT use unwrap here
-        let img = image::ImageReader::open(path).unwrap().decode().unwrap().to_rgba8();
+        let data = Arc::new(SyncMutex::new(vec![]));
+        let data2 = data.clone();
+        miniquad::fs::load_file(&path, move |res| {
+            *data2.lock().unwrap() = res.unwrap();
+        });
+        let data = std::mem::take(&mut *data.lock().unwrap());
+        let img = ImageReader::new(Cursor::new(data)).with_guessed_format().unwrap().decode().unwrap();
+        let img = img.to_rgba8();
+
+        //let img = image::ImageReader::open(path).unwrap().decode().unwrap().to_rgba8();
+
         let width = img.width() as u16;
         let height = img.height() as u16;
         let bmp = img.into_raw();