-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(window-state): add js api, closes #254 * symlink tsconfig.json * update symlink * Update plugins/window-state/package.json Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de> * Update Cargo.toml * move to cmd.rs * Update plugins/window-state/guest-js/index.ts --------- Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>
- Loading branch information
1 parent
6f39921
commit e5b07f4
Showing
10 changed files
with
1,083 additions
and
779 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { invoke } from "@tauri-apps/api/tauri"; | ||
import { WindowLabel, getCurrent } from "@tauri-apps/api/window"; | ||
|
||
export enum StateFlags { | ||
SIZE = 1 << 0, | ||
POSITION = 1 << 1, | ||
MAXIMIZED = 1 << 2, | ||
VISIBLE = 1 << 3, | ||
DECORATIONS = 1 << 4, | ||
FULLSCREEN = 1 << 5, | ||
ALL = SIZE | POSITION | MAXIMIZED | VISIBLE | DECORATIONS | FULLSCREEN, | ||
} | ||
|
||
/** | ||
* Save the state of all open windows to disk. | ||
*/ | ||
async function saveWindowState(flags: StateFlags) { | ||
invoke("plugin:window-state|js_save_window_state", { flags }); | ||
} | ||
|
||
/** | ||
* Restore the state for the specified window from disk. | ||
*/ | ||
async function restoreState(label: WindowLabel, flags: StateFlags) { | ||
invoke("plugin:window-state|js_restore_state", { label, flags }); | ||
} | ||
|
||
/** | ||
* Restore the state for the current window from disk. | ||
*/ | ||
async function restoreStateCurrent(flags: StateFlags) { | ||
restoreState(getCurrent().label, flags); | ||
} | ||
|
||
export { restoreState, restoreStateCurrent, saveWindowState }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "tauri-plugin-window-state-api", | ||
"version": "0.1.0", | ||
"description": "Save window positions and sizes and restore them when the app is reopened.", | ||
"license": "MIT or APACHE-2.0", | ||
"authors": [ | ||
"Tauri Programme within The Commons Conservancy" | ||
], | ||
"type": "module", | ||
"browser": "dist-js/index.min.js", | ||
"module": "dist-js/index.mjs", | ||
"types": "dist-js/index.d.ts", | ||
"exports": { | ||
"import": "./dist-js/index.mjs", | ||
"types": "./dist-js/index.d.ts", | ||
"browser": "./dist-js/index.min.js" | ||
}, | ||
"scripts": { | ||
"build": "rollup -c" | ||
}, | ||
"files": [ | ||
"dist-js", | ||
"!dist-js/**/*.map", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"devDependencies": { | ||
"tslib": "^2.4.1" | ||
}, | ||
"dependencies": { | ||
"@tauri-apps/api": "^1.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { readFileSync } from "fs"; | ||
|
||
import { createConfig } from "../../shared/rollup.config.mjs"; | ||
|
||
export default createConfig({ | ||
input: "guest-js/index.ts", | ||
pkg: JSON.parse( | ||
readFileSync(new URL("./package.json", import.meta.url), "utf8") | ||
), | ||
external: [/^@tauri-apps\/api/], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::{AppHandleExt, StateFlags, WindowExt}; | ||
use tauri::{command, AppHandle, Manager, Runtime}; | ||
|
||
#[command] | ||
pub async fn save_window_state<R: Runtime>( | ||
app: AppHandle<R>, | ||
flags: u32, | ||
) -> std::result::Result<(), String> { | ||
let flags = StateFlags::from_bits(flags) | ||
.ok_or_else(|| format!("Invalid state flags bits: {}", flags))?; | ||
app.save_window_state(flags).map_err(|e| e.to_string())?; | ||
Ok(()) | ||
} | ||
|
||
#[command] | ||
pub async fn restore_state<R: Runtime>( | ||
app: AppHandle<R>, | ||
label: String, | ||
flags: u32, | ||
) -> std::result::Result<(), String> { | ||
let flags = StateFlags::from_bits(flags) | ||
.ok_or_else(|| format!("Invalid state flags bits: {}", flags))?; | ||
app.get_window(&label) | ||
.ok_or_else(|| format!("Couldn't find window with label: {}", label))? | ||
.restore_state(flags) | ||
.map_err(|e| e.to_string())?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../shared/tsconfig.json |
Oops, something went wrong.