slint/tests/cases/widgets/menubar.slint
2025-08-21 09:23:49 +02:00

186 lines
8 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
import { AboutSlint, Button } from "std-widgets.slint";
export component TestCase inherits Window {
width: 300px;
height: 300px;
in-out property <string> result;
MenuBar {
Menu {
title: "File";
MenuSeparator {} // should be hidden
MenuItem {
title: "New";
activated => { result += self.title; debug("New"); }
}
MenuItem {
title: "Open";
activated => { debug("Open"); }
}
Menu {
function open-recent(entry: string) {
result += entry;
debug(entry);
}
title: "Open Recent";
MenuItem {
title: "Recent 1";
activated => { open-recent("Recent 1"); }
}
MenuItem {
title: "Recent 2";
activated => { open-recent("Recent 2"); }
}
MenuItem {
title: "Recent 3";
activated => { open-recent("Recent 3"); }
}
}
MenuItem {
title: "Checkable";
checkable: true;
checked <=> checkable-menu-item-checked;
}
MenuSeparator {}
MenuSeparator {}
MenuItem {
title: "Save";
activated => {
result += "Save";
debug("Save");
}
}
}
Menu {
title: "Edit";
MenuItem {
title: "Copy";
activated => { debug("Copy"); }
}
MenuItem {
title: "Paste";
activated => { debug("Paste"); }
}
}
}
vl := VerticalLayout {
AboutSlint {}
Button { text: "Hello"; }
}
out property <bool> check-geometry: vl.x == 0 && vl.y == 0 && vl.width == root.width && vl.height == root.height;
out property <bool> test: check-geometry;
out property <bool> checkable-menu-item-checked;
}
/*
```rust
use slint::{SharedString, platform::{Key}};
let instance = TestCase::new().unwrap();
assert!(instance.get_test());
// click on the file menu
slint_testing::send_mouse_click(&instance, 10., 16.);
// navigate using the keys to the "Open Recent" menu item
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::RightArrow));
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow));
assert_eq!(instance.get_result(), "");
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from("\n"));
assert_eq!(instance.get_result(), "Recent 3");
instance.set_result("".into());
//click on the file menu
slint_testing::send_mouse_click(&instance, 10., 16.);
assert_eq!(instance.get_result(), "");
// click on the first entry (new) (this value seems to work with all styles)
slint_testing::send_mouse_click(&instance, 30., 49.);
assert_eq!(instance.get_result(), "New");
instance.set_result("".into());
// click on the file menu
slint_testing::send_mouse_click(&instance, 10., 16.);
// arrow should skip the separators before "save"
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); // New
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); // Open
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); // Open Recent
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); // Checkable
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); // Save (skipped the separator)
assert_eq!(instance.get_result(), "");
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from("\n"));
assert_eq!(instance.get_result(), "Save");
// verify that the checked menu item is not checked
assert!(!instance.get_checkable_menu_item_checked());
// click on the file menu
slint_testing::send_mouse_click(&instance, 10., 16.);
// navigate using the keys to the "Checked" menu item and toggle it
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from("\n"));
// verify that the checked menu item is checked
assert!(instance.get_checkable_menu_item_checked());
```
```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert(instance.get_test());
// click on the file menu
slint_testing::send_mouse_click(&instance, 10., 16.);
// navigate using the keys to the "Open Recent" menu item
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow);
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow);
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow);
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::RightArrow);
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::UpArrow);
assert_eq(instance.get_result(), "");
slint_testing::send_keyboard_string_sequence(&instance, "\n");
assert_eq(instance.get_result(), "Recent 3");
instance.set_result("");
//click on the file menu
slint_testing::send_mouse_click(&instance, 10., 16.);
assert_eq(instance.get_result(), "");
// click on the first entry (new) (this value seems to work with all styles)
slint_testing::send_mouse_click(&instance, 30., 49.);
assert_eq(instance.get_result(), "New");
instance.set_result("");
// click on the file menu
slint_testing::send_mouse_click(&instance, 10., 16.);
// arrow should skip the separators before "save"
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow); // New
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow); // Open
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow); // Open Recent
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow); // Checkable
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow); // Save (skipped the separator)
assert_eq(instance.get_result(), "");
slint_testing::send_keyboard_string_sequence(&instance, "\n");
assert_eq(instance.get_result(), "Save");
// verify that the checked menu item is not checked
assert(!instance.get_checkable_menu_item_checked());
// click on the file menu
slint_testing::send_mouse_click(&instance, 10., 16.);
// navigate using the keys to the "Checked" menu item and toggle it
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow);
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow);
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow);
slint_testing::send_keyboard_string_sequence(&instance, slint::platform::key_codes::DownArrow);
slint_testing::send_keyboard_string_sequence(&instance, "\n");
// verify that the checked menu item is checked
assert(instance.get_checkable_menu_item_checked());
```
*/