apprt/embedded: add iOS platform with uivew

This commit is contained in:
Mitchell Hashimoto
2024-01-18 15:03:03 -08:00
parent 5fe3900efa
commit 26e6e8cec8
3 changed files with 25 additions and 0 deletions

View File

@ -33,6 +33,7 @@ typedef void *ghostty_inspector_t;
typedef enum { typedef enum {
GHOSTTY_PLATFORM_INVALID, GHOSTTY_PLATFORM_INVALID,
GHOSTTY_PLATFORM_MACOS, GHOSTTY_PLATFORM_MACOS,
GHOSTTY_PLATFORM_IOS,
} ghostty_platform_e; } ghostty_platform_e;
typedef enum { typedef enum {
@ -358,8 +359,13 @@ typedef struct {
void *nsview; void *nsview;
} ghostty_platform_macos_s; } ghostty_platform_macos_s;
typedef struct {
void *uiview;
} ghostty_platform_ios_s;
typedef union { typedef union {
ghostty_platform_macos_s macos; ghostty_platform_macos_s macos;
ghostty_platform_ios_s ios;
} ghostty_platform_u; } ghostty_platform_u;
typedef struct { typedef struct {

View File

@ -234,6 +234,7 @@ pub const App = struct {
/// Platform-specific configuration for libghostty. /// Platform-specific configuration for libghostty.
pub const Platform = union(PlatformTag) { pub const Platform = union(PlatformTag) {
macos: MacOS, macos: MacOS,
ios: IOS,
// If our build target for libghostty is not darwin then we do // If our build target for libghostty is not darwin then we do
// not include macos support at all. // not include macos support at all.
@ -242,12 +243,21 @@ pub const Platform = union(PlatformTag) {
nsview: objc.Object, nsview: objc.Object,
} else void; } else void;
pub const IOS = if (builtin.target.isDarwin()) struct {
/// The view to render the surface on.
uiview: objc.Object,
} else void;
// The C ABI compatible version of this union. The tag is expected // The C ABI compatible version of this union. The tag is expected
// to be stored elsewhere. // to be stored elsewhere.
pub const C = extern union { pub const C = extern union {
macos: extern struct { macos: extern struct {
nsview: ?*anyopaque, nsview: ?*anyopaque,
}, },
ios: extern struct {
uiview: ?*anyopaque,
},
}; };
/// Initialize a Platform a tag and configuration from the C ABI. /// Initialize a Platform a tag and configuration from the C ABI.
@ -260,6 +270,13 @@ pub const Platform = union(PlatformTag) {
break :macos error.NSViewMustBeSet); break :macos error.NSViewMustBeSet);
break :macos .{ .macos = .{ .nsview = nsview } }; break :macos .{ .macos = .{ .nsview = nsview } };
} else error.UnsupportedPlatform, } else error.UnsupportedPlatform,
.ios => if (IOS != void) ios: {
const config = c_platform.ios;
const uiview = objc.Object.fromId(config.uiview orelse
break :ios error.UIViewMustBeSet);
break :ios .{ .ios = .{ .uiview = uiview } };
} else error.UnsupportedPlatform,
}; };
} }
}; };
@ -269,6 +286,7 @@ pub const PlatformTag = enum(c_int) {
// from the C API. // from the C API.
macos = 1, macos = 1,
ios = 2,
}; };
pub const Surface = struct { pub const Surface = struct {

View File

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