inspector is scaled to native dpi

This commit is contained in:
Mitchell Hashimoto
2023-10-21 15:08:16 -07:00
parent e62a8bbaae
commit 55f681a0fd
4 changed files with 44 additions and 13 deletions

View File

@ -31,6 +31,7 @@ pub fn build(b: *std.Build) !void {
defer flags.deinit();
try flags.appendSlice(&.{
"-DCIMGUI_FREETYPE=1",
"-DIMGUI_USE_WCHAR32=1",
"-DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1",
});
if (target.isWindows()) {

View File

@ -4,6 +4,7 @@
const Inspector = @This();
const cimgui = @import("cimgui");
const Surface = @import("Surface.zig");
/// Setup the ImGui state. This requires an ImGui context to be set.
pub fn setup() void {
@ -14,6 +15,25 @@ pub fn setup() void {
// Our colorspace is sRGB.
io.ConfigFlags |= cimgui.c.ImGuiConfigFlags_IsSRGB;
// Get our style
const style = cimgui.c.igGetStyle();
cimgui.c.ImGuiStyle_ScaleAllSizes(style, 2);
// Use our own embedded font
{
const font_config: *cimgui.c.ImFontConfig = cimgui.c.ImFontConfig_ImFontConfig();
defer cimgui.c.ImFontConfig_destroy(font_config);
font_config.FontDataOwnedByAtlas = false;
_ = cimgui.c.ImFontAtlas_AddFontFromMemoryTTF(
io.Fonts,
@constCast(@ptrCast(Surface.face_ttf)),
Surface.face_ttf.len,
24,
font_config,
null,
);
}
}
pub fn init() Inspector {

View File

@ -2466,7 +2466,7 @@ fn completeClipboardReadOSC52(self: *Surface, data: []const u8, kind: u8) !void
self.io_thread.wakeup.notify() catch {};
}
const face_ttf = @embedFile("font/res/FiraCode-Regular.ttf");
const face_bold_ttf = @embedFile("font/res/FiraCode-Bold.ttf");
const face_emoji_ttf = @embedFile("font/res/NotoColorEmoji.ttf");
const face_emoji_text_ttf = @embedFile("font/res/NotoEmoji-Regular.ttf");
pub const face_ttf = @embedFile("font/res/FiraCode-Regular.ttf");
pub const face_bold_ttf = @embedFile("font/res/FiraCode-Bold.ttf");
pub const face_emoji_ttf = @embedFile("font/res/NotoColorEmoji.ttf");
pub const face_emoji_text_ttf = @embedFile("font/res/NotoEmoji-Regular.ttf");

View File

@ -184,14 +184,17 @@ fn gtkResize(area: *c.GtkGLArea, width: c.gint, height: c.gint, ud: ?*anyopaque)
scale_factor,
});
io.DisplaySize = .{
.x = @floatFromInt(@divFloor(width, scale_factor)),
.y = @floatFromInt(@divFloor(height, scale_factor)),
};
io.DisplayFramebufferScale = .{
.x = @floatFromInt(scale_factor),
.y = @floatFromInt(scale_factor),
};
// Our display size is always unscaled. We'll do the scaling in the
// style instead. This creates crisper looking fonts.
io.DisplaySize = .{ .x = @floatFromInt(width), .y = @floatFromInt(height) };
io.DisplayFramebufferScale = .{ .x = 1, .y = 1 };
// Setup a new style and scale it appropriately.
const style = cimgui.c.ImGuiStyle_ImGuiStyle();
defer cimgui.c.ImGuiStyle_destroy(style);
cimgui.c.ImGuiStyle_ScaleAllSizes(style, @floatFromInt(scale_factor));
const active_style = cimgui.c.igGetStyle();
active_style.* = style.*;
}
fn gtkRender(area: *c.GtkGLArea, ctx: *c.GdkGLContext, ud: ?*anyopaque) callconv(.C) c.gboolean {
@ -231,7 +234,14 @@ fn gtkMouseMotion(
const self: *ImguiWidget = @ptrCast(@alignCast(ud.?));
cimgui.c.igSetCurrentContext(self.ig_ctx);
const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO();
cimgui.c.ImGuiIO_AddMousePosEvent(io, @floatCast(x), @floatCast(y));
const scale_factor: f64 = @floatFromInt(c.gtk_widget_get_scale_factor(
@ptrCast(self.gl_area),
));
cimgui.c.ImGuiIO_AddMousePosEvent(
io,
@floatCast(x * scale_factor),
@floatCast(y * scale_factor),
);
self.queueRender();
}