Răsfoiți Sursa

wallet: ctrl+-/= will zoom in/out the window scale, and also persist this between restarts.

darkfi 1 an în urmă
părinte
comite
16a2f1f1cb
2 a modificat fișierele cu 77 adăugiri și 4 ștergeri
  1. 8 0
      bin/darkwallet/src/app/mod.rs
  2. 69 4
      bin/darkwallet/src/app/schema/mod.rs

+ 8 - 0
bin/darkwallet/src/app/mod.rs

@@ -24,6 +24,7 @@ use futures::{stream::FuturesUnordered, StreamExt};
 use sled_overlay::sled;
 use smol::Task;
 use std::{
+    fs::File,
     io::Cursor,
     sync::{Arc, Mutex as SyncMutex},
     thread,
@@ -47,6 +48,7 @@ use crate::{
 mod node;
 use node::create_darkirc;
 mod schema;
+use schema::get_window_scale_filename;
 
 macro_rules! d { ($($arg:tt)*) => { debug!(target: "app", $($arg)*); } }
 macro_rules! t { ($($arg:tt)*) => { trace!(target: "app", $($arg)*); } }
@@ -318,6 +320,12 @@ impl App {
         prop.clone().set_f32(atom, Role::App, 0, screen_width);
         prop.clone().set_f32(atom, Role::App, 1, screen_height);
 
+        let mut window_scale = 1.;
+        if let Ok(mut file) = File::open(get_window_scale_filename()) {
+            window_scale = Decodable::decode(&mut file).unwrap();
+        }
+        window_node.set_property_f32(atom, Role::App, "scale", window_scale).unwrap();
+
         drop(atom);
 
         // Access drawable in window node and call draw()

+ 69 - 4
bin/darkwallet/src/app/schema/mod.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use darkfi_serial::Encodable;
 use sled_overlay::sled;
 use std::fs::File;
 
@@ -23,7 +24,7 @@ use crate::{
     app::{
         node::{
             create_button, create_chatedit, create_chatview, create_editbox, create_image,
-            create_layer, create_text, create_vector_art,
+            create_layer, create_shortcut, create_text, create_vector_art,
         },
         populate_tree, App,
     },
@@ -39,8 +40,8 @@ use crate::{
     shape,
     text::TextShaperPtr,
     ui::{
-        emoji_picker, Button, ChatEdit, ChatView, EditBox, Image, Layer, ShapeVertex, Text,
-        VectorArt, VectorShape, Window,
+        emoji_picker, Button, ChatEdit, ChatView, EditBox, Image, Layer, ShapeVertex, Shortcut,
+        Text, VectorArt, VectorShape, Window,
     },
     ExecutorPtr,
 };
@@ -72,6 +73,10 @@ mod ui_consts {
     pub fn get_first_time_filename() -> PathBuf {
         get_appdata_path().join("first_time")
     }
+
+    pub fn get_window_scale_filename() -> PathBuf {
+        get_appdata_path().join("window_scale")
+    }
 }
 
 #[cfg(not(target_os = "android"))]
@@ -86,6 +91,10 @@ mod desktop_paths {
     pub fn get_first_time_filename() -> PathBuf {
         dirs::cache_dir().unwrap().join("darkfi/wallet/first_time")
     }
+
+    pub fn get_window_scale_filename() -> PathBuf {
+        dirs::cache_dir().unwrap().join("darkfi/wallet/window_scale")
+    }
 }
 
 #[cfg(feature = "emulate-android")]
@@ -104,7 +113,7 @@ mod ui_consts {
     pub use super::desktop_paths::*;
 }
 
-use ui_consts::*;
+pub use ui_consts::*;
 
 pub static CHANNELS: &'static [&str] =
     &["dev", "media", "hackers", "memes", "philosophy", "markets", "math", "random"];
@@ -121,6 +130,62 @@ pub async fn make(app: &App, window: SceneNodePtr) {
 
     let atom = &mut PropertyAtomicGuard::new();
 
+    let node = create_shortcut("zoom_out_shortcut");
+    node.set_property_str(atom, Role::App, "key", "ctrl+-").unwrap();
+    // Not sure what was eating my keys. This is a workaround.
+    node.set_property_u32(atom, Role::App, "priority", 10).unwrap();
+    let (slot, recvr) = Slot::new("zoom_out_pressed");
+    node.register("shortcut", slot).unwrap();
+    let window_scale = PropertyFloat32::wrap(&window, Role::App, "scale", 0).unwrap();
+    let window_scale2 = window_scale.clone();
+    let listen_zoom = app.ex.spawn(async move {
+        while let Ok(_) = recvr.recv().await {
+            let scale = 0.9 * window_scale2.get();
+
+            let filename = get_window_scale_filename();
+            if let Some(parent) = filename.parent() {
+                let _ = std::fs::create_dir_all(parent);
+            }
+            if let Ok(mut file) = File::create(filename) {
+                scale.encode(&mut file).unwrap();
+            }
+
+            let atom = &mut PropertyAtomicGuard::new();
+            window_scale2.set(atom, scale);
+        }
+    });
+    app.tasks.lock().unwrap().push(listen_zoom);
+    let node = node.setup(|me| Shortcut::new(me)).await;
+    window.clone().link(node);
+
+    let node = create_shortcut("zoom_in_shortcut");
+    node.set_property_str(atom, Role::App, "key", "ctrl+=").unwrap();
+    // Not sure what was eating my keys. This is a workaround.
+    node.set_property_u32(atom, Role::App, "priority", 10).unwrap();
+    let (slot, recvr) = Slot::new("zoom_in_pressed");
+    node.register("shortcut", slot).unwrap();
+    let window_scale = PropertyFloat32::wrap(&window, Role::App, "scale", 0).unwrap();
+    let window_scale2 = window_scale.clone();
+    let listen_zoom = app.ex.spawn(async move {
+        while let Ok(_) = recvr.recv().await {
+            let scale = 1.1 * window_scale2.get();
+
+            let filename = get_window_scale_filename();
+            if let Some(parent) = filename.parent() {
+                let _ = std::fs::create_dir_all(parent);
+            }
+            if let Ok(mut file) = File::create(filename) {
+                scale.encode(&mut file).unwrap();
+            }
+
+            let atom = &mut PropertyAtomicGuard::new();
+            window_scale2.set(atom, scale);
+        }
+    });
+    app.tasks.lock().unwrap().push(listen_zoom);
+    let node = node.setup(|me| Shortcut::new(me)).await;
+    window.clone().link(node);
+
     if COLOR_SCHEME == ColorScheme::DarkMode {
         // Bg layer
         let layer_node = create_layer("bg_layer");