Skip to content

Commit

Permalink
feat(window): refactor and improvements (#426)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog authored Aug 7, 2023
1 parent 5b3210c commit 84133b5
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 330 deletions.
2 changes: 1 addition & 1 deletion .changes/shell-command-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 10 additions & 0 deletions .changes/window-plugin-refactor.md
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 11 additions & 9 deletions examples/api/src/App.svelte
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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?");
Expand Down Expand Up @@ -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;
Expand All @@ -147,7 +149,7 @@
}
);
if (confirmed_close) {
getCurrent().close();
appWindow.close();
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion examples/api/src/views/Communication.svelte
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
8 changes: 5 additions & 3 deletions examples/api/src/views/Window.svelte
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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");
Expand Down
8 changes: 5 additions & 3 deletions plugins/window/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
```
Expand Down
Loading

0 comments on commit 84133b5

Please sign in to comment.