apprt/gtk: just heap allocate the title text

This commit is contained in:
Mitchell Hashimoto
2023-12-01 09:14:08 -08:00
parent c2c8f78cf8
commit 967e091e2c

View File

@ -184,9 +184,7 @@ cursor: ?*c.GdkCursor = null,
/// .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 /// When set the text in this buf will be null-terminated, because we need to
/// pass it to GTK. /// pass it to GTK.
/// TODO: what's a big enough value? title_text: ?[:0]const u8 = null,
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,
@ -300,8 +298,7 @@ pub fn init(self: *Surface, app: *App, opts: Options) !void {
.app = app, .app = app,
.container = .{ .none = {} }, .container = .{ .none = {} },
.gl_area = gl_area, .gl_area = gl_area,
.title_text_buf = undefined, .title_text = null,
.title_text_buf_len = 0,
.core_surface = undefined, .core_surface = undefined,
.font_size = font_size, .font_size = font_size,
.parentSurface = opts.parent != null, .parentSurface = opts.parent != null,
@ -378,6 +375,8 @@ fn realize(self: *Surface) !void {
} }
pub fn deinit(self: *Surface) void { pub fn deinit(self: *Surface) void {
if (self.title_text) |title| self.app.core_app.alloc.free(title);
// We don't allocate anything if we aren't realized. // We don't allocate anything if we aren't realized.
if (!self.realized) return; if (!self.realized) return;
@ -630,31 +629,29 @@ pub fn grabFocus(self: *Surface) void {
fn updateTitleLabels(self: *Surface) void { fn updateTitleLabels(self: *Surface) void {
// If we have no title, then we have nothing to update. // If we have no title, then we have nothing to update.
if (self.title_text_buf_len == 0) return; const title = self.title_text orelse return;
const slice: []u8 = self.title_text_buf[0..self.title_text_buf_len];
// If we have a tab, then we have to update the tab // If we have a tab, then we have to update the tab
if (self.container.tab()) |tab| { if (self.container.tab()) |tab| {
c.gtk_label_set_text(tab.label_text, slice.ptr); c.gtk_label_set_text(tab.label_text, title.ptr);
} }
// If we have a window, then we have to update the window title. // If we have a window, then we have to update the window title.
if (self.container.window()) |window| { if (self.container.window()) |window| {
c.gtk_window_set_title(window.window, slice.ptr); c.gtk_window_set_title(window.window, title.ptr);
} }
} }
pub fn setTitle(self: *Surface, slice: [:0]const u8) !void { pub fn setTitle(self: *Surface, slice: [:0]const u8) !void {
const len = @min(self.title_text_buf.len - 1, slice.len); const alloc = self.app.core_app.alloc;
@memcpy(self.title_text_buf[0..len], slice[0..]); const copy = try alloc.dupeZ(u8, slice);
// Null-terminate this because we then need to pass it to GTK. errdefer alloc.free(copy);
self.title_text_buf[len] = 0;
self.title_text_buf_len = len; if (self.title_text) |old| alloc.free(old);
self.title_text = copy;
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) self.updateTitleLabels();
self.updateTitleLabels();
}
} }
pub fn setMouseShape( pub fn setMouseShape(