2023-10-15 04:02:09 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2023-10-16 10:26:25 +08:00
|
|
|
"tinyrdm/backend/consts"
|
2023-10-15 04:02:09 +08:00
|
|
|
"tinyrdm/backend/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type systemService struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
var system *systemService
|
|
|
|
var onceSystem sync.Once
|
|
|
|
|
|
|
|
func System() *systemService {
|
|
|
|
if system == nil {
|
|
|
|
onceSystem.Do(func() {
|
|
|
|
system = &systemService{}
|
|
|
|
go system.loopWindowEvent()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return system
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *systemService) Start(ctx context.Context) {
|
|
|
|
s.ctx = ctx
|
2023-10-16 10:26:25 +08:00
|
|
|
|
|
|
|
// maximize the window if screen size is lower than the minimum window size
|
|
|
|
if screen, err := runtime.ScreenGetAll(ctx); err == nil && len(screen) > 0 {
|
|
|
|
for _, sc := range screen {
|
|
|
|
if sc.IsCurrent {
|
|
|
|
if sc.Size.Width < consts.MIN_WINDOW_WIDTH || sc.Size.Height < consts.MIN_WINDOW_HEIGHT {
|
|
|
|
runtime.WindowMaximise(ctx)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-15 04:02:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SelectFile open file dialog to select a file
|
|
|
|
func (s *systemService) SelectFile(title string) (resp types.JSResp) {
|
|
|
|
filepath, err := runtime.OpenFileDialog(s.ctx, runtime.OpenDialogOptions{
|
|
|
|
Title: title,
|
|
|
|
ShowHiddenFiles: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
resp.Msg = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Success = true
|
|
|
|
resp.Data = map[string]any{
|
|
|
|
"path": filepath,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *systemService) loopWindowEvent() {
|
|
|
|
var fullscreen, maximised, minimised, normal bool
|
2023-12-16 01:21:30 +08:00
|
|
|
var width, height int
|
2023-10-15 04:02:09 +08:00
|
|
|
var dirty bool
|
|
|
|
for {
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
|
|
|
if s.ctx == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dirty = false
|
|
|
|
if f := runtime.WindowIsFullscreen(s.ctx); f != fullscreen {
|
|
|
|
// full-screen switched
|
|
|
|
fullscreen = f
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if w, h := runtime.WindowGetSize(s.ctx); w != width || h != height {
|
|
|
|
// window size changed
|
|
|
|
width, height = w, h
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if m := runtime.WindowIsMaximised(s.ctx); m != maximised {
|
|
|
|
maximised = m
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if m := runtime.WindowIsMinimised(s.ctx); m != minimised {
|
|
|
|
minimised = m
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if n := runtime.WindowIsNormal(s.ctx); n != normal {
|
|
|
|
normal = n
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if dirty {
|
|
|
|
runtime.EventsEmit(s.ctx, "window_changed", map[string]any{
|
|
|
|
"fullscreen": fullscreen,
|
|
|
|
"width": width,
|
|
|
|
"height": height,
|
|
|
|
"maximised": maximised,
|
|
|
|
"minimised": minimised,
|
|
|
|
"normal": normal,
|
|
|
|
})
|
|
|
|
|
|
|
|
if !fullscreen && !minimised {
|
2023-12-12 11:40:22 +08:00
|
|
|
// save window size and position
|
2023-11-24 11:49:59 +08:00
|
|
|
Preferences().SaveWindowSize(width, height, maximised)
|
2023-10-15 04:02:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|