TextInput: remove pending_focused_surface

This state doesn't need to exist, just get the focus from the Seat.
This commit is contained in:
Isaac Freund 2023-12-31 13:32:59 -06:00
parent 55ed16efd1
commit 49defcfb7a
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
4 changed files with 16 additions and 57 deletions

View File

@ -196,10 +196,8 @@ fn handleNewInputMethod(
log.debug("new input method on seat {s}", .{relay.seat.wlr_seat.name}); log.debug("new input method on seat {s}", .{relay.seat.wlr_seat.name});
const text_input = relay.getFocusableTextInput() orelse return; if (seat.focused.surface()) |surface| {
if (text_input.pending_focused_surface) |surface| { relay.setSurfaceFocus(surface);
text_input.wlr_text_input.sendEnter(surface);
text_input.setPendingFocusedSurface(null);
} }
} }

View File

@ -102,11 +102,9 @@ fn handleInputMethodDestroy(
self.input_method = null; self.input_method = null;
const text_input = self.getFocusedTextInput() orelse return; if (self.getFocusedTextInput()) |text_input| {
if (text_input.wlr_text_input.focused_surface) |surface| { text_input.wlr_text_input.sendLeave();
text_input.setPendingFocusedSurface(surface);
} }
text_input.wlr_text_input.sendLeave();
} }
fn handleInputMethodGrabKeyboard( fn handleInputMethodGrabKeyboard(
@ -133,14 +131,6 @@ fn handleInputMethodGrabKeyboardDestroy(
} }
} }
pub fn getFocusableTextInput(self: *Self) ?*TextInput {
var it = self.text_inputs.first;
return while (it) |node| : (it = node.next) {
const text_input = &node.data;
if (text_input.pending_focused_surface != null) break text_input;
} else null;
}
pub fn getFocusedTextInput(self: *Self) ?*TextInput { pub fn getFocusedTextInput(self: *Self) ?*TextInput {
var it = self.text_inputs.first; var it = self.text_inputs.first;
return while (it) |node| : (it = node.next) { return while (it) |node| : (it = node.next) {
@ -194,13 +184,7 @@ pub fn setSurfaceFocus(self: *Self, wlr_surface: ?*wlr.Surface) void {
var it = self.text_inputs.first; var it = self.text_inputs.first;
while (it) |node| : (it = node.next) { while (it) |node| : (it = node.next) {
const text_input = &node.data; const text_input = &node.data;
if (text_input.pending_focused_surface) |surface| { if (text_input.wlr_text_input.focused_surface) |surface| {
assert(text_input.wlr_text_input.focused_surface == null);
if (wlr_surface != surface) {
text_input.setPendingFocusedSurface(null);
}
} else if (text_input.wlr_text_input.focused_surface) |surface| {
assert(text_input.pending_focused_surface == null);
if (wlr_surface != surface) { if (wlr_surface != surface) {
text_input.relay.disableTextInput(text_input); text_input.relay.disableTextInput(text_input);
text_input.wlr_text_input.sendLeave(); text_input.wlr_text_input.sendLeave();
@ -221,8 +205,6 @@ pub fn setSurfaceFocus(self: *Self, wlr_surface: ?*wlr.Surface) void {
if (new_text_input) |text_input| { if (new_text_input) |text_input| {
if (self.input_method != null) { if (self.input_method != null) {
text_input.wlr_text_input.sendEnter(wlr_surface.?); text_input.wlr_text_input.sendEnter(wlr_surface.?);
} else {
text_input.setPendingFocusedSurface(wlr_surface.?);
} }
} }
} }

View File

@ -53,6 +53,16 @@ pub const FocusTarget = union(enum) {
layer: *LayerSurface, layer: *LayerSurface,
lock_surface: *LockSurface, lock_surface: *LockSurface,
none: void, none: void,
pub fn surface(target: FocusTarget) ?*wlr.Surface {
return switch (target) {
.view => |view| view.rootSurface(),
.xwayland_override_redirect => |xwayland_or| xwayland_or.xwayland_surface.surface,
.layer => |layer| layer.wlr_layer_surface.surface,
.lock_surface => |lock_surface| lock_surface.wlr_lock_surface.surface,
.none => null,
};
}
}; };
wlr_seat: *wlr.Seat, wlr_seat: *wlr.Seat,
@ -216,14 +226,7 @@ pub fn setFocusRaw(self: *Self, new_focus: FocusTarget) void {
// If the target is already focused, do nothing // If the target is already focused, do nothing
if (std.meta.eql(new_focus, self.focused)) return; if (std.meta.eql(new_focus, self.focused)) return;
// Obtain the target surface const target_surface = new_focus.surface();
const target_surface = switch (new_focus) {
.view => |target_view| target_view.rootSurface(),
.xwayland_override_redirect => |target_or| target_or.xwayland_surface.surface,
.layer => |target_layer| target_layer.wlr_layer_surface.surface,
.lock_surface => |lock_surface| lock_surface.wlr_lock_surface.surface,
.none => null,
};
// First clear the current focus // First clear the current focus
switch (self.focused) { switch (self.focused) {

View File

@ -32,10 +32,6 @@ const log = std.log.scoped(.text_input);
relay: *InputRelay, relay: *InputRelay,
wlr_text_input: *wlr.TextInputV3, wlr_text_input: *wlr.TextInputV3,
/// Surface stored for when text-input can't receive an enter event immediately
/// after getting focus. Cleared once text-input receive the enter event.
pending_focused_surface: ?*wlr.Surface = null,
enable: wl.Listener(*wlr.TextInputV3) = enable: wl.Listener(*wlr.TextInputV3) =
wl.Listener(*wlr.TextInputV3).init(handleEnable), wl.Listener(*wlr.TextInputV3).init(handleEnable),
commit: wl.Listener(*wlr.TextInputV3) = commit: wl.Listener(*wlr.TextInputV3) =
@ -45,9 +41,6 @@ disable: wl.Listener(*wlr.TextInputV3) =
destroy: wl.Listener(*wlr.TextInputV3) = destroy: wl.Listener(*wlr.TextInputV3) =
wl.Listener(*wlr.TextInputV3).init(handleDestroy), wl.Listener(*wlr.TextInputV3).init(handleDestroy),
pending_focused_surface_destroy: wl.Listener(*wlr.Surface) =
wl.Listener(*wlr.Surface).init(handlePendingFocusedSurfaceDestroy),
pub fn init(self: *Self, relay: *InputRelay, wlr_text_input: *wlr.TextInputV3) void { pub fn init(self: *Self, relay: *InputRelay, wlr_text_input: *wlr.TextInputV3) void {
self.* = .{ self.* = .{
.relay = relay, .relay = relay,
@ -104,8 +97,6 @@ fn handleDestroy(listener: *wl.Listener(*wlr.TextInputV3), _: *wlr.TextInputV3)
if (self.wlr_text_input.current_enabled) self.relay.disableTextInput(self); if (self.wlr_text_input.current_enabled) self.relay.disableTextInput(self);
node.data.setPendingFocusedSurface(null);
self.enable.link.remove(); self.enable.link.remove();
self.commit.link.remove(); self.commit.link.remove();
self.disable.link.remove(); self.disable.link.remove();
@ -114,18 +105,3 @@ fn handleDestroy(listener: *wl.Listener(*wlr.TextInputV3), _: *wlr.TextInputV3)
self.relay.text_inputs.remove(node); self.relay.text_inputs.remove(node);
util.gpa.destroy(node); util.gpa.destroy(node);
} }
fn handlePendingFocusedSurfaceDestroy(listener: *wl.Listener(*wlr.Surface), surface: *wlr.Surface) void {
const self = @fieldParentPtr(Self, "pending_focused_surface_destroy", listener);
assert(self.pending_focused_surface == surface);
self.pending_focused_surface = null;
self.pending_focused_surface_destroy.link.remove();
}
pub fn setPendingFocusedSurface(self: *Self, wlr_surface: ?*wlr.Surface) void {
if (self.pending_focused_surface != null) self.pending_focused_surface_destroy.link.remove();
self.pending_focused_surface = wlr_surface;
if (self.pending_focused_surface) |surface| {
surface.events.destroy.add(&self.pending_focused_surface_destroy);
}
}