mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-14 03:25:50 +00:00
Use config to determine cwd inheritance for windows, tabs, and splits
- Define NewSurfaceContext enum (window, tab, split) - Add shouldInheritWorkingDirectory helper function - Thread surface context through newConfig and newSurfaceOptions - Replace window-inherit-working-directory checks with context-aware logic - Add context to embedded CAPI and GTK Surface structs
This commit is contained in:
@@ -456,6 +456,15 @@ pub const Surface = struct {
|
||||
|
||||
/// Wait after the command exits
|
||||
wait_after_command: bool = false,
|
||||
|
||||
/// Context for the new surface
|
||||
context: Context = .c_window,
|
||||
|
||||
pub const Context = enum(c_int) {
|
||||
c_window = 0,
|
||||
c_tab = 1,
|
||||
c_split = 2,
|
||||
};
|
||||
};
|
||||
|
||||
pub fn init(self: *Surface, app: *App, opts: Options) !void {
|
||||
@@ -477,7 +486,13 @@ pub const Surface = struct {
|
||||
errdefer app.core_app.deleteSurface(self);
|
||||
|
||||
// Shallow copy the config so that we can modify it.
|
||||
var config = try apprt.surface.newConfig(app.core_app, &app.config);
|
||||
const surface_context: apprt.surface.NewSurfaceContext = switch (opts.context) {
|
||||
.c_window => .window,
|
||||
.c_tab => .tab,
|
||||
.c_split => .split,
|
||||
};
|
||||
|
||||
var config = try apprt.surface.newConfig(app.core_app, &app.config, surface_context);
|
||||
defer config.deinit();
|
||||
|
||||
// If we have a working directory from the options then we set it.
|
||||
@@ -894,14 +909,27 @@ pub const Surface = struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn newSurfaceOptions(self: *const Surface) apprt.Surface.Options {
|
||||
pub fn newSurfaceOptions(self: *const Surface, context: apprt.surface.NewSurfaceContext) apprt.Surface.Options {
|
||||
const font_size: f32 = font_size: {
|
||||
if (!self.app.config.@"window-inherit-font-size") break :font_size 0;
|
||||
break :font_size self.core_surface.font_size.points;
|
||||
};
|
||||
|
||||
const working_directory: ?[*:0]const u8 = wd: {
|
||||
if (!apprt.surface.shouldInheritWorkingDirectory(context, &self.app.config)) break :wd null;
|
||||
const cwd = self.core_surface.pwd(self.app.core_app.alloc) catch null orelse break :wd null;
|
||||
defer self.app.core_app.alloc.free(cwd);
|
||||
break :wd self.app.core_app.alloc.dupeZ(u8, cwd) catch null;
|
||||
};
|
||||
|
||||
return .{
|
||||
.font_size = font_size,
|
||||
.working_directory = working_directory,
|
||||
.context = switch (context) {
|
||||
.window => .c_window,
|
||||
.tab => .c_tab,
|
||||
.split => .c_split,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -671,6 +671,9 @@ pub const Surface = extern struct {
|
||||
error_page: *adw.StatusPage,
|
||||
terminal_page: *gtk.Overlay,
|
||||
|
||||
/// The context for this surface (window, tab, or split)
|
||||
context: apprt.surface.NewSurfaceContext = .window,
|
||||
|
||||
pub var offset: c_int = 0;
|
||||
};
|
||||
|
||||
@@ -716,10 +719,8 @@ pub const Surface = extern struct {
|
||||
// Remainder needs a config. If there is no config we just assume
|
||||
// we aren't inheriting any of these values.
|
||||
if (priv.config) |config_obj| {
|
||||
const config = config_obj.get();
|
||||
|
||||
// Setup our pwd if configured to inherit
|
||||
if (config.@"window-inherit-working-directory") {
|
||||
// Setup our cwd if configured to inherit
|
||||
if (apprt.surface.shouldInheritWorkingDirectory(context, config_obj.get())) {
|
||||
if (parent.rt_surface.surface.getPwd()) |pwd| {
|
||||
priv.pwd = glib.ext.dupeZ(u8, pwd);
|
||||
self.as(gobject.Object).notifyByPspec(properties.pwd.impl.param_spec);
|
||||
@@ -3203,10 +3204,7 @@ pub const Surface = extern struct {
|
||||
errdefer app.core().deleteSurface(self.rt());
|
||||
|
||||
// Initialize our surface configuration.
|
||||
var config = try apprt.surface.newConfig(
|
||||
app.core(),
|
||||
priv.config.?.get(),
|
||||
);
|
||||
var config = try apprt.surface.newConfig(app.core(), priv.config.?.get(), priv.context);
|
||||
defer config.deinit();
|
||||
|
||||
// Properties that can impact surface init
|
||||
|
||||
@@ -159,12 +159,28 @@ pub const Mailbox = struct {
|
||||
}
|
||||
};
|
||||
|
||||
/// Context for new surface creation to determine inheritance behavior
|
||||
pub const NewSurfaceContext = enum {
|
||||
window,
|
||||
tab,
|
||||
split,
|
||||
};
|
||||
|
||||
pub fn shouldInheritWorkingDirectory(context: NewSurfaceContext, config: *const Config) bool {
|
||||
return switch (context) {
|
||||
.window => config.@"window-inherit-working-directory",
|
||||
.tab => config.@"tab-inherit-working-directory",
|
||||
.split => config.@"split-inherit-working-directory",
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns a new config for a surface for the given app that should be
|
||||
/// used for any new surfaces. The resulting config should be deinitialized
|
||||
/// after the surface is initialized.
|
||||
pub fn newConfig(
|
||||
app: *const App,
|
||||
config: *const Config,
|
||||
context: NewSurfaceContext,
|
||||
) Allocator.Error!Config {
|
||||
// Create a shallow clone
|
||||
var copy = config.shallowClone(app.alloc);
|
||||
@@ -175,7 +191,7 @@ pub fn newConfig(
|
||||
// Get our previously focused surface for some inherited values.
|
||||
const prev = app.focusedSurface();
|
||||
if (prev) |p| {
|
||||
if (config.@"window-inherit-working-directory") {
|
||||
if (shouldInheritWorkingDirectory(context, config)) {
|
||||
if (try p.pwd(alloc)) |pwd| {
|
||||
copy.@"working-directory" = pwd;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user