Browse Source

feat(core): ensure `window.print()`works on macOS (#1738)

Lucas Fernandes Nogueira 4 years ago
parent
commit
56e74ccf74

+ 18 - 7
.changes/config.json

@@ -8,7 +8,7 @@
       "getPublishedVersion": "cargo search ${ pkgFile.pkg.package.name } --limit 1 | sed -nE \"s/^[^\\\"]*\\\"//; s/\\\".*//1p\"",
       "prepublish": [
         "sudo apt-get update",
-        "sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0",
+        "sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev",
         "cargo install cargo-audit --features=fix",
         {
           "command": "cargo generate-lockfile",
@@ -217,29 +217,40 @@
     "tauri-codegen": {
       "path": "./core/tauri-codegen",
       "manager": "rust",
-      "dependencies": ["tauri-utils"]
+      "dependencies": [
+        "tauri-utils"
+      ]
     },
     "tauri-macros": {
       "path": "./core/tauri-macros",
       "manager": "rust",
-      "dependencies": ["tauri-codegen"]
+      "dependencies": [
+        "tauri-codegen"
+      ]
     },
     "tauri-build": {
       "path": "./core/tauri-build",
       "manager": "rust",
-      "dependencies": ["tauri-codegen"],
+      "dependencies": [
+        "tauri-codegen"
+      ],
       "postversion": "node ../../.scripts/sync-cli-metadata.js ${ pkg.pkg } ${ release.type }"
     },
     "tauri": {
       "path": "./core/tauri",
       "manager": "rust",
-      "dependencies": ["tauri-macros", "tauri-utils"],
+      "dependencies": [
+        "tauri-macros",
+        "tauri-utils"
+      ],
       "postversion": "node ../../.scripts/sync-cli-metadata.js ${ pkg.pkg } ${ release.type }"
     },
     "cli.js": {
       "path": "./tooling/cli.js",
       "manager": "javascript",
-      "dependencies": ["cli.rs"],
+      "dependencies": [
+        "cli.rs"
+      ],
       "postversion": "node ../../.scripts/sync-cli-metadata.js ${ pkg.pkg } ${ release.type }",
       "assets": [
         {
@@ -265,4 +276,4 @@
       "manager": "javascript"
     }
   }
-}
+}

+ 5 - 0
.changes/print.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+`window.print()` now works on all platforms.

+ 1 - 1
.github/workflows/artifacts-updater.yml

@@ -35,7 +35,7 @@ jobs:
         if: matrix.platform == 'ubuntu-latest'
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
         
       - name: Get current date
         run: echo "CURRENT_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV

+ 1 - 1
.github/workflows/build-smoke-tests.yml

@@ -69,7 +69,7 @@ jobs:
         if: matrix.platform == 'ubuntu-latest'
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
       - name: yarn install for cli
         working-directory: tauri/tooling/cli.js
         run: yarn

+ 2 - 2
.github/workflows/core-lint-fmt.yml

@@ -24,7 +24,7 @@ jobs:
       - name: install webkit2gtk
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
 
       - uses: actions-rs/toolchain@v1
         with:
@@ -151,7 +151,7 @@ jobs:
       - name: install webkit2gtk
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
 
       - uses: actions-rs/toolchain@v1
         with:

+ 1 - 1
.github/workflows/covector-version-or-publish.yml

@@ -70,7 +70,7 @@ jobs:
       - name: install webkit2gtk
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
       - name: generate rust docs
         working-directory: ./tauri/core/tauri
         run: cargo doc --no-deps

+ 1 - 1
.github/workflows/smoke-test-prod.yml

@@ -37,7 +37,7 @@ jobs:
         if: matrix.platform == 'ubuntu-latest'
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
       - name: cache rust bin
         id: cache_rust_bin
         uses: actions/cache@v1

+ 2 - 2
.github/workflows/test-bundler.yml

@@ -35,7 +35,7 @@ jobs:
         if: matrix.platform == 'ubuntu-latest'
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
 
       - name: Get current date
         run: echo "CURRENT_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV
@@ -100,7 +100,7 @@ jobs:
         if: matrix.platform == 'ubuntu-latest'
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
       - name: clippy check
         uses: actions-rs/clippy-check@v1
         with:

+ 2 - 2
.github/workflows/test-core.yml

@@ -37,7 +37,7 @@ jobs:
         if: matrix.platform == 'ubuntu-latest'
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
 
       - name: Get current date
         run: echo "CURRENT_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV
@@ -161,7 +161,7 @@ jobs:
         if: matrix.platform == 'ubuntu-latest'
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
 
       - name: Get current date
         run: echo "CURRENT_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV

+ 2 - 2
.github/workflows/test-cta.yml

@@ -47,7 +47,7 @@ jobs:
       - name: install webkit2gtk
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
       - run: yarn
         working-directory: tooling/create-tauri-app
       - run: yarn build
@@ -83,7 +83,7 @@ jobs:
       - name: install webkit2gtk
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
       - run: yarn
         working-directory: tooling/create-tauri-app
       - run: yarn build

+ 1 - 1
.github/workflows/udeps.yml

@@ -90,7 +90,7 @@ jobs:
     - name: Install required packages
       run: |
         sudo apt-get update
-        sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+        sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
 
     - uses: actions-rs/cargo@v1
       with:

+ 1 - 1
.github/workflows/update-docs.yml

@@ -32,7 +32,7 @@ jobs:
       - name: install webkit2gtk
         run: |
           sudo apt-get update
-          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0
+          sudo apt-get install -y libgtk-3-dev libgtksourceview-3.0-dev webkit2gtk-4.0 libappindicator3-dev
       - name: generate rust docs
         working-directory: ./tauri/core/tauri
         run: cargo doc --no-deps

+ 1 - 1
core/tauri/Cargo.toml

@@ -24,7 +24,7 @@ thiserror = "1.0.24"
 once_cell = "1.7.2"
 tauri-macros = { version = "1.0.0-beta-rc.1", path = "../tauri-macros" }
 tauri-utils = { version = "1.0.0-beta-rc.1", path = "../tauri-utils" }
-wry = { git = "https://github.com/tauri-apps/wry", rev = "483bad0fc7e7564500f7183547c15604fa387258" }
+wry = { git = "https://github.com/tauri-apps/wry", rev = "0570dcab90087af5b1d29218d9d25186a7ade357" }
 rand = "0.8"
 reqwest = { version = "0.11", features = [ "json", "multipart" ] }
 tempfile = "3"

+ 12 - 3
core/tauri/scripts/core.js

@@ -239,7 +239,6 @@ if (!String.prototype.startsWith) {
     return window.__TAURI__
       .invoke('tauri', {
         __tauriModule: "Notification",
-        mainThread: true,
         message: {
           cmd: "requestNotificationPermission",
         },
@@ -308,7 +307,6 @@ if (!String.prototype.startsWith) {
   window.alert = function (message) {
     window.__TAURI__.invoke('tauri', {
       __tauriModule: "Dialog",
-      mainThread: true,
       message: {
         cmd: "messageDialog",
         message: message,
@@ -319,11 +317,22 @@ if (!String.prototype.startsWith) {
   window.confirm = function (message) {
     return window.__TAURI__.invoke('tauri', {
       __tauriModule: "Dialog",
-      mainThread: true,
       message: {
         cmd: "askDialog",
         message: message,
       },
     });
   };
+
+  // window.print works on Linux/Windows; need to use the API on macOS
+  if (navigator.userAgent.includes('Mac')) {
+    window.print = function () {
+      return window.__TAURI__.invoke('tauri', {
+        __tauriModule: "Window",
+        message: {
+          cmd: "print"
+        },
+      });
+    }
+  }
 })();

+ 2 - 0
core/tauri/src/endpoints/window.rs

@@ -71,6 +71,7 @@ pub enum Cmd {
     icon: IconDto,
   },
   StartDragging,
+  Print,
 }
 
 #[cfg(window_create)]
@@ -146,6 +147,7 @@ impl Cmd {
         Self::SetFullscreen(fullscreen) => window.set_fullscreen(fullscreen)?,
         Self::SetIcon { icon } => window.set_icon(icon.into())?,
         Self::StartDragging => window.start_dragging()?,
+        Self::Print => window.print()?,
       }
       Ok(().into())
     }

+ 14 - 1
core/tauri/src/runtime/flavors/wry.rs

@@ -343,6 +343,7 @@ enum WindowMessage {
 #[derive(Debug, Clone)]
 enum WebviewMessage {
   EvaluateScript(String),
+  Print,
 }
 
 #[derive(Clone)]
@@ -412,7 +413,7 @@ impl Dispatch for WryDispatcher {
     id
   }
 
-  // GETTERS
+  // Getters
 
   fn scale_factor(&self) -> crate::Result<f64> {
     Ok(dispatcher_getter!(self, WindowMessage::ScaleFactor))
@@ -459,6 +460,15 @@ impl Dispatch for WryDispatcher {
     )
   }
 
+  // Setters
+
+  fn print(&self) -> crate::Result<()> {
+    self
+      .proxy
+      .send_event(Message::Webview(self.window_id, WebviewMessage::Print))
+      .map_err(|_| crate::Error::FailedToSendMessage)
+  }
+
   fn create_window<M: Params<Runtime = Self::Runtime>>(
     &mut self,
     pending: PendingWindow<M>,
@@ -818,6 +828,9 @@ impl Runtime for Wry {
                 WebviewMessage::EvaluateScript(script) => {
                   let _ = webview.dispatch_script(&script);
                 }
+                WebviewMessage::Print => {
+                  let _ = webview.print();
+                }
               }
             }
           }

+ 3 - 0
core/tauri/src/runtime/mod.rs

@@ -99,6 +99,9 @@ pub trait Dispatch: Clone + Send + Sized + 'static {
 
   // SETTERS
 
+  /// Opens the dialog to prints the contents of the webview.
+  fn print(&self) -> crate::Result<()>;
+
   /// Create a new webview window.
   fn create_window<P: Params<Runtime = Self::Runtime>>(
     &mut self,

+ 7 - 0
core/tauri/src/runtime/window.rs

@@ -412,6 +412,13 @@ pub(crate) mod export {
 
     // Setters
 
+    /// Opens the dialog to prints the contents of the webview.
+    /// Currently only supported on macOS on `wry`.
+    /// `window.print()` works on all platforms.
+    pub fn print(&self) -> crate::Result<()> {
+      self.window.dispatcher.print()
+    }
+
     /// Determines if this window should be resizable.
     pub fn set_resizable(&self, resizable: bool) -> crate::Result<()> {
       self.window.dispatcher.set_resizable(resizable)

+ 0 - 1
tooling/api/src/helpers/tauri.ts

@@ -22,7 +22,6 @@ export type TauriModule =
 
 export interface TauriCommand {
   __tauriModule: TauriModule
-  mainThread?: boolean
   [key: string]: unknown
 }