Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import (
"github.com/wailsapp/wails/v2/pkg/runtime"
)

// Preferred window size, in logical pixels. The window shrinks to fit when the
// screen cannot accommodate it, see fitWindowToScreen.
const (
defaultWidth = 1024
defaultHeight = 768
)

// Fraction of the screen the window may occupy on startup. The height leaves
// room for a taskbar plus the window title bar; Wails does not expose the
// taskbar-excluded work area, so we approximate it.
const (
maxScreenWidthRatio = 0.9
maxScreenHeightRatio = 0.85
)

type App struct {
ctx context.Context
}
Expand All @@ -17,6 +32,41 @@ func NewApp() *App {

func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.fitWindowToScreen()
}

// fitWindowToScreen shrinks and re-centers the window when the default size does
// not fit on the current screen. Wails scales the requested size by the monitor
// DPI, so on a 1920x1080 screen at 150% scaling the default would be created at
// 1536x1152 physical pixels and hang off the top and bottom of the display.
func (a *App) fitWindowToScreen() {
screens, err := runtime.ScreenGetAll(a.ctx)
if err != nil || len(screens) == 0 {
return
}

screen := screens[0]
for _, s := range screens {
if s.IsCurrent {
screen = s
break
}
}

// Size is in logical pixels, the same units WindowSetSize expects.
screenWidth, screenHeight := screen.Size.Width, screen.Size.Height
if screenWidth <= 0 || screenHeight <= 0 {
return
}

width := min(defaultWidth, int(float64(screenWidth)*maxScreenWidthRatio))
height := min(defaultHeight, int(float64(screenHeight)*maxScreenHeightRatio))
if width == defaultWidth && height == defaultHeight {
return
}

runtime.WindowSetSize(a.ctx, width, height)
runtime.WindowCenter(a.ctx)
}

func (a *App) DetectFileType(path string) string {
Expand Down
8 changes: 5 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ func main() {
app := NewApp()

err := wails.Run(&options.App{
Title: "GeodatExplorer",
Width: 1024,
Height: 768,
Title: "GeodatExplorer",
Width: defaultWidth,
Height: defaultHeight,
MinWidth: 640,
MinHeight: 480,
AssetServer: &assetserver.Options{
Assets: assets,
},
Expand Down