forked from agmmnn/tauri-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix agmmnn#31: Address issues raised in GitHub issue and improve impl…
…ementation of @tauri-controls/solid
- Loading branch information
1 parent
f3592f0
commit 6231cf8
Showing
5 changed files
with
35 additions
and
95 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
31 changes: 12 additions & 19 deletions
31
apps/tauri-controls-solid/src/tauri-controls/libs/plugin-os.ts
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 |
---|---|---|
@@ -1,24 +1,17 @@ | ||
import type { OsType } from "@tauri-apps/plugin-os" | ||
import { type, type OsType } from "@tauri-apps/plugin-os" | ||
|
||
let osType: OsType | undefined = undefined | ||
let osTypePromise: Promise<OsType> | null = null | ||
|
||
if (typeof window !== "undefined") { | ||
osTypePromise = import("@tauri-apps/plugin-os").then((module) => { | ||
return module.type().then((x) => { | ||
osType = x // Assign the value of osType here | ||
return x // Return the value to the promise chain | ||
}) | ||
}) | ||
// A helper function to get the OS type | ||
export function getOsType(): OsType { | ||
return type() | ||
} | ||
|
||
// A helper function to get the OS type, which returns a Promise | ||
export function getOsType(): Promise<OsType> { | ||
if (!osTypePromise) { | ||
// If the module was already loaded, just return the result | ||
return Promise.resolve(osType!) // Use non-null assertion | ||
export function getPlatform() { | ||
switch (getOsType()) { | ||
case "macos": | ||
return "macos" | ||
case "linux": | ||
return "gnome" | ||
default: | ||
return "windows" | ||
} | ||
|
||
// If the module is still loading, wait for it to finish and return the result | ||
return osTypePromise | ||
} |
58 changes: 15 additions & 43 deletions
58
apps/tauri-controls-solid/src/tauri-controls/libs/plugin-window.ts
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 |
---|---|---|
@@ -1,58 +1,30 @@ | ||
import type { Window } from "@tauri-apps/api/window" | ||
import { createEffect, createSignal, onCleanup } from "solid-js" | ||
import { getOsType } from "./plugin-os" | ||
import { getCurrentWindow } from "@tauri-apps/api/window" | ||
import { createSignal } from "solid-js" | ||
|
||
export const [appWindow, setAppWindow] = createSignal<Window | null>(null) | ||
export const [isWindowMaximized, setIsWindowMaximized] = createSignal(false) | ||
const [isWindowMaximized, setIsWindowMaximized] = createSignal(false) | ||
export { isWindowMaximized } | ||
|
||
import("@tauri-apps/api").then((module) => { | ||
setAppWindow(module.window.getCurrent()) | ||
}) | ||
|
||
// Update the isWindowMaximized state when the window is resized | ||
const updateIsWindowMaximized = async () => { | ||
const window = appWindow() | ||
if (window) { | ||
const resolvedPromise = await window.isMaximized() | ||
setIsWindowMaximized(resolvedPromise) | ||
} | ||
const watchWindowMaximized = () => { | ||
const window = getCurrentWindow() | ||
window.isMaximized().then(setIsWindowMaximized) | ||
window.onResized(() => window.isMaximized().then(setIsWindowMaximized)) | ||
} | ||
|
||
createEffect(async () => { | ||
const osname = await getOsType() | ||
// temporary: https://github.com/agmmnn/tauri-controls/issues/10#issuecomment-1675884962 | ||
if (osname !== "macos") { | ||
updateIsWindowMaximized() | ||
let unlisten: () => void = () => {} | ||
|
||
const window = appWindow() | ||
if (window) { | ||
unlisten = await window.onResized(() => { | ||
updateIsWindowMaximized() | ||
}) | ||
} | ||
|
||
// Cleanup the listener when the component unmounts | ||
unlisten && onCleanup(unlisten) | ||
} | ||
}) | ||
watchWindowMaximized() | ||
|
||
export const minimizeWindow = async () => { | ||
await appWindow()?.minimize() | ||
await getCurrentWindow().minimize() | ||
} | ||
|
||
export const maximizeWindow = async () => { | ||
await appWindow()?.toggleMaximize() | ||
await getCurrentWindow().toggleMaximize() | ||
} | ||
|
||
export const fullscreenWindow = async () => { | ||
const window = appWindow() | ||
if (window) { | ||
const fullscreen = await window.isFullscreen() | ||
await window.setFullscreen(!fullscreen) | ||
} | ||
const window = getCurrentWindow() | ||
const fullscreen = await window.isFullscreen() | ||
await window.setFullscreen(!fullscreen) | ||
} | ||
|
||
export const closeWindow = async () => { | ||
await appWindow()?.close() | ||
await getCurrentWindow()?.close() | ||
} |
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