Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document MenuBar and ContextMenu #7502

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 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,64 @@
---
<!-- Copyright © SixtyFPS GmbH <[email protected]> ; SPDX-License-Identifier: MIT -->
title: ContextMenu
description: ContextMenu element api.
---
import SlintProperty from '/src/components/SlintProperty.astro';
import Link from '/src/components/Link.astro';

Use the non-visual `ContextMenu` element to declare 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` has focus.
Call the `show()` function on the `ContextMenu` element to programmatically show the context menu.

Define the structure of the menu by placing `MenuItem` elements into the `ContextMenu`.

## Function

### show(Point)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the feeling that we might revisit these semantics in the API review ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, feel free to let me know what feels wrong there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guts feeling is that we may be better off accepting absolute coordinates, to disambiguate the API.

The relative coordinates make sense when ContextMenu surrounds the area that provides the context menu, but I'm not sure one would call show() then and instead rely on the automatic support for the right click?

And when it's not surrounding, the relative coordinates require additional steps:

ctx := ContextMenu { MenuItem { ... } MenuItem { ... } }

canvas := SomeCanvasOfThings {
    something-selected-via-right-click(pos) => {
        ctx.show({x: pos.x - canvas.x + ctx.x, y: pos.y - canvas.y + ctx.y}); // Is this correct?
    }
}

As opposed to just taking absolute coordinates:

ctx := ContextMenu { MenuItem { ... } MenuItem { ... } }

canvas := SomeCanvasOfThings {
    something-selected-via-right-click(pos) => {
        ctx.show({x: pos.x + canvas.absolute-position.x, y: pos.y + canvas.absolute-position.y});
    }
}


## `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.

:::note{Note}
This is only called if the menu entry doesn't have submenus.
:::

## Example

```slint
export component Example {
ContextMenu {
MenuItem {
title: @tr("Cut");
activated => { debug("Cut"); }
}
MenuItem {
title: @tr("Copy");
activated => { debug("Copy"); }
}
MenuItem {
title: @tr("Paste");
activated => { debug("Paste"); }
}
}
}
```
57 changes: 57 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,59 @@ 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

Place a single `MenuBar` element into a `Window` to declare a menu bar for the window.

:::note{Note}
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.
Nest `MenuItem` elements to declare sub-menus.

Depending on the platform, the menu bar might be native or rendered by Slint.
This means that for example, on macOS, the menu bar will be at the top of the screen.
The `width` and `height` property of the `Window` define the client area, excluding 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: @tr("File");
MenuItem {
title: @tr("New");
}
MenuItem {
title: @tr("Open");
}
}
MenuItem {
title: @tr("Edit");
MenuItem {
title: @tr("Copy");
}
MenuItem {
title: @tr("Paste");
}
MenuItem {
title: @tr("Find");
MenuItem {
title: @tr("Find in document...");
}
MenuItem {
title: @tr("Find Next");
}
MenuItem {
title: @tr("Find Previous");
}
}
}
}
// ... actual window content goes here
}
```
2 changes: 1 addition & 1 deletion internal/backends/selector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ i-slint-backend-testing = { workspace = true, optional = true }
i-slint-core-macros = { workspace = true }

[target.'cfg(not(target_os = "android"))'.dependencies]
i-slint-backend-winit = { workspace = true, features = ["default"], optional = true }
i-slint-backend-winit = { workspace = true, features = ["default", "muda"], optional = true }
i-slint-renderer-skia = { workspace = true, optional = true }

[target.'cfg(not(target_os = "wasm"))'.dependencies]
Expand Down
25 changes: 0 additions & 25 deletions internal/compiler/typeregister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,6 @@ impl TypeRegister {
_ => unreachable!(),
};

match &mut register.elements.get_mut("ContextMenu").unwrap() {
ElementType::Builtin(ref mut b) => {
let b = Rc::get_mut(b).unwrap();
Rc::get_mut(&mut b.native_class).unwrap().properties.remove("entries");
}
_ => unreachable!(),
};

let font_metrics_prop = crate::langtype::BuiltinPropertyInfo {
ty: font_metrics_type(),
property_visibility: PropertyVisibility::Output,
Expand Down Expand Up @@ -567,23 +559,6 @@ impl TypeRegister {

register.elements.remove("ComponentContainer");
register.types.remove("component-factory");
match register.elements.get_mut("Window").unwrap() {
&mut ElementType::Builtin(ref mut b) => {
Rc::get_mut(b)
.unwrap()
.additional_accepted_child_types
.remove("MenuBar")
.expect("MenuBar is an experimental type");
}
_ => panic!("Window is a builtin type"),
};
register.context_restricted_types.remove("MenuBar");
match register.elements.get_mut("ContextMenu").unwrap() {
&mut ElementType::Builtin(ref mut b) => {
Rc::get_mut(b).unwrap().is_internal = true;
}
_ => panic!("Window is a builtin type"),
};

Rc::new(RefCell::new(register))
}
Expand Down
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
Loading