From 236e57a1f4cc23c2468eaccea34c76e6a2599a8a Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Sat, 25 Nov 2023 16:06:28 +0100 Subject: [PATCH] gtk: restore focus-grabbing after closing one side in split --- src/apprt/gtk/Split.zig | 10 ++++++-- src/apprt/gtk/Surface.zig | 48 ++++++--------------------------------- 2 files changed, 15 insertions(+), 43 deletions(-) diff --git a/src/apprt/gtk/Split.zig b/src/apprt/gtk/Split.zig index 792968e4a..5ab4c3e8c 100644 --- a/src/apprt/gtk/Split.zig +++ b/src/apprt/gtk/Split.zig @@ -112,8 +112,6 @@ inline fn removeChild(self: *Split, remove: Surface.Container.Elem, keep: Surfac const window = self.container.window() orelse return; const alloc = window.app.core_app.alloc; - // TODO: Grab focus - // Keep a reference to the side that we want to keep, so it doesn't get // destroyed when it's removed from our underlying GtkPaned. const keep_object: *c.GObject = @ptrCast(keep.widget()); @@ -127,6 +125,9 @@ inline fn removeChild(self: *Split, remove: Surface.Container.Elem, keep: Surfac // Our container must become whatever our top left is self.container.replace(keep); + // Grab focus of the left-over side + keep.grabFocus(); + // TODO: is this correct? remove.deinit(alloc); alloc.destroy(self); @@ -149,6 +150,11 @@ pub fn replace( self.updateChildren(); } +// grabFocus grabs the focus of the top-left element. +pub fn grabFocus(self: *Split) void { + self.top_left.grabFocus(); +} + /// Update the paned children to represent the current state. /// This should be called anytime the top/left or bottom/right /// element is changed. diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index 21fe86a37..c1eefc3c6 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -86,11 +86,11 @@ pub const Container = union(enum) { } } - pub fn debugName(self: Elem) []const u8 { - return switch (self) { - .surface => "surface", - .split => "split", - }; + pub fn grabFocus(self: Elem) void { + switch (self) { + .surface => |s| s.grabFocus(), + .split => |s| s.grabFocus(), + } } }; @@ -132,14 +132,10 @@ pub const Container = union(enum) { /// from a surface to a split or a split back to a surface or /// a split to a nested split and so on. pub fn replace(self: Container, elem: Elem) void { - log.debug("Container.replace. self={s}, elem={s}", .{ self.debugName(), elem.debugName() }); - // Move the element into the container switch (self) { .none => {}, - .tab_ => |t| { - t.replaceElem(elem); - }, + .tab_ => |t| t.replaceElem(elem), inline .split_tl, .split_br => |ptr| { const s = self.split().?; s.replace(ptr, elem); @@ -154,7 +150,6 @@ pub const Container = union(enum) { /// children to effectively notify they're containing that /// all children at this level are exiting. pub fn remove(self: Container) void { - log.warn("Container.remove", .{}); switch (self) { .none => {}, .tab_ => |t| t.closeElem(), @@ -162,15 +157,6 @@ pub const Container = union(enum) { .split_br => self.split().?.removeBottomRight(), } } - - pub fn debugName(self: Container) []const u8 { - return switch (self) { - .none => "none", - .tab_ => "tab", - .split_tl => "split_tl", - .split_br => "split_br", - }; - } }; /// Whether the surface has been realized or not yet. When a surface is @@ -541,22 +527,8 @@ pub fn getTitleLabel(self: *Surface) ?*c.GtkWidget { } pub fn newSplit(self: *Surface, direction: input.SplitDirection) !void { - log.debug("splitting direction={}", .{direction}); const alloc = self.app.core_app.alloc; _ = try Split.create(alloc, self, direction); - - // switch (self.parent) { - // .none => return, - // .paned => |parent_paned_tuple| { - // const paned = parent_paned_tuple[0]; - // const position = parent_paned_tuple[1]; - // - // try paned.splitSurfaceInPosition(position, direction); - // }, - // .tab => |tab| { - // try tab.splitSurface(direction); - // }, - // } } pub fn newTab(self: *Surface) !void { @@ -1059,13 +1031,7 @@ fn gtkMouseDown( // If we don't have focus, grab it. const gl_widget = @as(*c.GtkWidget, @ptrCast(self.gl_area)); if (c.gtk_widget_has_focus(gl_widget) == 0) { - if (self.container.tab()) |tab| tab.focus_child = self; - _ = c.gtk_widget_grab_focus(gl_widget); - - // If we have siblings, we also update the title, since it means - // another sibling might have updated the title. - // TODO: fixme - //if (self.parent != Parent.tab) self.updateTitleLabels(); + self.grabFocus(); } self.core_surface.mouseButtonCallback(.press, button, mods) catch |err| {