Skip to content

Commit

Permalink
Fix agmmnn#31: Address issues raised in GitHub issue and improve impl…
Browse files Browse the repository at this point in the history
…ementation of @tauri-controls/solid
  • Loading branch information
Rafferty97 committed Aug 11, 2024
1 parent f3592f0 commit 6231cf8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 95 deletions.
6 changes: 3 additions & 3 deletions apps/tauri-controls-solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
"devDependencies": {
"@ianvs/prettier-plugin-sort-imports": "^4.2.1",
"@rollup/plugin-terser": "^0.4.4",
"@tauri-apps/api": "2.0.0-beta.6",
"@tauri-apps/cli": "2.0.0-beta.11",
"@tauri-apps/plugin-os": "2.0.0-beta.2",
"@tauri-apps/api": "2.0.0-rc.0",
"@tauri-apps/cli": "2.0.0-rc.0",
"@tauri-apps/plugin-os": "2.0.0-rc.0",
"autoprefixer": "^10.4.19",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
Expand Down
31 changes: 12 additions & 19 deletions apps/tauri-controls-solid/src/tauri-controls/libs/plugin-os.ts
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 apps/tauri-controls-solid/src/tauri-controls/libs/plugin-window.ts
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()
}
22 changes: 3 additions & 19 deletions apps/tauri-controls-solid/src/tauri-controls/window-controls.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Match, mergeProps, onMount, splitProps, Switch } from "solid-js"
import { Match, mergeProps, splitProps, Switch } from "solid-js"
import { twMerge } from "tailwind-merge"
import { Gnome, MacOS, Windows } from "./controls"
import { getOsType } from "./libs/plugin-os"
import { getPlatform } from "./libs/plugin-os"
import type { WindowControlsProps } from "./types"

export function WindowControls(props: WindowControlsProps) {
Expand All @@ -18,27 +18,11 @@ export function WindowControls(props: WindowControlsProps) {
justify: false,
hide: false,
hideMethod: "display",
platform: getPlatform(),
},
rawLocal
)

onMount(() => {
getOsType().then((type) => {
if (!local.platform) {
switch (type) {
case "macos":
local.platform = "macos"
break
case "linux":
local.platform = "gnome"
break
default:
local.platform = "windows"
}
}
})
})

const customClass = twMerge(
"flex",
local.class,
Expand Down
13 changes: 2 additions & 11 deletions apps/tauri-controls-solid/src/tauri-controls/window-titlebar.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { OsType } from "@tauri-apps/plugin-os"
import { createSignal, mergeProps, onMount, splitProps } from "solid-js"
import { mergeProps, splitProps } from "solid-js"
import { twMerge } from "tailwind-merge"
import { getOsType } from "./libs/plugin-os"
import type { WindowTitlebarProps } from "./types"
import { WindowControls } from "./window-controls"

export function WindowTitlebar(props: WindowTitlebarProps) {
const [osType, setOsType] = createSignal<OsType | undefined>(undefined)

const [rawLocal, otherProps] = splitProps(props, [
"children",
"controlsOrder",
Expand All @@ -22,17 +19,11 @@ export function WindowTitlebar(props: WindowTitlebarProps) {
rawLocal
)

onMount(() => {
getOsType().then((type) => {
setOsType(type)
})
})

const left = () =>
local.controlsOrder === "left" ||
(local.controlsOrder === "platform" &&
local.windowControlsProps?.platform === "macos") ||
(local.controlsOrder === "system" && osType() === "macos")
(local.controlsOrder === "system" && getOsType() === "macos")

const customProps = (ml: string) => {
if (local.windowControlsProps?.justify !== undefined)
Expand Down

0 comments on commit 6231cf8

Please sign in to comment.