gtk: rework surface title buffer handling

This commit is contained in:
Thorsten Ball
2023-11-01 19:57:37 +01:00
committed by Mitchell Hashimoto
parent ba2992d4d0
commit 8cfa9297bf

View File

@ -92,8 +92,11 @@ title: Title,
/// Our title. The raw value of the title. This will be kept up to date and /// Our title. The raw value of the title. This will be kept up to date and
/// .title will be updated if we have focus. /// .title will be updated if we have focus.
/// When set the text in this buf will be null-terminated, because we need to
/// pass it to GTK.
/// TODO: what's a big enough value? /// TODO: what's a big enough value?
titleText: [4096]u8, title_text_buf: [4096]u8,
title_text_buf_len: u13,
/// The core surface backing this surface /// The core surface backing this surface
core_surface: CoreSurface, core_surface: CoreSurface,
@ -180,7 +183,8 @@ pub fn init(self: *Surface, app: *App, opts: Options) !void {
.title = if (opts.title_label) |label| .{ .title = if (opts.title_label) |label| .{
.label = label, .label = label,
} else .{ .none = {} }, } else .{ .none = {} },
.titleText = undefined, .title_text_buf = undefined,
.title_text_buf_len = 0,
.core_surface = undefined, .core_surface = undefined,
.font_size = opts.font_size, .font_size = opts.font_size,
.parentSurface = opts.parentSurface, .parentSurface = opts.parentSurface,
@ -462,22 +466,21 @@ fn updateTitleLabels(self: *Surface) void {
switch (self.title) { switch (self.title) {
.none => {}, .none => {},
.label => |label| { .label => |label| {
const slice: []u8 = std.mem.sliceTo(&self.titleText, 0); if (self.title_text_buf_len == 0) return;
// Check whether there's even something in the buffer yet.
// TODO: This check is really bad. const slice: []u8 = self.title_text_buf[0..self.title_text_buf_len];
if (slice.len != self.titleText.len) { c.gtk_label_set_text(label, @as([*c]const u8, @ptrCast(slice)));
c.gtk_label_set_text(label, @as([*c]const u8, @ptrCast(slice))); c.gtk_window_set_title(self.window.window, c.gtk_label_get_text(label));
c.gtk_window_set_title(self.window.window, c.gtk_label_get_text(label));
}
}, },
} }
} }
pub fn setTitle(self: *Surface, slice: [:0]const u8) !void { pub fn setTitle(self: *Surface, slice: [:0]const u8) !void {
// TODO: I'm sure there has to be a better way than this in Zig. const len = @min(self.title_text_buf.len - 1, slice.len);
const len = @min(self.titleText.len - 1, slice.len); @memcpy(self.title_text_buf[0..len], slice[0..]);
@memcpy(self.titleText[0..len], slice[0..]); // Null-terminate this because we then need to pass it to GTK.
self.titleText[len] = 0; self.title_text_buf[len] = 0;
self.title_text_buf_len = len;
const widget = @as(*c.GtkWidget, @ptrCast(self.gl_area)); const widget = @as(*c.GtkWidget, @ptrCast(self.gl_area));
if (c.gtk_widget_is_focus(widget) == 1) { if (c.gtk_widget_is_focus(widget) == 1) {