From c70b1817eeb8b19835f38311e47a1285e538c867 Mon Sep 17 00:00:00 2001 From: foxnne Date: Thu, 12 Dec 2024 11:10:42 -0600 Subject: [PATCH] core: `window.color` -> `window.transparent`, `window.decorated`, `window.decoration_color` --- examples/core-transparent-window/App.zig | 11 +++- src/Core.zig | 13 +++- src/core/Darwin.zig | 83 ++++++++---------------- 3 files changed, 46 insertions(+), 61 deletions(-) diff --git a/examples/core-transparent-window/App.zig b/examples/core-transparent-window/App.zig index fcb20b07..9160d526 100644 --- a/examples/core-transparent-window/App.zig +++ b/examples/core-transparent-window/App.zig @@ -32,6 +32,7 @@ pub fn init( const window = try core.windows.new(.{ .title = "core-transparent-window", .vsync_mode = .double, + .transparent = true, }); // Store our render pipeline in our module's state, so we can access it later on. @@ -124,10 +125,10 @@ pub fn tick(app: *App, core: *mach.Core) void { defer encoder.release(); // Begin render pass - const sky_blue_background = gpu.Color{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 0.0 }; + const transparent_background = gpu.Color{ .r = 0.0, .g = 0.0, .b = 0.0, .a = 0.0 }; const color_attachments = [_]gpu.RenderPassColorAttachment{.{ .view = back_buffer_view, - .clear_value = sky_blue_background, + .clear_value = transparent_background, .load_op = .clear, .store_op = .store, }}; @@ -172,7 +173,11 @@ pub fn tick(app: *App, core: *mach.Core) void { const green = mach.math.lerp(0.2, 0.6, mach.math.clamp(app.color_time - 2.0, 0.0, 1.0)); const alpha = mach.math.lerp(0.3, 1.0, app.color_time / 4.0); - core.windows.set(app.window, .color, .{ .transparent = .{ .color = .{ .r = red, .g = green, .b = blue, .a = alpha }, .titlebar = true } }); + core.windows.set( + app.window, + .decoration_color, + .{ .r = red, .g = green, .b = blue, .a = alpha }, + ); } pub fn deinit(app: *App) void { diff --git a/src/Core.zig b/src/Core.zig index 9f61db96..2803c624 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -74,8 +74,17 @@ windows: mach.Objects( /// Target frames per second refresh_rate: u32 = 0, - /// Color of the window background/titlebar - color: WindowColor = .system, + /// Titlebar/window decorations + decorated: bool = true, + + /// Color of the window decorations, i.e. titlebar + /// if null, decoration is the system-determined color + decoration_color: ?gpu.Color = null, + + /// Whether the window should be completely transparent + /// or not. On macOS, to achieve a fully transparent window + /// decoration_color must also be set fully transparent. + transparent: bool = false, // GPU // When `native` is not null, the rest of the fields have been diff --git a/src/core/Darwin.zig b/src/core/Darwin.zig index 05e2bd22..d99c9f4a 100644 --- a/src/core/Darwin.zig +++ b/src/core/Darwin.zig @@ -70,36 +70,19 @@ pub fn tick(core: *Core) !void { if (core_window.native) |native| { const native_window: *objc.app_kit.Window = native.window; - const native_view: *objc.mach.View = native.view; - 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); - native_view.layer().setOpaque(false); - }, - .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); - native_view.layer().setOpaque(true); - }, - .system => { - native_window.setTitlebarAppearsTransparent(false); - native_view.layer().setOpaque(true); - }, + if (core.windows.updated(window_id, .decoration_color)) { + if (core_window.decoration_color) |decoration_color| { + const color = objc.app_kit.Color.colorWithRed_green_blue_alpha( + decoration_color.r, + decoration_color.g, + decoration_color.b, + decoration_color.a, + ); + native_window.setBackgroundColor(color); + native_window.setTitlebarAppearsTransparent(true); + } else { + native_window.setTitlebarAppearsTransparent(false); } } @@ -135,7 +118,7 @@ fn initWindow( const layer = objc.quartz_core.MetalLayer.new(); defer layer.release(); - if (core_window.color == .transparent) layer.setOpaque(false); + if (core_window.transparent) layer.setOpaque(false); metal_descriptor.* = .{ .layer = layer, @@ -151,11 +134,11 @@ fn initWindow( const window_style = (if (core_window.display_mode == .fullscreen) objc.app_kit.WindowStyleMaskFullScreen else 0) | - (if (core_window.display_mode == .windowed) objc.app_kit.WindowStyleMaskTitled else 0) | - (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); + (if (core_window.decorated) objc.app_kit.WindowStyleMaskTitled else 0) | + (if (core_window.decorated) objc.app_kit.WindowStyleMaskClosable else 0) | + (if (core_window.decorated) objc.app_kit.WindowStyleMaskMiniaturizable else 0) | + (if (core_window.decorated) objc.app_kit.WindowStyleMaskResizable else 0) | + (if (!core_window.decorated) 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, @@ -254,27 +237,15 @@ fn initWindow( native_window.setIsVisible(true); native_window.makeKeyAndOrderFront(null); - 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 => {}, + if (core_window.decoration_color) |decoration_color| { + const color = objc.app_kit.Color.colorWithRed_green_blue_alpha( + decoration_color.r, + decoration_color.g, + decoration_color.b, + decoration_color.a, + ); + native_window.setBackgroundColor(color); + native_window.setTitlebarAppearsTransparent(true); } const string = objc.foundation.String.allocInit();