It is currently Sun Apr 23, 2017 10:43 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Single Task App Switch Menu Code
PostPosted: Mon Jan 23, 2017 10:32 pm 
Offline
User
User

Joined: Thu Jun 13, 2013 1:00 am
Posts: 21
sharing the code for attempt to create the classic app switcher. I tried to mimic the behaviour of the classic Mac OS Task switch menu but could not figure out how to hide/close the Finder (when app switching finder will stay in view if window is open) but here it is:

Code:
EnableExplicit

#POPUPMENU = 1
#IMG_ICON = 1
#ITEM_LEN = 24
#NSApplicationActivationPolicyRegular = 0
#NSApplicationActivationPolicyAccessory = 1
#NSApplicationActivationPolicyProhibited = 2
#NSSquareStatusBarItemLength = -2

;------------------------------------------------------------

UsePNGImageDecoder()
Define itemLength.CGFloat = #ITEM_LEN

Global G_QuitNum = 0
Global G_sharedapp.i = CocoaMessage(0,0,"NSApplication sharedApplication")
Global G_workspace.I = CocoaMessage(0, 0, "NSWorkspace sharedWorkspace")
Global G_status_bar = CocoaMessage(0,0,"NSStatusBar systemStatusBar")
Global G_status_item = CocoaMessage(0,CocoaMessage(0,G_status_bar,"_statusItemWithLength:",#NSSquareStatusBarItemLength,"withPriority:", 2147483647),"retain")

;for app notify
Global G_ncenter = CocoaMessage(0, G_workspace, "notificationCenter")
Global G_appdelegate = CocoaMessage(0, G_sharedapp, "delegate")
Global G_delegateclass = CocoaMessage(0, G_appdelegate, "class")

;set application policy
CocoaMessage(0, G_sharedapp, "setActivationPolicy:", #NSApplicationActivationPolicyAccessory)

Procedure.s GetNSRunningAppName(appobj)
   Define lname = CocoaMessage(0,appobj,"localizedName")
   If lname
      Define lname_str.s = PeekS(CocoaMessage(0, lname, "UTF8String"), -1, #PB_UTF8)
      ProcedureReturn lname_str
   Else
      ProcedureReturn ""
   EndIf
EndProcedure

Procedure ReBuildMenu()
   Define itemLength.CGFloat = #ITEM_LEN
   Define frontapp = CocoaMessage(0, G_workspace, "frontmostApplication")
   Define fronticon = CocoaMessage(0, frontapp, "icon")
   Protected IconSize.NSSize
   IconSize\width = 20 : IconSize\height = 20 : CocoaMessage(0, fronticon, "setSize:@", @IconSize)

   
   If IsMenu(#POPUPMENU)
      If IsMenu(#POPUPMENU) : FreeMenu(#POPUPMENU) : EndIf
   EndIf
   
   CreatePopupImageMenu(#POPUPMENU)
   Define runningapps = CocoaMessage(0,G_workspace,"runningApplications")
   Define count = CocoaMessage(0,runningapps,"count")
   Define i = 0
   Define appcount = 0
   For i = 0 To count-1
      Define current_app = CocoaMessage(0,runningapps,"objectAtIndex:",i)
      If current_app
         If CocoaMessage(0,current_app,"activationPolicy") = #NSApplicationActivationPolicyRegular
            Define lname = CocoaMessage(0,current_app,"localizedName")
            If lname
               Define icon = CocoaMessage(0, current_app, "icon")
               Define lname_str.s = PeekS(CocoaMessage(0, lname, "UTF8String"), -1, #PB_UTF8)
               MenuItem(appcount, lname_str,icon)
               appcount + 1
            EndIf
         EndIf
      EndIf
   Next
   MenuBar()
   MenuItem(appcount,"Quit")
   G_QuitNum = appcount
   CocoaMessage(0,G_status_item,"setHighlightMode:",#YES)
   CocoaMessage(0,G_status_item,"setLength:@",@itemLength)
   CocoaMessage(0,G_status_item,"setImage:",fronticon)
   CocoaMessage(0,G_status_item,"setMenu:",CocoaMessage(0,MenuID(#POPUPMENU),"firstObject"))
EndProcedure

ProcedureC ApplicationActivated(obj, sel, notification)
   ReBuildMenu()
EndProcedure

ProcedureC ApplicationSwitched(obj, sel, notification)
   
EndProcedure

Procedure ActivateApp(menu_num)
   ReBuildMenu()
   Define runningapps = CocoaMessage(0,G_workspace,"runningApplications")
   Define count = CocoaMessage(0,runningapps,"count")
   Define i = 0
   Define appcount = 0
   Define actapp = 0
   For i = 0 To count-1
      Define current_app = CocoaMessage(0,runningapps,"objectAtIndex:",i)
      If current_app
         If CocoaMessage(0,current_app,"activationPolicy") = #NSApplicationActivationPolicyRegular
            Define appname.s = GetNSRunningAppName(current_app)
            If CocoaMessage(0,current_app, "isHidden") = 0 And appname <> "Finder"
               CocoaMessage(0,current_app, "hide")
            EndIf
            
            If appcount = menu_num
               actapp = current_app
            EndIf
            appcount + 1
         EndIf
      EndIf
   Next
   
   If actapp <> 0
      CocoaMessage(0,actapp, "activateWithOptions:",2)
   EndIf
EndProcedure

OpenWindow(0,#PB_Ignore,#PB_Ignore,170,40,"statusBar",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible|#PB_Window_Tool )

Define selector = sel_registerName_("ApplicationActivated:")
class_addMethod_(G_delegateclass, selector, @ApplicationActivated(), "v@:@")
CocoaMessage(0, G_ncenter, "addObserver:", G_appdelegate, "selector:", selector, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
CocoaMessage(0, G_ncenter, "addObserver:", G_appdelegate, "selector:", selector, "name:$", @"NSWorkspaceDidDeactivateApplicationNotification", "object:", #nil)

ReBuildMenu()

Define eventnum = 0
Repeat
   Select WaitWindowEvent()
      Case #PB_Event_Menu
         eventnum = EventMenu()
         Select eventnum
            Case G_QuitNum
               Break
            Default
               ActivateApp(eventnum)
         EndSelect
   EndSelect
ForEver

;-------------------EXIT------------------
CocoaMessage(0,G_status_bar,"removeStatusItem:",G_status_item)
FreeMenu(#POPUPMENU)
CocoaMessage(0, G_ncenter, "removeObserver:", G_appdelegate, "name:$", @"NSWorkspaceDidActivateApplicationNotification", "object:", #nil)
CocoaMessage(0, G_ncenter, "removeObserver:", G_appdelegate, "name:$", @"NSWorkspaceDidDeactivateApplicationNotification", "object:", #nil)



Top
 Profile  
Reply with quote  
 Post subject: Re: Single Task App Switch Menu Code
PostPosted: Tue Jan 24, 2017 7:02 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Apr 29, 2003 9:18 pm
Posts: 1412
Location: North by Northwest
I've never used the classic macOS, so I'm a little bit lost here.
Can you (or someone else) please explain further?
Thanks

_________________

I am to provide the public with beneficial shocks.
Alfred Hitshock


Top
 Profile  
Reply with quote  
 Post subject: Re: Single Task App Switch Menu Code
PostPosted: Tue Jan 24, 2017 9:47 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 253
The Classic MacOS hasn't a dock, it has an menu in the upper right corner to switch between all running programs.
(Program is an old word for App) ;-)


Top
 Profile  
Reply with quote  
 Post subject: Re: Single Task App Switch Menu Code
PostPosted: Tue Jan 24, 2017 11:49 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Apr 29, 2003 9:18 pm
Posts: 1412
Location: North by Northwest
Wolfram wrote:
(Program is an old word for App) ;-)

Yeah, I remember programs.
Used to use programs on DOS 2.11 :mrgreen:
Thanks

_________________

I am to provide the public with beneficial shocks.
Alfred Hitshock


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye