2022-09-20 04:00:44 +08:00
|
|
|
use zero_ui::{
|
|
|
|
core::{
|
2022-11-19 03:17:15 +08:00
|
|
|
app::{AppExtended, AppExtension, HeadlessApp},
|
2022-11-17 08:08:28 +08:00
|
|
|
event::EventReceiver,
|
2022-09-20 04:00:44 +08:00
|
|
|
focus::{FocusChangedArgs, FocusChangedCause, ReturnFocusChangedArgs, FOCUS_CHANGED_EVENT, RETURN_FOCUS_CHANGED_EVENT},
|
|
|
|
gesture::HeadlessAppGestureExt,
|
|
|
|
keyboard::HeadlessAppKeyboardExt,
|
|
|
|
window::{HeadlessAppWindowExt, WindowId},
|
|
|
|
},
|
2023-06-19 07:00:16 +08:00
|
|
|
prelude::{new_widget::WidgetUpdateMode, *},
|
2022-09-20 04:00:44 +08:00
|
|
|
};
|
2021-04-24 05:41:28 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn first_and_last_window_events() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![Button! { child = Text!("Button 0") }, Button! { child = Text!("Button 1") },];
|
2021-04-24 05:41:28 +08:00
|
|
|
|
|
|
|
let root_id = WidgetId::new_unique();
|
|
|
|
let stack_id = WidgetId::new_unique();
|
2022-05-27 11:41:32 +08:00
|
|
|
let button_0_id = buttons.item_id(0);
|
2021-04-24 05:41:28 +08:00
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
let mut app = app.run_window(Window! {
|
2023-04-13 05:02:22 +08:00
|
|
|
child = Stack!(id = stack_id; direction = StackDirection::top_to_bottom(); children = buttons);
|
2022-10-25 11:56:12 +08:00
|
|
|
id = root_id;
|
2021-04-24 05:41:28 +08:00
|
|
|
});
|
2023-02-21 22:36:07 +08:00
|
|
|
let root_path = InteractionPath::new_enabled(app.window_id, vec![root_id].into());
|
|
|
|
let button_0_path = InteractionPath::new_enabled(app.window_id, vec![root_id, stack_id, button_0_id].into());
|
2021-04-24 05:41:28 +08:00
|
|
|
|
|
|
|
let events = app.take_focus_changed();
|
|
|
|
assert_eq!(2, events.len());
|
|
|
|
|
2021-05-18 09:24:48 +08:00
|
|
|
// "recover" focus to the focused window root.
|
2021-04-24 05:41:28 +08:00
|
|
|
assert!(events[0].prev_focus.is_none());
|
|
|
|
assert_eq!(Some(root_path.clone()), events[0].new_focus);
|
|
|
|
assert_eq!(FocusChangedCause::Recovery, events[0].cause);
|
|
|
|
assert!(!events[0].highlight);
|
|
|
|
|
|
|
|
// root is a scope that auto-advanced focus to first focusable child.
|
|
|
|
assert_eq!(Some(root_path), events[1].prev_focus);
|
|
|
|
assert_eq!(Some(button_0_path.clone()), events[1].new_focus);
|
|
|
|
assert_eq!(FocusChangedCause::ScopeGotFocus(false), events[1].cause);
|
|
|
|
assert!(!events[1].highlight);
|
|
|
|
|
|
|
|
let events = app.take_return_focus_changed();
|
|
|
|
assert_eq!(1, events.len());
|
|
|
|
|
2021-12-01 12:33:08 +08:00
|
|
|
// the window remembers its previous focused descendant.
|
2021-04-24 05:41:28 +08:00
|
|
|
assert!(events[0].prev_return.is_none());
|
2022-06-03 02:48:47 +08:00
|
|
|
assert_eq!(root_id, events[0].scope.as_ref().map(|p| p.widget_id()).unwrap());
|
2021-04-24 05:41:28 +08:00
|
|
|
assert_eq!(Some(button_0_path.clone()), events[0].new_return);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Last Events
|
|
|
|
*/
|
|
|
|
|
2021-05-14 12:12:14 +08:00
|
|
|
app.close_main_window();
|
2021-04-24 05:41:28 +08:00
|
|
|
|
|
|
|
let events = app.take_focus_changed();
|
|
|
|
assert_eq!(1, events.len());
|
|
|
|
|
|
|
|
// "recover" to focus nothing.
|
|
|
|
assert_eq!(Some(button_0_path.clone()), events[0].prev_focus);
|
|
|
|
assert!(events[0].new_focus.is_none());
|
|
|
|
assert_eq!(FocusChangedCause::Recovery, events[0].cause);
|
|
|
|
assert!(!events[0].highlight);
|
|
|
|
|
|
|
|
let events = app.take_return_focus_changed();
|
|
|
|
assert_eq!(1, events.len());
|
|
|
|
|
|
|
|
// cleanup return focus.
|
|
|
|
assert_eq!(Some(button_0_path), events[0].prev_return);
|
|
|
|
assert!(events[0].new_return.is_none());
|
|
|
|
}
|
2021-04-14 07:54:19 +08:00
|
|
|
|
|
|
|
#[test]
|
2021-04-16 07:08:12 +08:00
|
|
|
pub fn window_tab_cycle_index_auto() {
|
2023-04-13 02:42:03 +08:00
|
|
|
// default Window! cycles TAB navigation.
|
2021-04-16 07:08:12 +08:00
|
|
|
t(|_| TabIndex::AUTO);
|
|
|
|
t(TabIndex);
|
|
|
|
t(|i| TabIndex(TabIndex::AUTO.0 - i - 1));
|
2021-04-15 08:52:19 +08:00
|
|
|
|
2021-04-16 07:08:12 +08:00
|
|
|
fn t(make_index: impl FnMut(u32) -> TabIndex) {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 07:08:12 +08:00
|
|
|
// all TAB navigation must respect the `tab_index` value
|
|
|
|
// that by default is AUTO, but can be not in the same order
|
|
|
|
// as the widgets are declared.
|
|
|
|
let tab_ids: Vec<_> = (0..3).map(make_index).collect();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "btn-0"; child = Text!("Button 0"); tab_index = tab_ids[0] },
|
|
|
|
Button! { id = "btn-1"; child = Text!("Button 1"); tab_index = tab_ids[1] },
|
|
|
|
Button! { id = "btn-2"; child = Text!("Button 2"); tab_index = tab_ids[2] },
|
2021-04-16 07:08:12 +08:00
|
|
|
];
|
|
|
|
// we collect the widget_id values in the TAB navigation order.
|
2022-05-27 11:41:32 +08:00
|
|
|
let mut ids: Vec<_> = (0..3).map(|i| (buttons.item_id(i), tab_ids[i])).collect();
|
2021-04-16 07:08:12 +08:00
|
|
|
ids.sort_by_key(|(_, ti)| *ti);
|
|
|
|
let ids: Vec<_> = ids.into_iter().map(|(id, _)| id).collect();
|
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(buttons));
|
2021-04-16 07:08:12 +08:00
|
|
|
|
2021-04-16 09:38:04 +08:00
|
|
|
// advance normally.
|
2021-04-16 07:08:12 +08:00
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
2022-06-21 11:22:45 +08:00
|
|
|
assert!(app.can_tab());
|
2021-04-16 07:08:12 +08:00
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
// then cycles back.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
// same backwards.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
// cycles back.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
}
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
2021-04-14 07:54:19 +08:00
|
|
|
|
2021-04-14 11:38:26 +08:00
|
|
|
#[test]
|
2021-04-16 03:28:30 +08:00
|
|
|
pub fn window_tab_cycle_and_alt_scope() {
|
2023-04-13 02:42:03 +08:00
|
|
|
// default Window! with an ALT scope, TAB navigation cycles
|
2021-04-15 08:52:19 +08:00
|
|
|
// by default in the ALT scope too.
|
|
|
|
|
2021-04-16 09:38:04 +08:00
|
|
|
t(|_| TabIndex::AUTO);
|
|
|
|
t(TabIndex);
|
|
|
|
t(|i| TabIndex(TabIndex::AUTO.0 - i - 1));
|
|
|
|
|
|
|
|
fn t(make_index: impl FnMut(u32) -> TabIndex) {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 09:38:04 +08:00
|
|
|
let tab_ids: Vec<_> = (0..5).map(make_index).collect();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "btn-0"; child = Text!("Button 0"); tab_index = tab_ids[0] },
|
|
|
|
Button! { id = "btn-1"; child = Text!("Button 1"); tab_index = tab_ids[1] },
|
2021-04-16 09:38:04 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let mut ids: Vec<_> = (0..2).map(|i| (buttons.item_id(i), tab_ids[i])).collect();
|
2021-04-16 09:38:04 +08:00
|
|
|
ids.sort_by_key(|(_, ti)| *ti);
|
|
|
|
let ids: Vec<_> = ids.into_iter().map(|(id, _)| id).collect();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut alt_buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "alt-0"; child = Text!("Alt 0"); tab_index = tab_ids[2] },
|
|
|
|
Button! { id = "alt-1"; child = Text!("Alt 1"); tab_index = tab_ids[3] },
|
|
|
|
Button! { id = "alt-2"; child = Text!("Alt 2"); tab_index = tab_ids[4] },
|
2021-04-16 09:38:04 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let mut alt_ids: Vec<_> = (0..3).map(|i| (alt_buttons.item_id(i), tab_ids[i + 2])).collect();
|
2021-04-16 09:38:04 +08:00
|
|
|
alt_ids.sort_by_key(|(_, ti)| *ti);
|
|
|
|
let alt_ids: Vec<_> = alt_ids.into_iter().map(|(id, _)| id).collect();
|
|
|
|
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(v_stack(ui_vec![
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::left_to_right();
|
2021-04-16 09:38:04 +08:00
|
|
|
alt_focus_scope = true;
|
2022-10-25 11:56:12 +08:00
|
|
|
children = alt_buttons;
|
2021-04-16 09:38:04 +08:00
|
|
|
},
|
|
|
|
v_stack(buttons)
|
|
|
|
]));
|
|
|
|
|
|
|
|
// cycle in the window scope does not enter the ALT scope.
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
// and back.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
// make the "Button 1" be the return focus from the ALT scope.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
|
|
|
|
// goes to the ALT scope.
|
|
|
|
app.press_alt();
|
|
|
|
assert_eq!(Some(alt_ids[0]), app.focused());
|
|
|
|
|
|
|
|
// cycle in the ALT scope.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(alt_ids[1]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(alt_ids[2]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(alt_ids[0]), app.focused());
|
|
|
|
// and back.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(alt_ids[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(alt_ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(alt_ids[0]), app.focused());
|
|
|
|
|
|
|
|
// return to the window scope that focus on the "Button 1".
|
|
|
|
app.press_esc();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
|
|
|
|
// we are back to cycling the window scope.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
|
|
|
|
// also can return from ALT scope by pressing ALT again.
|
|
|
|
app.press_alt();
|
|
|
|
assert_eq!(Some(alt_ids[0]), app.focused());
|
|
|
|
app.press_alt();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
}
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-04-16 03:28:30 +08:00
|
|
|
pub fn window_tab_contained() {
|
2021-04-16 05:32:51 +08:00
|
|
|
// TabNav::Contained stops at the last item and back
|
|
|
|
// the root scope behaves just like any other Contained scope.
|
|
|
|
window_tab_contained_and_continue(TabNav::Contained);
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
|
|
|
#[test]
|
2021-04-16 03:28:30 +08:00
|
|
|
pub fn window_tab_continue() {
|
2021-04-16 05:32:51 +08:00
|
|
|
// TabNav::Continue in the root scope behaves like a Contained
|
|
|
|
// scope because there is no outer-scope to continue to.
|
|
|
|
window_tab_contained_and_continue(TabNav::Continue);
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
2021-04-16 05:32:51 +08:00
|
|
|
fn window_tab_contained_and_continue(tab_nav: TabNav) {
|
2021-04-16 09:38:04 +08:00
|
|
|
t(tab_nav, |_| TabIndex::AUTO);
|
|
|
|
t(tab_nav, TabIndex);
|
|
|
|
t(tab_nav, |i| TabIndex(TabIndex::AUTO.0 - i - 1));
|
|
|
|
|
|
|
|
fn t(tab_nav: TabNav, make_index: impl FnMut(u32) -> TabIndex) {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 09:38:04 +08:00
|
|
|
let tab_ids: Vec<_> = (0..3).map(make_index).collect();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "btn-0"; child = Text!("Button 0"); tab_index = tab_ids[0] },
|
|
|
|
Button! { id = "btn-1"; child = Text!("Button 1"); tab_index = tab_ids[1] },
|
|
|
|
Button! { id = "btn-2"; child = Text!("Button 2"); tab_index = tab_ids[2] },
|
2021-04-16 09:38:04 +08:00
|
|
|
];
|
|
|
|
// we collect the widget_id values in the TAB navigation order.
|
2022-05-27 11:41:32 +08:00
|
|
|
let mut ids: Vec<_> = (0..3).map(|i| (buttons.item_id(i), tab_ids[i])).collect();
|
2021-04-16 09:38:04 +08:00
|
|
|
ids.sort_by_key(|(_, ti)| *ti);
|
|
|
|
let ids: Vec<_> = ids.into_iter().map(|(id, _)| id).collect();
|
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
let mut app = app.run_window(Window! {
|
2021-04-16 09:38:04 +08:00
|
|
|
tab_nav;
|
2022-10-25 11:56:12 +08:00
|
|
|
child = v_stack(buttons);
|
2021-04-16 09:38:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// navigates normally forward.
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
// but after reaching the end does not move.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
|
|
|
|
// same backwards.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
}
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-04-16 03:28:30 +08:00
|
|
|
pub fn window_tab_once() {
|
2021-04-15 08:52:19 +08:00
|
|
|
// we already start focused inside so Once==None in root widgets.
|
2021-04-16 05:32:51 +08:00
|
|
|
window_tab_once_and_none(TabNav::Once);
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
|
|
|
#[test]
|
2021-04-16 03:28:30 +08:00
|
|
|
pub fn window_tab_none() {
|
2021-04-15 08:52:19 +08:00
|
|
|
// we already start focused inside so Once==None in root widgets.
|
2021-04-16 05:32:51 +08:00
|
|
|
window_tab_once_and_none(TabNav::None);
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
2021-04-16 05:32:51 +08:00
|
|
|
fn window_tab_once_and_none(tab_nav: TabNav) {
|
2021-04-16 09:38:04 +08:00
|
|
|
t(tab_nav, |_| TabIndex::AUTO);
|
|
|
|
t(tab_nav, TabIndex);
|
|
|
|
t(tab_nav, |i| TabIndex(TabIndex::AUTO.0 - i - 1));
|
|
|
|
|
|
|
|
fn t(tab_nav: TabNav, make_index: impl FnMut(u32) -> TabIndex) {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 09:38:04 +08:00
|
|
|
let tab_ids: Vec<_> = (0..3).map(make_index).collect();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0"); tab_index = tab_ids[0] },
|
|
|
|
Button! { child = Text!("Button 1"); tab_index = tab_ids[1] },
|
|
|
|
Button! { child = Text!("Button 2"); tab_index = tab_ids[2] },
|
2021-04-16 09:38:04 +08:00
|
|
|
];
|
|
|
|
// we collect the widget_id values in the TAB navigation order.
|
2022-05-27 11:41:32 +08:00
|
|
|
let mut ids: Vec<_> = (0..3).map(|i| (buttons.item_id(i), tab_ids[i])).collect();
|
2021-04-16 09:38:04 +08:00
|
|
|
ids.sort_by_key(|(_, ti)| *ti);
|
|
|
|
let ids: Vec<_> = ids.into_iter().map(|(id, _)| id).collect();
|
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
let mut app = app.run_window(Window! {
|
2022-10-25 11:56:12 +08:00
|
|
|
child = v_stack(buttons);
|
2021-04-16 09:38:04 +08:00
|
|
|
tab_nav;
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.focus(ids[1]);
|
|
|
|
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
}
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-04-16 03:28:30 +08:00
|
|
|
pub fn two_continue_scopes_in_tab_cycle_window() {
|
2021-04-15 08:52:19 +08:00
|
|
|
// TabNav::Continue in non-root widget scopes that are
|
2021-04-15 11:14:37 +08:00
|
|
|
// FocusScopeOnFocus::FirstDescendant just behaves like normal containers.
|
2021-04-16 05:32:51 +08:00
|
|
|
two_continue_scopes_or_containers_in_tab_cycle_window(true);
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
|
|
|
#[test]
|
2021-04-16 03:28:30 +08:00
|
|
|
pub fn two_containers_in_tab_cycle_window() {
|
|
|
|
// the containers are not focus scopes, but they naturally
|
2021-04-16 10:40:56 +08:00
|
|
|
// behave like one with TabNav::Continue, as long as the tab-indexes
|
|
|
|
// are linear or AUTO.
|
2021-04-16 05:32:51 +08:00
|
|
|
two_continue_scopes_or_containers_in_tab_cycle_window(false);
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
2021-04-16 05:32:51 +08:00
|
|
|
fn two_continue_scopes_or_containers_in_tab_cycle_window(focus_scope: bool) {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons_a = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-04-15 08:52:19 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids_a: Vec<_> = (0..3).map(|i| buttons_a.item_id(i)).collect();
|
2021-04-15 08:52:19 +08:00
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons_b = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-04-15 08:52:19 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids_b: Vec<_> = (0..3).map(|i| buttons_b.item_id(i)).collect();
|
2021-04-15 08:52:19 +08:00
|
|
|
|
2023-04-13 05:02:22 +08:00
|
|
|
let a = Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = buttons_a;
|
2021-04-15 08:52:19 +08:00
|
|
|
focus_scope;
|
|
|
|
tab_nav = TabNav::Continue;
|
|
|
|
};
|
2023-04-13 05:02:22 +08:00
|
|
|
let b = Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = buttons_b;
|
2021-04-15 08:52:19 +08:00
|
|
|
focus_scope;
|
|
|
|
tab_nav = TabNav::Continue;
|
|
|
|
};
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(h_stack(ui_vec![a, b]));
|
2021-04-15 08:52:19 +08:00
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
// forward nav goes through the first stack.
|
2021-04-15 08:52:19 +08:00
|
|
|
assert_eq!(Some(ids_a[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_a[1]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_a[2]), app.focused());
|
|
|
|
app.press_tab();
|
2021-04-16 05:32:51 +08:00
|
|
|
// and then the second stack.
|
2021-04-15 08:52:19 +08:00
|
|
|
assert_eq!(Some(ids_b[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_b[1]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_b[2]), app.focused());
|
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
// and then cycles back to the first item in the first stack.
|
2021-04-15 08:52:19 +08:00
|
|
|
app.press_tab();
|
2021-04-16 05:32:51 +08:00
|
|
|
assert_eq!(Some(ids_a[0]), app.focused());
|
2021-04-15 08:52:19 +08:00
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
// backward nav does the same in reverse.
|
2021-04-15 08:52:19 +08:00
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
// cycles back to the last item of the last stack.
|
2021-04-15 08:52:19 +08:00
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[0]), app.focused());
|
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
// then moves back to the last item of the first stack.
|
2021-04-15 08:52:19 +08:00
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_a[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_a[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_a[0]), app.focused());
|
2021-04-16 05:32:51 +08:00
|
|
|
// then cycles again.
|
2021-04-15 08:52:19 +08:00
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[2]), app.focused());
|
|
|
|
}
|
2021-04-14 11:38:26 +08:00
|
|
|
|
2021-04-16 10:40:56 +08:00
|
|
|
#[test]
|
|
|
|
pub fn two_continue_scopes_with_mixed_indexes() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 10:40:56 +08:00
|
|
|
// the tab_index sequence goes back and forth, but
|
|
|
|
// because the containers are scopes they do each container
|
|
|
|
// at a time.
|
|
|
|
//
|
|
|
|
// we are testing that scopes contain their navigation here,
|
|
|
|
// and because scopes exclude their branch when navigating out
|
|
|
|
// it all works here. But you can break this by adding another
|
|
|
|
// scope or other widgets with weird tab indexes in the root scope,
|
|
|
|
// because the navigation goes back to the root momentarily, it can
|
|
|
|
// jump back to a higher priority index without visiting all indexes.
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons_a = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0"); tab_index = 0; },
|
|
|
|
Button! { child = Text!("Button 2"); tab_index = 5; },
|
|
|
|
Button! { child = Text!("Button 1"); tab_index = 3; },
|
2021-04-16 10:40:56 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids_a: Vec<_> = (0..3).map(|i| buttons_a.item_id(i)).collect();
|
2021-04-16 10:40:56 +08:00
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons_b = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 3"); tab_index = 2; },
|
|
|
|
Button! { child = Text!("Button 4"); tab_index = 4; },
|
|
|
|
Button! { child = Text!("Button 5"); tab_index = 6; },
|
2021-04-16 10:40:56 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids_b: Vec<_> = (0..3).map(|i| buttons_b.item_id(i)).collect();
|
2021-04-16 10:40:56 +08:00
|
|
|
|
2023-04-13 05:02:22 +08:00
|
|
|
let a = Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = buttons_a;
|
2021-04-16 10:40:56 +08:00
|
|
|
focus_scope = true;
|
|
|
|
tab_nav = TabNav::Continue;
|
|
|
|
};
|
2023-04-13 05:02:22 +08:00
|
|
|
let b = Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = buttons_b;
|
2021-04-16 10:40:56 +08:00
|
|
|
focus_scope = true;
|
|
|
|
tab_nav = TabNav::Continue;
|
|
|
|
};
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(h_stack(ui_vec![a, b]));
|
2021-04-16 10:40:56 +08:00
|
|
|
|
|
|
|
// window starts at (0), that is also inside `a`.
|
|
|
|
assert_eq!(Some(ids_a[0]), app.focused());
|
|
|
|
|
|
|
|
// goes to next index in the same scope (3), does not goes to (2)
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_a[2]), app.focused());
|
|
|
|
|
|
|
|
// goes to next index in the same scope (5), again did not go to (4)
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_a[1]), app.focused());
|
|
|
|
|
|
|
|
// goes to (2) in the `b` scope now.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_b[0]), app.focused());
|
|
|
|
// goes next to (4)
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_b[1]), app.focused());
|
|
|
|
// goes next to (6)
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_b[2]), app.focused());
|
|
|
|
|
|
|
|
// cycle back to (0)
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_a[0]), app.focused());
|
|
|
|
|
|
|
|
// the same backwards.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_a[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_a[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_a[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn two_containers_with_mixed_indexes() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 10:40:56 +08:00
|
|
|
// the tab-indexes go back and forth in the containers
|
|
|
|
// and because they are not each a scope the focus jumps
|
|
|
|
// from on container to another.
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons_a = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0"); tab_index = 0; },
|
|
|
|
Button! { child = Text!("Button 2"); tab_index = 5; },
|
|
|
|
Button! { child = Text!("Button 1"); tab_index = 3; },
|
2021-04-16 10:40:56 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids_a: Vec<_> = (0..3).map(|i| buttons_a.item_id(i)).collect();
|
2021-04-16 10:40:56 +08:00
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons_b = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 3"); tab_index = 2; },
|
|
|
|
Button! { child = Text!("Button 4"); tab_index = 4; },
|
|
|
|
Button! { child = Text!("Button 5"); tab_index = 6; },
|
2021-04-16 10:40:56 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids_b: Vec<_> = (0..3).map(|i| buttons_b.item_id(i)).collect();
|
2021-04-16 10:40:56 +08:00
|
|
|
|
|
|
|
let a = v_stack(buttons_a);
|
|
|
|
let b = v_stack(buttons_b);
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(h_stack(ui_vec![a, b]));
|
2021-04-16 10:40:56 +08:00
|
|
|
|
|
|
|
// forward.
|
|
|
|
|
|
|
|
// starts at `0`
|
|
|
|
assert_eq!(Some(ids_a[0]), app.focused());
|
|
|
|
// goes to `2` in `b`
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_b[0]), app.focused());
|
|
|
|
// goes to `3` back in `a`
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_a[2]), app.focused());
|
|
|
|
// goes to `4` back in `b`
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_b[1]), app.focused());
|
|
|
|
// goes to `5` back in `a`
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_a[1]), app.focused());
|
|
|
|
// goes to `6` back in `b`
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_b[2]), app.focused());
|
|
|
|
// cycle back to `0` in `a`
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids_a[0]), app.focused());
|
|
|
|
|
|
|
|
// backward.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_a[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_a[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_b[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids_a[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
2021-04-16 03:28:30 +08:00
|
|
|
#[test]
|
|
|
|
pub fn tab_index_skip() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "Button 0"; child = Text!("Button 0") },
|
|
|
|
Button! { id = "Button 1"; child = Text!("Button 1"); tab_index = TabIndex::SKIP; },
|
|
|
|
Button! { id = "Button 2"; child = Text!("Button 2") },
|
2021-04-16 03:28:30 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-04-16 03:28:30 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(buttons));
|
2021-04-16 03:28:30 +08:00
|
|
|
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn tab_inner_container() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 03:28:30 +08:00
|
|
|
// sanity check for `tab_skip_inner_container`.
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut inner_buttons = ui_vec![Button! { child = Text!("Button 1") }, Button! { child = Text!("Button 2") },];
|
2022-05-27 11:41:32 +08:00
|
|
|
let inner_ids: Vec<_> = (0..2).map(|i| inner_buttons.item_id(i)).collect();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2021-04-16 03:28:30 +08:00
|
|
|
v_stack(inner_buttons),
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 3") },
|
2021-04-16 03:28:30 +08:00
|
|
|
];
|
2022-10-25 22:29:57 +08:00
|
|
|
let item_ids: Vec<_> = (0..3).map(|i| children.item_id(i)).collect();
|
2021-04-16 03:28:30 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(children));
|
2021-04-16 03:28:30 +08:00
|
|
|
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn tab_skip_inner_container() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 03:28:30 +08:00
|
|
|
// we expect that TabIndex::SKIP skips the full widget branch
|
|
|
|
// but that the items inside will still tab navigate if focused
|
|
|
|
// directly.
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut inner_buttons = ui_vec![Button! { child = Text!("Button 1") }, Button! { child = Text!("Button 2") },];
|
2022-05-27 11:41:32 +08:00
|
|
|
let inner_ids: Vec<_> = (0..2).map(|i| inner_buttons.item_id(i)).collect();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = inner_buttons;
|
2021-04-16 03:28:30 +08:00
|
|
|
tab_index = TabIndex::SKIP;
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 3") },
|
2021-04-16 03:28:30 +08:00
|
|
|
];
|
2022-10-25 22:29:57 +08:00
|
|
|
let item_ids: Vec<_> = (0..3).map(|i| children.item_id(i)).collect();
|
2021-04-16 03:28:30 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(children));
|
2021-04-16 03:28:30 +08:00
|
|
|
|
|
|
|
// assert skipped inner.
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
|
|
|
|
// assert that focused directly it still works.
|
|
|
|
app.focus(inner_ids[0]);
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
// and continues normally.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
// but is skipped from the outside.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
|
|
|
|
// and the same in reverse.
|
|
|
|
app.focus(inner_ids[1]);
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
}
|
|
|
|
|
2021-04-16 03:33:35 +08:00
|
|
|
#[test]
|
|
|
|
pub fn tab_inner_scope_continue() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 03:33:35 +08:00
|
|
|
// sanity check for `tab_skip_inner_scope_continue`.
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut inner_buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "Button 1"; child = Text!("Button 1") },
|
|
|
|
Button! { id = "Button 2"; child = Text!("Button 2") },
|
2022-07-14 03:32:17 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let inner_ids: Vec<_> = (0..2).map(|i| inner_buttons.item_id(i)).collect();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "Button 0"; child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-07-14 03:32:17 +08:00
|
|
|
id = "Scope Continue";
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = inner_buttons;
|
2021-04-16 03:33:35 +08:00
|
|
|
focus_scope = true;
|
|
|
|
tab_nav = TabNav::Continue;
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "Button 3"; child = Text!("Button 3") },
|
2021-04-16 03:33:35 +08:00
|
|
|
];
|
2022-10-25 22:29:57 +08:00
|
|
|
let item_ids: Vec<_> = (0..3).map(|i| children.item_id(i)).collect();
|
2021-04-16 03:33:35 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(children));
|
2021-04-16 03:33:35 +08:00
|
|
|
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn tab_skip_inner_scope_continue() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 03:33:35 +08:00
|
|
|
// we expect that TabIndex::SKIP skips the full widget branch
|
|
|
|
// but that the items inside will still tab navigate if focused
|
|
|
|
// directly.
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut inner_buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "Button 1"; child = Text!("Button 1") },
|
|
|
|
Button! { id = "Button 2"; child = Text!("Button 2") },
|
2022-07-14 03:32:17 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let inner_ids: Vec<_> = (0..2).map(|i| inner_buttons.item_id(i)).collect();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "Button 0"; child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-07-13 12:43:42 +08:00
|
|
|
id = "v_stack";
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = inner_buttons;
|
2021-04-16 03:33:35 +08:00
|
|
|
focus_scope = true;
|
|
|
|
tab_nav = TabNav::Continue;
|
|
|
|
tab_index = TabIndex::SKIP;
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "Button 3"; child = Text!("Button 3") },
|
2021-04-16 03:33:35 +08:00
|
|
|
];
|
2022-10-25 22:29:57 +08:00
|
|
|
let item_ids: Vec<_> = (0..3).map(|i| children.item_id(i)).collect();
|
2021-04-16 03:33:35 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(children));
|
2021-04-16 03:33:35 +08:00
|
|
|
|
|
|
|
// assert skipped inner.
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
|
|
|
|
// assert that focused directly it still works.
|
|
|
|
app.focus(inner_ids[0]);
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
// and continues normally.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
// but is skipped from the outside.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
|
|
|
|
// and the same in reverse.
|
|
|
|
app.focus(inner_ids[1]);
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
}
|
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
#[test]
|
|
|
|
pub fn tab_inner_scope_cycle() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
// we expect tab navigation to enter the inner scope and get trapped in there.
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut inner_buttons = ui_vec![Button! { child = Text!("Button 1") }, Button! { child = Text!("Button 2") },];
|
2022-05-27 11:41:32 +08:00
|
|
|
let inner_ids: Vec<_> = (0..2).map(|i| inner_buttons.item_id(i)).collect();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = inner_buttons;
|
2021-04-16 05:32:51 +08:00
|
|
|
focus_scope = true;
|
|
|
|
tab_nav = TabNav::Cycle;
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 3") },
|
2021-04-16 05:32:51 +08:00
|
|
|
];
|
2022-10-25 22:29:57 +08:00
|
|
|
let item_ids: Vec<_> = (0..3).map(|i| children.item_id(i)).collect();
|
2021-04-16 05:32:51 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(children));
|
2021-04-16 05:32:51 +08:00
|
|
|
|
|
|
|
// focus starts outside of inner cycle.
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
|
|
|
|
// focus enters the inner cycle.
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
// we still are in the inner cycle.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
|
|
|
|
// same in reverse.
|
|
|
|
app.focus(item_ids[2]);
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn tab_inner_scope_contained() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
// we expect tab navigation to enter the inner scope and get trapped in there.
|
2021-04-16 03:33:35 +08:00
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut inner_buttons = ui_vec![Button! { child = Text!("Button 1") }, Button! { child = Text!("Button 2") },];
|
2022-05-27 11:41:32 +08:00
|
|
|
let inner_ids: Vec<_> = (0..2).map(|i| inner_buttons.item_id(i)).collect();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = inner_buttons;
|
2021-04-16 05:32:51 +08:00
|
|
|
focus_scope = true;
|
|
|
|
tab_nav = TabNav::Contained;
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 3") },
|
2021-04-16 05:32:51 +08:00
|
|
|
];
|
2022-10-25 22:29:57 +08:00
|
|
|
let item_ids: Vec<_> = (0..3).map(|i| children.item_id(i)).collect();
|
2021-04-16 05:32:51 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(children));
|
2021-04-16 05:32:51 +08:00
|
|
|
|
|
|
|
// focus starts outside of inner container.
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
|
|
|
|
// focus enters the inner container.
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
// we still are in the inner container.
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
|
|
|
|
// same in reverse.
|
|
|
|
app.focus(item_ids[2]);
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn tab_inner_scope_once() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
// we expect tab navigation to enter the inner scope but then leave it.
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut inner_buttons = ui_vec![Button! { child = Text!("Button 1") }, Button! { child = Text!("Button 2") },];
|
2022-05-27 11:41:32 +08:00
|
|
|
let inner_ids: Vec<_> = (0..2).map(|i| inner_buttons.item_id(i)).collect();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = inner_buttons;
|
2021-04-16 05:32:51 +08:00
|
|
|
focus_scope = true;
|
|
|
|
tab_nav = TabNav::Once;
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 3") },
|
2021-04-16 05:32:51 +08:00
|
|
|
];
|
2022-10-25 22:29:57 +08:00
|
|
|
let item_ids: Vec<_> = (0..3).map(|i| children.item_id(i)).collect();
|
2021-04-16 05:32:51 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(children));
|
2021-04-16 05:32:51 +08:00
|
|
|
|
|
|
|
// focus starts outside of inner scope.
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
|
|
|
|
// focus enters the inner scope.
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
// and we leave it already.
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
|
|
|
|
|
|
|
// same in reverse.
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn tab_inner_scope_none() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-16 05:32:51 +08:00
|
|
|
// we expect tab navigation to enter the inner scope and then not move.
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut inner_buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "btn-1"; child = Text!("Button 1") },
|
|
|
|
Button! { id = "btn-2"; child = Text!("Button 2") },
|
2022-06-21 13:03:21 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let inner_ids: Vec<_> = (0..2).map(|i| inner_buttons.item_id(i)).collect();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "btn-0"; child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-06-21 13:03:21 +08:00
|
|
|
id = "v-stack";
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = inner_buttons;
|
2021-04-16 05:32:51 +08:00
|
|
|
focus_scope = true;
|
|
|
|
tab_nav = TabNav::None;
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { id = "btn-3"; child = Text!("Button 3") },
|
2021-04-16 05:32:51 +08:00
|
|
|
];
|
2022-10-25 22:29:57 +08:00
|
|
|
let item_ids: Vec<_> = (0..3).map(|i| children.item_id(i)).collect();
|
2021-04-16 05:32:51 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(children));
|
2021-04-16 05:32:51 +08:00
|
|
|
|
|
|
|
// focus starts outside of inner scope.
|
|
|
|
assert_eq!(Some(item_ids[0]), app.focused());
|
2022-06-21 13:03:21 +08:00
|
|
|
assert!(app.can_tab());
|
2021-04-16 05:32:51 +08:00
|
|
|
app.press_tab();
|
|
|
|
|
|
|
|
// focus enters the inner scope.
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
// and we did not move.
|
|
|
|
assert_eq!(Some(inner_ids[0]), app.focused());
|
2022-06-21 13:03:21 +08:00
|
|
|
assert!(!app.can_tab());
|
2021-04-16 05:32:51 +08:00
|
|
|
|
|
|
|
// same in reverse.
|
|
|
|
app.focus(item_ids[2]);
|
|
|
|
assert_eq!(Some(item_ids[2]), app.focused());
|
2022-06-21 13:03:21 +08:00
|
|
|
assert!(app.can_shift_tab());
|
2021-04-16 05:32:51 +08:00
|
|
|
app.press_shift_tab();
|
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
app.press_shift_tab();
|
2022-06-21 13:03:21 +08:00
|
|
|
assert!(!app.can_shift_tab());
|
2021-04-16 05:32:51 +08:00
|
|
|
assert_eq!(Some(inner_ids[1]), app.focused());
|
|
|
|
}
|
2021-04-16 03:33:35 +08:00
|
|
|
|
2021-05-18 07:19:01 +08:00
|
|
|
#[test]
|
|
|
|
pub fn tab_inner_scope_continue_to_non_focusable_siblings_focusable_child() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2022-06-30 01:57:36 +08:00
|
|
|
let btn1 = WidgetId::named("btn-1");
|
|
|
|
let btn2 = WidgetId::named("btn-2");
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(h_stack(ui_vec![
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-06-30 01:57:36 +08:00
|
|
|
id = "initial-scope";
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2021-05-18 07:19:01 +08:00
|
|
|
focus_scope = true;
|
|
|
|
tab_nav = TabNav::Continue;
|
2023-04-13 02:42:03 +08:00
|
|
|
children = ui_vec![Button! { id = btn1; child = Text!("Btn 1"); }];
|
2021-05-18 07:19:01 +08:00
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
v_stack(ui_vec![Button! { id = btn2; child = Text!("Btn 2"); }])
|
2021-05-18 07:19:01 +08:00
|
|
|
]));
|
|
|
|
|
|
|
|
assert_eq!(Some(btn1), app.focused());
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(btn2), app.focused());
|
|
|
|
}
|
|
|
|
|
2021-04-27 07:13:38 +08:00
|
|
|
#[test]
|
|
|
|
pub fn dont_focus_alt_when_alt_pressed_before_focusing_window() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-27 07:13:38 +08:00
|
|
|
let start_focus_id = WidgetId::new_unique();
|
2022-12-29 04:37:14 +08:00
|
|
|
let buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0"); id = start_focus_id; },
|
|
|
|
Button! { child = Text!("Button 1"); },
|
2021-04-27 07:13:38 +08:00
|
|
|
];
|
2023-04-13 02:42:03 +08:00
|
|
|
let alt_buttons = ui_vec![Button! { child = Text!("Alt 0"); }, Button! { child = Text!("Alt 1"); },];
|
2021-04-27 07:13:38 +08:00
|
|
|
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(v_stack(ui_vec![
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::left_to_right();
|
2021-04-27 07:13:38 +08:00
|
|
|
alt_focus_scope = true;
|
2022-10-25 11:56:12 +08:00
|
|
|
children = alt_buttons;
|
2021-04-27 07:13:38 +08:00
|
|
|
},
|
|
|
|
v_stack(buttons)
|
|
|
|
]));
|
|
|
|
|
|
|
|
// clear
|
|
|
|
app.take_focus_changed();
|
|
|
|
app.take_return_focus_changed();
|
|
|
|
assert_eq!(Some(start_focus_id), app.focused());
|
|
|
|
|
|
|
|
// just an ALT release, no press:
|
|
|
|
app.just_release_alt();
|
|
|
|
assert!(app.take_focus_changed().is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-18 09:24:48 +08:00
|
|
|
pub fn window_blur_focus() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-04-27 07:13:38 +08:00
|
|
|
let expected_id = WidgetId::new_unique();
|
2022-12-29 04:37:14 +08:00
|
|
|
let buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0"); },
|
|
|
|
Button! { child = Text!("Button 1"); id = expected_id; },
|
2021-04-27 07:13:38 +08:00
|
|
|
];
|
2023-04-13 02:42:03 +08:00
|
|
|
let alt_buttons = ui_vec![Button! { child = Text!("Alt 0"); }, Button! { child = Text!("Alt 1"); },];
|
2021-04-27 07:13:38 +08:00
|
|
|
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(v_stack(ui_vec![
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2021-04-27 07:13:38 +08:00
|
|
|
alt_focus_scope = true;
|
2022-10-25 11:56:12 +08:00
|
|
|
children = alt_buttons;
|
2021-04-27 07:13:38 +08:00
|
|
|
},
|
|
|
|
v_stack(buttons)
|
|
|
|
]));
|
|
|
|
|
|
|
|
app.press_tab();
|
|
|
|
assert_eq!(Some(expected_id), app.focused());
|
|
|
|
|
2021-05-18 09:24:48 +08:00
|
|
|
app.blur_window();
|
2021-04-27 07:13:38 +08:00
|
|
|
assert_eq!(None, app.focused());
|
2021-05-18 09:24:48 +08:00
|
|
|
app.focus_window();
|
2021-04-27 07:13:38 +08:00
|
|
|
assert_eq!(Some(expected_id), app.focused());
|
|
|
|
|
|
|
|
app.press_alt();
|
|
|
|
assert_ne!(Some(expected_id), app.focused());
|
|
|
|
|
2021-05-18 09:24:48 +08:00
|
|
|
app.blur_window();
|
2021-04-27 07:13:38 +08:00
|
|
|
assert_eq!(None, app.focused());
|
2021-05-18 09:24:48 +08:00
|
|
|
app.focus_window();
|
2021-04-27 07:13:38 +08:00
|
|
|
assert_eq!(Some(expected_id), app.focused());
|
|
|
|
}
|
|
|
|
|
2021-05-14 09:52:37 +08:00
|
|
|
#[test]
|
2022-06-05 05:13:51 +08:00
|
|
|
pub fn focused_removed_by_interacivity() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
2022-06-05 05:13:51 +08:00
|
|
|
let interactive = var(true);
|
2022-11-19 03:17:15 +08:00
|
|
|
focused_removed_test(
|
|
|
|
app,
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 1"); interactive = interactive.clone() },
|
2023-02-24 05:10:57 +08:00
|
|
|
|| interactive.set(false),
|
2022-11-19 03:17:15 +08:00
|
|
|
)
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
pub fn focused_removed_by_collapsing() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
2021-05-14 09:52:37 +08:00
|
|
|
let visibility = var(Visibility::Visible);
|
2023-04-13 02:42:03 +08:00
|
|
|
focused_removed_test(app, Button! { child = Text!("Button 1"); visibility = visibility.clone() }, || {
|
2023-02-24 06:43:46 +08:00
|
|
|
visibility.set(Visibility::Collapsed)
|
|
|
|
})
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
pub fn focused_removed_by_making_not_focusable() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
2021-05-14 09:52:37 +08:00
|
|
|
let focusable = var(true);
|
2023-04-13 02:42:03 +08:00
|
|
|
focused_removed_test(app, Button! { child = Text!("Button 1"); focusable = focusable.clone() }, || {
|
2023-02-24 05:10:57 +08:00
|
|
|
focusable.set(false)
|
2021-05-14 09:52:37 +08:00
|
|
|
})
|
|
|
|
}
|
2023-02-24 05:10:57 +08:00
|
|
|
fn focused_removed_test(app: TestAppBuilder<impl AppExtension>, button1: impl UiNode, set_var: impl FnOnce()) {
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2022-12-29 10:09:26 +08:00
|
|
|
button1,
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 2") },
|
2022-12-29 10:09:26 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-14 09:52:37 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(buttons));
|
2021-05-14 09:52:37 +08:00
|
|
|
|
|
|
|
app.focus(ids[1]);
|
|
|
|
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
|
|
|
|
app.set_vars(set_var);
|
|
|
|
|
|
|
|
assert_ne!(Some(ids[1]), app.focused());
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
pub fn focused_removed_by_deleting() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2022-06-05 10:06:22 +08:00
|
|
|
let exist = var(true);
|
2021-05-14 09:52:37 +08:00
|
|
|
let button1_id = WidgetId::new_unique();
|
2022-06-05 10:06:22 +08:00
|
|
|
|
2022-12-29 04:37:14 +08:00
|
|
|
let buttons = ui_vec! {
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2023-04-29 11:27:49 +08:00
|
|
|
view(exist.clone(), hn!(|a: &ViewArgs<bool>| {
|
|
|
|
if a.data().get() {
|
|
|
|
a.set_view(Button! { id = button1_id; child = Text!("Button 1") });
|
2022-06-05 10:06:22 +08:00
|
|
|
} else {
|
2023-04-29 11:27:49 +08:00
|
|
|
a.unset_view();
|
2022-06-05 10:06:22 +08:00
|
|
|
}
|
2023-04-29 11:27:49 +08:00
|
|
|
})),
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 2") }
|
2021-05-14 09:52:37 +08:00
|
|
|
};
|
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(buttons));
|
2021-05-14 09:52:37 +08:00
|
|
|
|
|
|
|
app.focus(button1_id);
|
|
|
|
assert_eq!(Some(button1_id), app.focused());
|
|
|
|
|
2023-02-24 06:43:46 +08:00
|
|
|
app.set_vars(|| {
|
2023-02-24 05:10:57 +08:00
|
|
|
exist.set(false);
|
2021-05-14 09:52:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
assert_ne!(Some(button1_id), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn focus_widget_or_parent_goes_to_parent() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-05-14 09:52:37 +08:00
|
|
|
let first_focus_id = WidgetId::new_unique();
|
|
|
|
let parent_id = WidgetId::new_unique();
|
|
|
|
let child_id = WidgetId::new_unique();
|
|
|
|
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(v_stack(ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! {
|
2021-05-14 09:52:37 +08:00
|
|
|
id = first_focus_id;
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Text!("initial focus")
|
2021-05-14 09:52:37 +08:00
|
|
|
},
|
2023-04-13 05:02:22 +08:00
|
|
|
Container! {
|
2021-05-14 09:52:37 +08:00
|
|
|
id = parent_id;
|
|
|
|
focusable = true;
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Text! {
|
2021-05-14 09:52:37 +08:00
|
|
|
id = child_id;
|
|
|
|
focusable = false;
|
2022-11-09 02:52:15 +08:00
|
|
|
txt = "not focusable"
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]));
|
|
|
|
|
|
|
|
assert_eq!(Some(first_focus_id), app.focused());
|
|
|
|
app.focus(child_id); // not focusable, does nothing.
|
|
|
|
assert_eq!(Some(first_focus_id), app.focused());
|
|
|
|
|
|
|
|
app.focus_or_parent(child_id);
|
|
|
|
assert_eq!(Some(parent_id), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn focus_widget_or_child_goes_to_child() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-12-02 07:35:57 +08:00
|
|
|
let first_focus_id = WidgetId::named("first_focus");
|
|
|
|
let parent_id = WidgetId::named("parent");
|
|
|
|
let child_id = WidgetId::named("child");
|
2021-05-14 09:52:37 +08:00
|
|
|
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(v_stack(ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! {
|
2021-05-14 09:52:37 +08:00
|
|
|
id = first_focus_id;
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Text!("initial focus")
|
2021-05-14 09:52:37 +08:00
|
|
|
},
|
2023-04-13 05:02:22 +08:00
|
|
|
Container! {
|
2021-05-14 09:52:37 +08:00
|
|
|
id = parent_id;
|
|
|
|
focusable = false;
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Text! {
|
2021-05-14 09:52:37 +08:00
|
|
|
id = child_id;
|
|
|
|
focusable = true;
|
2022-11-09 02:52:15 +08:00
|
|
|
txt = "focusable focusable"
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]));
|
|
|
|
|
|
|
|
assert_eq!(Some(first_focus_id), app.focused());
|
|
|
|
app.focus(parent_id); // not focusable, does nothing.
|
|
|
|
assert_eq!(Some(first_focus_id), app.focused());
|
|
|
|
|
|
|
|
app.focus_or_child(parent_id);
|
2021-12-02 07:35:57 +08:00
|
|
|
|
2021-05-14 09:52:37 +08:00
|
|
|
assert_eq!(Some(child_id), app.focused());
|
|
|
|
}
|
2021-05-14 12:12:14 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn focus_continued_after_widget_id_move() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2022-06-05 10:06:22 +08:00
|
|
|
let id = WidgetId::new_unique();
|
|
|
|
|
|
|
|
let do_move_id = var(false);
|
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(view(
|
2022-06-05 11:50:27 +08:00
|
|
|
do_move_id.clone(),
|
2023-04-29 11:27:49 +08:00
|
|
|
hn!(|a: &ViewArgs<bool>| {
|
|
|
|
if a.data().get() {
|
|
|
|
a.set_view(Container! {
|
|
|
|
id = "some_other_place";
|
|
|
|
child = Button! { id; child = Text!("Button 1") };
|
|
|
|
});
|
|
|
|
} else if a.is_nil() {
|
|
|
|
a.set_view(Wgt! { focusable = true; id; });
|
2022-06-05 11:50:27 +08:00
|
|
|
}
|
2023-04-29 11:27:49 +08:00
|
|
|
}),
|
2022-06-05 11:50:27 +08:00
|
|
|
));
|
2022-06-05 10:06:22 +08:00
|
|
|
|
2021-05-14 12:12:14 +08:00
|
|
|
assert_eq!(Some(id), app.focused());
|
|
|
|
app.take_focus_changed();
|
2023-02-24 06:43:46 +08:00
|
|
|
app.set_vars(|| do_move_id.set(true));
|
2021-05-14 12:12:14 +08:00
|
|
|
|
|
|
|
assert_eq!(Some(id), app.focused());
|
|
|
|
let evs = app.take_focus_changed();
|
|
|
|
assert_eq!(1, evs.len());
|
|
|
|
assert!(evs[0].is_widget_move());
|
|
|
|
assert_eq!(FocusChangedCause::Recovery, evs[0].cause);
|
|
|
|
}
|
|
|
|
|
2021-05-14 09:52:37 +08:00
|
|
|
#[test]
|
2021-05-14 12:12:14 +08:00
|
|
|
pub fn focus_continued_after_widget_move_same_window() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-05-14 09:52:37 +08:00
|
|
|
let id = WidgetId::new_unique();
|
2023-04-13 02:42:03 +08:00
|
|
|
let button = ArcNode::new(Button! {
|
2021-05-14 09:52:37 +08:00
|
|
|
id;
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Text!("Click Me!");
|
2021-05-14 09:52:37 +08:00
|
|
|
});
|
|
|
|
let do_move = var(false);
|
|
|
|
|
2022-12-29 04:37:14 +08:00
|
|
|
let mut app = app.run(v_stack(ui_vec![
|
2023-04-13 05:02:22 +08:00
|
|
|
Container! {
|
2022-10-25 11:56:12 +08:00
|
|
|
child = button.take_when(true)
|
2021-05-14 09:52:37 +08:00
|
|
|
},
|
2023-04-13 05:02:22 +08:00
|
|
|
Container! {
|
2022-10-25 11:56:12 +08:00
|
|
|
child = button.take_when(do_move.clone())
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
]));
|
|
|
|
assert_eq!(Some(id), app.focused());
|
|
|
|
app.take_focus_changed();
|
|
|
|
|
2023-02-24 06:43:46 +08:00
|
|
|
app.set_vars(|| do_move.set(true));
|
2021-05-14 09:52:37 +08:00
|
|
|
|
|
|
|
assert_eq!(Some(id), app.focused());
|
|
|
|
let evs = app.take_focus_changed();
|
|
|
|
assert_eq!(1, evs.len());
|
|
|
|
assert!(evs[0].is_widget_move());
|
|
|
|
assert_eq!(FocusChangedCause::Recovery, evs[0].cause);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-05-14 12:12:14 +08:00
|
|
|
pub fn focus_moves_to_new_window() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-05-14 12:12:14 +08:00
|
|
|
let main_id = WidgetId::new_unique();
|
2021-05-18 12:27:27 +08:00
|
|
|
let win2_id = WidgetId::new_unique();
|
|
|
|
let win3_id = WidgetId::new_unique();
|
2021-05-14 12:12:14 +08:00
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
let mut app = app.run(Button! {
|
2021-05-14 12:12:14 +08:00
|
|
|
id = main_id;
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Text!("Button in main window");
|
2021-05-14 12:12:14 +08:00
|
|
|
});
|
|
|
|
assert_eq!(Some(main_id), app.focused());
|
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
app.open_window(Button! {
|
2021-05-18 12:27:27 +08:00
|
|
|
id = win2_id;
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Text!("Button in second window");
|
2021-05-14 12:12:14 +08:00
|
|
|
});
|
2021-05-18 12:27:27 +08:00
|
|
|
assert_eq!(Some(win2_id), app.focused());
|
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
app.open_window(Button! {
|
2021-05-18 12:27:27 +08:00
|
|
|
id = win3_id;
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Text!("Button in third window");
|
2021-05-18 12:27:27 +08:00
|
|
|
});
|
|
|
|
assert_eq!(Some(win3_id), app.focused());
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn focus_goes_to_parent_after_remove() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2022-01-29 11:03:12 +08:00
|
|
|
let parent_id = WidgetId::named("parent");
|
|
|
|
let child_id = WidgetId::named("child");
|
2021-05-14 09:52:37 +08:00
|
|
|
|
2022-06-05 05:13:51 +08:00
|
|
|
let interactive = var(true);
|
2021-05-14 09:52:37 +08:00
|
|
|
|
2023-04-13 05:02:22 +08:00
|
|
|
let mut app = app.run(v_stack(ui_vec![Container! {
|
2021-05-14 09:52:37 +08:00
|
|
|
id = parent_id;
|
|
|
|
focusable = true;
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Button! {
|
2021-05-14 09:52:37 +08:00
|
|
|
id = child_id;
|
2022-06-05 05:13:51 +08:00
|
|
|
interactive = interactive.clone();
|
2023-04-13 02:42:03 +08:00
|
|
|
child = Text!( "item 'removed'")
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
}]));
|
|
|
|
|
|
|
|
app.focus(child_id);
|
|
|
|
assert_eq!(Some(child_id), app.focused());
|
|
|
|
app.take_focus_changed();
|
|
|
|
|
2023-02-24 06:43:46 +08:00
|
|
|
app.set_vars(|| {
|
2023-02-24 05:10:57 +08:00
|
|
|
interactive.set(false);
|
2021-05-14 09:52:37 +08:00
|
|
|
});
|
|
|
|
assert_eq!(Some(parent_id), app.focused());
|
|
|
|
let evs = app.take_focus_changed();
|
|
|
|
assert_eq!(1, evs.len());
|
|
|
|
assert_eq!(FocusChangedCause::Recovery, evs[0].cause);
|
|
|
|
}
|
|
|
|
|
2021-05-27 05:09:33 +08:00
|
|
|
#[test]
|
|
|
|
pub fn directional_focus_up() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(buttons));
|
2021-05-27 05:09:33 +08:00
|
|
|
|
|
|
|
app.focus(ids[2]);
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
|
|
|
|
app.press_up();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
|
|
|
|
app.press_up();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_focus_down() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(buttons));
|
2021-05-27 05:09:33 +08:00
|
|
|
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.press_down();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
|
|
|
|
app.press_down();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_focus_left() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(h_stack(buttons));
|
2021-05-27 05:09:33 +08:00
|
|
|
|
|
|
|
app.focus(ids[2]);
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
|
|
|
|
app.press_left();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
|
|
|
|
app.press_left();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_focus_right() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(h_stack(buttons));
|
2021-05-27 05:09:33 +08:00
|
|
|
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.press_right();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
|
|
|
|
app.press_right();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_cycle_vertical() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
let mut app = app.run_window(Window! {
|
2021-05-27 05:09:33 +08:00
|
|
|
directional_nav = DirectionalNav::Cycle;
|
2022-10-25 11:56:12 +08:00
|
|
|
child = v_stack(buttons);
|
2021-05-27 05:09:33 +08:00
|
|
|
});
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.press_up();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
|
|
|
|
app.press_down();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_cycle_horizontal() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
let mut app = app.run_window(Window! {
|
2021-05-27 05:09:33 +08:00
|
|
|
directional_nav = DirectionalNav::Cycle;
|
2022-10-25 11:56:12 +08:00
|
|
|
child = h_stack(buttons);
|
2021-05-27 05:09:33 +08:00
|
|
|
});
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.press_left();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
|
|
|
|
app.press_right();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_contained_vertical() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
let mut app = app.run_window(Window! {
|
2021-05-27 05:09:33 +08:00
|
|
|
directional_nav = DirectionalNav::Contained;
|
2022-10-25 11:56:12 +08:00
|
|
|
child = v_stack(buttons);
|
2021-05-27 05:09:33 +08:00
|
|
|
});
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.press_up();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.press_down();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_contained_horizontal() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
let mut app = app.run_window(Window! {
|
2021-05-27 05:09:33 +08:00
|
|
|
directional_nav = DirectionalNav::Contained;
|
2022-10-25 11:56:12 +08:00
|
|
|
child = h_stack(buttons);
|
2021-05-27 05:09:33 +08:00
|
|
|
});
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.press_left();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
|
|
|
|
app.press_right();
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_none() {
|
|
|
|
fn test(press: impl Fn(&mut TestApp)) {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
|
|
|
Button! { child = Text!("Button 1") },
|
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2023-04-13 02:42:03 +08:00
|
|
|
let mut app = app.run_window(Window! {
|
2021-05-27 05:09:33 +08:00
|
|
|
directional_nav = DirectionalNav::None;
|
2022-10-25 11:56:12 +08:00
|
|
|
child = h_stack(buttons);
|
2021-05-27 05:09:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
app.focus(ids[1]);
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
|
|
|
|
press(&mut app);
|
|
|
|
assert_eq!(Some(ids[1]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
test(|a| a.press_up());
|
|
|
|
test(|a| a.press_down());
|
|
|
|
test(|a| a.press_left());
|
|
|
|
test(|a| a.press_right());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_continue_up() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-05-27 05:09:33 +08:00
|
|
|
let start_id = WidgetId::new_unique();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2021-05-27 05:09:33 +08:00
|
|
|
focus_scope = true;
|
|
|
|
directional_nav = DirectionalNav::Continue;
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 1"); id = start_id; },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(buttons));
|
2021-05-27 05:09:33 +08:00
|
|
|
|
|
|
|
app.focus(start_id);
|
|
|
|
assert_eq!(Some(start_id), app.focused());
|
|
|
|
|
|
|
|
app.press_up();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_continue_down() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-05-27 05:09:33 +08:00
|
|
|
let start_id = WidgetId::new_unique();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2021-05-27 05:09:33 +08:00
|
|
|
focus_scope = true;
|
|
|
|
directional_nav = DirectionalNav::Continue;
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 1"); id = start_id; },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(v_stack(buttons));
|
2021-05-27 05:09:33 +08:00
|
|
|
|
|
|
|
app.focus(start_id);
|
|
|
|
assert_eq!(Some(start_id), app.focused());
|
|
|
|
|
|
|
|
app.press_down();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_continue_left() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-05-27 05:09:33 +08:00
|
|
|
let start_id = WidgetId::new_unique();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2021-05-27 05:09:33 +08:00
|
|
|
focus_scope = true;
|
|
|
|
directional_nav = DirectionalNav::Continue;
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 1"); id = start_id; },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(h_stack(buttons));
|
2021-05-27 05:09:33 +08:00
|
|
|
|
|
|
|
app.focus(start_id);
|
|
|
|
assert_eq!(Some(start_id), app.focused());
|
|
|
|
|
|
|
|
app.press_left();
|
|
|
|
assert_eq!(Some(ids[0]), app.focused());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn directional_continue_right() {
|
2022-11-19 03:17:15 +08:00
|
|
|
let app = TestApp::start();
|
|
|
|
|
2021-05-27 05:09:33 +08:00
|
|
|
let start_id = WidgetId::new_unique();
|
2023-04-19 07:49:06 +08:00
|
|
|
let mut buttons = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 0") },
|
2023-04-13 05:02:22 +08:00
|
|
|
Stack! {
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2021-05-27 05:09:33 +08:00
|
|
|
focus_scope = true;
|
|
|
|
directional_nav = DirectionalNav::Continue;
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 1"); id = start_id; },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
|
|
|
},
|
2023-04-13 02:42:03 +08:00
|
|
|
Button! { child = Text!("Button 2") },
|
2021-05-27 05:09:33 +08:00
|
|
|
];
|
2022-05-27 11:41:32 +08:00
|
|
|
let ids: Vec<_> = (0..3).map(|i| buttons.item_id(i)).collect();
|
2021-05-27 05:09:33 +08:00
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = app.run(h_stack(buttons));
|
2021-05-27 05:09:33 +08:00
|
|
|
|
|
|
|
app.focus(start_id);
|
|
|
|
assert_eq!(Some(start_id), app.focused());
|
|
|
|
|
|
|
|
app.press_right();
|
|
|
|
assert_eq!(Some(ids[2]), app.focused());
|
|
|
|
}
|
|
|
|
|
2022-11-19 03:17:15 +08:00
|
|
|
struct TestAppBuilder<E: AppExtension> {
|
|
|
|
app: AppExtended<E>,
|
2021-04-14 07:54:19 +08:00
|
|
|
}
|
2022-11-19 03:17:15 +08:00
|
|
|
impl<E: AppExtension> TestAppBuilder<E> {
|
|
|
|
pub fn run(self, child: impl UiNode) -> TestApp {
|
2023-04-13 02:42:03 +08:00
|
|
|
self.run_window(Window!(child; id = "window root"))
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
2023-04-15 02:13:27 +08:00
|
|
|
pub fn run_window(self, window: WindowRoot) -> TestApp {
|
2022-11-19 03:17:15 +08:00
|
|
|
let mut app = self.app.run_headless(false);
|
2021-04-24 05:41:28 +08:00
|
|
|
|
2021-06-03 08:37:37 +08:00
|
|
|
let (focus_changed, return_focus_changed) = {
|
2022-11-17 08:08:28 +08:00
|
|
|
let a = FOCUS_CHANGED_EVENT.receiver();
|
|
|
|
let b = RETURN_FOCUS_CHANGED_EVENT.receiver();
|
2021-06-16 12:11:33 +08:00
|
|
|
(a, b)
|
2021-06-03 08:37:37 +08:00
|
|
|
};
|
2021-04-24 05:41:28 +08:00
|
|
|
|
2023-04-01 23:22:48 +08:00
|
|
|
let window_id = app.open_window(async move { window });
|
2021-04-24 05:41:28 +08:00
|
|
|
TestApp {
|
|
|
|
app,
|
|
|
|
window_id,
|
|
|
|
focus_changed,
|
|
|
|
return_focus_changed,
|
|
|
|
}
|
|
|
|
}
|
2022-11-19 03:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TestApp {
|
|
|
|
app: HeadlessApp,
|
|
|
|
pub window_id: WindowId,
|
|
|
|
|
|
|
|
focus_changed: EventReceiver<FocusChangedArgs>,
|
|
|
|
return_focus_changed: EventReceiver<ReturnFocusChangedArgs>,
|
|
|
|
}
|
|
|
|
impl TestApp {
|
|
|
|
/// Start app scope.
|
|
|
|
pub fn start() -> TestAppBuilder<impl AppExtension> {
|
|
|
|
TestAppBuilder { app: App::default() }
|
|
|
|
}
|
2021-04-24 05:41:28 +08:00
|
|
|
|
2023-02-24 06:43:46 +08:00
|
|
|
pub fn set_vars(&mut self, set: impl FnOnce()) {
|
|
|
|
set();
|
2021-08-20 06:55:00 +08:00
|
|
|
let _ = self.app.update(false);
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-14 12:12:14 +08:00
|
|
|
pub fn close_main_window(&mut self) {
|
2021-04-24 05:41:28 +08:00
|
|
|
let closed = self.app.close_window(self.window_id);
|
|
|
|
assert!(closed);
|
|
|
|
}
|
|
|
|
|
2022-10-25 22:29:57 +08:00
|
|
|
pub fn open_window(&mut self, child: impl UiNode) -> WindowId {
|
2023-04-01 23:22:48 +08:00
|
|
|
let id = self.app.open_window(async {
|
2023-04-13 02:42:03 +08:00
|
|
|
Window! {
|
2022-10-25 22:29:57 +08:00
|
|
|
child
|
2021-05-14 12:12:14 +08:00
|
|
|
}
|
|
|
|
});
|
2021-08-20 06:55:00 +08:00
|
|
|
let _ = self.app.update(false);
|
2021-05-14 12:12:14 +08:00
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
pub fn close_window(&mut self, window_id: WindowId) {
|
|
|
|
let closed = self.app.close_window(window_id);
|
|
|
|
assert!(closed);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-04-24 05:41:28 +08:00
|
|
|
pub fn take_focus_changed(&mut self) -> Vec<FocusChangedArgs> {
|
2022-11-17 08:08:28 +08:00
|
|
|
self.focus_changed.try_iter().collect()
|
2021-04-24 05:41:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_return_focus_changed(&mut self) -> Vec<ReturnFocusChangedArgs> {
|
2022-11-17 08:08:28 +08:00
|
|
|
self.return_focus_changed.try_iter().collect()
|
2021-04-14 07:54:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focused(&mut self) -> Option<WidgetId> {
|
2023-02-22 08:14:29 +08:00
|
|
|
FOCUS.focused().get().as_ref().map(|w| w.widget_id())
|
2021-04-14 07:54:19 +08:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:22:45 +08:00
|
|
|
pub fn can_tab(&self) -> bool {
|
2022-10-02 04:57:37 +08:00
|
|
|
zero_ui::core::focus::commands::FOCUS_NEXT_CMD.is_enabled().get()
|
2022-06-21 11:22:45 +08:00
|
|
|
}
|
2022-06-21 13:03:21 +08:00
|
|
|
pub fn can_shift_tab(&self) -> bool {
|
2022-10-02 04:57:37 +08:00
|
|
|
zero_ui::core::focus::commands::FOCUS_PREV_CMD.is_enabled().get()
|
2022-06-21 13:03:21 +08:00
|
|
|
}
|
2022-06-21 11:22:45 +08:00
|
|
|
|
2021-04-14 07:54:19 +08:00
|
|
|
pub fn press_tab(&mut self) {
|
|
|
|
self.app.press_key(self.window_id, Key::Tab)
|
|
|
|
}
|
2021-04-15 08:52:19 +08:00
|
|
|
pub fn press_shift_tab(&mut self) {
|
|
|
|
self.app.press_shortcut(self.window_id, shortcut!(SHIFT + Tab));
|
|
|
|
}
|
|
|
|
|
2021-04-14 11:38:26 +08:00
|
|
|
pub fn press_alt(&mut self) {
|
|
|
|
self.app.press_key(self.window_id, Key::LAlt);
|
|
|
|
}
|
|
|
|
pub fn press_esc(&mut self) {
|
|
|
|
self.app.press_key(self.window_id, Key::Escape);
|
|
|
|
}
|
2021-04-15 08:52:19 +08:00
|
|
|
|
2021-05-27 05:09:33 +08:00
|
|
|
pub fn press_up(&mut self) {
|
|
|
|
self.app.press_key(self.window_id, Key::Up);
|
|
|
|
}
|
|
|
|
pub fn press_down(&mut self) {
|
|
|
|
self.app.press_key(self.window_id, Key::Down);
|
|
|
|
}
|
|
|
|
pub fn press_left(&mut self) {
|
|
|
|
self.app.press_key(self.window_id, Key::Left);
|
|
|
|
}
|
|
|
|
pub fn press_right(&mut self) {
|
|
|
|
self.app.press_key(self.window_id, Key::Right);
|
|
|
|
}
|
|
|
|
|
2021-04-27 07:13:38 +08:00
|
|
|
pub fn just_release_alt(&mut self) {
|
2021-06-30 08:46:20 +08:00
|
|
|
self.app.on_keyboard_input(self.window_id, Key::LAlt, KeyState::Released);
|
2021-08-20 06:55:00 +08:00
|
|
|
let _ = self.app.update(false);
|
2021-04-27 07:13:38 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 08:52:19 +08:00
|
|
|
pub fn focus(&mut self, widget_id: WidgetId) {
|
2023-02-22 08:14:29 +08:00
|
|
|
FOCUS.focus_widget(widget_id, true);
|
2021-08-20 06:55:00 +08:00
|
|
|
let _ = self.app.update(false);
|
2021-04-15 08:52:19 +08:00
|
|
|
}
|
2021-04-27 07:13:38 +08:00
|
|
|
|
2021-05-14 09:52:37 +08:00
|
|
|
pub fn focus_or_parent(&mut self, widget_id: WidgetId) {
|
2023-02-22 08:14:29 +08:00
|
|
|
FOCUS.focus_widget_or_exit(widget_id, true);
|
2021-08-20 06:55:00 +08:00
|
|
|
let _ = self.app.update(false);
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focus_or_child(&mut self, widget_id: WidgetId) {
|
2023-02-22 08:14:29 +08:00
|
|
|
FOCUS.focus_widget_or_enter(widget_id, true);
|
2021-08-20 06:55:00 +08:00
|
|
|
let _ = self.app.update(false);
|
2021-05-14 09:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-05-18 09:24:48 +08:00
|
|
|
pub fn focus_window(&mut self) {
|
|
|
|
self.app.focus_window(self.window_id)
|
2021-04-27 07:13:38 +08:00
|
|
|
}
|
|
|
|
|
2021-05-18 09:24:48 +08:00
|
|
|
pub fn blur_window(&mut self) {
|
|
|
|
self.app.blur_window(self.window_id)
|
2021-04-27 07:13:38 +08:00
|
|
|
}
|
2022-10-25 22:29:57 +08:00
|
|
|
}
|
2021-12-02 07:35:57 +08:00
|
|
|
|
2022-10-25 22:29:57 +08:00
|
|
|
trait TestList {
|
2023-04-19 07:49:06 +08:00
|
|
|
fn item_id(&mut self, i: usize) -> WidgetId;
|
2021-04-14 07:54:19 +08:00
|
|
|
}
|
2022-10-25 22:29:57 +08:00
|
|
|
impl<L: UiNodeList> TestList for L {
|
2023-04-19 07:49:06 +08:00
|
|
|
fn item_id(&mut self, i: usize) -> WidgetId {
|
2023-06-19 07:00:16 +08:00
|
|
|
self.with_node(i, |n| n.with_context(WidgetUpdateMode::Ignore, || WIDGET.id()).unwrap())
|
2022-10-25 22:29:57 +08:00
|
|
|
}
|
2022-10-25 23:00:03 +08:00
|
|
|
}
|