From 553e09eff988d83854e5a81af74f8b8d4f5b8eb4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 31 May 2023 19:12:01 -0700 Subject: [PATCH] apprt/embedded: new surfaces inherit last focused --- src/App.zig | 8 ++++++++ src/Surface.zig | 10 ++++++++++ src/apprt/embedded.zig | 20 ++++++++++++++++++-- src/terminal/Terminal.zig | 2 +- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/App.zig b/src/App.zig index 52c926d2a..5b638fdf2 100644 --- a/src/App.zig +++ b/src/App.zig @@ -127,6 +127,14 @@ pub fn deleteSurface(self: *App, rt_surface: *apprt.Surface) void { } } +/// The last focused surface. This is only valid while on the main thread +/// before tick is called. +pub fn focusedSurface(self: *App) ?*Surface { + const surface = self.focused_surface orelse return null; + if (!self.hasSurface(surface)) return null; + return surface; +} + /// Drain the mailbox. fn drainMailbox(self: *App, rt_app: *apprt.App) !void { while (self.mailbox.pop()) |message| { diff --git a/src/Surface.zig b/src/Surface.zig index e9ea5eafb..2a5a9f6a9 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -631,6 +631,16 @@ fn changeConfig(self: *Surface, config: *const configpkg.Config) !void { }; } +/// Returns the pwd of the terminal, if any. This is always copied because +/// the pwd can change at any point from termio. If we are calling from the IO +/// thread you should just check the terminal directly. +pub fn pwd(self: *const Surface, alloc: Allocator) !?[]const u8 { + self.renderer_state.mutex.lock(); + defer self.renderer_state.mutex.unlock(); + const terminal_pwd = self.io.terminal.getPwd() orelse return null; + return try alloc.dupe(u8, terminal_pwd); +} + /// Returns the x/y coordinate of where the IME (Input Method Editor) /// keyboard should be rendered. pub fn imePoint(self: *const Surface) apprt.IMEPos { diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index b4c8577f3..29a8c96e7 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -144,6 +144,8 @@ pub const Surface = struct { }; pub fn init(self: *Surface, app: *App, opts: Options) !void { + const alloc = app.core_app.alloc; + self.* = .{ .app = app, .core_surface = undefined, @@ -161,11 +163,25 @@ pub const Surface = struct { try app.core_app.addSurface(self); errdefer app.core_app.deleteSurface(self); + // Our parent pwd will be tracked here + var parent_pwd: ?[]const u8 = null; + defer if (parent_pwd) |v| alloc.free(v); + + // Shallow copy the config so that we can modify it. + var config = app.config.*; + + // Get our previously focused surface + const parent = app.core_app.focusedSurface(); + if (parent) |p| { + parent_pwd = try p.pwd(alloc); + if (parent_pwd) |v| config.@"working-directory" = v; + } + // Initialize our surface right away. We're given a view that is // ready to use. try self.core_surface.init( - app.core_app.alloc, - app.config, + alloc, + &config, .{ .rt_app = app, .mailbox = &app.core_app.mailbox }, self, ); diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 420d622c5..57f95841f 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -1514,7 +1514,7 @@ pub fn setPwd(self: *Terminal, pwd: []const u8) !void { /// Returns the pwd for the terminal, if any. The memory is owned by the /// Terminal and is not copied. It is safe until a reset or setPwd. -pub fn getPwd(self: *Terminal) ?[]const u8 { +pub fn getPwd(self: *const Terminal) ?[]const u8 { if (self.pwd.items.len == 0) return null; return self.pwd.items; }