From 72747610695feef8867a8aac48935274072bd14b Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Fri, 21 Aug 2020 16:23:23 +0200 Subject: [PATCH] cursor: leave mode if target view is destroyed --- river/Cursor.zig | 32 ++++++++++++++++++++++++++------ river/InputManager.zig | 8 +------- river/Seat.zig | 2 ++ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/river/Cursor.zig b/river/Cursor.zig index abca096..4e24b7e 100644 --- a/river/Cursor.zig +++ b/river/Cursor.zig @@ -201,12 +201,7 @@ const Mode = union(enum) { // There is either no surface under the cursor or input is disallowed // Reset the cursor image to the default and clear focus. - c.wlr_xcursor_manager_set_cursor_image( - self.wlr_xcursor_manager, - "left_ptr", - self.wlr_cursor, - ); - c.wlr_seat_pointer_clear_focus(self.seat.wlr_seat); + self.clearFocus(); } }; @@ -321,6 +316,31 @@ pub fn setTheme(self: *Self, theme: ?[*:0]const u8, _size: ?u32) !void { } } +pub fn isCursorActionTarget(self: Self, view: *const View) bool { + return switch (self.mode) { + .passthrough => false, + .down => |target_view| target_view == view, + .move => |target_view| target_view == view, + .resize => |data| data.view == view, + }; +} + +pub fn handleViewUnmap(self: *Self, view: *View) void { + if (self.isCursorActionTarget(view)) { + self.mode = .passthrough; + self.clearFocus(); + } +} + +fn clearFocus(self: Self) void { + c.wlr_xcursor_manager_set_cursor_image( + self.wlr_xcursor_manager, + "left_ptr", + self.wlr_cursor, + ); + c.wlr_seat_pointer_clear_focus(self.seat.wlr_seat); +} + fn handleAxis(listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void { // This event is forwarded by the cursor when a pointer emits an axis event, // for example when you move the scroll wheel. diff --git a/river/InputManager.zig b/river/InputManager.zig index feb1ef2..a4e39ed 100644 --- a/river/InputManager.zig +++ b/river/InputManager.zig @@ -107,13 +107,7 @@ pub fn inputAllowed(self: Self, wlr_surface: *c.wlr_surface) bool { pub fn isCursorActionTarget(self: Self, view: *View) bool { var it = self.seats.first; return while (it) |node| : (it = node.next) { - const seat = &node.data; - switch (seat.cursor.mode) { - .passthrough => {}, - .down => |target_view| if (target_view == view) break true, - .move => |target_view| if (target_view == view) break true, - .resize => |data| if (data.view == view) break true, - } + if (node.data.cursor.isCursorActionTarget(view)) break true; } else false; } diff --git a/river/Seat.zig b/river/Seat.zig index a1825ed..8f03550 100644 --- a/river/Seat.zig +++ b/river/Seat.zig @@ -261,6 +261,8 @@ pub fn handleViewUnmap(self: *Self, view: *View) void { } } + self.cursor.handleViewUnmap(view); + // If the unmapped view is focused, choose a new focus if (self.focused == .view and self.focused.view == view) self.focus(null); }