1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
const std = @import("std");
const assert = std.debug.assert;
const adw = @import("adw");
const gobject = @import("gobject");
const gtk = @import("gtk");
const gresource = @import("../build/gresource.zig");
const Common = @import("../class.zig").Common;
const Application = @import("application.zig").Application;
const Surface = @import("surface.zig").Surface;
const Config = @import("config.zig").Config;
const log = std.log.scoped(.gtk_ghostty_window);
pub const Window = extern struct {
const Self = @This();
parent_instance: Parent,
pub const Parent = adw.ApplicationWindow;
pub const getGObjectType = gobject.ext.defineClass(Self, .{
.name = "GhosttyWindow",
.instanceInit = &init,
.classInit = &Class.init,
.parent_class = &Class.parent,
.private = .{ .Type = Private, .offset = &Private.offset },
});
pub const properties = struct {
pub const config = struct {
pub const name = "config";
const impl = gobject.ext.defineProperty(
name,
Self,
?*Config,
.{
.nick = "Config",
.blurb = "The configuration that this window is using.",
.accessor = gobject.ext.privateFieldAccessor(
Self,
Private,
&Private.offset,
"config",
),
},
);
};
};
const Private = struct {
/// The configuration that this window is using.
config: ?*Config = null,
/// The window title widget.
window_title: *adw.WindowTitle = undefined,
/// The surface in the view.
surface: *Surface = undefined,
pub var offset: c_int = 0;
};
pub fn new(app: *Application) *Self {
return gobject.ext.newInstance(Self, .{ .application = app });
}
pub fn setupInitialFocus(self: *Self) void {
_ = self.private().surface.as(gtk.Widget).grabFocus();
}
fn init(self: *Self, _: *Class) callconv(.C) void {
gtk.Widget.initTemplate(self.as(gtk.Widget));
const priv = self.private();
const app = Application.default();
priv.config = app.getConfig();
const surface = priv.surface;
_ = Surface.signals.@"close-request".connect(
surface,
*Self,
surfaceCloseRequest,
self,
.{},
);
_ = gobject.Object.signals.notify.connect(
surface,
*Self,
&surfaceNotifyHasFocus,
self,
.{ .detail = "has-focus" },
);
self.setupSurfacePropertyConnections(surface);
}
fn setupSurfacePropertyConnections(self: *Self, surface: *Surface) void {
_ = gobject.Object.signals.notify.connect(
surface,
*Self,
&surfaceNotifyTitle,
self,
.{ .detail = "title" },
);
_ = gobject.Object.signals.notify.connect(
surface,
*Self,
&surfaceNotifyPwd,
self,
.{ .detail = "pwd" },
);
}
//---------------------------------------------------------------
// Virtual methods
fn dispose(self: *Self) callconv(.C) void {
gtk.Widget.disposeTemplate(
self.as(gtk.Widget),
getGObjectType(),
);
const priv = self.private();
if (priv.config) |v| {
v.unref();
priv.config = null;
}
gobject.Object.virtual_methods.dispose.call(
Class.parent,
self.as(Parent),
);
}
/// Set the title of the window.
fn setTitle(self: *Self, title: [:0]const u8) void {
const window_title = self.private().window_title;
window_title.setTitle(title);
}
/// Set the subtitle of the window.
fn setSubtitle(self: *Self, subtitle: [:0]const u8) void {
const window_title = self.private().window_title;
window_title.setSubtitle(subtitle);
}
//---------------------------------------------------------------
// Signal handlers
fn surfaceCloseRequest(
surface: *Surface,
process_active: bool,
self: *Self,
) callconv(.c) void {
// Todo
_ = process_active;
assert(surface == self.private().surface);
self.as(gtk.Window).close();
}
fn surfaceNotifyHasFocus(surface: *Surface, _: *gobject.ParamSpec, self: *Self) callconv(.c) void {
assert(surface == self.private().surface);
self.setTitle(surface.getTitle().?);
const subtitle: [:0]const u8 = switch (Application.default().getConfig().get().@"window-subtitle") {
.@"working-directory" => surface.getPwd() orelse "",
.false => "",
};
self.setSubtitle(subtitle);
}
fn surfaceNotifyTitle(surface: *Surface, _: *gobject.ParamSpec, self: *Self) callconv(.c) void {
assert(surface == self.private().surface);
if (surface.as(gtk.Widget).grabFocus() == 0) {
return;
}
self.setTitle(surface.getTitle().?);
}
fn surfaceNotifyPwd(surface: *Surface, _: *gobject.ParamSpec, self: *Self) callconv(.c) void {
assert(surface == self.private().surface);
if (surface.as(gtk.Widget).grabFocus() == 0) {
return;
}
if (Application.default().getConfig().get().@"window-subtitle" != .@"working-directory") {
return;
}
self.setSubtitle(surface.getPwd() orelse "");
}
const C = Common(Self, Private);
pub const as = C.as;
pub const ref = C.ref;
pub const unref = C.unref;
const private = C.private;
pub const Class = extern struct {
parent_class: Parent.Class,
var parent: *Parent.Class = undefined;
pub const Instance = Self;
fn init(class: *Class) callconv(.C) void {
gobject.ext.ensureType(Surface);
gtk.Widget.Class.setTemplateFromResource(
class.as(gtk.Widget.Class),
comptime gresource.blueprint(.{
.major = 1,
.minor = 5,
.name = "window",
}),
);
// Bindings
class.bindTemplateChildPrivate("window_title", .{});
class.bindTemplateChildPrivate("surface", .{});
// Properties
gobject.ext.registerProperties(class, &.{
properties.config.impl,
});
// Virtual methods
gobject.Object.virtual_methods.dispose.implement(class, &dispose);
}
pub const as = C.Class.as;
pub const bindTemplateChildPrivate = C.Class.bindTemplateChildPrivate;
};
};
|