apprt/embedded: fix initialization on macOS

This commit is contained in:
Mitchell Hashimoto
2024-01-18 08:55:03 -08:00
parent df09c21103
commit 01afa293c8
4 changed files with 27 additions and 9 deletions

View File

@ -31,6 +31,7 @@ typedef void *ghostty_inspector_t;
// Enums are up top so we can reference them later.
typedef enum {
GHOSTTY_PLATFORM_INVALID,
GHOSTTY_PLATFORM_MACOS,
} ghostty_platform_e;
@ -354,8 +355,17 @@ typedef struct {
} ghostty_error_s;
typedef struct {
void *userdata;
void *nsview;
} ghostty_platform_macos_s;
typedef union {
ghostty_platform_macos_s macos;
} ghostty_platform_u;
typedef struct {
ghostty_platform_e platform_tag;
ghostty_platform_u platform;
void *userdata;
double scale_factor;
uint16_t font_size;
const char *working_directory;
@ -476,9 +486,6 @@ uintptr_t ghostty_surface_pwd(ghostty_surface_t, char *, uintptr_t);
ghostty_inspector_t ghostty_surface_inspector(ghostty_surface_t);
void ghostty_inspector_free(ghostty_surface_t);
bool ghostty_inspector_metal_init(ghostty_inspector_t, void *);
void ghostty_inspector_metal_render(ghostty_inspector_t, void *, void *);
bool ghostty_inspector_metal_shutdown(ghostty_inspector_t);
void ghostty_inspector_set_focus(ghostty_inspector_t, bool);
void ghostty_inspector_set_content_scale(ghostty_inspector_t, double, double);
void ghostty_inspector_set_size(ghostty_inspector_t, uint32_t, uint32_t);
@ -488,6 +495,12 @@ void ghostty_inspector_mouse_scroll(ghostty_inspector_t, double, double, ghostty
void ghostty_inspector_key(ghostty_inspector_t, ghostty_input_action_e, ghostty_input_key_e, ghostty_input_mods_e);
void ghostty_inspector_text(ghostty_inspector_t, const char *);
#ifdef __APPLE__
bool ghostty_inspector_metal_init(ghostty_inspector_t, void *);
void ghostty_inspector_metal_render(ghostty_inspector_t, void *, void *);
bool ghostty_inspector_metal_shutdown(ghostty_inspector_t);
#endif
// APIs I'd like to get rid of eventually but are still needed for now.
// Don't use these unless you know what you're doing.
void ghostty_set_window_background_blur(ghostty_surface_t, void *);

View File

@ -277,8 +277,11 @@ extension Ghostty {
/// in the returned struct is only valid as long as this struct is retained.
func ghosttyConfig(view: SurfaceView) -> ghostty_surface_config_s {
var config = ghostty_surface_config_new()
config.platform_tag = GHOSTTY_PLATFORM_MACOS
config.platform = ghostty_platform_u(macos: ghostty_platform_macos_s(
nsview: Unmanaged.passUnretained(view).toOpaque()
))
config.userdata = Unmanaged.passUnretained(view).toOpaque()
config.nsview = Unmanaged.passUnretained(view).toOpaque()
config.scale_factor = NSScreen.main!.backingScaleFactor
if let fontSize = fontSize { config.font_size = fontSize }

View File

@ -283,14 +283,14 @@ pub const Surface = struct {
inspector: ?*Inspector = null,
pub const Options = extern struct {
/// Userdata passed to some of the callbacks.
userdata: ?*anyopaque = null,
/// The platform that this surface is being initialized for and
/// the associated platform-specific configuration.
platform_tag: c_int = 0,
platform: Platform.C = undefined,
/// Userdata passed to some of the callbacks.
userdata: ?*anyopaque = null,
/// The scale factor of the screen.
scale_factor: f64 = 1,

View File

@ -466,7 +466,9 @@ pub fn finalizeSurfaceInit(self: *const Metal, surface: *apprt.Surface) !void {
},
apprt.embedded => .{
.view = surface.nsview,
.view = switch (surface.platform) {
.macos => |v| v.nsview,
},
.scaleFactor = @floatCast(surface.content_scale.x),
},