mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
feat(window): refactor and improvements (#426)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
@@ -2,4 +2,4 @@
|
||||
"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>
|
||||
import { writable } from "svelte/store";
|
||||
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 Welcome from "./views/Welcome.svelte";
|
||||
@@ -22,6 +22,8 @@
|
||||
import { onMount } from "svelte";
|
||||
import { ask } from "@tauri-apps/plugin-dialog";
|
||||
|
||||
const appWindow = getCurrent();
|
||||
|
||||
if (appWindow.label !== "main") {
|
||||
appWindow.onCloseRequested(async (event) => {
|
||||
const confirmed = await confirm("Are you sure?");
|
||||
@@ -121,20 +123,20 @@
|
||||
// Window controls
|
||||
let isWindowMaximized;
|
||||
onMount(async () => {
|
||||
const window = getCurrent();
|
||||
isWindowMaximized = await window.isMaximized();
|
||||
window.onResized(async () => {
|
||||
isWindowMaximized = await window.isMaximized();
|
||||
isWindowMaximized = await appWindow.isMaximized();
|
||||
appWindow.onResized(async () => {
|
||||
isWindowMaximized = await appWindow.isMaximized();
|
||||
});
|
||||
});
|
||||
|
||||
function minimize() {
|
||||
getCurrent().minimize();
|
||||
appWindow.minimize();
|
||||
}
|
||||
|
||||
async function toggleMaximize() {
|
||||
const window = getCurrent();
|
||||
(await window.isMaximized()) ? window.unmaximize() : window.maximize();
|
||||
(await appWindow.isMaximized())
|
||||
? appWindow.unmaximize()
|
||||
: appWindow.maximize();
|
||||
}
|
||||
|
||||
let confirmed_close = false;
|
||||
@@ -147,7 +149,7 @@
|
||||
}
|
||||
);
|
||||
if (confirmed_close) {
|
||||
getCurrent().close();
|
||||
appWindow.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
<script>
|
||||
import { appWindow } from "@tauri-apps/plugin-window";
|
||||
import { getCurrent } from "@tauri-apps/plugin-window";
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
import { onMount, onDestroy } from "svelte";
|
||||
|
||||
const appWindow = getCurrent();
|
||||
|
||||
export let onMessage;
|
||||
let unlisten;
|
||||
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
<script>
|
||||
import {
|
||||
appWindow,
|
||||
WebviewWindow,
|
||||
getCurrent,
|
||||
LogicalSize,
|
||||
UserAttentionType,
|
||||
PhysicalSize,
|
||||
PhysicalPosition,
|
||||
Effect,
|
||||
EffectState,
|
||||
Window
|
||||
} from "@tauri-apps/plugin-window";
|
||||
import { open as openDialog } from "@tauri-apps/plugin-dialog";
|
||||
import { open } from "@tauri-apps/plugin-shell";
|
||||
|
||||
const appWindow = getCurrent();
|
||||
|
||||
let selectedWindow = appWindow.label;
|
||||
const windowMap = {
|
||||
[appWindow.label]: appWindow,
|
||||
@@ -146,7 +148,7 @@
|
||||
function createWindow() {
|
||||
if (!newWindowLabel) return;
|
||||
|
||||
const webview = new WebviewWindow(newWindowLabel);
|
||||
const webview = new Window(newWindowLabel);
|
||||
windowMap[newWindowLabel] = webview;
|
||||
webview.once("tauri://error", function () {
|
||||
onMessage("Error creating new webview");
|
||||
|
||||
@@ -60,18 +60,20 @@ fn main() {
|
||||
Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
|
||||
|
||||
```javascript
|
||||
import { appWindow, WebviewWindow } from "@tauri-apps/plugin-window";
|
||||
import { getCurrent, Window } from "@tauri-apps/plugin-window";
|
||||
|
||||
const appWindow = getCurrent();
|
||||
|
||||
// manipulating this window
|
||||
await appWindow.setResizable(false);
|
||||
|
||||
// Creating new windows:
|
||||
// loading embedded asset:
|
||||
const webview = new WebviewWindow("theUniqueLabel", {
|
||||
const webview = new Window("theUniqueLabel", {
|
||||
url: "path/to/page.html",
|
||||
});
|
||||
// alternatively, load a remote URL:
|
||||
const webview = new WebviewWindow("theUniqueLabel", {
|
||||
const webview = new Window("theUniqueLabel", {
|
||||
url: "https://github.com/tauri-apps/tauri",
|
||||
});
|
||||
```
|
||||
|
||||
+287
-312
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user