Seite 1 von 1

Systray-Beispiel erscheint im Dock

Verfasst: 27.04.2025 15:42
von Kiffi
Moin,

ich mache gerade meine ersten Schritte in der Entwicklung von MacOS Apps auf meinem Mac Mini M4.

Habe hierzu testweise den Source unter https://www.purebasic.com/documentation ... ay.pb.html ausprobiert.

Den Source habe ich zu einer App (ClipboardTool) kompiliert und diese dann gestartet.

Zusätzlich zum gewünschten Symbol in der Menüzeile erscheint die App allerdings auch im Dock. Wie kann ich das verhindern?

Überdies kommt beim Ausschalten meines Macs folgende Meldung:

Bild

Danke im Voraus & Grüße ... Peter

Re: Systray-Beispiel erscheint im Dock

Verfasst: 27.04.2025 21:14
von mk-soft
Da musste ich mal in meine CocoaMessage Kiste schauen ;)

Update

Code: Alles auswählen

;-TOP 
; By mk-soft, v1.01.2, Create: 18.09.2016, Update 28.04.2025

; Link: https://www.purebasic.fr/english/viewtopic.php?t=66626

; The following activation policies control whether and how an application may be activated.
; They are determined by the `Info.plist`.

Enumeration NSApplicationActivationPolicy
  ; The application is an ordinary app that appears in the Dock and may have a user interface.
  ; This is the Default For bundled apps, unless overridden in the Info.plist.
  #NSApplicationActivationPolicyRegular

  ; The application does not appear in the Dock and does not have a menu bar, 
  ; but it may be activated programmatically Or by clicking on one of its windows.
  ; This corresponds To LSUIElement=1 in the Info.plist.
  #NSApplicationActivationPolicyAccessory
  
  ; The application does not appear in the Dock and may not create windows or be activated. 
  ; This corresponds to LSBackgroundOnly=1 in the Info.plist. 
  ; This is also the Default For unbundled executables that do not have Info.plists.
  #NSApplicationActivationPolicyProhibited
EndEnumeration

Procedure HideApplication(Window, State)
  Protected NSApp
  NSApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
  If State
    CocoaMessage(0, NSApp, "setActivationPolicy:", #NSApplicationActivationPolicyAccessory)
  Else
    CocoaMessage(0, NSApp, "setActivationPolicy:", #NSApplicationActivationPolicyRegular)
  EndIf
  HideWindow(Window, State)
EndProcedure

; ***

CompilerIf #PB_Compiler_IsMainFile
  
  ;-TOP Main Window
  
  #ProgramTitle = "Main Window Example Hide Dock Application"
  #ProgramVersion = "v1.01.2"
  
  Enumeration Windows
    #Main
  EndEnumeration
  
  Enumeration MenuBar
    #MainMenu
    #MainSystrayMenu
  EndEnumeration
  
  Enumeration MenuItems
    #MainMenuExit
    #MainMenuAbout
    #MainMenuPrefs
    #MainSystrayMenuHideApp
    #MainSystrayMenuShowApp
  EndEnumeration
  
  Enumeration Gadgets
    
  EndEnumeration
  
  Enumeration StatusBar
    #MainStatusBar
  EndEnumeration
  
  ; ----
  
  Procedure UpdateWindow()
    Protected dx, dy
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    ; Resize gadgets
  EndProcedure
  
  Procedure Main()
    Protected dx, dy
    Protected settings_value.s
    
    #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
    
    If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
      ; Menu
      CreateMenu(#MainMenu, WindowID(#Main))
      MenuTitle("&File")
      CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
        MenuItem(#PB_Menu_About, "")
        MenuItem(#PB_Menu_Preferences, "")
      CompilerElse
        MenuItem(#MainMenuAbout, "About")
        MenuItem(#MainMenuPrefs, "Settings")
      CompilerEndIf
      ; Menu File Items
      
      CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
        MenuBar()
        MenuItem(#MainMenuExit, "E&xit")
      CompilerEndIf
      
      ; StatusBar
      CreateStatusBar(#MainStatusBar, WindowID(#Main))
      AddStatusBarField(#PB_Ignore)
      
      ; Gadgets
      dx = WindowWidth(#Main)
      dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
      
      ; Systray
      If CreatePopupMenu(#MainSystrayMenu)
        MenuItem(#MainSystrayMenuHideApp, "Hide App")
        MenuItem(#MainSystrayMenuShowApp, "Show App")
        MenuBar()
        MenuItem(#MainMenuExit, "Exit App")
      EndIf
      
      If CreateImage(0, 16, 16, 32, #Red)
        ;
      EndIf
        
      AddSysTrayIcon(1, WindowID(#Main), ImageID(0))
      SysTrayIconToolTip(1, "App 1")
      SysTrayIconMenu(1, MenuID(#MainSystrayMenu))
      
      ; Bind Events
      BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
      
      ; Event Loop
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Select EventWindow()
              Case #Main
                Break
                
            EndSelect
            
          Case #PB_Event_Menu
            Select EventMenu()
              CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
                Case #PB_Menu_About
                  PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                  
                Case #PB_Menu_Preferences
                  PostEvent(#PB_Event_Menu, #Main, #MainMenuPrefs)
                  
                Case #PB_Menu_Quit
                  PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                  
              CompilerEndIf
              
            Case #MainMenuAbout
              MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
            Case #MainMenuPrefs
              settings_value = InputRequester("Settings ...", "Value" , settings_value)
              
            Case #MainMenuExit
              PostEvent(#PB_Event_CloseWindow, #Main, #Null)
              
            Case #MainSystrayMenuHideApp
              HideApplication(#Main, #True)
              
            Case #MainSystrayMenuShowApp
              HideApplication(#Main, #False)
              
            EndSelect
            
          Case #PB_Event_Gadget
            Select EventGadget()
                
            EndSelect
          
        EndSelect
      ForEver
      
    EndIf
    
  EndProcedure : Main()
  
CompilerEndIf

Re: Systray-Beispiel erscheint im Dock

Verfasst: 28.04.2025 08:20
von Kiffi
Hi Michael,

super, danke für Deine Hilfe! Klappt jetzt. :D

Seltsam, dass man ein unsichtbares (#PB_Window_Invisible) Window unter MacOS noch zusätzlich 'hiden' muss...

Und meine App verhindert nicht mehr das Herunterfahren, seitdem ich auf #PB_Event_CloseWindow reagiere.

Danke & Grüße ... Peter