apprt/embedded: new surfaces inherit last focused

This commit is contained in:
Mitchell Hashimoto
2023-05-31 19:12:01 -07:00
parent a158813a3d
commit 553e09eff9
4 changed files with 37 additions and 3 deletions

View File

@ -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| {

View File

@ -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 {

View File

@ -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,
);

View File

@ -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;
}