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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
const std = @import("std");
const adw = @import("adw");
const glib = @import("glib");
const gobject = @import("gobject");
const gtk = @import("gtk");
const gresource = @import("../build/gresource.zig");
const Common = @import("../class.zig").Common;
const log = std.log.scoped(.gtk_ghostty_resize_overlay);
/// The overlay that shows the current size while a surface is resizing.
/// This can be used generically to show pretty much anything with a
/// disappearing overlay, but we have no other use at this point so it
/// is named specifically for what it does.
///
/// General usage:
///
/// 1. Add it to an overlay
/// 2. Set the label with `setLabel`
/// 3. Schedule to show it with `schedule`
///
/// Set any properties to change the behavior.
pub const ResizeOverlay = extern struct {
const Self = @This();
parent_instance: Parent,
pub const Parent = adw.Bin;
pub const getGObjectType = gobject.ext.defineClass(Self, .{
.name = "GhosttyResizeOverlay",
.instanceInit = &init,
.classInit = &Class.init,
.parent_class = &Class.parent,
.private = .{ .Type = Private, .offset = &Private.offset },
});
pub const properties = struct {
pub const duration = struct {
pub const name = "duration";
const impl = gobject.ext.defineProperty(
name,
Self,
c_uint,
.{
.default = 750,
.minimum = 250,
.maximum = std.math.maxInt(c_uint),
.accessor = gobject.ext.privateFieldAccessor(
Self,
Private,
&Private.offset,
"duration",
),
},
);
};
pub const @"first-delay" = struct {
pub const name = "first-delay";
const impl = gobject.ext.defineProperty(
name,
Self,
c_uint,
.{
.default = 250,
.minimum = 250,
.maximum = std.math.maxInt(c_uint),
.accessor = gobject.ext.privateFieldAccessor(
Self,
Private,
&Private.offset,
"first_delay",
),
},
);
};
pub const label = struct {
pub const name = "label";
const impl = gobject.ext.defineProperty(
name,
Self,
?[:0]const u8,
.{
.default = null,
.accessor = C.privateStringFieldAccessor("label_text"),
},
);
};
pub const @"overlay-halign" = struct {
pub const name = "overlay-halign";
const impl = gobject.ext.defineProperty(
name,
Self,
gtk.Align,
.{
.default = .center,
.accessor = gobject.ext.privateFieldAccessor(
Self,
Private,
&Private.offset,
"halign",
),
},
);
};
pub const @"overlay-valign" = struct {
pub const name = "overlay-valign";
const impl = gobject.ext.defineProperty(
name,
Self,
gtk.Align,
.{
.default = .center,
.accessor = gobject.ext.privateFieldAccessor(
Self,
Private,
&Private.offset,
"valign",
),
},
);
};
};
const Private = struct {
/// The label with the text
label: *gtk.Label,
/// The text to set on the label when scheduled.
label_text: ?[:0]const u8,
/// The time that the overlay appears.
duration: c_uint,
/// The first delay before any overlay is shown. Must be specified
/// during construction otherwise it has no effect.
first_delay: c_uint,
/// The idle source that we use to update the label.
idler: ?c_uint = null,
/// The timer for dismissing the overlay.
timer: ?c_uint = null,
/// The first delay timer.
delay_timer: ?c_uint = null,
/// The alignment of the label
halign: gtk.Align,
valign: gtk.Align,
pub var offset: c_int = 0;
};
fn init(self: *Self, _: *Class) callconv(.c) void {
gtk.Widget.initTemplate(self.as(gtk.Widget));
const priv = self.private();
if (priv.first_delay > 0) {
priv.delay_timer = glib.timeoutAdd(
priv.first_delay,
onDelayTimer,
self,
);
}
}
/// Set the label for the overlay. This will not show the
/// overlay if it is currently hidden; you must call schedule.
pub fn setLabel(self: *Self, label: ?[:0]const u8) void {
const priv = self.private();
if (priv.label_text) |v| glib.free(@ptrCast(@constCast(v)));
priv.label_text = null;
if (label) |v| priv.label_text = glib.ext.dupeZ(u8, v);
self.as(gobject.Object).notifyByPspec(properties.label.impl.param_spec);
}
/// Schedule the overlay to be shown. To avoid flickering during
/// resizes we schedule the overlay to be shown on the next idle tick.
pub fn schedule(self: *Self) void {
const priv = self.private();
// If we have a delay timer then we're not showing anything
// yet so do nothing.
if (priv.delay_timer != null) return;
// When updating a widget, wait until GTK is "idle", i.e. not in the middle
// of doing any other updates. Since we are called in the middle of resizing
// GTK is doing a lot of work rearranging all of the widgets. Not doing this
// results in a lot of warnings from GTK and _horrible_ flickering of the
// resize overlay.
if (priv.idler != null) return;
priv.idler = glib.idleAdd(onIdle, self);
}
fn onIdle(ud: ?*anyopaque) callconv(.c) c_int {
const self: *Self = @ptrCast(@alignCast(ud orelse return 0));
const priv = self.private();
// No matter what our idler is complete with this callback
priv.idler = null;
// Cancel our previous show timer no matter what.
if (priv.timer) |timer| {
if (glib.Source.remove(timer) == 0) {
log.warn("unable to remove size overlay timer", .{});
}
priv.timer = null;
}
// If we have a label to show, show ourselves. If we don't have
// label text, then hide our label.
const text = priv.label_text orelse {
self.as(gtk.Widget).setVisible(0);
return 0;
};
// Set our label and show it.
priv.label.setLabel(text);
self.as(gtk.Widget).setVisible(1);
// Setup the new timer to hide ourselves after the delay.
priv.timer = glib.timeoutAdd(
priv.duration,
onTimer,
self,
);
return 0;
}
fn onTimer(ud: ?*anyopaque) callconv(.c) c_int {
const self: *Self = @ptrCast(@alignCast(ud orelse return 0));
const priv = self.private();
priv.timer = null;
self.as(gtk.Widget).setVisible(0);
return 0;
}
fn onDelayTimer(ud: ?*anyopaque) callconv(.c) c_int {
const self: *Self = @ptrCast(@alignCast(ud orelse return 0));
const priv = self.private();
priv.delay_timer = null;
return 0;
}
//---------------------------------------------------------------
// Virtual methods
fn dispose(self: *Self) callconv(.c) void {
const priv = self.private();
if (priv.idler) |v| {
if (glib.Source.remove(v) == 0) {
log.warn("unable to remove resize overlay idler", .{});
}
priv.idler = null;
}
if (priv.timer) |v| {
if (glib.Source.remove(v) == 0) {
log.warn("unable to remove resize overlay timer", .{});
}
priv.timer = null;
}
if (priv.delay_timer) |v| {
if (glib.Source.remove(v) == 0) {
log.warn("unable to remove resize overlay delay timer", .{});
}
priv.delay_timer = null;
}
gtk.Widget.disposeTemplate(
self.as(gtk.Widget),
getGObjectType(),
);
gobject.Object.virtual_methods.dispose.call(
Class.parent,
self.as(Parent),
);
}
fn finalize(self: *Self) callconv(.c) void {
const priv = self.private();
if (priv.label_text) |v| {
glib.free(@ptrCast(@constCast(v)));
priv.label_text = null;
}
gobject.Object.virtual_methods.finalize.call(
Class.parent,
self.as(Parent),
);
}
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 {
gtk.Widget.Class.setTemplateFromResource(
class.as(gtk.Widget.Class),
comptime gresource.blueprint(.{
.major = 1,
.minor = 2,
.name = "resize-overlay",
}),
);
// Bindings
class.bindTemplateChildPrivate("label", .{});
// Properties
gobject.ext.registerProperties(class, &.{
properties.duration.impl,
properties.label.impl,
properties.@"first-delay".impl,
properties.@"overlay-halign".impl,
properties.@"overlay-valign".impl,
});
// Virtual methods
gobject.Object.virtual_methods.dispose.implement(class, &dispose);
gobject.Object.virtual_methods.finalize.implement(class, &finalize);
}
pub const as = C.Class.as;
pub const bindTemplateChildPrivate = C.Class.bindTemplateChildPrivate;
};
};
|