feat(window): refactor and improvements (#426)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
pull/539/head
Amr Bashir 2 years ago committed by GitHub
parent 5b3210c224
commit 84133b57b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,4 +2,4 @@
"shell": "patch" "shell": "patch"
--- ---
Added `Command::arg`, `Command::env` and changed `Command::new` input type. Added `Command::arg`, `Command::env` and changed `Command::new` input type.

@ -0,0 +1,10 @@
---
"window": "patch"
"window-js": "patch"
---
The window plugin is recieving a few changes to improve consistency and add new features:
- Removed `appWindow` variable from JS module, use `getCurrent` or `Window.getCurrent`.
- Removed `WindowManager`, `WebviewWindow` and `WebviewHandle` types and merged them into one `Window` type that matches the name of the rust window type.
- Added `Window.getCurrent` and `Window.getAll` which is a convenient method for `getCurrent` and `getAll` functions.

@ -1,7 +1,7 @@
<script> <script>
import { writable } from "svelte/store"; import { writable } from "svelte/store";
import { open } from "@tauri-apps/plugin-shell"; import { open } from "@tauri-apps/plugin-shell";
import { appWindow, getCurrent } from "@tauri-apps/plugin-window"; import { getCurrent } from "@tauri-apps/plugin-window";
import * as os from "@tauri-apps/plugin-os"; import * as os from "@tauri-apps/plugin-os";
import Welcome from "./views/Welcome.svelte"; import Welcome from "./views/Welcome.svelte";
@ -22,6 +22,8 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
import { ask } from "@tauri-apps/plugin-dialog"; import { ask } from "@tauri-apps/plugin-dialog";
const appWindow = getCurrent();
if (appWindow.label !== "main") { if (appWindow.label !== "main") {
appWindow.onCloseRequested(async (event) => { appWindow.onCloseRequested(async (event) => {
const confirmed = await confirm("Are you sure?"); const confirmed = await confirm("Are you sure?");
@ -121,20 +123,20 @@
// Window controls // Window controls
let isWindowMaximized; let isWindowMaximized;
onMount(async () => { onMount(async () => {
const window = getCurrent(); isWindowMaximized = await appWindow.isMaximized();
isWindowMaximized = await window.isMaximized(); appWindow.onResized(async () => {
window.onResized(async () => { isWindowMaximized = await appWindow.isMaximized();
isWindowMaximized = await window.isMaximized();
}); });
}); });
function minimize() { function minimize() {
getCurrent().minimize(); appWindow.minimize();
} }
async function toggleMaximize() { async function toggleMaximize() {
const window = getCurrent(); (await appWindow.isMaximized())
(await window.isMaximized()) ? window.unmaximize() : window.maximize(); ? appWindow.unmaximize()
: appWindow.maximize();
} }
let confirmed_close = false; let confirmed_close = false;
@ -147,7 +149,7 @@
} }
); );
if (confirmed_close) { if (confirmed_close) {
getCurrent().close(); appWindow.close();
} }
} }
} }

@ -1,8 +1,10 @@
<script> <script>
import { appWindow } from "@tauri-apps/plugin-window"; import { getCurrent } from "@tauri-apps/plugin-window";
import { invoke } from "@tauri-apps/api/tauri"; import { invoke } from "@tauri-apps/api/tauri";
import { onMount, onDestroy } from "svelte"; import { onMount, onDestroy } from "svelte";
const appWindow = getCurrent();
export let onMessage; export let onMessage;
let unlisten; let unlisten;

@ -1,17 +1,19 @@
<script> <script>
import { import {
appWindow, getCurrent,
WebviewWindow,
LogicalSize, LogicalSize,
UserAttentionType, UserAttentionType,
PhysicalSize, PhysicalSize,
PhysicalPosition, PhysicalPosition,
Effect, Effect,
EffectState, EffectState,
Window
} from "@tauri-apps/plugin-window"; } from "@tauri-apps/plugin-window";
import { open as openDialog } from "@tauri-apps/plugin-dialog"; import { open as openDialog } from "@tauri-apps/plugin-dialog";
import { open } from "@tauri-apps/plugin-shell"; import { open } from "@tauri-apps/plugin-shell";
const appWindow = getCurrent();
let selectedWindow = appWindow.label; let selectedWindow = appWindow.label;
const windowMap = { const windowMap = {
[appWindow.label]: appWindow, [appWindow.label]: appWindow,
@ -146,7 +148,7 @@
function createWindow() { function createWindow() {
if (!newWindowLabel) return; if (!newWindowLabel) return;
const webview = new WebviewWindow(newWindowLabel); const webview = new Window(newWindowLabel);
windowMap[newWindowLabel] = webview; windowMap[newWindowLabel] = webview;
webview.once("tauri://error", function () { webview.once("tauri://error", function () {
onMessage("Error creating new webview"); onMessage("Error creating new webview");

@ -60,18 +60,20 @@ fn main() {
Afterwards all the plugin's APIs are available through the JavaScript guest bindings: Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
```javascript ```javascript
import { appWindow, WebviewWindow } from "@tauri-apps/plugin-window"; import { getCurrent, Window } from "@tauri-apps/plugin-window";
const appWindow = getCurrent();
// manipulating this window // manipulating this window
await appWindow.setResizable(false); await appWindow.setResizable(false);
// Creating new windows: // Creating new windows:
// loading embedded asset: // loading embedded asset:
const webview = new WebviewWindow("theUniqueLabel", { const webview = new Window("theUniqueLabel", {
url: "path/to/page.html", url: "path/to/page.html",
}); });
// alternatively, load a remote URL: // alternatively, load a remote URL:
const webview = new WebviewWindow("theUniqueLabel", { const webview = new Window("theUniqueLabel", {
url: "https://github.com/tauri-apps/tauri", url: "https://github.com/tauri-apps/tauri",
}); });
``` ```

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save