Skip to content

Commit

Permalink
feat(core): ensure window.print()works on macOS (#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 7, 2021
1 parent cea3ba9 commit 56e74cc
Show file tree
Hide file tree
Showing 19 changed files with 76 additions and 27 deletions.
25 changes: 18 additions & 7 deletions .changes/config.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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": [
{
Expand All @@ -265,4 +276,4 @@
"manager": "javascript"
}
}
}
}
5 changes: 5 additions & 0 deletions .changes/print.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

`window.print()` now works on all platforms.
2 changes: 1 addition & 1 deletion .github/workflows/artifacts-updater.yml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-smoke-tests.yml
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/core-lint-fmt.yml
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/covector-version-or-publish.yml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-prod.yml
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-bundler.yml
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-core.yml
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-cta.yml
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/udeps.yml
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/update-docs.yml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/Cargo.toml
Expand Up @@ -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"
Expand Down
15 changes: 12 additions & 3 deletions core/tauri/scripts/core.js
Expand Up @@ -239,7 +239,6 @@ if (!String.prototype.startsWith) {
return window.__TAURI__
.invoke('tauri', {
__tauriModule: "Notification",
mainThread: true,
message: {
cmd: "requestNotificationPermission",
},
Expand Down Expand Up @@ -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,
Expand All @@ -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 changes: 2 additions & 0 deletions core/tauri/src/endpoints/window.rs
Expand Up @@ -71,6 +71,7 @@ pub enum Cmd {
icon: IconDto,
},
StartDragging,
Print,
}

#[cfg(window_create)]
Expand Down Expand Up @@ -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())
}
Expand Down
15 changes: 14 additions & 1 deletion core/tauri/src/runtime/flavors/wry.rs
Expand Up @@ -343,6 +343,7 @@ enum WindowMessage {
#[derive(Debug, Clone)]
enum WebviewMessage {
EvaluateScript(String),
Print,
}

#[derive(Clone)]
Expand Down Expand Up @@ -412,7 +413,7 @@ impl Dispatch for WryDispatcher {
id
}

// GETTERS
// Getters

fn scale_factor(&self) -> crate::Result<f64> {
Ok(dispatcher_getter!(self, WindowMessage::ScaleFactor))
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -818,6 +828,9 @@ impl Runtime for Wry {
WebviewMessage::EvaluateScript(script) => {
let _ = webview.dispatch_script(&script);
}
WebviewMessage::Print => {
let _ = webview.print();
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions core/tauri/src/runtime/mod.rs
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions core/tauri/src/runtime/window.rs
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion tooling/api/src/helpers/tauri.ts
Expand Up @@ -22,7 +22,6 @@ export type TauriModule =

export interface TauriCommand {
__tauriModule: TauriModule
mainThread?: boolean
[key: string]: unknown
}

Expand Down

0 comments on commit 56e74cc

Please sign in to comment.