Skip to content

Commit

Permalink
Documentation for the MenuBar and the ContextMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart committed Jan 30, 2025
1 parent 7677d00 commit 332d4cf
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
61 changes: 61 additions & 0 deletions docs/astro/src/content/docs/reference/window/contextmenu.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
<!-- Copyright © SixtyFPS GmbH <[email protected]> ; SPDX-License-Identifier: MIT -->
title: Window
description: Window element api.
---
import SlintProperty from '/src/components/SlintProperty.astro';

`ContextMenu` is an invisible element that is used to show a context menu.

The context menu is shown if the user right-clicks on the area covered by the `ContextMenu` element,
or if the user presses the "Menu" key on their keyboard while a `FocusScope` within the `ContextMenu` is focused.
It is also possible to show the context menu by calling the `show` function on the `ContextMenu` element.

In addition to normal sub elements, the `ContextMenu` element can contain `MenuItem` elements.
The MenuItem elements are defining the actual menu.

## Function

### show(Point)

Call this function to programmatically show the context menu at the given position relative to the `ContextMenu` element.

## `MenuItem`

A `MenuItem` represents a single menu entry. It can be placed as a child of a `ContextMenu`,
<Link type="MenuBar" />, or another `MenuItem`.

### Properties of `MenuItem`

#### title
<SlintProperty propName="title" typeName="string" defaultValue='""'>
The label of the entry within the menu.
</SlintProperty>

### Callbacks of `MenuItem`

#### activated()

Invoked when the menu entry is activated.
This is only called if the menu entry doesn't have submenus

## Example

```slint
export component Example {
ContextMenu {
MenuItem {
title: "Copy";
activated => { debug("Copy"); }
}
MenuItem {
title: "Paste";
activated => { debug("Paste"); }
}
MenuItem {
title: "Cut";
activated => { debug("Cut"); }
}
}
}
```
41 changes: 41 additions & 0 deletions docs/astro/src/content/docs/reference/window/window.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: Window
description: Window element api.
---
import SlintProperty from '/src/components/SlintProperty.astro';
import Link from '/src/components/Link.astro';

`Window` is the root of the tree of elements that are visible on the screen.

Expand Down Expand Up @@ -57,3 +58,43 @@ Whether the window should be borderless/frameless or not.
<SlintProperty propName="title" typeName="string">
The window title that is shown in the title bar.
</SlintProperty>

## `MenuBar` element

A `Window` can contain one `MenuBar` element. This special element is used to create a menu bar for that window.
There can only be one `MenuBar` element in a `Window` and it must not be in a `for` or a `if`.
The `MenuBar` doesn't have properties, but it should contain <Link type="MenuItem" /> as children that represent top level entries in the menu bar.
Each `MenuItem` can contain further `MenuItem` as children that represent sub-menu

Depending on the platform, the menu bar might be native or rendered by Slint.
This means that for example, on MacOs, the menu bar be in top bar on the desktop where application usually have their top bar.
The `width` and `height` property of the `Window` are the one of the client area, without counting the menu bar.
The `x` and `y` properties of `Window` children are also relative to the client area.

### Example

```slint
export component Example inherits Window {
MenuBar {
MenuItem {
title: "File";
MenuItem {
title: "New";
}
MenuItem {
title: "Open";
}
}
MenuItem {
title: "Edit";
MenuItem {
title: "Copy";
}
MenuItem {
title: "Paste";
}
}
}
// ... actual window content goes here
}
```
6 changes: 6 additions & 0 deletions internal/core-macros/link-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@
"LinuxkmsBackend": {
"href": "guide/backends-and-renderers/backend_linuxkms/"
},
"MenuBar": {
"href": "reference/window/window/#menubar"
},
"MenuItem": {
"href": "reference/window/contextmenu/#menuitem"
},
"Modules": {
"href": "guide/language/coding/file/#modules"
},
Expand Down

0 comments on commit 332d4cf

Please sign in to comment.