diff --git a/src/App.zig b/src/App.zig index b39938392..52c926d2a 100644 --- a/src/App.zig +++ b/src/App.zig @@ -29,6 +29,10 @@ alloc: Allocator, /// The list of surfaces that are currently active. surfaces: SurfaceList, +/// The last focused surface. This surface may not be valid; +/// you must always call hasSurface to validate it. +focused_surface: ?*Surface = null, + /// The mailbox that can be used to send this thread messages. Note /// this is a blocking queue so if it is full you will get errors (or block). mailbox: Mailbox.Queue, @@ -131,6 +135,7 @@ fn drainMailbox(self: *App, rt_app: *apprt.App) !void { .reload_config => try self.reloadConfig(rt_app), .new_window => |msg| try self.newWindow(rt_app, msg), .close => |surface| try self.closeSurface(rt_app, surface), + .focus => |surface| try self.focusSurface(rt_app, surface), .quit => try self.setQuit(), .surface_message => |msg| try self.surfaceMessage(msg.surface, msg.message), .redraw_surface => |surface| try self.redrawSurface(rt_app, surface), @@ -153,6 +158,13 @@ fn closeSurface(self: *App, rt_app: *apprt.App, surface: *Surface) !void { surface.close(); } +fn focusSurface(self: *App, rt_app: *apprt.App, surface: *Surface) !void { + _ = rt_app; + + if (!self.hasSurface(surface)) return; + self.focused_surface = surface; +} + fn redrawSurface(self: *App, rt_app: *apprt.App, surface: *apprt.Surface) !void { if (!self.hasSurface(&surface.core_surface)) return; rt_app.redrawSurface(surface); @@ -215,6 +227,11 @@ pub const Message = union(enum) { /// should close. close: *Surface, + /// The last focused surface. The app keeps track of this to + /// enable "inheriting" various configurations from the last + /// surface. + focus: *Surface, + /// Quit quit: void, diff --git a/src/Surface.zig b/src/Surface.zig index 0d621f758..e9ea5eafb 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -1224,6 +1224,13 @@ pub fn focusCallback(self: *Surface, focused: bool) !void { .focus = focused, }, .{ .forever = {} }); + // Notify our app if we gained focus. + if (focused) { + _ = self.app_mailbox.push(.{ + .focus = self, + }, .{ .forever = {} }); + } + // Schedule render which also drains our mailbox try self.queueRender();