gtk-ng: add debug warning banner (#8057)

This commit is contained in:
Jeffrey C. Ollie
2025-07-25 13:18:58 -05:00
committed by GitHub
7 changed files with 129 additions and 2 deletions

View File

@ -41,6 +41,8 @@ pub const blueprints: []const Blueprint = &.{
.{ .major = 1, .minor = 2, .name = "surface" },
.{ .major = 1, .minor = 3, .name = "surface-child-exited" },
.{ .major = 1, .minor = 5, .name = "window" },
.{ .major = 1, .minor = 2, .name = "debug-warning" },
.{ .major = 1, .minor = 3, .name = "debug-warning" },
};
/// CSS files in css_path

View File

@ -0,0 +1,52 @@
const std = @import("std");
const adw = @import("adw");
const gobject = @import("gobject");
const gtk = @import("gtk");
const build_config = @import("../../../build_config.zig");
const adw_version = @import("../adw_version.zig");
const gresource = @import("../build/gresource.zig");
const Common = @import("../class.zig").Common;
/// Debug warning banner. It will be based on adw.Banner if we're using Adwaita
/// 1.3 or newer. Otherwise it will use a gtk.Label.
pub const DebugWarning = extern struct {
const Self = @This();
parent_instance: Parent,
pub const Parent = if (adw_version.supportsBanner()) adw.Bin else gtk.Box;
pub const getGObjectType = gobject.ext.defineClass(Self, .{
.name = "GhosttyDebugWarning",
.instanceInit = &init,
.classInit = &Class.init,
.parent_class = &Class.parent,
});
fn init(self: *Self, _: *Class) callconv(.c) void {
gtk.Widget.initTemplate(self.as(gtk.Widget));
}
const C = Common(Self, null);
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 = if (adw_version.supportsBanner()) 3 else 2,
.name = "debug-warning",
}),
);
}
pub const as = C.Class.as;
};
};

View File

@ -1,4 +1,5 @@
const std = @import("std");
const build_config = @import("../../../build_config.zig");
const assert = std.debug.assert;
const adw = @import("adw");
const gobject = @import("gobject");
@ -9,6 +10,7 @@ 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 DebugWarning = @import("debug_warning.zig").DebugWarning;
const log = std.log.scoped(.gtk_ghostty_window);
@ -24,6 +26,29 @@ pub const Window = extern struct {
.private = .{ .Type = Private, .offset = &Private.offset },
});
pub const properties = struct {
pub const debug = struct {
pub const name = "debug";
const impl = gobject.ext.defineProperty(
name,
Self,
bool,
.{
.nick = "Debug",
.blurb = "True if runtime safety checks are enabled.",
.default = build_config.is_debug,
.accessor = gobject.ext.typedAccessor(Self, bool, .{
.getter = struct {
pub fn getter(_: *Window) bool {
return build_config.is_debug;
}
}.getter,
}),
},
);
};
};
const Private = struct {
/// The surface in the view.
surface: *Surface = undefined,
@ -46,6 +71,9 @@ pub const Window = extern struct {
fn init(self: *Self, _: *Class) callconv(.C) void {
gtk.Widget.initTemplate(self.as(gtk.Widget));
if (comptime build_config.is_debug)
self.as(gtk.Widget).addCssClass("devel");
}
//---------------------------------------------------------------
@ -91,6 +119,7 @@ pub const Window = extern struct {
fn init(class: *Class) callconv(.C) void {
gobject.ext.ensureType(Surface);
gobject.ext.ensureType(DebugWarning);
gtk.Widget.Class.setTemplateFromResource(
class.as(gtk.Widget.Class),
comptime gresource.blueprint(.{
@ -100,6 +129,11 @@ pub const Window = extern struct {
}),
);
// Properties
gobject.ext.registerProperties(class, &.{
properties.debug.impl,
});
// Bindings
class.bindTemplateChildPrivate("surface", .{});

View File

@ -0,0 +1,11 @@
using Gtk 4.0;
template $GhosttyDebugWarning: Gtk.Box {
orientation: vertical;
Gtk.Label {
label: _("⚠️ You're running a debug build of Ghostty! Performance will be degraded.");
margin-top: 10;
margin-bottom: 10;
}
}

View File

@ -0,0 +1,9 @@
using Gtk 4.0;
using Adw 1;
template $GhosttyDebugWarning: Adw.Bin {
Adw.Banner {
title: _("⚠️ You're running a debug build of Ghostty! Performance will be degraded.");
revealed: true;
}
}

View File

@ -2,10 +2,23 @@ using Gtk 4.0;
using Adw 1;
template $GhosttyWindow: Adw.ApplicationWindow {
styles [
"window",
]
default-width: 800;
default-height: 600;
content: $GhosttySurface surface {
close-request => $surface_close_request();
content: Gtk.Box {
orientation: vertical;
spacing: 0;
$GhosttyDebugWarning {
visible: bind template.debug;
}
$GhosttySurface surface {
close-request => $surface_close_request();
}
};
}

View File

@ -96,3 +96,9 @@ pub const Artifact = enum {
};
}
};
/// True if runtime safety checks are enabled.
pub const is_debug = switch (builtin.mode) {
.Debug, .ReleaseSafe => true,
.ReleaseFast, .ReleaseSmall => false,
};