Skip to content

Commit

Permalink
remember app window size
Browse files Browse the repository at this point in the history
  • Loading branch information
nicanordlc committed Jun 7, 2024
1 parent 975d48f commit fc53d70
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
41 changes: 41 additions & 0 deletions backend/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,39 @@ import (
rt "github.com/wailsapp/wails/v2/pkg/runtime"
)

type JsonApp struct {
Size struct {
Width int
Height int
}
}

// App struct
type App struct {
ctx context.Context
filename string
Settings *Settings
JsonApp
}

// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
a.filename = "app.json"
utils.CreateConfigJsonIfNoExists[JsonApp](a.filename)

app, err := a.ReadApp()
if err != nil {
panic(err)
}
if a.Size == struct {
Width int
Height int
}{} {
a.Size = app.Size
rt.WindowSetSize(a.ctx, app.Size.Width, app.Size.Height)
}
}

func (a *App) ToggleFullScreen() {
Expand All @@ -41,6 +64,24 @@ func (a *App) GetOS() string {
return osName
}

func (a *App) UpdateAppSize(width, height int) error {
a.Size.Width = width
a.Size.Height = height
return a.updateJson()
}

func (a *App) ReadApp() (*JsonApp, error) {
appJson, err := utils.ReadConfigFrom[JsonApp](a.filename)
if err != nil {
return appJson, err
}
return appJson, nil
}

func (a *App) updateJson() error {
return utils.WriteStructTo(a.filename, a.JsonApp)
}

// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@preact/signals-react": "^2.0.1",
"@tanstack/react-query": "^5.32.1",
"clsx": "^2.1.0",
"debounce": "^2.1.0",
"localforage": "^1.10.0",
"match-sorter": "^6.3.4",
"react": "^18.2.0",
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Route, Routes } from "react-router-dom";
import { Flip, ToastContainer } from "react-toastify";
import { useCallback, useEffect } from "react";
import debounce from "debounce";
import { UpdateAppSize } from "@wailsjs/go/backend/App";
import Saves from "@/pages/Saves";
import NotFound from "@/pages/NotFound";
import Layout from "@/components/ui/Layout";
Expand All @@ -9,6 +12,22 @@ import useSettings from "@/hooks/useSettings";

const App = () => {
useSettings();

const handleResize = useCallback(async () => {
const width = window.innerWidth;
const height = window.innerHeight;
await UpdateAppSize(width, height);
// @ts-expect-error 2339
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
window.onresize?.clear();
}, []);

// save app size on the backend
useEffect(() => {
const WAIT_FOR_RESIZE = 500;
window.onresize = debounce(handleResize, WAIT_FOR_RESIZE);
}, [handleResize]);

return (
<>
<Layout>
Expand Down
5 changes: 5 additions & 0 deletions frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,11 @@ data-view-byte-offset@^1.0.0:
es-errors "^1.3.0"
is-data-view "^1.0.1"

debounce@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/debounce/-/debounce-2.1.0.tgz#eab25eaf254b848fcfccffbde75bdaa44842caa3"
integrity sha512-OkL3+0pPWCqoBc/nhO9u6TIQNTK44fnBnzuVtJAbp13Naxw9R6u21x+8tVTka87AhDZ3htqZ2pSSsZl9fqL2Wg==

debug@4, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
Expand Down

0 comments on commit fc53d70

Please sign in to comment.