5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 02:50:36 +08:00
wails/website/i18n/ja/docusaurus-plugin-content-docs/current/reference/menus.mdx
Lea Anthony b5e98d5780
New Crowdin updates (#1726)
* New translations minecraftupdater.mdx (Korean)
[ci skip]

* New translations links.mdx (Korean)
[ci skip]

* New translations templates.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations developing-new-features.mdx (Korean)
[ci skip]

* New translations documenting.mdx (Korean)
[ci skip]

* New translations troubleshooting.mdx (Korean)
[ci skip]

* New translations windows-installer.mdx (Korean)
[ci skip]

* New translations faq.mdx (Korean)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Korean)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Korean)
[ci skip]

* New translations authors.yml (Korean)
[ci skip]

* New translations credits.mdx (Korean)
[ci skip]

* New translations markdown-page.md (Korean)
[ci skip]

* New translations stats.mdx (Korean)
[ci skip]

* New translations faq.mdx (Japanese)
[ci skip]

* New translations faq.mdx (Russian)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations faq.mdx (Chinese Simplified)
[ci skip]

* New translations options.json (Korean)
[ci skip]

* New translations current.json (Korean)
[ci skip]

* New translations version-v2.0.0-beta.38.json (Korean)
[ci skip]

* New translations version-v2.0.0-beta.39.json (Korean)
[ci skip]

* New translations footer.json (Korean)
[ci skip]

* New translations navbar.json (Korean)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Korean)
[ci skip]

* New translations howdoesitwork.mdx (Korean)
[ci skip]

* New translations windows.mdx (Korean)
[ci skip]

* New translations log.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations linux.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations browser.mdx (Korean)
[ci skip]

* New translations dialog.mdx (Korean)
[ci skip]

* New translations events.mdx (Korean)
[ci skip]

* New translations intro.mdx (Korean)
[ci skip]

* New translations menu.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations window.mdx (Korean)
[ci skip]

* New translations cli.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations menus.mdx (Korean)
[ci skip]

* New translations options.mdx (Korean)
[ci skip]

* New translations project-config.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations helloworld.mdx (Korean)
[ci skip]

* New translations code.json (Korean)
[ci skip]

* New translations developing-new-features.mdx (Chinese Simplified)
[ci skip]

* New translations firstproject.mdx (Japanese)
[ci skip]

* New translations firstproject.mdx (Russian)
[ci skip]

* New translations firstproject.mdx (Chinese Simplified)
[ci skip]

* New translations installation.mdx (Japanese)
[ci skip]

* New translations installation.mdx (Russian)
[ci skip]

* New translations installation.mdx (Chinese Simplified)
[ci skip]

* New translations dynamic-assets.mdx (Japanese)
[ci skip]

* New translations dynamic-assets.mdx (Russian)
[ci skip]

* New translations dynamic-assets.mdx (Chinese Simplified)
[ci skip]

* New translations frameless.mdx (Japanese)
[ci skip]

* New translations frameless.mdx (Russian)
[ci skip]

* New translations frameless.mdx (Chinese Simplified)
[ci skip]

* New translations documenting.mdx (Japanese)
[ci skip]

* New translations documenting.mdx (Russian)
[ci skip]

* New translations frontend.mdx (Japanese)
[ci skip]

* New translations frontend.mdx (Russian)
[ci skip]

* New translations windows.mdx (Chinese Simplified)
[ci skip]

* New translations troubleshooting.mdx (Japanese)
[ci skip]

* New translations troubleshooting.mdx (Russian)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations windows-installer.mdx (Japanese)
[ci skip]

* New translations windows-installer.mdx (Russian)
[ci skip]

* New translations windows-installer.mdx (Chinese Simplified)
[ci skip]

* New translations dialog.mdx (Japanese)
[ci skip]

* New translations dialog.mdx (Russian)
[ci skip]

* New translations signing.mdx (Chinese Simplified)
[ci skip]

* New translations migrating.mdx (Russian)
[ci skip]

* New translations ides.mdx (Japanese)
[ci skip]

* New translations ides.mdx (Russian)
[ci skip]

* New translations ides.mdx (Chinese Simplified)
[ci skip]

* New translations migrating.mdx (Japanese)
[ci skip]

* New translations migrating.mdx (Chinese Simplified)
[ci skip]

* New translations signing.mdx (Russian)
[ci skip]

* New translations mouse-buttons.mdx (Japanese)
[ci skip]

* New translations mouse-buttons.mdx (Russian)
[ci skip]

* New translations mouse-buttons.mdx (Chinese Simplified)
[ci skip]

* New translations overscroll.mdx (Japanese)
[ci skip]

* New translations overscroll.mdx (Russian)
[ci skip]

* New translations routing.mdx (Japanese)
[ci skip]

* New translations routing.mdx (Russian)
[ci skip]

* New translations routing.mdx (Chinese Simplified)
[ci skip]

* New translations signing.mdx (Japanese)
[ci skip]

* New translations dialog.mdx (Chinese Simplified)
[ci skip]

* New translations project-config.mdx (Chinese Simplified)
[ci skip]

* New translations options.mdx (Chinese Simplified)
[ci skip]

* New translations howdoesitwork.mdx (Japanese)
[ci skip]

* New translations howdoesitwork.mdx (Russian)
[ci skip]

* New translations howdoesitwork.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Japanese)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Russian)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Japanese)
[ci skip]

* New translations options.mdx (Russian)
[ci skip]

* New translations window.mdx (Japanese)
[ci skip]

* New translations menu.mdx (Japanese)
[ci skip]

* New translations menu.mdx (Russian)
[ci skip]

* New translations menu.mdx (Chinese Simplified)
[ci skip]

* New translations window.mdx (Russian)
[ci skip]

* New translations options.mdx (Japanese)
[ci skip]

* New translations cli.mdx (Chinese Simplified)
[ci skip]

* New translations menus.mdx (Japanese)
[ci skip]

* New translations menus.mdx (Russian)
[ci skip]

* New translations menus.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Russian)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations emailit.mdx (Chinese Simplified)
[ci skip]

* New translations modalfilemanager.mdx (Chinese Simplified)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Japanese)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Russian)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations credits.mdx (Japanese)
[ci skip]

* New translations credits.mdx (Russian)
[ci skip]

* New translations credits.mdx (Chinese Simplified)
[ci skip]

* New translations code.json (Korean)
[ci skip]

* New translations developing-new-features.mdx (Japanese)
[ci skip]

* New translations developing-new-features.mdx (Russian)
[ci skip]

* New translations ways-of-contributing.mdx (Japanese)
[ci skip]

* New translations documenting.mdx (Japanese)
[ci skip]

* New translations testing.mdx (Japanese)
[ci skip]

* New translations setting-up-a-dev-environment.mdx (Japanese)
[ci skip]

* New translations fixing-bugs.mdx (Japanese)
[ci skip]

* New translations helping-others.mdx (Japanese)
[ci skip]

* New translations ways-of-contributing.mdx (Russian)
[ci skip]

* New translations fixing-bugs.mdx (Chinese Simplified)
[ci skip]

* New translations documenting.mdx (Russian)
[ci skip]

* New translations documenting.mdx (Chinese Simplified)
[ci skip]

* New translations testing.mdx (Chinese Simplified)
[ci skip]

* New translations fixing-bugs.mdx (Russian)
[ci skip]

* New translations ways-of-contributing.mdx (Chinese Simplified)
[ci skip]

* New translations testing.mdx (Russian)
[ci skip]

* New translations helping-others.mdx (Russian)
[ci skip]

* New translations helping-others.mdx (Chinese Simplified)
[ci skip]

* New translations setting-up-a-dev-environment.mdx (Chinese Simplified)
[ci skip]

* New translations setting-up-a-dev-environment.mdx (Russian)
[ci skip]

* New translations developing-new-features.mdx (Chinese Simplified)
[ci skip]

* New translations current.json (Japanese)
[ci skip]

* New translations current.json (Russian)
[ci skip]

* New translations current.json (Chinese Simplified)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Japanese)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Russian)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Chinese Simplified)
[ci skip]

* New translations version-v2.0.0-beta.43.json (Japanese)
[ci skip]

* New translations version-v2.0.0-beta.43.json (Russian)
[ci skip]

* New translations version-v2.0.0-beta.43.json (Chinese Simplified)
[ci skip]

* New translations installation.mdx (Korean)
[ci skip]

* New translations ways-of-contributing.mdx (Korean)
[ci skip]

* New translations testing.mdx (Korean)
[ci skip]

* New translations developing-new-features.mdx (Korean)
[ci skip]

* New translations documenting.mdx (Korean)
[ci skip]

* New translations fixing-bugs.mdx (Korean)
[ci skip]

* New translations helping-others.mdx (Korean)
[ci skip]

* New translations setting-up-a-dev-environment.mdx (Korean)
[ci skip]

* New translations current.json (Korean)
[ci skip]

* New translations options.json (Korean)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Korean)
[ci skip]

* New translations version-v2.0.0-beta.43.json (Korean)
[ci skip]

* New translations footer.json (Korean)
[ci skip]

* New translations navbar.json (Korean)
[ci skip]

* New translations code.json (Korean)
[ci skip]

* New translations community-guide.mdx (Japanese)
[ci skip]

* New translations community-guide.mdx (Korean)
[ci skip]

* New translations community-guide.mdx (Russian)
[ci skip]

* New translations community-guide.mdx (Chinese Simplified)
[ci skip]

* New translations current.json (Japanese)
[ci skip]

* New translations current.json (Russian)
[ci skip]

* New translations current.json (Chinese Simplified)
[ci skip]

* New translations code.json (Japanese)
[ci skip]

* New translations code.json (Russian)
[ci skip]

* New translations code.json (Chinese Simplified)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations current.json (Korean)
[ci skip]

* New translations code.json (Korean)
[ci skip]

* New translations community-guide.mdx (Japanese)
[ci skip]

* New translations community-guide.mdx (Korean)
[ci skip]

* New translations community-guide.mdx (Russian)
[ci skip]

* New translations community-guide.mdx (Chinese Simplified)
[ci skip]

* New translations community-guide.mdx (Chinese Simplified)
[ci skip]

* New translations vscode.mdx (Japanese)
[ci skip]

* New translations vscode.mdx (Korean)
[ci skip]

* New translations vscode.mdx (Russian)
[ci skip]

* New translations vscode.mdx (Chinese Simplified)
[ci skip]

* New translations current.json (Chinese Simplified)
[ci skip]

* New translations riftshare.mdx (Chinese Simplified)
[ci skip]

* New translations surge.mdx (Chinese Simplified)
[ci skip]

* New translations wally.mdx (Chinese Simplified)
[ci skip]

* New translations encrypteasy.mdx (Chinese Simplified)
[ci skip]

* New translations filehound.mdx (Chinese Simplified)
[ci skip]

* New translations mollywallet.mdx (Chinese Simplified)
[ci skip]

* New translations optimus.mdx (Chinese Simplified)
[ci skip]

* New translations _category_.json (Chinese Simplified)
[ci skip]

* New translations emailit.mdx (Chinese Simplified)
[ci skip]

* New translations modalfilemanager.mdx (Chinese Simplified)
[ci skip]

* New translations browser.mdx (Chinese Simplified)
[ci skip]

* New translations vscode.mdx (Russian)
[ci skip]

* New translations vscode.mdx (Chinese Simplified)
[ci skip]

* New translations browser.mdx (Japanese)
[ci skip]

* New translations browser.mdx (Korean)
[ci skip]

* New translations browser.mdx (Russian)
[ci skip]

* New translations dialog.mdx (Japanese)
[ci skip]

* New translations vscode.mdx (Japanese)
[ci skip]

* New translations dialog.mdx (Korean)
[ci skip]

* New translations dialog.mdx (Russian)
[ci skip]

* New translations dialog.mdx (Chinese Simplified)
[ci skip]

* New translations events.mdx (Japanese)
[ci skip]

* New translations events.mdx (Korean)
[ci skip]

* New translations events.mdx (Russian)
[ci skip]

* New translations events.mdx (Chinese Simplified)
[ci skip]

* New translations intro.mdx (Japanese)
[ci skip]

* New translations intro.mdx (Korean)
[ci skip]

* New translations intro.mdx (Russian)
[ci skip]

* New translations vscode.mdx (Korean)
[ci skip]

* New translations intro.mdx (Chinese Simplified)
[ci skip]

* New translations log.mdx (Japanese)
[ci skip]

* New translations options.mdx (Russian)
[ci skip]

* New translations introduction.mdx (Japanese)
[ci skip]

* New translations options.mdx (Chinese Simplified)
[ci skip]

* New translations options.mdx (Korean)
[ci skip]

* New translations log.mdx (Korean)
[ci skip]

* New translations log.mdx (Russian)
[ci skip]

* New translations log.mdx (Chinese Simplified)
[ci skip]

* New translations menu.mdx (Japanese)
[ci skip]

* New translations menu.mdx (Korean)
[ci skip]

* New translations menu.mdx (Russian)
[ci skip]

* New translations menu.mdx (Chinese Simplified)
[ci skip]

* New translations window.mdx (Japanese)
[ci skip]

* New translations window.mdx (Korean)
[ci skip]

* New translations window.mdx (Russian)
[ci skip]

* New translations window.mdx (Chinese Simplified)
[ci skip]

* New translations options.mdx (Japanese)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations introduction.mdx (Russian)
[ci skip]

* New translations community-guide.mdx (Chinese Simplified)
[ci skip]

* New translations stats.mdx (Chinese Simplified)
[ci skip]

* New translations faq.mdx (Chinese Simplified)
[ci skip]

* New translations markdown-page.md (Chinese Simplified)
[ci skip]

* New translations introduction.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Chinese Simplified)
[ci skip]

* Empty-Commit

* Fix crowdin screwups again
2022-08-27 20:17:15 +10:00

267 lines
8.9 KiB
Plaintext

---
sidebar_position: 4
---
# Menus
It is possible to add an application menu to Wails projects. This is achieved by defining a [Menu](#menu) struct and setting it in the [`Menu`](../reference/options.mdx#menu) application config, or by calling the runtime method [MenuSetApplicationMenu](../reference/runtime/menu.mdx#menusetapplicationmenu).
An example of how to create a menu:
```go
AppMenu := menu.NewMenu()
FileMenu := AppMenu.AddSubmenu("File")
FileMenu.AddText("&Open", keys.CmdOrCtrl("o"), openFile)
FileMenu.AddSeparator()
FileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) {
runtime.Quit()
})
if runtime.GOOS == "darwin" {
AppMenu.Append(menu.EditMenu()) // on macos platform, we should append EditMenu to enable Cmd+C,Cmd+V,Cmd+Z... shortcut
}
err := wails.Run(&options.App{
Title: "Menus Demo",
Width: 800,
Height: 600,
Menu: AppMenu,
Bind: []interface{}{
app,
},
)
// ...
```
```` It is also possible to dynamically update the menu, by updating the menu struct and calling \[MenuUpdateApplicationMenu\](../reference/runtime/menu.mdx#menuupdateapplicationmenu).
The example above uses helper methods, however it's possible to build the menu structs manually.
## Menu
A Menu is a collection of MenuItems:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu"
## Menu
A Menu is a collection of MenuItems:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu"
type Menu struct {
Items []*MenuItem
}
```
For the Application menu, each MenuItem represents a single menu such as "Edit".
A simple helper method is provided for building menus:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu"
func NewMenuFromItems(first *MenuItem, rest ...*MenuItem) *Menu
```
This makes the layout of the code more like that of a menu without the need to add the menu items manually after creating them. Alternatively, you can just create the menu items and add them to the menu manually.
## MenuItem
A MenuItem represents an item within a Menu.
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu"
// MenuItem represents a menu item contained in a menu
type MenuItem struct {
Label string
Role Role
Accelerator *keys.Accelerator
Type Type
Disabled bool
Hidden bool
Checked bool
SubMenu *Menu
Click Callback
}
```
| Field | Type | Notes |
| ----------- | ------------------------------------ | ------------------------------------------------------------- |
| Label | string | The menu text |
| Accelerator | [\*keys.Accelerator](#accelerator) | Key binding for this menu item |
| Type | [Type](#type) | Type of MenuItem |
| Disabled | bool | Disables the menu item |
| Hidden | bool | Hides this menu item |
| Checked | bool | Adds check to item (Checkbox & Radio types) |
| SubMenu | [\*Menu](#menu) | Sets the submenu |
| Click | [Callback](#callback) | Callback function when menu clicked |
| Role | string | Defines a [role](#role) for this menu item. Mac only for now. |
### Accelerator
Accelerators (sometimes called keyboard shortcuts) define a binding between a keystroke and a menu item. Wails defines an Accelerator as a combination or key + [Modifier](#modifier). They are available in the `"github.com/wailsapp/wails/v2/pkg/menu/keys"` package.
Example:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu/keys"
// Defines cmd+o on Mac and ctrl-o on Window/Linux
myShortcut := keys.CmdOrCtrl("o")
```
Keys are any single character on a keyboard with the exception of `+`, which is defined as `plus`. Some keys cannot be represented as characters so there are a set of named characters that may be used:
- `backspace`
- `tab`
- `return`
- `enter`
- `escape`
- `left`
- `right`
- `up`
- `down`
- `space`
- `delete`
- `home`
- `end`
- `page up`
- `page down`
- `f1`
- `f2`
- `f3`
- `f4`
- `f5`
- `f6`
- `f7`
- `f8`
- `f9`
- `f10`
- `f11`
- `f12`
- `f13`
- `f14`
- `f15`
- `f16`
- `f17`
- `f18`
- `f19`
- `f20`
- `f21`
- `f22`
- `f23`
- `f24`
- `f25`
- `f26`
- `f27`
- `f28`
- `f29`
- `f30`
- `f31`
- `f32`
- `f33`
- `f34`
- `f35`
- `numlock`
Wails also supports parsing accelerators using the same syntax as Electron. This is useful for storing accelerators in config files.
Example:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu/keys"
// Defines cmd+o on Mac and ctrl-o on Window/Linux
myShortcut, err := keys.Parse("Ctrl+Option+A")
```
#### Modifier
The following modifiers are keys that may be used in combination with the accelerator key:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu/keys"
const (
// CmdOrCtrlKey represents Command on Mac and Control on other platforms
CmdOrCtrlKey Modifier = "cmdorctrl"
// OptionOrAltKey represents Option on Mac and Alt on other platforms
OptionOrAltKey Modifier = "optionoralt"
// ShiftKey represents the shift key on all systems
ShiftKey Modifier = "shift"
// ControlKey represents the control key on all systems
ControlKey Modifier = "ctrl"
)
```
A number of helper methods are available to create Accelerators using modifiers:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu/keys"
func CmdOrCtrl(key string) *Accelerator
func OptionOrAlt(key string) *Accelerator
func Shift(key string) *Accelerator
func Control(key string) *Accelerator
```
Modifiers can be combined using `keys.Combo(key string, modifier1 Modifier, modifier2 Modifier, rest ...Modifier)`:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu/keys"
// Defines "Ctrl+Option+A" on Mac and "Ctrl+Alt+A" on Window/Linux
myShortcut := keys.Combo("a", ControlKey, OptionOrAltKey)
```
### Type
Each menu item must have a type and there are 5 types available:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu"
const (
TextType Type = "Text"
SeparatorType Type = "Separator"
SubmenuType Type = "Submenu"
CheckboxType Type = "Checkbox"
RadioType Type = "Radio"
)
```
For convenience, helper methods are provided to quickly create a menu item:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu"
func Text(label string, accelerator *keys.Accelerator, click Callback) *MenuItem
func Separator() *MenuItem
func Radio(label string, selected bool, accelerator *keys.Accelerator, click Callback) *MenuItem
func Checkbox(label string, checked bool, accelerator *keys.Accelerator, click Callback) *MenuItem
func SubMenu(label string, menu *Menu) *Menu
```
You can also create menu items directly on a menu by using the "Add" helpers:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu"
func (m *Menu) AddText(label string, accelerator *keys.Accelerator, click Callback) *MenuItem
func (m *Menu) AddSeparator() *MenuItem
func (m *Menu) AddRadio(label string, selected bool, accelerator *keys.Accelerator, click Callback) *MenuItem
func (m *Menu) AddCheckbox(label string, checked bool, accelerator *keys.Accelerator, click Callback) *MenuItem
func (m *Menu) AddSubMenu(label string, menu *Menu) *MenuI
```
A note on radio groups: A radio group is defined as a number of radio menu items that are next to each other in the menu. This means that you do not need to group items together as it is automatic. However, that also means you cannot have 2 radio groups next to each other - there must be a non-radio item between them.
### Callback
Each menu item may have a callback that is executed when the item is clicked:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu"
type Callback func(*CallbackData)
type CallbackData struct {
MenuItem *MenuItem
}
```
The function is given a `CallbackData` struct which indicates which menu item triggered the callback. This is useful when using radio groups that may share a callback.
### Role
:::info Roles
Roles are currently supported on Mac only.
:::
A menu item may have a role, which is essentially a pre-defined menu item. We currently support the following roles:
| Role | Description |
| ------------ | ------------------------------------------------------------------------ |
| AppMenuRole | The standard Mac application menu. Can be created using `menu.AppMenu()` |
| EditMenuRole | The standard Mac edit menu. Can be created using `menu.EditMenu()` |