Skip to content

Commit

Permalink
Ignore nested events in GTK shell (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
luleyleo authored Apr 14, 2020
1 parent b7619d6 commit 482ea62
Showing 1 changed file with 88 additions and 68 deletions.
156 changes: 88 additions & 68 deletions druid-shell/src/platform/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ impl WindowBuilder {
if anim {
widget.queue_draw();
}
} else {
log::warn!("Drawing was skipped because the handler was already borrowed");
}

}
Expand All @@ -279,31 +281,37 @@ impl WindowBuilder {

drawing_area.connect_button_press_event(clone!(handle => move |_widget, button| {
if let Some(state) = handle.state.upgrade() {

state.handler.borrow_mut().mouse_down(
&MouseEvent {
pos: Point::from(button.get_position()),
count: get_mouse_click_count(button.get_event_type()),
mods: get_modifiers(button.get_state()),
button: get_mouse_button(button.get_button()),
},
);
if let Ok(mut handler) = state.handler.try_borrow_mut() {
handler.mouse_down(
&MouseEvent {
pos: Point::from(button.get_position()),
count: get_mouse_click_count(button.get_event_type()),
mods: get_modifiers(button.get_state()),
button: get_mouse_button(button.get_button()),
},
);
} else {
log::info!("GTK event was dropped because the handler was already borrowed");
}
}

Inhibit(true)
}));

drawing_area.connect_button_release_event(clone!(handle => move |_widget, button| {
if let Some(state) = handle.state.upgrade() {

state.handler.borrow_mut().mouse_up(
&MouseEvent {
pos: Point::from(button.get_position()),
mods: get_modifiers(button.get_state()),
count: 0,
button: get_mouse_button(button.get_button()),
},
);
if let Ok(mut handler) = state.handler.try_borrow_mut() {
handler.mouse_up(
&MouseEvent {
pos: Point::from(button.get_position()),
mods: get_modifiers(button.get_state()),
count: 0,
button: get_mouse_button(button.get_button()),
},
);
} else {
log::info!("GTK event was dropped because the handler was already borrowed");
}
}

Inhibit(true)
Expand All @@ -320,10 +328,11 @@ impl WindowBuilder {
button: get_mouse_button_from_modifiers(motion.get_state()),
};

state
.handler
.borrow_mut()
.mouse_move(&mouse_event);
if let Ok(mut handler) = state.handler.try_borrow_mut() {
handler.mouse_move(&mouse_event);
} else {
log::info!("GTK event was dropped because the handler was already borrowed");
}
}

Inhibit(true)
Expand All @@ -340,61 +349,65 @@ impl WindowBuilder {
button: get_mouse_button_from_modifiers(crossing.get_state()),
};

state
.handler
.borrow_mut()
.mouse_move(&mouse_event);
if let Ok(mut handler) = state.handler.try_borrow_mut() {
handler.mouse_move(&mouse_event);
} else {
log::info!("GTK event was dropped because the handler was already borrowed");
}
}

Inhibit(true)
}));

drawing_area.connect_scroll_event(clone!(handle => move |_widget, scroll| {
if let Some(state) = handle.state.upgrade() {

let modifiers = get_modifiers(scroll.get_state());

// The magic "120"s are from Microsoft's documentation for WM_MOUSEWHEEL.
// They claim that one "tick" on a scroll wheel should be 120 units.
let mut handler = state.handler.borrow_mut();
match scroll.get_direction() {
ScrollDirection::Up => {
if modifiers.shift {
if let Ok(mut handler) = state.handler.try_borrow_mut() {

let modifiers = get_modifiers(scroll.get_state());

// The magic "120"s are from Microsoft's documentation for WM_MOUSEWHEEL.
// They claim that one "tick" on a scroll wheel should be 120 units.
match scroll.get_direction() {
ScrollDirection::Up => {
if modifiers.shift {
handler.wheel(Vec2::from((-120.0, 0.0)), modifiers);
} else {
handler.wheel(Vec2::from((0.0, -120.0)), modifiers);
}
}
ScrollDirection::Down => {
if modifiers.shift {
handler.wheel(Vec2::from((120.0, 0.0)), modifiers);
} else {
handler.wheel(Vec2::from((0.0, 120.0)), modifiers);
}
}
ScrollDirection::Left => {
handler.wheel(Vec2::from((-120.0, 0.0)), modifiers);
} else {
handler.wheel(Vec2::from((0.0, -120.0)), modifiers);
}
}
ScrollDirection::Down => {
if modifiers.shift {
ScrollDirection::Right => {
handler.wheel(Vec2::from((120.0, 0.0)), modifiers);
} else {
handler.wheel(Vec2::from((0.0, 120.0)), modifiers);
}
}
ScrollDirection::Left => {
handler.wheel(Vec2::from((-120.0, 0.0)), modifiers);
}
ScrollDirection::Right => {
handler.wheel(Vec2::from((120.0, 0.0)), modifiers);
}
ScrollDirection::Smooth => {
//TODO: Look at how gtk's scroll containers implements it
let (mut delta_x, mut delta_y) = scroll.get_delta();
delta_x *= 120.;
delta_y *= 120.;
if modifiers.shift {
delta_x += delta_y;
delta_y = 0.;
ScrollDirection::Smooth => {
//TODO: Look at how gtk's scroll containers implements it
let (mut delta_x, mut delta_y) = scroll.get_delta();
delta_x *= 120.;
delta_y *= 120.;
if modifiers.shift {
delta_x += delta_y;
delta_y = 0.;
}
handler.wheel(Vec2::from((delta_x, delta_y)), modifiers)
}
e => {
eprintln!(
"Warning: the Druid widget got some whacky scroll direction {:?}",
e
);
}
handler.wheel(Vec2::from((delta_x, delta_y)), modifiers)
}
e => {
eprintln!(
"Warning: the Druid widget got some whacky scroll direction {:?}",
e
);
}
} else {
log::info!("GTK event was dropped because the handler was already borrowed");
}
}

Expand All @@ -409,8 +422,11 @@ impl WindowBuilder {

*current_keyval = Some(key.get_keyval());

let key_event = make_key_event(key, repeat);
state.handler.borrow_mut().key_down(key_event);
if let Ok(mut handler) = state.handler.try_borrow_mut() {
handler.key_down(make_key_event(key, repeat));
} else {
log::info!("GTK event was dropped because the handler was already borrowed");
}
}

Inhibit(true)
Expand All @@ -421,8 +437,11 @@ impl WindowBuilder {

*(state.current_keyval.borrow_mut()) = None;

let key_event = make_key_event(key, false);
state.handler.borrow_mut().key_up(key_event);
if let Ok(mut handler) = state.handler.try_borrow_mut() {
handler.key_up(make_key_event(key, false));
} else {
log::info!("GTK event was dropped because the handler was already borrowed");
}
}

Inhibit(true)
Expand Down Expand Up @@ -616,6 +635,7 @@ impl WindowHandle {
window.add_accel_group(&accel_group);

let menu = menu.into_gtk_menu(&self, &accel_group);
menu.set_property_attach_widget(Some(window));
menu.show_all();
menu.popup_easy(3, gtk::get_current_event_time());
}
Expand Down

0 comments on commit 482ea62

Please sign in to comment.