Example #1
0
func (a Action) createAction(builder *Builder, menu *walk.Menu) (*walk.Action, error) {
	action := walk.NewAction()

	if err := action.SetText(a.Text); err != nil {
		return nil, err
	}
	if err := setActionImage(action, a.Image); err != nil {
		return nil, err
	}

	if a.Enabled != nil {
		if b, ok := a.Enabled.(bool); ok {
			if err := action.SetEnabled(b); err != nil {
				return nil, err
			}
		} else if s := builder.conditionOrProperty(a.Enabled); s != nil {
			if c, ok := s.(walk.Condition); ok {
				action.SetEnabledCondition(c)
			} else {
				return nil, fmt.Errorf("value of invalid type bound to Action.Enabled: %T", s)
			}
		}
	}
	if a.Visible != nil {
		if b, ok := a.Visible.(bool); ok {
			if err := action.SetVisible(b); err != nil {
				return nil, err
			}
		} else if s := builder.conditionOrProperty(a.Visible); s != nil {
			if c, ok := s.(walk.Condition); ok {
				action.SetVisibleCondition(c)
			} else {
				return nil, fmt.Errorf("value of invalid type bound to Action.Visible: %T", s)
			}
		}
	}

	s := a.Shortcut
	if err := action.SetShortcut(walk.Shortcut{s.Modifiers, s.Key}); err != nil {
		return nil, err
	}

	if a.OnTriggered != nil {
		action.Triggered().Attach(a.OnTriggered)
	}

	if menu != nil {
		if err := menu.Actions().Add(action); err != nil {
			return nil, err
		}
	}

	if a.AssignTo != nil {
		*a.AssignTo = action
	}

	return action, nil
}
Example #2
0
func main() {
	// We need either a walk.MainWindow or a walk.Dialog for their message loop.
	// We will not make it visible in this example, though.
	mw, err := walk.NewMainWindow()
	if err != nil {
		log.Fatal(err)
	}

	// We load our icon from a file.
	icon, err := walk.NewIconFromFile("../img/x.ico")
	if err != nil {
		log.Fatal(err)
	}

	// Create the notify icon and make sure we clean it up on exit.
	ni, err := walk.NewNotifyIcon()
	if err != nil {
		log.Fatal(err)
	}
	defer ni.Dispose()

	// Set the icon and a tool tip text.
	if err := ni.SetIcon(icon); err != nil {
		log.Fatal(err)
	}
	if err := ni.SetToolTip("Click for info or use the context menu to exit."); err != nil {
		log.Fatal(err)
	}

	// When the left mouse button is pressed, bring up our balloon.
	ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
		if button != walk.LeftButton {
			return
		}

		if err := ni.ShowCustom(
			"Walk NotifyIcon Example",
			"There are multiple ShowX methods sporting different icons."); err != nil {

			log.Fatal(err)
		}
	})

	// We put an exit action into the context menu.
	exitAction := walk.NewAction()
	if err := exitAction.SetText("E&xit"); err != nil {
		log.Fatal(err)
	}
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
		log.Fatal(err)
	}

	// The notify icon is hidden initially, so we have to make it visible.
	if err := ni.SetVisible(true); err != nil {
		log.Fatal(err)
	}

	// Now that the icon is visible, we can bring up an info balloon.
	if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
		log.Fatal(err)
	}

	// Run the message loop.
	mw.Run()
}
Example #3
0
func main() {
	MustRegisterCondition("isSpecialMode", isSpecialMode)

	mw := new(MyMainWindow)

	var openAction, showAboutBoxAction *walk.Action
	var recentMenu *walk.Menu
	var toggleSpecialModePB *walk.PushButton

	if err := (MainWindow{
		AssignTo: &mw.MainWindow,
		Title:    "Walk Actions Example",
		MenuItems: []MenuItem{
			Menu{
				Text: "&File",
				Items: []MenuItem{
					Action{
						AssignTo:    &openAction,
						Text:        "&Open",
						Image:       "../img/open.png",
						Enabled:     Bind("enabledCB.Checked"),
						Visible:     Bind("openVisibleCB.Checked"),
						Shortcut:    Shortcut{walk.ModControl, walk.KeyO},
						OnTriggered: mw.openAction_Triggered,
					},
					Menu{
						AssignTo: &recentMenu,
						Text:     "Recent",
					},
					Separator{},
					Action{
						Text:        "E&xit",
						OnTriggered: func() { mw.Close() },
					},
				},
			},
			Menu{
				Text: "&Help",
				Items: []MenuItem{
					Action{
						AssignTo:    &showAboutBoxAction,
						Text:        "About",
						OnTriggered: mw.showAboutBoxAction_Triggered,
					},
				},
			},
		},
		ToolBarItems: []MenuItem{
			ActionRef{&openAction},
			Menu{
				Text:  "New A",
				Image: "../img/document-new.png",
				Items: []MenuItem{
					Action{
						Text:        "A",
						OnTriggered: mw.newAction_Triggered,
					},
					Action{
						Text:        "B",
						OnTriggered: mw.newAction_Triggered,
					},
					Action{
						Text:        "C",
						OnTriggered: mw.newAction_Triggered,
					},
				},
				OnTriggered: mw.newAction_Triggered,
			},
			Separator{},
			Menu{
				Text:  "View",
				Image: "../img/document-properties.png",
				Items: []MenuItem{
					Action{
						Text:        "X",
						OnTriggered: mw.changeViewAction_Triggered,
					},
					Action{
						Text:        "Y",
						OnTriggered: mw.changeViewAction_Triggered,
					},
					Action{
						Text:        "Z",
						OnTriggered: mw.changeViewAction_Triggered,
					},
				},
			},
			Separator{},
			Action{
				Text:        "Special",
				Image:       "../img/system-shutdown.png",
				Enabled:     Bind("isSpecialMode && enabledCB.Checked"),
				OnTriggered: mw.specialAction_Triggered,
			},
		},
		ContextMenuItems: []MenuItem{
			ActionRef{&showAboutBoxAction},
		},
		MinSize: Size{300, 200},
		Layout:  VBox{},
		Children: []Widget{
			CheckBox{
				Name:    "enabledCB",
				Text:    "Open / Special Enabled",
				Checked: true,
			},
			CheckBox{
				Name:    "openVisibleCB",
				Text:    "Open Visible",
				Checked: true,
			},
			PushButton{
				AssignTo: &toggleSpecialModePB,
				Text:     "Enable Special Mode",
				OnClicked: func() {
					isSpecialMode.SetSatisfied(!isSpecialMode.Satisfied())

					if isSpecialMode.Satisfied() {
						toggleSpecialModePB.SetText("Disable Special Mode")
					} else {
						toggleSpecialModePB.SetText("Enable Special Mode")
					}
				},
			},
		},
	}.Create()); err != nil {
		log.Fatal(err)
	}

	addRecentFileActions := func(texts ...string) {
		for _, text := range texts {
			a := walk.NewAction()
			a.SetText(text)
			a.Triggered().Attach(mw.openAction_Triggered)
			recentMenu.Actions().Add(a)
		}
	}

	addRecentFileActions("Foo", "Bar", "Baz")

	mw.Run()
}