Skip to content

Commit

Permalink
refactored UI
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsgrill committed Oct 5, 2024
1 parent c1e13c5 commit aea60c3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
17 changes: 12 additions & 5 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ func main() {
return
}

icon := ui.CreateNotifyIcon(ui.UIContext{
uicontext := &ui.UIContext{
Logger: mgr.Logger,
LogFile: mgr.logfilepath,
Version: Version,
})
}
statuslist := ui.NewStatusList()
ui.CreateStatusWindow(uicontext, statuslist)

icon := ui.CreateNotifyIcon(uicontext)
icon.Logger.Info().Str("version", Version).Msg("Starting PotatoDrive")
defer icon.Close()

Expand All @@ -32,9 +36,8 @@ func main() {
return
}

statuslist := ui.NewStatusList()
icon.AddAction("Show statuses", func() {
go ui.StatusWindow(statuslist)
uicontext.MainWindow.Show()
})

keys, _ := mgr.InstanceList()
Expand All @@ -47,7 +50,11 @@ func main() {
icon.Logger.Err(err).Msgf("%s is offline %v", keyname, err)
}
},
FileStateCallback: statuslist.AddState,
FileStateCallback: func(fss core.FileSyncState) {
go uicontext.MainWindow.Synchronize(func() {
statuslist.AddState(fss)
})
},
}
err := mgr.StartInstance(keyname, context)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion ui/aboutdialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func openlink(link *walk.LinkLabelLink) {
}
}

func aboutDialog(_ walk.Form, context UIContext) (int, error) {
func aboutDialog(_ walk.Form, context *UIContext) (int, error) {
return MainWindow{
Title: "About PotatoDrive",
Icon: "#2\\0409",
Expand Down
6 changes: 4 additions & 2 deletions ui/notifyicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ type UIContext struct {
Logger zerolog.Logger
LogFile string
Version string

*walk.MainWindow
}

type NotifyIcon struct {
UIContext
*UIContext

*walk.MainWindow
zerolog.Logger
Expand Down Expand Up @@ -66,7 +68,7 @@ func (ui *NotifyIcon) Close() {
ui.ni.Dispose()
}

func CreateNotifyIcon(context UIContext) *NotifyIcon {
func CreateNotifyIcon(context *UIContext) *NotifyIcon {
ui := &NotifyIcon{
UIContext: context,
}
Expand Down
20 changes: 16 additions & 4 deletions ui/statuswindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package ui

import (
"log"
"syscall"

"github.com/balazsgrill/potatodrive/core"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/lxn/win"
)

type StatusList struct {
Expand Down Expand Up @@ -38,8 +40,7 @@ func (sl *StatusList) AddState(state core.FileSyncState) {
sl.PublishItemsReset()
}

func StatusWindow(model *StatusList) {
var mw *walk.MainWindow
func CreateStatusWindow(context *UIContext, model *StatusList) {
var lb *walk.ListBox

styler := &Styler{
Expand All @@ -54,12 +55,13 @@ func StatusWindow(model *StatusList) {
styler.loadIcons()

if err := (MainWindow{
AssignTo: &mw,
AssignTo: &context.MainWindow,
Title: "PotatoDrive status",
MinSize: Size{200, 200},
Size: Size{800, 600},
Font: Font{Family: "Segoe UI", PointSize: 9},
Layout: VBox{},
Visible: false,
Children: []Widget{
Composite{
DoubleBuffering: true,
Expand All @@ -77,5 +79,15 @@ func StatusWindow(model *StatusList) {
}).Create(); err != nil {
log.Fatal(err)
}
mw.Run()

// https://github.com/lxn/walk/issues/326#issuecomment-461074992
var prevWndProcPtr uintptr
prevWndProcPtr = win.SetWindowLongPtr(context.MainWindow.Handle(), win.GWL_WNDPROC,
syscall.NewCallback(func(hWnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
if msg == win.WM_CLOSE {
win.ShowWindow(hWnd, win.SW_HIDE)
return 0
}
return win.CallWindowProc(prevWndProcPtr, hWnd, msg, wParam, lParam)
}))
}

0 comments on commit aea60c3

Please sign in to comment.