mirror of
https://github.com/taigrr/wails.git
synced 2026-04-02 05:08:54 -07:00
[windows-x] Support Application Menu, menu items, callbacks, checkboxes
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
83
v2/internal/frontend/windows/menu.go
Normal file
83
v2/internal/frontend/windows/menu.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user