2020-08-07 00:07:32 +08:00
|
|
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
2022-09-20 08:05:02 +08:00
|
|
|
use zero_ui::core::focus::{FocusRequest, FocusTarget, FOCUS_CHANGED_EVENT};
|
2023-02-25 12:16:05 +08:00
|
|
|
use zero_ui::prelude::new_widget::WINDOW;
|
2020-08-27 11:51:40 +08:00
|
|
|
use zero_ui::prelude::*;
|
2023-03-03 13:00:06 +08:00
|
|
|
use zero_ui::widgets::window::{LayerIndex, LAYERS};
|
2020-07-22 12:16:15 +08:00
|
|
|
|
2022-06-13 10:10:21 +08:00
|
|
|
use zero_ui_view_prebuilt as zero_ui_view;
|
2021-11-07 12:15:13 +08:00
|
|
|
|
2020-07-22 12:16:15 +08:00
|
|
|
fn main() {
|
2022-06-13 06:04:09 +08:00
|
|
|
examples_util::print_info();
|
2021-09-29 01:52:56 +08:00
|
|
|
zero_ui_view::init();
|
2021-12-26 12:47:49 +08:00
|
|
|
|
2022-07-27 23:46:40 +08:00
|
|
|
// let rec = examples_util::record_profile("focus");
|
2022-06-03 02:48:47 +08:00
|
|
|
|
2022-06-13 10:10:21 +08:00
|
|
|
// zero_ui_view::run_same_process(app_main);
|
|
|
|
app_main();
|
2022-06-03 02:48:47 +08:00
|
|
|
|
2022-06-13 06:04:09 +08:00
|
|
|
// rec.finish();
|
2021-10-12 09:27:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn app_main() {
|
2023-04-01 23:22:48 +08:00
|
|
|
App::default().run_window(async {
|
2023-02-25 12:16:05 +08:00
|
|
|
WINDOW.id().set_name("main").unwrap();
|
2022-06-13 06:04:09 +08:00
|
|
|
|
2022-10-09 12:11:51 +08:00
|
|
|
trace_focus();
|
2022-01-30 00:07:50 +08:00
|
|
|
let window_enabled = var(true);
|
2020-07-22 12:16:15 +08:00
|
|
|
window! {
|
2021-03-12 03:40:31 +08:00
|
|
|
title = "Focus Example";
|
2022-01-30 00:07:50 +08:00
|
|
|
enabled = window_enabled.clone();
|
2022-06-20 11:08:38 +08:00
|
|
|
background = commands();
|
2022-12-28 11:57:28 +08:00
|
|
|
child = stack! {
|
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2020-09-10 02:44:15 +08:00
|
|
|
alt_scope(),
|
2022-12-28 11:57:28 +08:00
|
|
|
stack! {
|
|
|
|
direction = StackDirection::left_to_right();
|
2021-05-14 12:12:14 +08:00
|
|
|
margin = (50, 0, 0, 0);
|
2022-02-15 03:19:44 +08:00
|
|
|
align = Align::CENTER;
|
2021-05-14 12:12:14 +08:00
|
|
|
spacing = 10;
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2021-05-17 02:24:30 +08:00
|
|
|
tab_index(),
|
2022-06-14 01:58:49 +08:00
|
|
|
functions(window_enabled),
|
|
|
|
delayed_focus(),
|
2021-05-14 12:12:14 +08:00
|
|
|
]
|
|
|
|
}
|
2021-03-31 06:49:36 +08:00
|
|
|
];
|
2020-07-22 12:16:15 +08:00
|
|
|
};
|
2022-07-03 09:39:44 +08:00
|
|
|
// zero_ui::properties::inspector::show_center_points = true;
|
|
|
|
// zero_ui::properties::inspector::show_bounds = true;
|
2022-07-24 03:47:36 +08:00
|
|
|
// zero_ui::properties::inspector::show_hit_test = true;
|
2022-07-06 10:11:20 +08:00
|
|
|
// zero_ui::properties::inspector::show_directional_query = Some(zero_ui::core::units::Orientation2D::Below);
|
2020-07-22 12:16:15 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-21 23:07:26 +08:00
|
|
|
fn alt_scope() -> impl UiNode {
|
2022-12-28 11:57:28 +08:00
|
|
|
stack! {
|
|
|
|
direction = StackDirection::left_to_right();
|
2021-03-12 03:40:31 +08:00
|
|
|
alt_focus_scope = true;
|
2023-04-07 00:31:51 +08:00
|
|
|
button::vis::extend_style = style_fn!(|_| style! {
|
2022-09-16 11:18:17 +08:00
|
|
|
border = unset!;
|
|
|
|
corner_radius = unset!;
|
2022-08-15 09:12:20 +08:00
|
|
|
});
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2020-09-10 02:44:15 +08:00
|
|
|
button("alt", TabIndex::AUTO),
|
|
|
|
button("scope", TabIndex::AUTO),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 23:07:26 +08:00
|
|
|
fn tab_index() -> impl UiNode {
|
2022-12-28 11:57:28 +08:00
|
|
|
stack! {
|
|
|
|
direction = StackDirection::top_to_bottom();
|
2021-03-12 03:40:31 +08:00
|
|
|
spacing = 5;
|
2021-05-18 02:14:52 +08:00
|
|
|
focus_shortcut = shortcut!(T);
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2021-05-14 12:12:14 +08:00
|
|
|
title("TabIndex (T)"),
|
2021-05-20 03:35:57 +08:00
|
|
|
button("Button A5", 5),
|
|
|
|
button("Button A4", 3),
|
|
|
|
button("Button A3", 2),
|
|
|
|
button("Button A1", 0),
|
|
|
|
button("Button A2", 0),
|
2021-03-31 06:49:36 +08:00
|
|
|
];
|
2020-09-10 02:44:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 08:16:39 +08:00
|
|
|
fn functions(window_enabled: ArcVar<bool>) -> impl UiNode {
|
2022-12-28 11:57:28 +08:00
|
|
|
stack! {
|
|
|
|
direction = StackDirection::top_to_bottom();
|
2021-05-14 12:12:14 +08:00
|
|
|
spacing = 5;
|
2021-05-17 02:24:30 +08:00
|
|
|
focus_shortcut = shortcut!(F);
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2021-05-14 12:12:14 +08:00
|
|
|
title("Functions (F)"),
|
2022-01-30 00:07:50 +08:00
|
|
|
// New Window
|
2021-05-14 12:12:14 +08:00
|
|
|
button! {
|
2022-12-29 10:09:26 +08:00
|
|
|
child = text!("New Window");
|
2023-02-27 03:59:36 +08:00
|
|
|
on_click = hn!(|_| {
|
2023-04-01 23:22:48 +08:00
|
|
|
WINDOWS.open(async {
|
2023-02-25 12:16:05 +08:00
|
|
|
let _ = WINDOW.id().set_name("other");
|
2022-06-13 06:04:09 +08:00
|
|
|
window! {
|
|
|
|
title = "Other Window";
|
|
|
|
focus_shortcut = shortcut!(W);
|
2022-12-28 11:57:28 +08:00
|
|
|
child = stack! {
|
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-06-13 06:04:09 +08:00
|
|
|
align = Align::CENTER;
|
|
|
|
spacing = 5;
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2022-06-13 06:04:09 +08:00
|
|
|
title("Other Window (W)"),
|
|
|
|
button("Button B5", 5),
|
|
|
|
button("Button B4", 3),
|
|
|
|
button("Button B3", 2),
|
|
|
|
button("Button B1", 0),
|
|
|
|
button("Button B2", 0),
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
2021-07-31 11:37:04 +08:00
|
|
|
});
|
2021-06-22 10:00:40 +08:00
|
|
|
});
|
2021-05-18 07:19:01 +08:00
|
|
|
},
|
2022-01-30 00:07:50 +08:00
|
|
|
// Detach Button
|
2021-05-18 02:14:52 +08:00
|
|
|
{
|
2022-11-23 08:16:39 +08:00
|
|
|
let detach_focused = ArcNode::new_cyclic(|wk| {
|
2021-05-21 00:01:07 +08:00
|
|
|
let btn = button! {
|
2022-12-29 10:09:26 +08:00
|
|
|
child = text!("Detach Button");
|
2022-09-07 05:25:30 +08:00
|
|
|
// focus_on_init = true;
|
2023-02-27 03:59:36 +08:00
|
|
|
on_click = hn!(|_| {
|
2021-05-16 02:31:55 +08:00
|
|
|
let wwk = wk.clone();
|
2023-04-01 23:22:48 +08:00
|
|
|
WINDOWS.open(async move {
|
2021-05-16 02:31:55 +08:00
|
|
|
window! {
|
|
|
|
title = "Detached Button";
|
2022-10-25 11:56:12 +08:00
|
|
|
child_align = Align::CENTER;
|
|
|
|
child = wwk.upgrade().unwrap().take_when(true);
|
2021-05-16 02:31:55 +08:00
|
|
|
}
|
2021-07-31 11:37:04 +08:00
|
|
|
});
|
2021-06-22 10:00:40 +08:00
|
|
|
});
|
2021-05-21 00:01:07 +08:00
|
|
|
};
|
|
|
|
btn.boxed()
|
2021-05-15 09:45:49 +08:00
|
|
|
});
|
2022-10-05 11:57:59 +08:00
|
|
|
detach_focused.take_when(true).into_widget()
|
2022-01-30 00:07:50 +08:00
|
|
|
},
|
2022-02-02 03:56:09 +08:00
|
|
|
// Disable Window
|
|
|
|
disable_window(window_enabled.clone()),
|
2022-02-01 12:18:58 +08:00
|
|
|
// Overlay Scope
|
2022-01-30 00:07:50 +08:00
|
|
|
button! {
|
2022-12-29 10:09:26 +08:00
|
|
|
child = text!("Overlay Scope");
|
2023-02-27 03:59:36 +08:00
|
|
|
on_click = hn!(|_| {
|
2023-03-03 13:00:06 +08:00
|
|
|
LAYERS.insert(LayerIndex::TOP_MOST, overlay(window_enabled.clone()));
|
2022-01-30 00:07:50 +08:00
|
|
|
});
|
2022-06-14 01:58:49 +08:00
|
|
|
},
|
2022-06-22 05:58:53 +08:00
|
|
|
nested_focusables(),
|
2021-05-14 12:12:14 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:16:39 +08:00
|
|
|
fn disable_window(window_enabled: ArcVar<bool>) -> impl UiNode {
|
2022-02-02 03:56:09 +08:00
|
|
|
button! {
|
2022-12-29 10:09:26 +08:00
|
|
|
child = text!(window_enabled.map(|&e| if e { "Disable Window" } else { "Enabling in 1s..." }.into()));
|
2022-06-22 05:58:53 +08:00
|
|
|
min_width = 140;
|
2023-02-27 03:59:36 +08:00
|
|
|
on_click = async_hn!(window_enabled, |_| {
|
2023-02-24 05:10:57 +08:00
|
|
|
window_enabled.set(false);
|
2022-08-09 04:50:47 +08:00
|
|
|
task::deadline(1.secs()).await;
|
2023-02-24 05:10:57 +08:00
|
|
|
window_enabled.set(true);
|
2022-02-02 03:56:09 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 08:16:39 +08:00
|
|
|
fn overlay(window_enabled: ArcVar<bool>) -> impl UiNode {
|
2022-01-30 00:07:50 +08:00
|
|
|
container! {
|
|
|
|
id = "overlay";
|
2022-02-01 10:31:20 +08:00
|
|
|
modal = true;
|
2022-10-25 11:56:12 +08:00
|
|
|
child_align = Align::CENTER;
|
|
|
|
child = container! {
|
2022-01-30 00:07:50 +08:00
|
|
|
focus_scope = true;
|
2022-02-02 11:24:25 +08:00
|
|
|
tab_nav = TabNav::Cycle;
|
|
|
|
directional_nav = DirectionalNav::Cycle;
|
2022-09-18 03:28:00 +08:00
|
|
|
background_color = color_scheme_map(colors::BLACK.with_alpha(90.pct()), colors::WHITE.with_alpha(90.pct()));
|
2022-02-01 12:18:58 +08:00
|
|
|
drop_shadow = (0, 0), 4, colors::BLACK;
|
2022-01-30 00:07:50 +08:00
|
|
|
padding = 2;
|
2022-12-28 11:57:28 +08:00
|
|
|
child = stack! {
|
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children_align = Align::RIGHT;
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2022-01-30 00:07:50 +08:00
|
|
|
text! {
|
2022-11-09 02:52:15 +08:00
|
|
|
txt = "Window scope is disabled so the overlay scope is the root scope.";
|
2022-01-30 00:07:50 +08:00
|
|
|
margin = 15;
|
|
|
|
},
|
2022-12-28 11:57:28 +08:00
|
|
|
stack! {
|
|
|
|
direction = StackDirection::left_to_right();
|
2022-02-02 03:56:09 +08:00
|
|
|
spacing = 2;
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2022-02-02 03:56:09 +08:00
|
|
|
disable_window(window_enabled),
|
|
|
|
button! {
|
2022-12-29 10:09:26 +08:00
|
|
|
child = text!("Close");
|
2023-02-27 03:59:36 +08:00
|
|
|
on_click = hn!(|_| {
|
2023-03-03 13:00:06 +08:00
|
|
|
LAYERS.remove("overlay");
|
2022-02-02 03:56:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
2022-01-30 00:07:50 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-14 12:12:14 +08:00
|
|
|
|
2022-10-21 23:07:26 +08:00
|
|
|
fn delayed_focus() -> impl UiNode {
|
2022-12-28 11:57:28 +08:00
|
|
|
stack! {
|
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-06-14 01:58:49 +08:00
|
|
|
spacing = 5;
|
|
|
|
focus_shortcut = shortcut!(D);
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2022-06-14 01:58:49 +08:00
|
|
|
title("Delayed 4s (D)"),
|
|
|
|
|
2023-02-27 03:59:36 +08:00
|
|
|
delayed_btn("Force Focus", || {
|
2023-02-22 08:14:29 +08:00
|
|
|
FOCUS.focus(FocusRequest {
|
2022-06-14 01:58:49 +08:00
|
|
|
target: FocusTarget::Direct(WidgetId::named("target")),
|
|
|
|
highlight: true,
|
|
|
|
force_window_focus: true,
|
|
|
|
window_indicator: None,
|
|
|
|
});
|
|
|
|
}),
|
2023-02-27 03:59:36 +08:00
|
|
|
delayed_btn("Info Indicator", || {
|
2023-02-22 08:14:29 +08:00
|
|
|
FOCUS.focus(FocusRequest {
|
2022-06-14 01:58:49 +08:00
|
|
|
target: FocusTarget::Direct(WidgetId::named("target")),
|
|
|
|
highlight: true,
|
|
|
|
force_window_focus: false,
|
|
|
|
window_indicator: Some(FocusIndicator::Info),
|
|
|
|
});
|
|
|
|
}),
|
2023-02-27 03:59:36 +08:00
|
|
|
delayed_btn("Critical Indicator", || {
|
2023-02-22 08:14:29 +08:00
|
|
|
FOCUS.focus(FocusRequest {
|
2022-06-14 01:58:49 +08:00
|
|
|
target: FocusTarget::Direct(WidgetId::named("target")),
|
|
|
|
highlight: true,
|
|
|
|
force_window_focus: false,
|
|
|
|
window_indicator: Some(FocusIndicator::Critical),
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
|
|
|
text! {
|
|
|
|
id = "target";
|
2022-08-15 09:12:20 +08:00
|
|
|
padding = (7, 15);
|
|
|
|
corner_radius = 4;
|
2022-11-09 02:52:15 +08:00
|
|
|
txt = "delayed target";
|
2022-06-14 01:58:49 +08:00
|
|
|
font_style = FontStyle::Italic;
|
2022-12-26 02:40:10 +08:00
|
|
|
txt_align = Align::CENTER;
|
2022-09-18 03:28:00 +08:00
|
|
|
background_color = color_scheme_map(rgb(30, 30, 30), rgb(225, 225, 225));
|
2022-06-14 01:58:49 +08:00
|
|
|
|
|
|
|
focusable = true;
|
2022-10-21 23:07:26 +08:00
|
|
|
when *#is_focused {
|
2022-11-09 02:52:15 +08:00
|
|
|
txt = "focused";
|
2022-09-18 03:28:00 +08:00
|
|
|
background_color = color_scheme_map(colors::DARK_GREEN, colors::LIGHT_GREEN);
|
2022-06-14 01:58:49 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2023-02-27 03:59:36 +08:00
|
|
|
fn delayed_btn(content: impl Into<Text>, on_timeout: impl FnMut() + Send + 'static) -> impl UiNode {
|
2022-11-18 01:35:39 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
use zero_ui::core::task::parking_lot::Mutex;
|
|
|
|
|
|
|
|
let on_timeout = Arc::new(Mutex::new(Box::new(on_timeout)));
|
2022-06-14 01:58:49 +08:00
|
|
|
let enabled = var(true);
|
|
|
|
button! {
|
2022-12-29 10:09:26 +08:00
|
|
|
child = text!(content.into());
|
2023-02-27 03:59:36 +08:00
|
|
|
on_click = async_hn!(enabled, on_timeout, |_| {
|
2023-02-24 05:10:57 +08:00
|
|
|
enabled.set(false);
|
2022-08-09 04:50:47 +08:00
|
|
|
task::deadline(4.secs()).await;
|
2023-02-27 03:59:36 +08:00
|
|
|
let mut on_timeout = on_timeout.lock();
|
|
|
|
on_timeout();
|
2023-02-24 05:10:57 +08:00
|
|
|
enabled.set(true);
|
2022-06-14 01:58:49 +08:00
|
|
|
});
|
|
|
|
enabled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 02:52:15 +08:00
|
|
|
fn title(txt: impl IntoVar<Text>) -> impl UiNode {
|
|
|
|
text! { txt; font_weight = FontWeight::BOLD; align = Align::CENTER; }
|
2021-05-14 12:12:14 +08:00
|
|
|
}
|
|
|
|
|
2022-10-21 23:07:26 +08:00
|
|
|
fn button(content: impl Into<Text>, tab_index: impl Into<TabIndex>) -> impl UiNode {
|
2020-08-23 07:15:33 +08:00
|
|
|
let content = content.into();
|
2021-05-20 03:35:57 +08:00
|
|
|
let tab_index = tab_index.into();
|
2020-07-22 12:16:15 +08:00
|
|
|
button! {
|
2022-12-29 10:09:26 +08:00
|
|
|
child = text!(content.clone());
|
2020-07-22 12:16:15 +08:00
|
|
|
tab_index;
|
2023-02-27 03:59:36 +08:00
|
|
|
on_click = hn!(|_| {
|
2022-01-19 13:07:24 +08:00
|
|
|
println!("Clicked {content} {tab_index:?}")
|
2021-06-22 10:00:40 +08:00
|
|
|
});
|
2020-07-22 12:16:15 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-27 11:51:40 +08:00
|
|
|
|
2022-10-21 23:07:26 +08:00
|
|
|
fn commands() -> impl UiNode {
|
2022-06-20 11:08:38 +08:00
|
|
|
use zero_ui::core::focus::commands::*;
|
|
|
|
|
|
|
|
let cmds = [
|
2022-09-20 04:00:44 +08:00
|
|
|
FOCUS_NEXT_CMD,
|
|
|
|
FOCUS_PREV_CMD,
|
|
|
|
FOCUS_UP_CMD,
|
|
|
|
FOCUS_RIGHT_CMD,
|
|
|
|
FOCUS_DOWN_CMD,
|
|
|
|
FOCUS_LEFT_CMD,
|
|
|
|
FOCUS_ALT_CMD,
|
|
|
|
FOCUS_ENTER_CMD,
|
|
|
|
FOCUS_EXIT_CMD,
|
2022-06-20 11:08:38 +08:00
|
|
|
];
|
|
|
|
|
2022-12-28 11:57:28 +08:00
|
|
|
stack! {
|
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-06-20 11:08:38 +08:00
|
|
|
align = Align::BOTTOM_RIGHT;
|
|
|
|
padding = 10;
|
|
|
|
spacing = 8;
|
2022-10-25 11:56:12 +08:00
|
|
|
children_align = Align::RIGHT;
|
2022-06-20 11:08:38 +08:00
|
|
|
|
|
|
|
font_family = FontName::monospace();
|
2022-11-09 02:52:15 +08:00
|
|
|
txt_color = colors::GRAY;
|
2022-06-20 11:08:38 +08:00
|
|
|
|
2022-10-25 11:56:12 +08:00
|
|
|
children = cmds.into_iter().map(|cmd| {
|
2022-06-20 11:08:38 +08:00
|
|
|
text! {
|
2022-11-09 02:52:15 +08:00
|
|
|
txt = cmd.name_with_shortcut();
|
2022-06-20 11:08:38 +08:00
|
|
|
|
2022-09-20 04:00:44 +08:00
|
|
|
when *#{cmd.is_enabled()} {
|
2022-11-09 02:52:15 +08:00
|
|
|
txt_color = color_scheme_map(colors::WHITE, colors::BLACK);
|
2022-06-20 11:08:38 +08:00
|
|
|
}
|
2022-10-25 11:56:12 +08:00
|
|
|
}.boxed()
|
|
|
|
}).collect::<Vec<_>>();
|
2022-06-20 11:08:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 12:11:51 +08:00
|
|
|
fn trace_focus() {
|
|
|
|
FOCUS_CHANGED_EVENT
|
2023-02-25 10:36:48 +08:00
|
|
|
.on_pre_event(app_hn!(|args: &FocusChangedArgs, _| {
|
2022-10-09 12:11:51 +08:00
|
|
|
if args.is_hightlight_changed() {
|
|
|
|
println!("highlight: {}", args.highlight);
|
|
|
|
} else if args.is_widget_move() {
|
|
|
|
println!("focused {:?} moved", args.new_focus.as_ref().unwrap());
|
|
|
|
} else if args.is_enabled_change() {
|
|
|
|
println!("focused {:?} enabled/disabled", args.new_focus.as_ref().unwrap());
|
|
|
|
} else {
|
2023-02-21 04:47:00 +08:00
|
|
|
println!("{} -> {}", inspect::focus(&args.prev_focus), inspect::focus(&args.new_focus));
|
2022-10-09 12:11:51 +08:00
|
|
|
}
|
|
|
|
}))
|
2022-04-27 10:25:55 +08:00
|
|
|
.perm();
|
2021-05-15 04:39:50 +08:00
|
|
|
}
|
|
|
|
|
2022-10-21 23:07:26 +08:00
|
|
|
fn nested_focusables() -> impl UiNode {
|
2022-06-22 05:58:53 +08:00
|
|
|
button! {
|
2022-12-29 10:09:26 +08:00
|
|
|
child = text!("Nested Focusables");
|
2022-06-22 05:58:53 +08:00
|
|
|
|
2023-02-27 03:59:36 +08:00
|
|
|
on_click = hn!(|args: &ClickArgs| {
|
2022-06-22 05:58:53 +08:00
|
|
|
args.propagation().stop();
|
2023-04-01 23:22:48 +08:00
|
|
|
WINDOWS.focus_or_open("nested-focusables", async {
|
2022-06-22 07:27:33 +08:00
|
|
|
window! {
|
|
|
|
title = "Focus Example - Nested Focusables";
|
2022-09-17 12:56:38 +08:00
|
|
|
|
2022-09-18 03:28:00 +08:00
|
|
|
color_scheme = ColorScheme::Dark;
|
2022-09-17 12:56:38 +08:00
|
|
|
background_color = colors::DIM_GRAY;
|
|
|
|
|
2022-07-03 09:39:44 +08:00
|
|
|
// zero_ui::properties::inspector::show_center_points = true;
|
2022-10-25 11:56:12 +08:00
|
|
|
child_align = Align::CENTER;
|
2022-12-28 11:57:28 +08:00
|
|
|
child = stack! {
|
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-06-22 07:27:33 +08:00
|
|
|
spacing = 10;
|
2022-12-29 04:37:14 +08:00
|
|
|
children = ui_vec![
|
2022-06-22 07:27:33 +08:00
|
|
|
nested_focusables_group('a'),
|
|
|
|
nested_focusables_group('b'),
|
|
|
|
];
|
2022-06-22 05:58:53 +08:00
|
|
|
}
|
2022-06-22 07:27:33 +08:00
|
|
|
}
|
|
|
|
});
|
2022-06-22 05:58:53 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 23:07:26 +08:00
|
|
|
fn nested_focusables_group(g: char) -> impl UiNode {
|
2022-12-28 11:57:28 +08:00
|
|
|
stack! {
|
|
|
|
direction = StackDirection::left_to_right();
|
2022-06-22 05:58:53 +08:00
|
|
|
align = Align::TOP;
|
|
|
|
spacing = 10;
|
2022-10-25 11:56:12 +08:00
|
|
|
children = (0..5).map(|n| nested_focusable(g, n, 0).boxed()).collect::<Vec<_>>()
|
2022-06-22 05:58:53 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 23:07:26 +08:00
|
|
|
fn nested_focusable(g: char, column: u8, row: u8) -> impl UiNode {
|
2022-06-22 05:58:53 +08:00
|
|
|
let nested = text! {
|
2022-11-09 02:52:15 +08:00
|
|
|
txt = format!("Focusable {column}, {row}");
|
2022-06-22 05:58:53 +08:00
|
|
|
margin = 5;
|
|
|
|
};
|
2022-12-28 11:57:28 +08:00
|
|
|
stack! {
|
2022-06-22 12:12:02 +08:00
|
|
|
id = formatx!("focusable-{g}-{column}-{row}");
|
2022-06-22 05:58:53 +08:00
|
|
|
padding = 2;
|
2022-12-28 11:57:28 +08:00
|
|
|
direction = StackDirection::top_to_bottom();
|
2022-10-25 11:56:12 +08:00
|
|
|
children = if row == 5 {
|
2022-12-29 04:37:14 +08:00
|
|
|
ui_vec![nested]
|
2022-06-22 05:58:53 +08:00
|
|
|
} else {
|
2022-12-29 04:37:14 +08:00
|
|
|
ui_vec![
|
2022-06-22 05:58:53 +08:00
|
|
|
nested,
|
|
|
|
nested_focusable(g, column, row + 1),
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
focusable = true;
|
|
|
|
|
|
|
|
corner_radius = 5;
|
2022-06-22 07:56:41 +08:00
|
|
|
border = 1, colors::RED.with_alpha(30.pct());
|
2022-06-22 05:58:53 +08:00
|
|
|
background_color = colors::RED.with_alpha(20.pct());
|
2022-10-21 23:07:26 +08:00
|
|
|
when *#is_focused {
|
2022-06-22 05:58:53 +08:00
|
|
|
background_color = colors::GREEN;
|
|
|
|
}
|
2022-10-21 23:07:26 +08:00
|
|
|
when *#is_return_focus {
|
2022-06-22 07:56:41 +08:00
|
|
|
border = 1, colors::LIME_GREEN;
|
|
|
|
}
|
2022-06-22 05:58:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 11:51:40 +08:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
mod inspect {
|
2020-08-28 03:10:33 +08:00
|
|
|
use super::*;
|
2020-08-27 11:51:40 +08:00
|
|
|
use zero_ui::core::focus::WidgetInfoFocusExt;
|
2022-10-26 06:02:57 +08:00
|
|
|
use zero_ui::core::inspector::WidgetInfoInspectorExt;
|
2020-08-27 11:51:40 +08:00
|
|
|
|
2023-02-21 04:47:00 +08:00
|
|
|
pub fn focus(path: &Option<InteractionPath>) -> String {
|
2020-08-27 11:51:40 +08:00
|
|
|
path.as_ref()
|
2022-10-26 06:02:57 +08:00
|
|
|
.map(|p| {
|
2023-02-22 20:49:13 +08:00
|
|
|
let frame = if let Ok(w) = WINDOWS.widget_tree(p.window_id()) {
|
2021-05-15 04:39:50 +08:00
|
|
|
w
|
|
|
|
} else {
|
2022-01-19 13:07:24 +08:00
|
|
|
return format!("<{p}>");
|
2021-05-15 04:39:50 +08:00
|
|
|
};
|
2022-07-09 05:59:32 +08:00
|
|
|
let widget = if let Some(w) = frame.get(p.widget_id()) {
|
2021-05-19 11:17:59 +08:00
|
|
|
w
|
|
|
|
} else {
|
2022-01-19 13:07:24 +08:00
|
|
|
return format!("<{p}>");
|
2021-05-19 11:17:59 +08:00
|
|
|
};
|
2022-10-29 10:22:58 +08:00
|
|
|
let wgt_mod = if let Some(b) = widget.inspector_info() {
|
|
|
|
b.builder.widget_mod()
|
2022-10-26 06:02:57 +08:00
|
|
|
} else {
|
|
|
|
return format!("<{p}>");
|
|
|
|
};
|
|
|
|
if wgt_mod.path.ends_with("button") {
|
|
|
|
let txt = widget
|
|
|
|
.inspect_descendant("text")
|
|
|
|
.expect("expected text in button")
|
2022-11-09 05:55:11 +08:00
|
|
|
.inspect_property("txt")
|
|
|
|
.expect("expected txt property in text")
|
2022-10-26 06:02:57 +08:00
|
|
|
.live_debug(0)
|
|
|
|
.get();
|
|
|
|
|
|
|
|
format!("button({txt})")
|
|
|
|
} else if wgt_mod.path.ends_with("window") {
|
|
|
|
let title = widget.inspect_property("title").map(|p| p.live_debug(0).get()).unwrap_or_default();
|
|
|
|
|
2022-01-19 13:07:24 +08:00
|
|
|
format!("window({title})")
|
2020-08-27 11:51:40 +08:00
|
|
|
} else {
|
2023-03-24 00:16:39 +08:00
|
|
|
let focus_info = widget.into_focus_info(true, true);
|
2020-08-27 11:51:40 +08:00
|
|
|
if focus_info.is_alt_scope() {
|
2022-10-26 06:02:57 +08:00
|
|
|
format!("{}(is_alt_scope)", wgt_mod.name())
|
2020-08-27 11:51:40 +08:00
|
|
|
} else if focus_info.is_scope() {
|
2022-10-26 06:02:57 +08:00
|
|
|
format!("{}(is_scope)", wgt_mod.name())
|
2020-08-27 11:51:40 +08:00
|
|
|
} else {
|
2022-10-26 06:02:57 +08:00
|
|
|
format!("{}({})", wgt_mod.name(), p.widget_id())
|
2020-08-28 03:10:33 +08:00
|
|
|
}
|
2020-08-27 11:51:40 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| "<none>".to_owned())
|
2022-10-25 23:00:03 +08:00
|
|
|
}
|
2020-08-27 11:51:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
mod inspect {
|
|
|
|
use super::*;
|
|
|
|
|
2022-11-25 07:11:33 +08:00
|
|
|
pub fn focus(_: &mut Services, path: &Option<InteractionPath>) -> String {
|
2020-08-28 03:10:33 +08:00
|
|
|
path.as_ref()
|
|
|
|
.map(|p| format!("{:?}", p.widget_id()))
|
|
|
|
.unwrap_or_else(|| "<none>".to_owned())
|
2020-08-27 11:51:40 +08:00
|
|
|
}
|
2020-08-28 03:10:33 +08:00
|
|
|
}
|