2020-08-07 00:07:32 +08:00
|
|
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
2021-12-15 23:55:03 +08:00
|
|
|
use zero_ui::core::focus::FocusChangedEvent;
|
2020-08-27 11:51:40 +08:00
|
|
|
use zero_ui::prelude::*;
|
2022-02-01 03:18:23 +08:00
|
|
|
use zero_ui::widgets::window::{LayerIndex, WindowLayers};
|
2020-07-22 12:16:15 +08:00
|
|
|
|
2021-11-07 12:15:13 +08:00
|
|
|
use zero_ui_view_prebuilt as zero_ui_view;
|
|
|
|
|
2020-07-22 12:16:15 +08:00
|
|
|
fn main() {
|
2021-11-08 04:09:25 +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
|
|
|
|
|
|
|
// zero_ui_view::run_same_process(app_main);
|
2021-10-12 09:27:20 +08:00
|
|
|
app_main();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn app_main() {
|
2021-05-15 04:39:50 +08:00
|
|
|
App::default().run_window(|ctx| {
|
|
|
|
trace_focus(ctx.events);
|
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();
|
2021-03-12 03:40:31 +08:00
|
|
|
content_align = unset!;
|
|
|
|
content = v_stack! {
|
2021-03-31 06:49:36 +08:00
|
|
|
items = widgets![
|
2020-09-10 02:44:15 +08:00
|
|
|
alt_scope(),
|
2021-05-14 12:12:14 +08:00
|
|
|
h_stack! {
|
|
|
|
margin = (50, 0, 0, 0);
|
|
|
|
align = Alignment::CENTER;
|
|
|
|
spacing = 10;
|
|
|
|
items = widgets![
|
2021-05-17 02:24:30 +08:00
|
|
|
tab_index(),
|
2022-01-30 00:07:50 +08:00
|
|
|
functions(window_enabled)
|
2021-05-14 12:12:14 +08:00
|
|
|
]
|
|
|
|
}
|
2021-03-31 06:49:36 +08:00
|
|
|
];
|
2020-07-22 12:16:15 +08:00
|
|
|
};
|
2021-11-30 10:46:56 +08:00
|
|
|
// zero_ui::widgets::inspector::show_center_points = true;
|
|
|
|
// zero_ui::widgets::inspector::show_bounds = true;
|
2020-07-22 12:16:15 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-10 02:44:15 +08:00
|
|
|
fn alt_scope() -> impl Widget {
|
|
|
|
h_stack! {
|
2021-03-12 03:40:31 +08:00
|
|
|
alt_focus_scope = true;
|
|
|
|
spacing = 5;
|
|
|
|
margin = 5;
|
2021-03-31 06:49:36 +08:00
|
|
|
items = widgets![
|
2020-09-10 02:44:15 +08:00
|
|
|
button("alt", TabIndex::AUTO),
|
|
|
|
button("scope", TabIndex::AUTO),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 02:24:30 +08:00
|
|
|
fn tab_index() -> impl Widget {
|
2020-09-10 02:44:15 +08:00
|
|
|
v_stack! {
|
2021-03-12 03:40:31 +08:00
|
|
|
spacing = 5;
|
2021-05-18 02:14:52 +08:00
|
|
|
focus_shortcut = shortcut!(T);
|
2021-03-31 06:49:36 +08:00
|
|
|
items = widgets![
|
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-01-30 00:07:50 +08:00
|
|
|
fn functions(window_enabled: RcVar<bool>) -> impl Widget {
|
2021-05-14 12:12:14 +08:00
|
|
|
v_stack! {
|
|
|
|
spacing = 5;
|
2021-05-17 02:24:30 +08:00
|
|
|
focus_shortcut = shortcut!(F);
|
2021-05-14 12:12:14 +08:00
|
|
|
items = widgets![
|
|
|
|
title("Functions (F)"),
|
2022-01-30 00:07:50 +08:00
|
|
|
// New Window
|
2021-05-14 12:12:14 +08:00
|
|
|
button! {
|
|
|
|
content = text("New Window");
|
2021-06-22 10:00:40 +08:00
|
|
|
on_click = hn!(|ctx, _| {
|
2021-06-12 08:00:08 +08:00
|
|
|
ctx.services.windows().open(|_|window! {
|
2021-05-14 12:12:14 +08:00
|
|
|
title = "Other Window";
|
|
|
|
focus_shortcut = shortcut!(W);
|
|
|
|
content = v_stack! {
|
|
|
|
spacing = 5;
|
|
|
|
items = widgets![
|
|
|
|
title("Other Window (W)"),
|
2021-05-20 03:35:57 +08:00
|
|
|
button("Button B5", 5),
|
|
|
|
button("Button B4", 3),
|
|
|
|
button("Button B3", 2),
|
|
|
|
button("Button B1", 0),
|
|
|
|
button("Button B2", 0),
|
2021-05-14 12:12:14 +08:00
|
|
|
]
|
|
|
|
};
|
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
|
|
|
{
|
2021-05-16 02:31:55 +08:00
|
|
|
let detach_focused = RcNode::new_cyclic(|wk| {
|
2021-05-21 00:01:07 +08:00
|
|
|
let btn = button! {
|
2021-05-16 02:31:55 +08:00
|
|
|
content = text("Detach Button");
|
2021-06-22 10:00:40 +08:00
|
|
|
on_click = hn!(|ctx, _| {
|
2021-05-16 02:31:55 +08:00
|
|
|
let wwk = wk.clone();
|
2021-06-12 08:00:08 +08:00
|
|
|
ctx.services.windows().open(move |_| {
|
2021-05-16 02:31:55 +08:00
|
|
|
window! {
|
|
|
|
title = "Detached Button";
|
2021-05-20 03:35:57 +08:00
|
|
|
content = slot(wwk.upgrade().unwrap(), take_on_init());
|
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
|
|
|
});
|
|
|
|
slot(detach_focused, take_on_init())
|
2022-01-30 00:07:50 +08:00
|
|
|
},
|
|
|
|
// Disable Scope
|
2022-02-01 03:18:23 +08:00
|
|
|
button! {
|
2022-01-30 00:07:50 +08:00
|
|
|
content = text(window_enabled.map(|&e| if e { "Disable Scope" } else { "Enabling in 1s..." }.into()));
|
|
|
|
width = 140;
|
|
|
|
on_click = async_hn!(window_enabled, |ctx, _| {
|
|
|
|
window_enabled.set(&ctx, false);
|
|
|
|
task::timeout(1.secs()).await;
|
|
|
|
window_enabled.set(&ctx, true);
|
|
|
|
});
|
|
|
|
},
|
2022-02-01 12:18:58 +08:00
|
|
|
// Overlay Scope
|
2022-01-30 00:07:50 +08:00
|
|
|
button! {
|
2022-01-30 12:37:07 +08:00
|
|
|
content = text("Overlay Scope");
|
2022-01-30 00:07:50 +08:00
|
|
|
on_click = hn!(|ctx, _| {
|
2022-02-01 12:18:58 +08:00
|
|
|
WindowLayers::insert(ctx, LayerIndex::TOP_MOST, overlay());
|
2022-01-30 00:07:50 +08:00
|
|
|
});
|
2021-05-14 12:12:14 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2022-01-30 12:37:07 +08:00
|
|
|
fn overlay() -> impl Widget {
|
2022-01-30 00:07:50 +08:00
|
|
|
container! {
|
|
|
|
id = "overlay";
|
2022-02-01 10:31:20 +08:00
|
|
|
modal = true;
|
2022-01-30 00:07:50 +08:00
|
|
|
content = container! {
|
|
|
|
focus_scope = true;
|
|
|
|
background_color = colors::GRAY.darken(50.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;
|
|
|
|
content = v_stack! {
|
|
|
|
items_align = Alignment::RIGHT;
|
|
|
|
items = widgets![
|
|
|
|
text! {
|
|
|
|
text = "Window scope is disabled so the overlay scope is the root scope.";
|
|
|
|
margin = 15;
|
|
|
|
},
|
|
|
|
button! {
|
|
|
|
content = text("Ok");
|
|
|
|
on_click = hn!(|ctx, _| {
|
|
|
|
WindowLayers::remove(ctx, "overlay");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-14 12:12:14 +08:00
|
|
|
|
|
|
|
fn title(text: impl IntoVar<Text>) -> impl Widget {
|
|
|
|
text! { text; font_weight = FontWeight::BOLD; align = Alignment::CENTER; }
|
|
|
|
}
|
|
|
|
|
2021-05-20 03:35:57 +08:00
|
|
|
fn button(content: impl Into<Text>, tab_index: impl Into<TabIndex>) -> impl Widget {
|
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! {
|
2021-03-12 03:40:31 +08:00
|
|
|
content = text(content.clone());
|
2020-07-22 12:16:15 +08:00
|
|
|
tab_index;
|
2021-06-22 10:00:40 +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
|
|
|
|
2021-06-04 05:07:54 +08:00
|
|
|
fn trace_focus(events: &mut Events) {
|
2021-06-08 02:40:32 +08:00
|
|
|
events
|
2021-06-25 07:20:10 +08:00
|
|
|
.on_pre_event(
|
|
|
|
FocusChangedEvent,
|
|
|
|
app_hn!(|ctx, args: &FocusChangedArgs, _| {
|
|
|
|
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 {
|
|
|
|
println!(
|
|
|
|
"{} -> {}",
|
|
|
|
inspect::focus(&args.prev_focus, ctx.services),
|
|
|
|
inspect::focus(&args.new_focus, ctx.services)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
2021-06-23 09:05:10 +08:00
|
|
|
.permanent();
|
2021-05-15 04:39:50 +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;
|
2021-12-20 07:35:13 +08:00
|
|
|
use zero_ui::core::inspector::{WidgetInspectorInfo, WidgetNewFn};
|
2020-08-27 11:51:40 +08:00
|
|
|
|
2021-05-15 04:39:50 +08:00
|
|
|
pub fn focus(path: &Option<WidgetPath>, services: &mut Services) -> String {
|
2020-08-27 11:51:40 +08:00
|
|
|
path.as_ref()
|
|
|
|
.map(|p| {
|
2021-11-28 02:24:22 +08:00
|
|
|
let frame = if let Ok(w) = services.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
|
|
|
};
|
2021-05-19 11:17:59 +08:00
|
|
|
let widget = if let Some(w) = frame.get(p) {
|
|
|
|
w
|
|
|
|
} else {
|
2022-01-19 13:07:24 +08:00
|
|
|
return format!("<{p}>");
|
2021-05-19 11:17:59 +08:00
|
|
|
};
|
2020-08-27 11:51:40 +08:00
|
|
|
let info = widget.instance().expect("expected debug info").borrow();
|
2020-08-28 03:10:33 +08:00
|
|
|
|
2020-08-27 11:51:40 +08:00
|
|
|
if info.widget_name == "button" {
|
|
|
|
let text_wgt = widget.descendants().next().expect("expected text in button");
|
|
|
|
let info = text_wgt.instance().expect("expected debug info").borrow();
|
2020-08-28 03:10:33 +08:00
|
|
|
format!(
|
|
|
|
"button({})",
|
2021-07-10 03:00:41 +08:00
|
|
|
info.captures
|
|
|
|
.get(&WidgetNewFn::NewChild)
|
|
|
|
.unwrap()
|
2020-08-28 03:10:33 +08:00
|
|
|
.iter()
|
|
|
|
.find(|p| p.property_name == "text")
|
|
|
|
.expect("expected text in capture_new")
|
|
|
|
.args[0]
|
|
|
|
.value
|
2021-05-22 06:03:06 +08:00
|
|
|
.debug
|
2020-08-27 11:51:40 +08:00
|
|
|
)
|
2021-05-15 04:39:50 +08:00
|
|
|
} else if info.widget_name == "window" {
|
|
|
|
let title = widget
|
|
|
|
.properties()
|
|
|
|
.iter()
|
|
|
|
.find(|p| p.borrow().property_name == "title")
|
2021-05-22 06:03:06 +08:00
|
|
|
.map(|p| p.borrow().args[0].value.debug.clone())
|
2021-05-15 04:39:50 +08:00
|
|
|
.unwrap_or_default();
|
2022-01-19 13:07:24 +08:00
|
|
|
format!("window({title})")
|
2020-08-27 11:51:40 +08:00
|
|
|
} else {
|
|
|
|
let focus_info = widget.as_focus_info();
|
|
|
|
if focus_info.is_alt_scope() {
|
|
|
|
format!("{}(is_alt_scope)", info.widget_name)
|
|
|
|
} else if focus_info.is_scope() {
|
|
|
|
format!("{}(is_scope)", info.widget_name)
|
|
|
|
} else {
|
|
|
|
info.widget_name.to_owned()
|
2020-08-28 03:10:33 +08:00
|
|
|
}
|
2020-08-27 11:51:40 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| "<none>".to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
mod inspect {
|
|
|
|
use super::*;
|
|
|
|
|
2021-05-21 00:01:07 +08:00
|
|
|
pub fn focus(path: &Option<WidgetPath>, _: &mut Services) -> 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
|
|
|
}
|