diff --git a/v2/internal/appng/app.go b/v2/internal/appng/app.go index c173497f..d47fec90 100644 --- a/v2/internal/appng/app.go +++ b/v2/internal/appng/app.go @@ -127,31 +127,16 @@ func CreateApp(appoptions *options.App) (*App, error) { return nil, err } - // Process context menus - contextMenus := options.GetContextMenus(appoptions) - for _, contextMenu := range contextMenus { - menuManager.AddContextMenu(contextMenu) - } - - // Process tray menus - trayMenus := options.GetTrayMenus(appoptions) - for _, trayMenu := range trayMenus { - _, err := menuManager.AddTrayMenu(trayMenu) - if err != nil { - return nil, err - } - } - - //window := ffenestri.NewApplicationWithConfig(appoptions, myLogger, menuManager) - appFrontend := frontend.NewFrontend(appoptions, myLogger) - // Create binding exemptions - Ugly hack. There must be a better way bindingExemptions := []interface{}{appoptions.Startup, appoptions.Shutdown} + appBindings := binding.NewBindings(myLogger, appoptions.Bind, bindingExemptions) + + appFrontend := frontend.NewFrontend(appoptions, myLogger, appBindings) result := &App{ frontend: appFrontend, logger: myLogger, - bindings: binding.NewBindings(myLogger, appoptions.Bind, bindingExemptions), + bindings: appBindings, menuManager: menuManager, startupCallback: appoptions.Startup, shutdownCallback: appoptions.Shutdown, diff --git a/v2/internal/frontend/frontend.go b/v2/internal/frontend/frontend.go index 84be2373..f714ac97 100644 --- a/v2/internal/frontend/frontend.go +++ b/v2/internal/frontend/frontend.go @@ -34,7 +34,7 @@ type Frontend interface { WindowSetMaxSize(width int, height int) WindowFullscreen() WindowUnFullscreen() - //WindowSetColour(colour int) + WindowSetColour(colour int) // //// Menus //SetApplicationMenu(menu *menu.Menu) diff --git a/v2/internal/frontend/frontend_windows.go b/v2/internal/frontend/frontend_windows.go index b70238c1..a83647dc 100644 --- a/v2/internal/frontend/frontend_windows.go +++ b/v2/internal/frontend/frontend_windows.go @@ -1,11 +1,12 @@ package frontend import ( + "github.com/wailsapp/wails/v2/internal/binding" "github.com/wailsapp/wails/v2/internal/frontend/windows" "github.com/wailsapp/wails/v2/internal/logger" "github.com/wailsapp/wails/v2/pkg/options" ) -func NewFrontend(appoptions *options.App, myLogger *logger.Logger) *windows.Frontend { - return windows.NewFrontend(appoptions, myLogger) +func NewFrontend(appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings) *windows.Frontend { + return windows.NewFrontend(appoptions, myLogger, appBindings) } diff --git a/v2/internal/frontend/windows/frontend.go b/v2/internal/frontend/windows/frontend.go index d501db2a..b764776e 100644 --- a/v2/internal/frontend/windows/frontend.go +++ b/v2/internal/frontend/windows/frontend.go @@ -2,6 +2,7 @@ package windows import ( "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" @@ -14,6 +15,7 @@ type Frontend struct { // main window handle mainWindow *Window minWidth, minHeight, maxWidth, maxHeight int + bindings *binding.Bindings } func (f *Frontend) Run() error { @@ -113,14 +115,20 @@ func (f *Frontend) WindowSetMaxSize(width int, height int) { 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) *Frontend { +func NewFrontend(appoptions *options.App, myLogger *logger.Logger, appBindings *binding.Bindings) *Frontend { return &Frontend{ frontendOptions: appoptions, logger: myLogger, + bindings: appBindings, } } diff --git a/v2/internal/frontend/windows/menu.go b/v2/internal/frontend/windows/menu.go new file mode 100644 index 00000000..50f7fed5 --- /dev/null +++ b/v2/internal/frontend/windows/menu.go @@ -0,0 +1,83 @@ +package windows + +import ( + "fmt" + "github.com/tadvi/winc" + "github.com/wailsapp/wails/v2/pkg/menu" +) + +var checkboxMap = map[*menu.MenuItem][]*winc.MenuItem{} + +func toggleCheckBox(menuItem *menu.MenuItem) { + menuItem.Checked = !menuItem.Checked + for _, wincMenu := range checkboxMap[menuItem] { + wincMenu.SetChecked(menuItem.Checked) + } +} + +func addCheckBoxToMap(menuItem *menu.MenuItem, wincMenuItem *winc.MenuItem) { + if checkboxMap[menuItem] == nil { + checkboxMap[menuItem] = []*winc.MenuItem{} + } + checkboxMap[menuItem] = append(checkboxMap[menuItem], wincMenuItem) +} + +func processApplicationMenu(window *Window, menuToProcess *menu.Menu) { + mainMenu := window.NewMenu() + for _, menuItem := range menuToProcess.Items { + println("Adding menu:", menuItem.Label) + submenu := mainMenu.AddSubMenu(menuItem.Label) + for _, menuItem := range menuItem.SubMenu.Items { + fmt.Printf("Processing: %#v\n", menuItem) + processMenuItem(submenu, menuItem) + } + } + mainMenu.Show() +} + +func processMenuItem(parent *winc.MenuItem, menuItem *menu.MenuItem) { + if menuItem.Hidden { + return + } + switch menuItem.Type { + case menu.SeparatorType: + parent.SetSeparator() + case menu.TextType: + newItem := parent.AddItem(menuItem.Label, winc.NoShortcut) + if menuItem.Tooltip != "" { + newItem.SetToolTip(menuItem.Tooltip) + } + if menuItem.Click != nil { + newItem.OnClick().Bind(func(e *winc.Event) { + menuItem.Click(&menu.CallbackData{ + MenuItem: menuItem, + }) + }) + } + newItem.SetEnabled(!menuItem.Disabled) + + case menu.CheckboxType: + newItem := parent.AddItem(menuItem.Label, winc.NoShortcut) + newItem.SetCheckable(true) + newItem.SetChecked(menuItem.Checked) + if menuItem.Tooltip != "" { + newItem.SetToolTip(menuItem.Tooltip) + } + if menuItem.Click != nil { + newItem.OnClick().Bind(func(e *winc.Event) { + toggleCheckBox(menuItem) + menuItem.Click(&menu.CallbackData{ + MenuItem: menuItem, + }) + }) + } + newItem.SetEnabled(!menuItem.Disabled) + addCheckBoxToMap(menuItem, newItem) + case menu.RadioType: + case menu.SubmenuType: + submenu := parent.AddSubMenu(menuItem.Label) + for _, menuItem := range menuItem.SubMenu.Items { + processMenuItem(submenu, menuItem) + } + } +} diff --git a/v2/internal/frontend/windows/window.go b/v2/internal/frontend/windows/window.go index c15ac645..c1b63a41 100644 --- a/v2/internal/frontend/windows/window.go +++ b/v2/internal/frontend/windows/window.go @@ -60,5 +60,9 @@ func NewWindow(parent winc.Controller, options *options.App) *Window { result.Fullscreen() } + if options.Windows.Menu != nil { + processApplicationMenu(result, options.Windows.Menu) + } + return result }