generate gresource xml and dependencies at comptime rather than hardcoding

This commit is contained in:
Jeffrey C. Ollie
2024-02-12 16:40:06 -06:00
parent f943a4cf87
commit 9f0468f950
4 changed files with 135 additions and 66 deletions

View File

@ -1167,36 +1167,13 @@ fn addDeps(
if (config.libadwaita) step.linkSystemLibrary2("adwaita-1", dynamic_link_opts); if (config.libadwaita) step.linkSystemLibrary2("adwaita-1", dynamic_link_opts);
{ {
// TODO: find a way to dynamically update this from the output const gresource = @import("src/apprt/gtk/gresource.zig");
// of `glib-compile-resources --generate-dependencies`
const extra_file_dependencies = &.{
"src/apprt/gtk/gresource.xml",
"src/apprt/gtk/style.css",
"src/apprt/gtk/style-dark.css",
"src/apprt/gtk/style-hc.css",
"src/apprt/gtk/style-hc-dark.css",
"images/icons/icon_16x16@2x@2x.png",
"images/icons/icon_16x16.png",
"images/icons/icon_32x32@2x@2x.png",
"images/icons/icon_32x32.png",
"images/icons/icon_128x128@2x@2x.png",
"images/icons/icon_128x128.png",
"images/icons/icon_256x256@2x@2x.png",
"images/icons/icon_256x256.png",
"images/icons/icon_512x512.png",
};
const generate_resources_d = b.addSystemCommand(&.{ const wf = b.addWriteFiles();
"glib-compile-resources", const gresource_xml = wf.add(
"--generate-dependencies", "gresource.xml",
"src/apprt/gtk/gresource.xml", if (config.libadwaita) gresource.gresource_xml_libadwaita else gresource.gresource_xml_gtk,
}); );
_ = generate_resources_d.captureStdOut();
generate_resources_d.extra_file_dependencies = &.{
"src/apprt/gtk/gresource.xml",
};
const generate_resources_c = b.addSystemCommand(&.{ const generate_resources_c = b.addSystemCommand(&.{
"glib-compile-resources", "glib-compile-resources",
@ -1208,15 +1185,9 @@ fn addDeps(
const ghostty_resources_c = generate_resources_c.addOutputFileArg("ghostty_resources.c"); const ghostty_resources_c = generate_resources_c.addOutputFileArg("ghostty_resources.c");
generate_resources_c.addArgs(&.{ generate_resources_c.addFileArg(gresource_xml);
"src/apprt/gtk/gresource.xml",
});
generate_resources_c.step.dependOn(&generate_resources_d.step); generate_resources_c.extra_file_dependencies = if (config.libadwaita) &gresource.dependencies_libadwaita else &gresource.dependencies_gtk;
// TODO: find a way to dynamically update this from the output
// of `glib-compile-resources --generate-dependencies`
generate_resources_c.extra_file_dependencies = extra_file_dependencies;
step.addCSourceFile(.{ step.addCSourceFile(.{
.file = ghostty_resources_c, .file = ghostty_resources_c,
@ -1233,16 +1204,9 @@ fn addDeps(
const ghostty_resources_h = generate_resources_h.addOutputFileArg("ghostty_resources.h"); const ghostty_resources_h = generate_resources_h.addOutputFileArg("ghostty_resources.h");
generate_resources_h.addArgs(&.{ generate_resources_h.addFileArg(gresource_xml);
"src/apprt/gtk/gresource.xml",
});
generate_resources_h.step.dependOn(&generate_resources_d.step); generate_resources_h.extra_file_dependencies = if (config.libadwaita) &gresource.dependencies_libadwaita else &gresource.dependencies_gtk;
generate_resources_h.step.dependOn(&generate_resources_c.step);
// TODO: find a way to dynamically update this from the output
// of `glib-compile-resources --generate-dependencies`
generate_resources_h.extra_file_dependencies = extra_file_dependencies;
step.addIncludePath(ghostty_resources_h.dirname()); step.addIncludePath(ghostty_resources_h.dirname());
} }

View File

@ -53,6 +53,7 @@ fn init(self: *ConfigErrors, app: *App) !void {
c.gtk_window_set_title(gtk_window, "Configuration Errors"); c.gtk_window_set_title(gtk_window, "Configuration Errors");
c.gtk_window_set_default_size(gtk_window, 600, 275); c.gtk_window_set_default_size(gtk_window, 600, 275);
c.gtk_window_set_resizable(gtk_window, 0); c.gtk_window_set_resizable(gtk_window, 0);
c.gtk_window_set_icon_name(gtk_window, "com.mitchellh.ghostty");
_ = c.g_signal_connect_data(window, "destroy", c.G_CALLBACK(&gtkDestroy), self, null, c.G_CONNECT_DEFAULT); _ = c.g_signal_connect_data(window, "destroy", c.G_CALLBACK(&gtkDestroy), self, null, c.G_CONNECT_DEFAULT);
// Set some state // Set some state

View File

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/com/mitchellh/ghostty">
<file compressed="true" alias="style.css">src/apprt/gtk/style.css</file>
<file compressed="true" alias="style-dark.css">src/apprt/gtk/style-dark.css</file>
<file compressed="true" alias="style-hc.css">src/apprt/gtk/style-hc.css</file>
<file compressed="true" alias="style-hc-dark.css">src/apprt/gtk/style-hc-dark.css</file>
</gresource>
<gresource prefix="/com/mitchellh/ghostty/icons">
<file preprocess="to-pixdata" alias="16x16/apps/com.mitchellh.ghostty.png">images/icons/icon_16x16.png</file>
<file preprocess="to-pixdata" alias="16x16@2/apps/com.mitchellh.ghostty.png">images/icons/icon_16x16@2x@2x.png</file>
<file preprocess="to-pixdata" alias="32x32/apps/com.mitchellh.ghostty.png">images/icons/icon_32x32.png</file>
<file preprocess="to-pixdata" alias="32x32@2/apps/com.mitchellh.ghostty.png">images/icons/icon_32x32@2x@2x.png</file>
<file preprocess="to-pixdata" alias="128x128/apps/com.mitchellh.ghostty.png">images/icons/icon_128x128.png</file>
<file preprocess="to-pixdata" alias="128x128@2/apps/com.mitchellh.ghostty.png">images/icons/icon_128x128@2x@2x.png</file>
<file preprocess="to-pixdata" alias="256x256/apps/com.mitchellh.ghostty.png">images/icons/icon_256x256.png</file>
<file preprocess="to-pixdata" alias="256x256@2/apps/com.mitchellh.ghostty.png">images/icons/icon_256x256@2x@2x.png</file>
<file preprocess="to-pixdata" alias="512x512/apps/com.mitchellh.ghostty.png">images/icons/icon_512x512.png</file>
</gresource>
</gresources>

124
src/apprt/gtk/gresource.zig Normal file
View File

@ -0,0 +1,124 @@
const std = @import("std");
const css_files = [_][]const u8{
"style.css",
"style-dark.css",
"style-hc.css",
"style-hc-dark.css",
};
const icons = [_]struct {
alias: []const u8,
source: []const u8,
}{
.{
.alias = "16x16",
.source = "16x16",
},
.{
.alias = "16x16@2",
.source = "16x16@2x@2x",
},
.{
.alias = "32x32",
.source = "32x32",
},
.{
.alias = "32x32@2",
.source = "32x32@2x@2x",
},
.{
.alias = "128x128",
.source = "128x128",
},
.{
.alias = "128x128@2",
.source = "128x128@2x@2x",
},
.{
.alias = "256x256",
.source = "256x256",
},
.{
.alias = "256x256@2",
.source = "256x256@2x@2x",
},
.{
.alias = "512x512",
.source = "512x512",
},
};
pub const gresource_xml_gtk = comptimeGenerateGResourceXML(false);
pub const gresource_xml_libadwaita = comptimeGenerateGResourceXML(true);
fn comptimeGenerateGResourceXML(comptime libadwaita: bool) []const u8 {
comptime {
@setEvalBranchQuota(13000);
var counter = std.io.countingWriter(std.io.null_writer);
try writeGResourceXML(libadwaita, &counter.writer());
var buf: [counter.bytes_written]u8 = undefined;
var stream = std.io.fixedBufferStream(&buf);
try writeGResourceXML(libadwaita, stream.writer());
return stream.getWritten();
}
}
fn writeGResourceXML(libadwaita: bool, writer: anytype) !void {
try writer.writeAll(
\\<?xml version="1.0" encoding="UTF-8"?>
\\<gresources>
\\
);
if (libadwaita) {
try writer.writeAll(
\\ <gresource prefix="/com/mitchellh/ghostty">
\\
);
for (css_files) |css_file| {
try writer.print(
" <file compressed=\"true\" alias=\"{s}\">src/apprt/gtk/{s}</file>\n",
.{ css_file, css_file },
);
}
try writer.writeAll(
\\ </gresource>
\\
);
}
try writer.writeAll(
\\ <gresource prefix="/com/mitchellh/ghostty/icons">
\\
);
for (icons) |icon| {
try writer.print(
" <file preprocess=\"to-pixdata\" alias=\"{s}/apps/com.mitchellh.ghostty.png\">images/icons/icon_{s}.png</file>\n",
.{ icon.alias, icon.source },
);
}
try writer.writeAll(
\\ </gresource>
\\</gresources>
\\
);
}
pub const dependencies_gtk = deps: {
var deps: [icons.len][]const u8 = undefined;
for (icons, 0..) |icon, i| {
deps[i] = std.fmt.comptimePrint("images/icons/icon_{s}.png", .{icon.source});
}
break :deps deps;
};
pub const dependencies_libadwaita = deps: {
var deps: [css_files.len + icons.len][]const u8 = undefined;
for (css_files, 0..) |css_file, i| {
deps[i] = std.fmt.comptimePrint("src/apprt/gtk/{s}", .{css_file});
}
for (icons, css_files.len..) |icon, i| {
deps[i] = std.fmt.comptimePrint("images/icons/icon_{s}.png", .{icon.source});
}
break :deps deps;
};