fix initial size of windows (#1011)

This commit is contained in:
Mike Sawka 2024-10-10 16:12:56 -07:00 committed by GitHub
parent f3e0ba8148
commit 3d2d68bc73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 67 additions and 15 deletions

View File

@ -253,11 +253,7 @@ async function handleWSEvent(evtMsg: WSEventType) {
}
}
async function mainResizeHandler(_: any, windowId: string, win: WaveBrowserWindow) {
if (win == null || win.isDestroyed() || win.fullScreen) {
return;
}
const bounds = win.getBounds();
async function persistWindowBounds(windowId: string, bounds: electron.Rectangle) {
try {
await services.WindowService.SetWindowPosAndSize(
windowId,
@ -269,6 +265,14 @@ async function mainResizeHandler(_: any, windowId: string, win: WaveBrowserWindo
}
}
async function mainResizeHandler(_: any, windowId: string, win: WaveBrowserWindow) {
if (win == null || win.isDestroyed() || win.fullScreen) {
return;
}
const bounds = win.getBounds();
await persistWindowBounds(windowId, bounds);
}
function shNavHandler(event: Electron.Event<Electron.WebContentsWillNavigateEventParams>, url: string) {
if (url.startsWith("http://127.0.0.1:5173/index.html") || url.startsWith("http://localhost:5173/index.html")) {
// this is a dev-mode hot-reload, ignore it
@ -310,9 +314,42 @@ function shFrameNavHandler(event: Electron.Event<Electron.WebContentsWillFrameNa
console.log("frame navigation canceled");
}
// note, this does not *show* the window.
// to show, await win.readyPromise and then win.show()
function createBrowserWindow(clientId: string, waveWindow: WaveWindow, fullConfig: FullConfigType): WaveBrowserWindow {
function computeNewWinBounds(waveWindow: WaveWindow): Electron.Rectangle {
const targetWidth = waveWindow.winsize?.width || 2000;
const targetHeight = waveWindow.winsize?.height || 1080;
const primaryDisplay = electron.screen.getPrimaryDisplay();
const workSize = primaryDisplay.workAreaSize;
const targetPadding = 100;
let rtn = { x: 100, y: 100, width: targetWidth, height: targetHeight };
if (workSize.width < targetWidth + 2 * targetPadding) {
const spareWidth = workSize.width - targetWidth;
if (spareWidth < 20) {
rtn.x = 10;
rtn.width = workSize.width - 20;
} else if (spareWidth > 200) {
rtn.x = 100;
} else {
rtn.x = Math.floor(spareWidth / 2);
}
}
if (workSize.height < targetHeight + 2 * targetPadding) {
const spareHeight = workSize.height - targetHeight;
if (spareHeight < 20) {
rtn.y = 10;
rtn.height = workSize.height - 20;
} else if (spareHeight > 200) {
rtn.y = 100;
} else {
rtn.y = Math.floor(spareHeight / 2);
}
}
return rtn;
}
function computeWinBounds(waveWindow: WaveWindow): Electron.Rectangle {
if (waveWindow.isnew) {
return computeNewWinBounds(waveWindow);
}
let winWidth = waveWindow?.winsize?.width;
let winHeight = waveWindow?.winsize?.height;
let winPosX = waveWindow.pos.x;
@ -339,7 +376,15 @@ function createBrowserWindow(clientId: string, waveWindow: WaveWindow, fullConfi
width: winWidth,
height: winHeight,
};
return winBounds;
}
// note, this does not *show* the window.
// to show, await win.readyPromise and then win.show()
function createBrowserWindow(clientId: string, waveWindow: WaveWindow, fullConfig: FullConfigType): WaveBrowserWindow {
let winBounds = computeWinBounds(waveWindow);
winBounds = ensureBoundsAreVisible(winBounds);
persistWindowBounds(waveWindow.oid, winBounds);
const settings = fullConfig?.settings;
const winOpts: Electron.BrowserWindowConstructorOptions = {
titleBarStyle:

View File

@ -4,6 +4,9 @@
.webview {
height: 100%;
width: 100%;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
border: none !important;
outline: none !important;
overflow: hidden;
padding: 0;
margin: 0;
}

View File

@ -677,6 +677,7 @@ declare global {
type WaveWindow = WaveObj & {
workspaceid: string;
activetabid: string;
isnew?: boolean;
pos: Point;
winsize: WinSize;
lastfocusts: number;

View File

@ -38,6 +38,7 @@ func (ws *WindowService) SetWindowPosAndSize(ctx context.Context, windowId strin
if size != nil {
win.WinSize = *size
}
win.IsNew = false
err = wstore.DBUpdate(ctx, win)
if err != nil {
return nil, err

View File

@ -131,6 +131,7 @@ type Window struct {
Version int `json:"version"`
WorkspaceId string `json:"workspaceid"`
ActiveTabId string `json:"activetabid"`
IsNew bool `json:"isnew,omitempty"` // set when a window is created on the backend so the FE can size it properly. cleared on first resize
Pos Point `json:"pos"`
WinSize WinSize `json:"winsize"`
LastFocusTs int64 `json:"lastfocusts"`

View File

@ -96,16 +96,17 @@ func CreateWindow(ctx context.Context, winSize *waveobj.WinSize) (*waveobj.Windo
workspaceId := uuid.NewString()
if winSize == nil {
winSize = &waveobj.WinSize{
Width: 1200,
Height: 850,
Width: 0,
Height: 0,
}
}
window := &waveobj.Window{
OID: windowId,
WorkspaceId: workspaceId,
IsNew: true,
Pos: waveobj.Point{
X: 100,
Y: 100,
X: 0,
Y: 0,
},
WinSize: *winSize,
}
@ -178,7 +179,7 @@ func EnsureInitialData() (*waveobj.Window, bool, error) {
if len(client.WindowIds) > 0 {
return nil, false, nil
}
window, err := CreateWindow(ctx, &waveobj.WinSize{Height: 0, Width: 0})
window, err := CreateWindow(ctx, nil)
if err != nil {
return nil, false, fmt.Errorf("error creating window: %w", err)
}