From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jon Rubens Newsgroups: gmane.emacs.devel Subject: Re: macOS (Cocoa) alpha-background Date: Wed, 24 Jan 2024 15:25:26 -0800 Message-ID: References: <87ttn4spio.fsf@yahoo.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33232"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Po Lu , Jon Rubens Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Jan 25 08:08:04 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rStqF-0008Ts-FW for ged-emacs-devel@m.gmane-mx.org; Thu, 25 Jan 2024 08:08:03 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rStpY-0004kW-Vn; Thu, 25 Jan 2024 02:07:22 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rSmch-0004k3-93 for emacs-devel@gnu.org; Wed, 24 Jan 2024 18:25:35 -0500 Original-Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rSmce-0008B1-8y for emacs-devel@gnu.org; Wed, 24 Jan 2024 18:25:34 -0500 Original-Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1d50d0c98c3so1478235ad.1 for ; Wed, 24 Jan 2024 15:25:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706138728; x=1706743528; darn=gnu.org; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=BhptS+b5c+IN+TW0ngaFacFrVj+9okj95BhBy0/5Z+8=; b=h+qiehiNDXHf0vfhgvRWzFB7VvUdJ39JBISBlgbje6hqeG5AytuoqT/6oK8vhlCu0K d3tVN3Pqqz4+dpZvyJFeLETkE33oXju+JIH8vM6tymzkYd0At8JJwmBA8X0RuIvpLRXK vCNDSpVYxDVrX7fyDG9ZRJapngW9O22w6+G1YbgpfsLftJAjSJfv2qW/mlsgNmWfwWd/ 3fXxP47/qCcPptdEpzUao442X8kUatte3TourWpsmfQz9UQiah3uc5461PE0FpDj4K3j eCPbZV/ndl9IYYdynO3ZH1qJhingY9ObJfVpajIkui/xM8kltV7wN2dmbdMu0OKlEZ9T VDaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706138728; x=1706743528; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BhptS+b5c+IN+TW0ngaFacFrVj+9okj95BhBy0/5Z+8=; b=hA0fk1ht0cyLLFnHtAUqHXkjpz2rKJ3Vmo7Mp7voZUcrEiLJ/Kcw1w1BZ+wJ5ykS+U czXwfvRb9uzd10qlJlDeRQVSEOGCA6+BFcZ1OlnqRN/ulacJ9HgPWOLcfMGdrKzm6b1K TioltOFI0IOuwBUioDWVk0mTW9I688DkL8luFk3QGptr3LjICcLr+DAkZyVJLS03l5Wu Chw7zHLMBYxr3Dc/NoT/6sgy6kmGyfuuFspku4dCCYMAUEyzSTtLSEGAPULuL/KwQgLp URi3kOGTSVnQMeA8Nd3hZq/Sg7hJjSN4uR2M5daGQkymzZRXQWIhhApAW/JViw9w3FeF Wo8g== X-Gm-Message-State: AOJu0YyscZ7vlbtpuzSEvUzn0kPjXL37vrRSvMIuhO0Ptb4apnIFy7MO EIq9m1We+XMLB5bGTJhIhcc6Obgc8dasIoFHHAUCmV/i2gfxY4zH X-Google-Smtp-Source: AGHT+IEFt9Y2NBGiF4C32hH+EggPNrcaYEtBfITd+nEtHJtcq/hDaHtcrQXu2XwqcPeMNTugBev9wg== X-Received: by 2002:a17:902:a716:b0:1d7:55b1:4a3 with SMTP id w22-20020a170902a71600b001d755b104a3mr403091plq.11.1706138728342; Wed, 24 Jan 2024 15:25:28 -0800 (PST) Original-Received: from localhost (135-180-5-217.fiber.dynamic.sonic.net. [135.180.5.217]) by smtp.gmail.com with ESMTPSA id l12-20020a170902d04c00b001d73416e65bsm7192035pll.63.2024.01.24.15.25.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 15:25:27 -0800 (PST) X-Google-Original-From: Jon Rubens In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=jonathan.rubens@gmail.com; helo=mail-pl1-x636.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Thu, 25 Jan 2024 02:07:14 -0500 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:315338 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I took a deep dive on this and have got it working. I don=E2=80=99t see any obvious issues in this patched version but I=E2=80=99m certain there are ed= ge cases I haven=E2=80=99t tested. I haven=E2=80=99t tried modifying cursor colors or fringes or even diverse face backgrounds (I=E2=80=99ve only been working with default face), but th= is works for MacOS users. Only open question I have to look into: - The alpha on the ns transparent titlebar is off. I need to determine exac= tly where I set that. Cheers, --=20 Jon Rubens --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-ns_alpha_background.patch diff --git a/src/macfont.m b/src/macfont.m index 8aba440d196..56c1eb57024 100644 --- a/src/macfont.m +++ b/src/macfont.m @@ -2953,9 +2953,14 @@ So we use CTFontDescriptorCreateMatchingFontDescriptor (no CG_SET_FILL_COLOR_WITH_FACE_FOREGROUND (context, face); else CG_SET_FILL_COLOR_WITH_FRAME_CURSOR (context, f); - } + CGContextSetAlpha(context, 1); + } else - CG_SET_FILL_COLOR_WITH_FACE_BACKGROUND (context, face); + { + CGContextSetAlpha(context, f->alpha_background); + CG_SET_FILL_COLOR_WITH_FACE_BACKGROUND (context, face); + } + CGContextClearRect(context, background_rect); CGContextFillRects (context, &background_rect, 1); } @@ -2964,6 +2969,7 @@ So we use CTFontDescriptorCreateMatchingFontDescriptor (no CGAffineTransform atfm; CGContextScaleCTM (context, 1, -1); + CGContextSetAlpha(context, 1); if (s->hl == DRAW_CURSOR) { if (face && (NS_FACE_BACKGROUND (face) diff --git a/src/nsfns.m b/src/nsfns.m index b0281aac257..37b21e526e7 100644 --- a/src/nsfns.m +++ b/src/nsfns.m @@ -301,7 +301,7 @@ Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side. struct face *face; NSColor *col; NSView *view = FRAME_NS_VIEW (f); - EmacsCGFloat alpha; + EmacsCGFloat alpha = f->alpha_background; block_input (); if (ns_lisp_to_color (arg, &col)) @@ -316,11 +316,10 @@ Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side. f->output_data.ns->background_color = col; FRAME_BACKGROUND_PIXEL (f) = [col unsignedLong]; - alpha = [col alphaComponent]; if (view != nil) { - [[view window] setBackgroundColor: col]; + [[view window] setBackgroundColor: [col colorWithAlphaComponent: alpha]]; if (alpha != (EmacsCGFloat) 1.0) [[view window] setOpaque: NO]; @@ -330,10 +329,7 @@ Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side. face = FRAME_DEFAULT_FACE (f); if (face) { - col = [NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)]; - face->background = [[col colorWithAlphaComponent: alpha] - unsignedLong]; - + face->background = [col unsignedLong]; update_face_from_frame_parameter (f, Qbackground_color, arg); } @@ -346,6 +342,36 @@ Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side. unblock_input (); } +static void +ns_set_alpha_background (struct frame *f, Lisp_Object arg, Lisp_Object oldval) +{ + NSView *view = FRAME_NS_VIEW (f); + double alpha = 1.0; + + if (NILP (arg)) + alpha = 1.0; + else if (FLOATP (arg)) + { + alpha = XFLOAT_DATA (arg); + if (! (0 <= alpha && alpha <= 1.0)) + args_out_of_range (make_float (0.0), make_float (1.0)); + } + else if (FIXNUMP (arg)) + { + EMACS_INT ialpha = XFIXNUM (arg); + if (! (0 <= ialpha && ialpha <= 100)) + args_out_of_range (make_fixnum (0), make_fixnum (100)); + alpha = ialpha / 100.0; + } + else + wrong_type_argument (Qnumberp, arg); + + f->alpha_background = alpha; + [[view window] setBackgroundColor: [f->output_data.ns->background_color colorWithAlphaComponent: alpha]]; + + recompute_basic_faces (f); + SET_FRAME_GARBAGED (f); +} static void ns_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval) @@ -1065,7 +1091,7 @@ Turn the input menu (an NSMenu) into a lisp list for tracking on lisp side. ns_set_z_group, 0, /* x_set_override_redirect */ gui_set_no_special_glyphs, - gui_set_alpha_background, + ns_set_alpha_background, NULL, #ifdef NS_IMPL_COCOA ns_set_appearance, diff --git a/src/nsfont.m b/src/nsfont.m index 1205fbe5263..9da964956e1 100644 --- a/src/nsfont.m +++ b/src/nsfont.m @@ -1185,7 +1185,7 @@ is false when (FROM > 0 || TO < S->nchars). */ { if (s->hl != DRAW_CURSOR) [(NS_FACE_BACKGROUND (face) != 0 - ? [NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] + ? [[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] colorWithAlphaComponent: s->f->alpha_background] : FRAME_BACKGROUND_COLOR (s->f)) set]; else [FRAME_CURSOR_COLOR (s->f) set]; diff --git a/src/nsterm.m b/src/nsterm.m index 518b38658d1..bda3a12172f 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -2618,8 +2618,9 @@ Hide the window (X11 semantics) block_input (); ns_focus (f, &r, 1); - [[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND - (FACE_FROM_ID (f, DEFAULT_FACE_ID))] set]; + [[[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND + (FACE_FROM_ID (f, DEFAULT_FACE_ID))] + colorWithAlphaComponent: f->alpha_background] set]; NSRectFill (r); ns_unfocus (f); @@ -2647,7 +2648,7 @@ Hide the window (X11 semantics) r = NSIntersectionRect (r, [view frame]); ns_focus (f, &r, 1); - [[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] set]; + [[[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] colorWithAlphaComponent: f->alpha_background] set]; NSRectFill (r); @@ -2751,7 +2752,7 @@ Hide the window (X11 semantics) return; ns_focus (f, NULL, 1); - [[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] set]; + [[[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] colorWithAlphaComponent: f->alpha_background] set]; NSRectFill (NSMakeRect (0, margin, width, border)); NSRectFill (NSMakeRect (0, 0, border, height)); NSRectFill (NSMakeRect (0, margin, width, border)); @@ -2802,7 +2803,7 @@ Hide the window (X11 semantics) NSRect r = NSMakeRect (0, y, FRAME_PIXEL_WIDTH (f), height); ns_focus (f, &r, 1); - [[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] set]; + [[[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] colorWithAlphaComponent: f->alpha_background] set]; NSRectFill (NSMakeRect (0, y, width, height)); NSRectFill (NSMakeRect (FRAME_PIXEL_WIDTH (f) - width, y, width, height)); @@ -2966,8 +2967,7 @@ Hide the window (X11 semantics) if (! NSIsEmptyRect (clearRect)) { NSTRACE_RECT ("clearRect", clearRect); - - [[NSColor colorWithUnsignedLong:face->background] set]; + [[[NSColor colorWithUnsignedLong:face->background] colorWithAlphaComponent: f->alpha_background] set]; NSRectFill (clearRect); } @@ -2998,7 +2998,7 @@ Hide the window (X11 semantics) else bm_color = f->output_data.ns->cursor_color; - [bm_color set]; + [[bm_color colorWithAlphaComponent:f->alpha_background] set]; [bmp fill]; [bmp release]; @@ -3719,7 +3719,7 @@ Function modeled after x_draw_glyph_string_box (). if (s->face->box == FACE_SIMPLE_BOX && s->face->box_color) { ns_draw_box (r, abs (hthickness), abs (vthickness), - [NSColor colorWithUnsignedLong:face->box_color], + [[NSColor colorWithUnsignedLong:face->box_color] colorWithAlphaComponent: s->f->alpha_background], left_p, right_p); } else @@ -3757,8 +3757,10 @@ Function modeled after x_draw_glyph_string_box (). { if (s->hl != DRAW_CURSOR) [(NS_FACE_BACKGROUND (face) != 0 - ? [NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] + ? [[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] + colorWithAlphaComponent: s->f->alpha_background] : FRAME_BACKGROUND_COLOR (s->f)) set]; + else if (face && (NS_FACE_BACKGROUND (face) == [(NSColor *) FRAME_CURSOR_COLOR (s->f) unsignedLong])) @@ -3902,7 +3904,7 @@ Function modeled after x_draw_glyph_string_box (). otherwise, since we composite the image under NS (instead of mucking with its background color), we must clear just the image area. */ - [[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] set]; + [[[NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND (face)] colorWithAlphaComponent: s->f->alpha_background] set]; if (bg_height > s->slice.height || s->img->hmargin || s->img->vmargin || s->img->mask || s->img->pixmap == 0 || s->width != s->background_width) @@ -3972,7 +3974,7 @@ Function modeled after x_draw_glyph_string_box (). if (s->hl == DRAW_CURSOR) { [FRAME_CURSOR_COLOR (s->f) set]; - tdCol = [NSColor colorWithUnsignedLong: NS_FACE_BACKGROUND (face)]; + tdCol = [[NSColor colorWithUnsignedLong: NS_FACE_BACKGROUND (face)] colorWithAlphaComponent: s->f->alpha_background]; } else tdCol = [NSColor colorWithUnsignedLong: NS_FACE_FOREGROUND (face)]; @@ -4066,10 +4068,10 @@ Function modeled after x_draw_glyph_string_box (). face = FACE_FROM_ID (s->f, MOUSE_FACE_ID); prepare_face_for_display (s->f, face); - [[NSColor colorWithUnsignedLong: face->background] set]; + [[[NSColor colorWithUnsignedLong: face->background] colorWithAlphaComponent: s->f->alpha_background] set]; } else - [[NSColor colorWithUnsignedLong: s->face->background] set]; + [[[NSColor colorWithUnsignedLong: s->face->background] colorWithAlphaComponent: s->f->alpha_background] set]; NSRectFill (NSMakeRect (x, y, w, h)); } } @@ -4095,7 +4097,7 @@ Function modeled after x_draw_glyph_string_box (). if (s->hl == DRAW_CURSOR) [FRAME_CURSOR_COLOR (s->f) set]; else - [[NSColor colorWithUnsignedLong: s->face->background] set]; + [[[NSColor colorWithUnsignedLong: s->face->background] colorWithAlphaComponent: s->f->alpha_background] set]; NSRectFill (NSMakeRect (x, s->y, background_width, s->height)); } @@ -8436,8 +8438,8 @@ - (void)toggleFullScreen: (id)sender } [w setContentView:[fw contentView]]; - [w setBackgroundColor: col]; - if ([col alphaComponent] != (EmacsCGFloat) 1.0) + [w setBackgroundColor: [col colorWithAlphaComponent: f->alpha_background]]; + if (f->alpha_background != (EmacsCGFloat) 1.0) [w setOpaque: NO]; f->border_width = [w borderWidth]; @@ -9172,9 +9174,9 @@ - (instancetype) initWithEmacsFrame: (struct frame *) f f->border_width = [self borderWidth]; col = [NSColor colorWithUnsignedLong:NS_FACE_BACKGROUND - (FACE_FROM_ID (f, DEFAULT_FACE_ID))]; - [self setBackgroundColor:col]; - if ([col alphaComponent] != (EmacsCGFloat) 1.0) + (FACE_FROM_ID (f, DEFAULT_FACE_ID))]; + [self setBackgroundColor:[col colorWithAlphaComponent:f->alpha_background]]; + if (f->alpha_background != (EmacsCGFloat) 1.0) [self setOpaque:NO]; /* toolbar support */ --=-=-=--