[SOLVED] Big Sur - Fix for automatically adjusting TrayIcon to menubar color lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- Hansaplast · 2020-11-22 15:26 With Big Sur, one can no longer rely on dark vs light theme to determine if an TrayIcon should be using light or dark colors. The system wide menubar is by default transparent and the color and type of wallpaper influences if the background turns out to be light or dark. After a long search I found that the image of a trayicon (NSStatusItem?) can have an option to do this automatically by using the setTemplate function of the image to true. When doing this, macOS will automatically adjust a transparent icon with only black pixels. This can be activated relatively easy by adding a single line to the file lazarus/lcl/interfaces/cocoa/cocoatrayicon.inc and simply setting the image to a "template". In cocoatrayicon.inc add the following line to the lclSetTrayIcon procedure: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i image.setTemplate(true); I've added it below to the if (ATrayIcon.icon <> nil) section (line 14): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCocoaStatusItemHandle.lclSetTrayIcon(ATrayIcon: TCustomTrayIcon);var  image: NSImage;begin  TrayIcon := ATrayIcon;    // Shows the icon    if (ATrayIcon.icon <> nil) and (ATrayIcon.icon.Handle <> 0) then  begin    image := TCocoaBitmap(ATrayIcon.icon.Handle).image;    image.setTemplate(true); // < --- added this line    if image <> nil then statusitem.setImage(image);  end;    // Show the menu    if (ATrayIcon.PopUpMenu <> nil) then  begin    ATrayIcon.PopUpMenu.HandleNeeded();    //ATrayIcon.PopUpMenu.    if Assigned(statusitem.menu) and (statusitem.menu.delegate = NSMenuDelegateProtocol(self)) then      statusitem.menu.setDelegate(nil);      statusitem.setMenu(TCocoaMenu(ATrayIcon.PopUpMenu.Handle));      TCocoaMenu(ATrayIcon.PopUpMenu.Handle).setDelegate( self);  end;end; I've done some testing with this and this work great. Downside: when using trayicons with multiple colors, this may result in not so pretty icons. So my questions: Since this only works well with transparent icons with black pixels, this may cause issues with multicolor icons. So it is not safe to assume this is fine (even though it seems that macOS is steering towards simple icons like that), or is it? Can we add this as a macOS-only option for the TTrayIcon? Or does anyone know how to override the TCocoaStatusItemHandle.lclSetTrayIcon procedure? ---------------------------------------------------------------------- Hansaplast · 2020-11-23 12:07 Maybe this is useful for others, if we do not wish to make this an option for TTrayIcon; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Uses ... CocoaGDIObjects ...;  ...   TForm1 = class(TForm)     ...     TrayIcon1: TTrayIcon;     ...  private    public    end;                              ... procedure TForm1.FormCreate(Sender: TObject);var  image: NSImage;begin  if Assigned(TrayIcon1.Icon) then  begin      image := TCocoaBitmap(TrayIcon1.icon.Handle).image;      image.setTemplate(true);      TrayIcon1.InternalUpdate;    end;end;