Skip to content

Commit

Permalink
Add with_cursor_moved unix extension method (#644)
Browse files Browse the repository at this point in the history
* Add `with_cursor_moved` unix extension method

* Default should be true

* Fix typo

Co-authored-by: Wu Yu Wei <wusyong9104@gmail.com>
  • Loading branch information
wusyong and Wu Yu Wei committed Dec 14, 2022
1 parent d8b6f5e commit 8c6b2d0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changes/surcor-moved.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Add `with_cursor_moved` Unix extension trait method.
2 changes: 1 addition & 1 deletion examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() {
}
Event::MainEventsCleared => {
if let Some(w) = &window {
w.request_redraw();
// w.request_redraw();
}
}
_ => (),
Expand Down
12 changes: 12 additions & 0 deletions src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ pub trait WindowBuilderExtUnix {
///
/// Default is `false` but is always `true` if [`WindowAttributes::transparent`](crate::window::WindowAttributes::transparent) is `true`
fn with_app_paintable(self, app_paintable: bool) -> WindowBuilder;

/// Whether to set cursor moved event. Cursor event is suited for native GUI frameworks and
/// games. But it can block gtk's own pipeline occasionally. Turn this off can help Gtk looks
/// smoother.
///
/// Default is `true`.
fn with_cursor_moved_event(self, cursor_moved: bool) -> WindowBuilder;
}

impl WindowBuilderExtUnix for WindowBuilder {
Expand Down Expand Up @@ -106,6 +113,11 @@ impl WindowBuilderExtUnix for WindowBuilder {
self.platform_specific.app_paintable = app_paintable;
self
}

fn with_cursor_moved_event(mut self, cursor_moved: bool) -> WindowBuilder {
self.platform_specific.cursor_moved = cursor_moved;
self
}
}

/// Additional methods on `EventLoop` that are specific to Unix.
Expand Down
33 changes: 19 additions & 14 deletions src/platform_impl/linux/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,10 @@ impl<T: 'static> EventLoop<T> {
window.input_shape_combine_region(None)
};
}
WindowRequest::WireUpEvents { transparent } => {
WindowRequest::WireUpEvents {
transparent,
cursor_moved,
} => {
window.add_events(
EventMask::POINTER_MOTION_MASK
| EventMask::BUTTON1_MOTION_MASK
Expand Down Expand Up @@ -534,19 +537,21 @@ impl<T: 'static> EventLoop<T> {

let tx_clone = event_tx.clone();
window.connect_motion_notify_event(move |window, motion| {
if let Some(cursor) = motion.device() {
let scale_factor = window.scale_factor();
let (_, x, y) = cursor.window_at_position();
if let Err(e) = tx_clone.send(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
position: LogicalPosition::new(x, y).to_physical(scale_factor as f64),
device_id: DEVICE_ID,
// this field is depracted so it is fine to pass empty state
modifiers: ModifiersState::empty(),
},
}) {
log::warn!("Failed to send cursor moved event to event channel: {}", e);
if cursor_moved {
if let Some(cursor) = motion.device() {
let scale_factor = window.scale_factor();
let (_, x, y) = cursor.window_at_position();
if let Err(e) = tx_clone.send(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
position: LogicalPosition::new(x, y).to_physical(scale_factor as f64),
device_id: DEVICE_ID,
// this field is depracted so it is fine to pass empty state
modifiers: ModifiersState::empty(),
},
}) {
log::warn!("Failed to send cursor moved event to event channel: {}", e);
}
}
}
Inhibit(false)
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub double_buffered: bool,
pub app_paintable: bool,
pub rgba_visual: bool,
pub cursor_moved: bool,
}

impl Default for PlatformSpecificWindowBuilderAttributes {
Expand All @@ -77,6 +78,7 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
double_buffered: true,
app_paintable: false,
rgba_visual: false,
cursor_moved: true,
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/platform_impl/linux/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,14 @@ impl Window {
if attributes.transparent && pl_attribs.auto_transparent {
transparent = true;
}
if let Err(e) =
window_requests_tx.send((window_id, WindowRequest::WireUpEvents { transparent }))
{
let cursor_moved = pl_attribs.cursor_moved;
if let Err(e) = window_requests_tx.send((
window_id,
WindowRequest::WireUpEvents {
transparent,
cursor_moved,
},
)) {
log::warn!("Fail to send wire up events request: {}", e);
}

Expand Down Expand Up @@ -832,7 +837,10 @@ pub enum WindowRequest {
CursorIcon(Option<CursorIcon>),
CursorPosition((i32, i32)),
CursorIgnoreEvents(bool),
WireUpEvents { transparent: bool },
WireUpEvents {
transparent: bool,
cursor_moved: bool,
},
Menu((Option<MenuItem>, Option<MenuId>)),
SetMenu((Option<menu::Menu>, AccelGroup, gtk::MenuBar)),
GlobalHotKey(u16),
Expand Down

0 comments on commit 8c6b2d0

Please sign in to comment.