From 017da411f8935c6b249ff2d2b9f4fb89751854c3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 3 Jul 2023 17:59:50 -0700 Subject: [PATCH] metal: start setting up background transparency --- src/apprt/glfw.zig | 2 +- src/config.zig | 5 +++++ src/renderer/Metal.zig | 11 ++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index 47bf9c422..4863e5511 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -278,7 +278,7 @@ pub const Surface = struct { "ghostty", null, null, - Renderer.glfwWindowHints(), + Renderer.glfwWindowHints(&app.config), ) orelse return glfw.mustGetErrorCode(); errdefer win.destroy(); diff --git a/src/config.zig b/src/config.zig index 63038c74a..e5a8d7871 100644 --- a/src/config.zig +++ b/src/config.zig @@ -65,6 +65,11 @@ pub const Config = struct { /// The opacity level (opposite of transparency) of the background. /// A value of 1 is fully opaque and a value of 0 is fully transparent. + /// A value less than 0 or greater than 1 will be clamped to the nearest + /// valid value. + /// + /// This can be changed at runtime for native macOS and Linux GTK builds. + /// This can NOT be changed at runtime for GLFW builds (not common). @"background-opacity": f64 = 1.0, /// The command to run, usually a shell. If this is not an absolute path, diff --git a/src/renderer/Metal.zig b/src/renderer/Metal.zig index 7d75219bf..c955c5bbc 100644 --- a/src/renderer/Metal.zig +++ b/src/renderer/Metal.zig @@ -132,6 +132,7 @@ pub const DerivedConfig = struct { font_thicken: bool, cursor_color: ?terminal.color.RGB, background: terminal.color.RGB, + background_opacity: f64, foreground: terminal.color.RGB, selection_background: ?terminal.color.RGB, selection_foreground: ?terminal.color.RGB, @@ -143,6 +144,7 @@ pub const DerivedConfig = struct { _ = alloc_gpa; return .{ + .background_opacity = @max(0, @min(1, config.@"background-opacity")), .font_thicken = config.@"font-thicken", .cursor_color = if (config.@"cursor-color") |col| @@ -171,11 +173,10 @@ pub const DerivedConfig = struct { }; /// Returns the hints that we want for this -pub fn glfwWindowHints() glfw.Window.Hints { +pub fn glfwWindowHints(config: *const configpkg.Config) glfw.Window.Hints { return .{ .client_api = .no_api, - // .cocoa_graphics_switching = builtin.os.tag == .macos, - // .cocoa_retina_framebuffer = true, + .transparent_framebuffer = config.@"background-opacity" < 1, }; } @@ -196,7 +197,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !Metal { const CAMetalLayer = objc.Class.getClass("CAMetalLayer").?; const swapchain = CAMetalLayer.msgSend(objc.Object, objc.sel("layer"), .{}); swapchain.setProperty("device", device.value); - swapchain.setProperty("opaque", true); + swapchain.setProperty("opaque", options.config.background_opacity >= 1); // disable v-sync swapchain.setProperty("displaySyncEnabled", false); @@ -628,7 +629,7 @@ pub fn render( .red = @as(f32, @floatFromInt(critical.bg.r)) / 255, .green = @as(f32, @floatFromInt(critical.bg.g)) / 255, .blue = @as(f32, @floatFromInt(critical.bg.b)) / 255, - .alpha = 1.0, + .alpha = self.config.background_opacity, }); }