func main() { var tool *walk.Action var menutool *walk.Menu var mw *walk.MainWindow mw.SetMaximizeBox(false) mw.SetFixedSize(true) mw, _ = walk.NewMainWindowCody() mw.SetTitle("测试") mw.SetSize(walk.Size{300, 200}) menutool, _ = walk.NewMenu() tool = walk.NewMenuAction(menutool) tool.SetText("文件") open := walk.NewAction() open.SetText("打开") exit := walk.NewAction() exit.SetText("退出") menutool.Actions().Add(open) menutool.Actions().Add(exit) men2, _ := walk.NewMenu() too2 := walk.NewMenuAction(men2) too2.SetText("工具") mw.Menu().Actions().Add(tool) mw.Menu().Actions().Add(too2) mw.Show() mw.Run() }
func setActionImage(action *walk.Action, image interface{}) (err error) { var img *walk.Bitmap switch image := image.(type) { case nil: return nil case *walk.Bitmap: img = image case string: if img, err = walk.NewBitmapFromFile(image); err != nil { return } default: return errors.New("invalid type for Image") } return action.SetImage(img) }
func setActionImage(action *walk.Action, image interface{}) (err error) { var img walk.Image switch image := image.(type) { case nil: return nil case *walk.Bitmap: img = image case string: if img, err = imageFromFile(image); err != nil { return } } if bmp, ok := img.(*walk.Bitmap); ok { return action.SetImage(bmp) } return errors.New("invalid type for Image") }
func (m Menu) createAction(builder *Builder, menu *walk.Menu) (*walk.Action, error) { subMenu, err := walk.NewMenu() if err != nil { return nil, err } var action *walk.Action if menu == nil { action = walk.NewMenuAction(subMenu) } else if action, err = menu.Actions().AddMenu(subMenu); err != nil { return nil, err } if err := action.SetText(m.Text); err != nil { return nil, err } if err := setActionImage(action, m.Image); err != nil { return nil, err } for _, item := range m.Items { if _, err := item.createAction(builder, subMenu); err != nil { return nil, err } } if m.OnTriggered != nil { action.Triggered().Attach(m.OnTriggered) } if m.AssignActionTo != nil { *m.AssignActionTo = action } if m.AssignTo != nil { *m.AssignTo = subMenu } return action, nil }
func (a Action) initAction(wa *walk.Action) (*walk.Action, error) { text := a.Text if text == "" { text = "-" } if err := wa.SetText(text); err != nil { return nil, err } if err := wa.SetImage(a.Image); err != nil { return nil, err } if a.OnTriggered != nil { wa.Triggered().Attach(a.OnTriggered) } if a.AssignTo != nil { *a.AssignTo = wa } return wa, nil }