example: core-transparent-window now animates the window color and transparency

This commit is contained in:
foxnne 2024-12-05 15:55:00 -06:00 committed by Emi Gutekanst
parent 4bbca0eb95
commit 6ef58d8c1f
3 changed files with 98 additions and 68 deletions

View file

@ -74,6 +74,9 @@ windows: mach.Objects(
/// Target frames per second
refresh_rate: u32 = 0,
/// Color of the window background/titlebar
color: WindowColor = .system,
// GPU
// When `native` is not null, the rest of the fields have been
// initialized.
@ -657,6 +660,20 @@ pub const InputState = struct {
}
};
pub const WindowColor = union(enum) {
system: void, // Default window colors
transparent: struct {
color: gpu.Color,
// If true, and the OS supports it, the titlebar will also be set to color
titlebar: bool = false,
},
solid: struct {
color: gpu.Color,
// If titlebar is true, and the OS supports it, the titlebar will also be set to color
titlebar: bool = false,
},
};
pub const Event = union(enum) {
key_press: KeyEvent,
key_repeat: KeyEvent,
@ -957,39 +974,6 @@ const RequestAdapterResponse = struct {
message: ?[*:0]const u8,
};
// Verifies that a platform implementation exposes the expected function declarations.
// comptime {
// // Core
// assertHasField(Platform, "surface_descriptor");
// assertHasField(Platform, "refresh_rate");
// assertHasDecl(Platform, "init");
// assertHasDecl(Platform, "deinit");
// assertHasDecl(Platform, "setTitle");
// assertHasDecl(Platform, "setDisplayMode");
// assertHasField(Platform, "display_mode");
// assertHasDecl(Platform, "setBorder");
// assertHasField(Platform, "border");
// assertHasDecl(Platform, "setHeadless");
// assertHasField(Platform, "headless");
// assertHasDecl(Platform, "setVSync");
// assertHasField(Platform, "vsync_mode");
// assertHasDecl(Platform, "setSize");
// assertHasField(Platform, "size");
// assertHasDecl(Platform, "setCursorMode");
// assertHasField(Platform, "cursor_mode");
// assertHasDecl(Platform, "setCursorShape");
// assertHasField(Platform, "cursor_shape");
// }
fn assertHasDecl(comptime T: anytype, comptime decl_name: []const u8) void {
if (!@hasDecl(T, decl_name)) @compileError(@typeName(T) ++ " missing declaration: " ++ decl_name);
}

View file

@ -70,6 +70,34 @@ pub fn tick(core: *Core) !void {
if (core_window.native) |native| {
const native_window: *objc.app_kit.Window = native.window;
if (core.windows.updated(window_id, .color)) {
switch (core_window.color) {
.transparent => |wc| {
const color = objc.app_kit.Color.colorWithRed_green_blue_alpha(
wc.color.r,
wc.color.g,
wc.color.b,
wc.color.a,
);
native_window.setBackgroundColor(color);
native_window.setTitlebarAppearsTransparent(true);
},
.solid => |wc| {
const color = objc.app_kit.Color.colorWithRed_green_blue_alpha(
wc.color.r,
wc.color.g,
wc.color.b,
wc.color.a,
);
native_window.setBackgroundColor(color);
native_window.setTitlebarAppearsTransparent(false);
},
.system => {
native_window.setTitlebarAppearsTransparent(false);
},
}
}
if (core.windows.updated(window_id, .title)) {
const string = objc.foundation.String.allocInit();
defer string.release();
@ -123,6 +151,7 @@ fn initWindow(
(if (core_window.display_mode == .windowed) objc.app_kit.WindowStyleMaskClosable else 0) |
(if (core_window.display_mode == .windowed) objc.app_kit.WindowStyleMaskMiniaturizable else 0) |
(if (core_window.display_mode == .windowed) objc.app_kit.WindowStyleMaskResizable else 0);
// (if (core_window.display_mode == .windowed) objc.app_kit.WindowStyleMaskFullSizeContentView else 0);
const native_window_opt: ?*objc.app_kit.Window = objc.app_kit.Window.alloc().initWithContentRect_styleMask_backing_defer_screen(
rect,
@ -163,9 +192,28 @@ fn initWindow(
native_window.setIsVisible(true);
native_window.makeKeyAndOrderFront(null);
const color = objc.app_kit.Color.colorWithRed_green_blue_alpha(0.5, 0.5, 0.5, 0.5);
native_window.setBackgroundColor(color);
native_window.setTitlebarAppearsTransparent(true);
switch (core_window.color) {
.transparent => |wc| {
const color = objc.app_kit.Color.colorWithRed_green_blue_alpha(
wc.color.r,
wc.color.g,
wc.color.b,
wc.color.a,
);
native_window.setBackgroundColor(color);
native_window.setTitlebarAppearsTransparent(true);
},
.solid => |wc| {
const color = objc.app_kit.Color.colorWithRed_green_blue_alpha(
wc.color.r,
wc.color.g,
wc.color.b,
wc.color.a,
);
native_window.setBackgroundColor(color);
},
.system => {},
}
const string = objc.foundation.String.allocInit();
defer string.release();