Browse Source

feat(lib) open link command (#10)

Lucas Fernandes Nogueira 6 years ago
parent
commit
32b2e951be
4 changed files with 20 additions and 1 deletions
  1. 2 1
      lib/rust/Cargo.toml
  2. 6 0
      lib/rust/src/api.rs
  3. 2 0
      lib/rust/src/api/cmd.rs
  4. 10 0
      lib/rust/src/lib.rs

+ 2 - 1
lib/rust/Cargo.toml

@@ -26,6 +26,7 @@ tar = "0.4"
 flate2 = "1"
 hyper-old-types = "0.11.0"
 sysinfo = "0.9"
+webbrowser = "0.5.1"
 
 [features]
 api = []
@@ -37,4 +38,4 @@ listFiles = []
 listDirs = []
 setTitle = []
 execute = []
-
+open = []

+ 6 - 0
lib/rust/src/api.rs

@@ -64,6 +64,12 @@ pub fn handler<T: 'static>(webview: &mut WebView<'_, T>, arg: &str) -> bool {
             error,
           } => {
             super::command::call(webview, command, args, callback, error);
+          },
+          #[cfg(any(feature = "all-api", feature = "open"))]
+          Open { uri } => {
+            super::spawn(move || {
+              webbrowser::open(&uri).unwrap();
+            }); 
           }
         }
         true

+ 2 - 0
lib/rust/src/api/cmd.rs

@@ -42,4 +42,6 @@ pub enum Cmd {
     callback: String,
     error: String,
   },
+  #[cfg(any(feature = "all-api", feature = "open"))]
+  Open { uri: String }
 }

+ 10 - 0
lib/rust/src/lib.rs

@@ -23,6 +23,16 @@ use threadpool::ThreadPool;
 
 thread_local!(static POOL: ThreadPool = ThreadPool::new(4));
 
+pub fn spawn<F: FnOnce() -> () + Send + 'static>(
+  what: F
+) {
+  POOL.with(|thread| {
+    thread.execute(move || {
+      what();
+    });
+  });
+}
+
 pub fn run_async<T: 'static, F: FnOnce() -> Result<String, String> + Send + 'static>(
   webview: &mut WebView<'_, T>,
   what: F,