5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 06:01:52 +08:00
wails/v2/internal/frontend/windows/frontend.go
2021-07-25 20:21:21 +10:00

142 lines
2.9 KiB
Go

package windows
import (
"context"
"github.com/tadvi/winc"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/logger"
"github.com/wailsapp/wails/v2/pkg/options"
"runtime"
)
type Frontend struct {
frontendOptions *options.App
logger *logger.Logger
// main window handle
mainWindow *Window
minWidth, minHeight, maxWidth, maxHeight int
bindings *binding.Bindings
}
func (f *Frontend) Run() error {
mainWindow := NewWindow(nil, f.frontendOptions)
f.mainWindow = mainWindow
f.WindowCenter()
if !f.frontendOptions.StartHidden {
mainWindow.Show()
}
mainWindow.OnClose().Bind(func(arg *winc.Event) {
if f.frontendOptions.HideWindowOnClose {
f.WindowHide()
} else {
f.Quit()
}
})
// TODO: Move this into a callback from frontend
go func() {
ctx := context.WithValue(context.Background(), "frontend", f)
f.frontendOptions.Startup(ctx)
}()
winc.RunMainLoop()
return nil
}
func (f *Frontend) WindowCenter() {
runtime.LockOSThread()
f.mainWindow.Center()
}
func (f *Frontend) WindowSetPos(x, y int) {
runtime.LockOSThread()
f.mainWindow.SetPos(x, y)
}
func (f *Frontend) WindowGetPos() (int, int) {
runtime.LockOSThread()
return f.mainWindow.Pos()
}
func (f *Frontend) WindowSetSize(width, height int) {
runtime.LockOSThread()
f.mainWindow.SetSize(width, height)
}
func (f *Frontend) WindowGetSize() (int, int) {
runtime.LockOSThread()
return f.mainWindow.Size()
}
func (f *Frontend) WindowSetTitle(title string) {
runtime.LockOSThread()
f.mainWindow.SetText(title)
}
func (f *Frontend) WindowFullscreen() {
runtime.LockOSThread()
f.mainWindow.Fullscreen()
}
func (f *Frontend) WindowUnFullscreen() {
runtime.LockOSThread()
f.mainWindow.UnFullscreen()
}
func (f *Frontend) WindowShow() {
runtime.LockOSThread()
f.mainWindow.Show()
}
func (f *Frontend) WindowHide() {
runtime.LockOSThread()
f.mainWindow.Hide()
}
func (f *Frontend) WindowMaximise() {
runtime.LockOSThread()
f.mainWindow.Maximise()
}
func (f *Frontend) WindowUnmaximise() {
runtime.LockOSThread()
f.mainWindow.Restore()
}
func (f *Frontend) WindowMinimise() {
runtime.LockOSThread()
f.mainWindow.Minimise()
}
func (f *Frontend) WindowUnminimise() {
runtime.LockOSThread()
f.mainWindow.Restore()
}
func (f *Frontend) WindowSetMinSize(width int, height int) {
runtime.LockOSThread()
f.mainWindow.SetMinSize(width, height)
}
func (f *Frontend) WindowSetMaxSize(width int, height int) {
runtime.LockOSThread()
f.mainWindow.SetMaxSize(width, height)
}
func (f *Frontend) WindowSetColour(colour int) {
runtime.LockOSThread()
// TODO: Set webview2 background to this colour
}
func (f *Frontend) Quit() {
winc.Exit()
}
func NewFrontend(appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings) *Frontend {
return &Frontend{
frontendOptions: appoptions,
logger: myLogger,
bindings: appBindings,
}
}