2023-05-04 16:39:46 +00:00
|
|
|
use dioxus::html::geometry::euclid::Vector3D;
|
2023-04-27 23:00:43 +00:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
use dioxus_desktop::DesktopContext;
|
|
|
|
|
2023-05-04 16:04:06 +00:00
|
|
|
pub(crate) fn check_app_exits(app: Component) {
|
2023-05-05 16:11:50 +00:00
|
|
|
use dioxus_desktop::tao::window::WindowBuilder;
|
|
|
|
use dioxus_desktop::Config;
|
2023-05-04 16:04:06 +00:00
|
|
|
// This is a deadman's switch to ensure that the app exits
|
|
|
|
let should_panic = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
|
|
|
|
let should_panic_clone = should_panic.clone();
|
|
|
|
std::thread::spawn(move || {
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(100));
|
|
|
|
if should_panic_clone.load(std::sync::atomic::Ordering::SeqCst) {
|
|
|
|
std::process::exit(exitcode::SOFTWARE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-05-05 16:11:50 +00:00
|
|
|
dioxus_desktop::launch_cfg(
|
|
|
|
app,
|
2023-12-17 17:30:17 +00:00
|
|
|
Config::new().with_window(WindowBuilder::new().with_visible(true)),
|
2023-05-05 16:11:50 +00:00
|
|
|
);
|
2023-05-04 16:04:06 +00:00
|
|
|
|
2023-07-05 16:52:54 +00:00
|
|
|
// Stop deadman's switch
|
2023-05-04 16:04:06 +00:00
|
|
|
should_panic.store(false, std::sync::atomic::Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2023-04-27 23:00:43 +00:00
|
|
|
check_app_exits(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mock_event(cx: &ScopeState, id: &'static str, value: &'static str) {
|
2023-07-21 22:36:25 +00:00
|
|
|
let eval_provider = use_eval(cx).clone();
|
|
|
|
|
|
|
|
use_effect(cx, (), move |_| async move {
|
|
|
|
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
|
|
|
let js = format!(
|
|
|
|
r#"
|
|
|
|
//console.log("ran");
|
2023-04-27 23:00:43 +00:00
|
|
|
// Dispatch a synthetic event
|
2023-07-21 22:36:25 +00:00
|
|
|
let event = {};
|
|
|
|
let element = document.getElementById('{}');
|
2023-04-27 23:36:28 +00:00
|
|
|
console.log(element, event);
|
2023-04-27 23:00:43 +00:00
|
|
|
element.dispatchEvent(event);
|
|
|
|
"#,
|
2023-07-21 22:36:25 +00:00
|
|
|
value, id
|
|
|
|
);
|
|
|
|
|
|
|
|
eval_provider(&js).unwrap();
|
|
|
|
})
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
fn app(cx: Scope) -> Element {
|
|
|
|
let desktop_context: DesktopContext = cx.consume_context().unwrap();
|
2023-12-08 14:44:04 +00:00
|
|
|
let received_events = use_state(cx, || 0);
|
2023-04-27 23:00:43 +00:00
|
|
|
|
|
|
|
// button
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"button",
|
|
|
|
r#"new MouseEvent("click", {
|
2023-07-21 22:36:25 +00:00
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
button: 0,
|
|
|
|
})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// mouse_move_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"mouse_move_div",
|
|
|
|
r#"new MouseEvent("mousemove", {
|
2023-07-21 22:36:25 +00:00
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
buttons: 2,
|
|
|
|
})"#,
|
2023-05-04 16:39:46 +00:00
|
|
|
);
|
2023-04-27 23:00:43 +00:00
|
|
|
// mouse_click_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"mouse_click_div",
|
|
|
|
r#"new MouseEvent("click", {
|
2023-07-21 22:36:25 +00:00
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
buttons: 2,
|
|
|
|
button: 2,
|
|
|
|
})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// mouse_dblclick_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"mouse_dblclick_div",
|
|
|
|
r#"new MouseEvent("dblclick", {
|
2023-07-21 22:36:25 +00:00
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
buttons: 1|2,
|
|
|
|
button: 2,
|
|
|
|
})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// mouse_down_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"mouse_down_div",
|
|
|
|
r#"new MouseEvent("mousedown", {
|
2023-07-21 22:36:25 +00:00
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
buttons: 2,
|
|
|
|
button: 2,
|
|
|
|
})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// mouse_up_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"mouse_up_div",
|
|
|
|
r#"new MouseEvent("mouseup", {
|
2023-07-21 22:36:25 +00:00
|
|
|
view: window,
|
|
|
|
bubbles: true,
|
|
|
|
cancelable: true,
|
|
|
|
buttons: 0,
|
|
|
|
button: 0,
|
|
|
|
})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// wheel_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"wheel_div",
|
|
|
|
r#"new WheelEvent("wheel", {
|
2023-07-21 22:36:25 +00:00
|
|
|
view: window,
|
|
|
|
deltaX: 1.0,
|
|
|
|
deltaY: 2.0,
|
|
|
|
deltaZ: 3.0,
|
|
|
|
deltaMode: 0x00,
|
|
|
|
bubbles: true,
|
|
|
|
})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// key_down_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"key_down_div",
|
|
|
|
r#"new KeyboardEvent("keydown", {
|
2023-07-21 22:36:25 +00:00
|
|
|
key: "a",
|
|
|
|
code: "KeyA",
|
|
|
|
location: 0,
|
|
|
|
repeat: true,
|
|
|
|
keyCode: 65,
|
|
|
|
charCode: 97,
|
|
|
|
char: "a",
|
|
|
|
charCode: 0,
|
|
|
|
altKey: false,
|
|
|
|
ctrlKey: false,
|
|
|
|
metaKey: false,
|
|
|
|
shiftKey: false,
|
|
|
|
isComposing: false,
|
|
|
|
which: 65,
|
|
|
|
bubbles: true,
|
|
|
|
})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// key_up_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"key_up_div",
|
|
|
|
r#"new KeyboardEvent("keyup", {
|
2023-07-21 22:36:25 +00:00
|
|
|
key: "a",
|
|
|
|
code: "KeyA",
|
|
|
|
location: 0,
|
|
|
|
repeat: false,
|
|
|
|
keyCode: 65,
|
|
|
|
charCode: 97,
|
|
|
|
char: "a",
|
|
|
|
charCode: 0,
|
|
|
|
altKey: false,
|
|
|
|
ctrlKey: false,
|
|
|
|
metaKey: false,
|
|
|
|
shiftKey: false,
|
|
|
|
isComposing: false,
|
|
|
|
which: 65,
|
|
|
|
bubbles: true,
|
|
|
|
})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// key_press_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"key_press_div",
|
|
|
|
r#"new KeyboardEvent("keypress", {
|
2023-07-21 22:36:25 +00:00
|
|
|
key: "a",
|
|
|
|
code: "KeyA",
|
|
|
|
location: 0,
|
|
|
|
repeat: false,
|
|
|
|
keyCode: 65,
|
|
|
|
charCode: 97,
|
|
|
|
char: "a",
|
|
|
|
charCode: 0,
|
|
|
|
altKey: false,
|
|
|
|
ctrlKey: false,
|
|
|
|
metaKey: false,
|
|
|
|
shiftKey: false,
|
|
|
|
isComposing: false,
|
|
|
|
which: 65,
|
|
|
|
bubbles: true,
|
|
|
|
})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// focus_in_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"focus_in_div",
|
2023-04-27 23:36:28 +00:00
|
|
|
r#"new FocusEvent("focusin", {bubbles: true})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
// focus_out_div
|
|
|
|
mock_event(
|
2023-05-05 20:45:38 +00:00
|
|
|
cx,
|
2023-04-27 23:00:43 +00:00
|
|
|
"focus_out_div",
|
2023-04-27 23:36:28 +00:00
|
|
|
r#"new FocusEvent("focusout",{bubbles: true})"#,
|
2023-04-27 23:00:43 +00:00
|
|
|
);
|
|
|
|
|
2023-12-08 14:44:04 +00:00
|
|
|
if **received_events == 12 {
|
2023-05-04 16:04:06 +00:00
|
|
|
println!("all events recieved");
|
2023-04-27 23:00:43 +00:00
|
|
|
desktop_context.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
button {
|
|
|
|
id: "button",
|
|
|
|
onclick: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
|
|
|
assert!(event.data.modifiers().is_empty());
|
|
|
|
assert!(event.data.held_buttons().is_empty());
|
|
|
|
assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Primary));
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-04-27 23:00:43 +00:00
|
|
|
},
|
2024-01-03 20:02:08 +00:00
|
|
|
assert_eq!(
|
|
|
|
event.data.trigger_button(),
|
|
|
|
Some(dioxus_html::input_data::MouseButton::Primary),
|
|
|
|
);
|
2023-04-27 23:00:43 +00:00
|
|
|
recieved_events.modify(|x| *x + 1)
|
2024-01-03 20:02:08 +00:00
|
|
|
}
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
div {
|
|
|
|
id: "mouse_move_div",
|
|
|
|
onmousemove: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
|
|
|
assert!(event.data.modifiers().is_empty());
|
|
|
|
assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2024-01-03 20:02:08 +00:00
|
|
|
}
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
div {
|
|
|
|
id: "mouse_click_div",
|
|
|
|
onclick: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
|
|
|
assert!(event.data.modifiers().is_empty());
|
|
|
|
assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
|
|
|
|
assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Secondary));
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2024-01-03 20:02:08 +00:00
|
|
|
}
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
div {
|
2023-04-27 23:00:43 +00:00
|
|
|
id: "mouse_dblclick_div",
|
2024-01-03 20:02:08 +00:00
|
|
|
ondoubleclick: move |event| {
|
2023-04-27 23:00:43 +00:00
|
|
|
println!("{:?}", event.data);
|
|
|
|
assert!(event.data.modifiers().is_empty());
|
|
|
|
assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Primary));
|
|
|
|
assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
|
|
|
|
assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Secondary));
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
div {
|
2023-04-27 23:00:43 +00:00
|
|
|
id: "mouse_down_div",
|
|
|
|
onmousedown: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
|
|
|
assert!(event.data.modifiers().is_empty());
|
|
|
|
assert!(event.data.held_buttons().contains(dioxus_html::input_data::MouseButton::Secondary));
|
|
|
|
assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Secondary));
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
div {
|
2023-04-27 23:00:43 +00:00
|
|
|
id: "mouse_up_div",
|
|
|
|
onmouseup: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
|
|
|
assert!(event.data.modifiers().is_empty());
|
|
|
|
assert!(event.data.held_buttons().is_empty());
|
|
|
|
assert_eq!(event.data.trigger_button(), Some(dioxus_html::input_data::MouseButton::Primary));
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
div {
|
2023-04-27 23:00:43 +00:00
|
|
|
id: "wheel_div",
|
2023-04-27 23:36:28 +00:00
|
|
|
width: "100px",
|
|
|
|
height: "100px",
|
|
|
|
background_color: "red",
|
2023-04-27 23:00:43 +00:00
|
|
|
onwheel: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
2024-01-03 20:02:08 +00:00
|
|
|
let dioxus_html::geometry::WheelDelta::Pixels(delta) = event.data.delta() else {
|
|
|
|
panic!("Expected delta to be in pixels") };
|
2023-04-27 23:00:43 +00:00
|
|
|
assert_eq!(delta, Vector3D::new(1.0, 2.0, 3.0));
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
input {
|
2023-04-27 23:00:43 +00:00
|
|
|
id: "key_down_div",
|
|
|
|
onkeydown: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
|
|
|
assert!(event.data.modifiers().is_empty());
|
|
|
|
assert_eq!(event.data.key().to_string(), "a");
|
|
|
|
assert_eq!(event.data.code().to_string(), "KeyA");
|
|
|
|
assert_eq!(event.data.location, 0);
|
|
|
|
assert!(event.data.is_auto_repeating());
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-05-04 16:39:46 +00:00
|
|
|
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
input {
|
2023-04-27 23:00:43 +00:00
|
|
|
id: "key_up_div",
|
|
|
|
onkeyup: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
|
|
|
assert!(event.data.modifiers().is_empty());
|
|
|
|
assert_eq!(event.data.key().to_string(), "a");
|
|
|
|
assert_eq!(event.data.code().to_string(), "KeyA");
|
|
|
|
assert_eq!(event.data.location, 0);
|
|
|
|
assert!(!event.data.is_auto_repeating());
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
input {
|
2023-04-27 23:00:43 +00:00
|
|
|
id: "key_press_div",
|
|
|
|
onkeypress: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
|
|
|
assert!(event.data.modifiers().is_empty());
|
|
|
|
assert_eq!(event.data.key().to_string(), "a");
|
|
|
|
assert_eq!(event.data.code().to_string(), "KeyA");
|
|
|
|
assert_eq!(event.data.location, 0);
|
|
|
|
assert!(!event.data.is_auto_repeating());
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
input {
|
2023-04-27 23:00:43 +00:00
|
|
|
id: "focus_in_div",
|
|
|
|
onfocusin: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 20:02:08 +00:00
|
|
|
input {
|
2023-04-27 23:00:43 +00:00
|
|
|
id: "focus_out_div",
|
|
|
|
onfocusout: move |event| {
|
|
|
|
println!("{:?}", event.data);
|
2023-12-08 14:44:04 +00:00
|
|
|
received_events.modify(|x| *x + 1)
|
2023-04-27 23:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|