68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
|
package launcher
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
|
||
|
"gioui.org/app"
|
||
|
"gioui.org/io/event"
|
||
|
"git.mstar.dev/mstar/timer/shared"
|
||
|
"github.com/rs/zerolog/log"
|
||
|
)
|
||
|
|
||
|
type LauncherState struct {
|
||
|
possibleTargets map[string]func() shared.WindowState
|
||
|
possibleTargetsLock sync.RWMutex
|
||
|
activeWindows map[string]bool
|
||
|
activeWindowsLock sync.RWMutex
|
||
|
initialSetupDone bool
|
||
|
initialSetupLock sync.RWMutex
|
||
|
}
|
||
|
|
||
|
func (s *LauncherState) Run(globalState *shared.GlobalState, event event.Event) shared.NewState {
|
||
|
if !s.initialSetupDone {
|
||
|
s.initialSetup(globalState)
|
||
|
}
|
||
|
switch event := event.(type) {
|
||
|
case app.FrameEvent:
|
||
|
return s.handleFrameEvent(globalState, &event)
|
||
|
case app.DestroyEvent:
|
||
|
return s.handleQuitEvent()
|
||
|
case app.WaylandViewEvent, app.X11ViewEvent:
|
||
|
return shared.EmptyEvent()
|
||
|
default:
|
||
|
log.Debug().Any("event", event).Type("type", event).Msg("Unknown event")
|
||
|
return shared.NewState{}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *LauncherState) Exit(globalState *shared.GlobalState, wg *sync.WaitGroup) {
|
||
|
wg.Done()
|
||
|
}
|
||
|
|
||
|
func (s *LauncherState) AddTarget(name string, builder func() shared.WindowState) {
|
||
|
s.possibleTargets[name] = builder
|
||
|
}
|
||
|
|
||
|
func (s *LauncherState) initialSetup(globalState *shared.GlobalState) {
|
||
|
s.activeWindowsLock.Lock()
|
||
|
defer s.activeWindowsLock.Unlock()
|
||
|
s.possibleTargetsLock.Lock()
|
||
|
defer s.possibleTargetsLock.Unlock()
|
||
|
s.initialSetupLock.Lock()
|
||
|
defer s.initialSetupLock.Unlock()
|
||
|
s.initialSetupDone = true
|
||
|
s.possibleTargets = map[string]func() shared.WindowState{}
|
||
|
s.activeWindows = map[string]bool{}
|
||
|
}
|
||
|
|
||
|
func (s *LauncherState) handleQuitEvent() shared.NewState {
|
||
|
return shared.NewState{ExitCode: shared.ErrExitOk}
|
||
|
}
|
||
|
|
||
|
func (s *LauncherState) handleFrameEvent(
|
||
|
globalState *shared.GlobalState,
|
||
|
frameEvent *app.FrameEvent,
|
||
|
) shared.NewState {
|
||
|
return shared.NewState{}
|
||
|
}
|