Mac: Show all window event

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Mac: Show all window event

Post by Polo »

Hi,

Usually on the Mac when you close a window it simply hides itself, and get back on when you click on the dock.
To reproduce this behaviour on our PB apps, we'd need an event that tells us that the user clicked on our icon on the dock - or rightclicked and chose "Show all window".

Could that be possible?
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Mac: Show all window event

Post by Shardik »

Code: Select all

EnableExplicit

ImportC ""
  CollapseWindow(WindowRef.L, Collapse.L)
EndImport

#kAEReopenApplication = 'rapp'
#kCoreEventClass = 'aevt'
#kEventClassWindow = 'wind'
#kEventWindowClose = 72
#kWindowCollapseBoxAttribute = 1 << 3

Structure EventTypeSpec
  EventClass.L
  EventKind.L
EndStructure

ProcedureC WindowCloseHandler(*NextEventHandler, Event.L, UserData.L)
  ChangeWindowAttributes_(WindowID(0), #kWindowCollapseBoxAttribute, 0)
  CollapseWindow(WindowID(0), #True)
EndProcedure

ProcedureC DockClickHandler()
  CollapseWindow(WindowID(0), #False)
  ChangeWindowAttributes_(WindowID(0), 0, #kWindowCollapseBoxAttribute)
EndProcedure

Dim EventTypes.EventTypeSpec(0)

OpenWindow(0, 270, 100, 300, 120, "On WindowClose minimize to dock")

; ----- Intercept closing of window
EventTypes(0)\EventClass = #kEventClassWindow
EventTypes(0)\EventKind = #kEventWindowClose
InstallEventHandler_(GetWindowEventTarget_(WindowID(0)), @WindowCloseHandler(), 1, @EventTypes(), WindowID(0), 0)

; ----- Intercept click on icon in dock
AEInstallEventHandler_(#kCoreEventClass, #kAEReopenApplication, @DockClickHandler(), 0, #False)

Repeat
  If WaitWindowEvent() = #PB_Event_Menu
    If EventMenu() = -1
      Break
    EndIf
  EndIf
ForEver
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Mac: Show all window event

Post by J. Baker »

That's one thing that has annoyed me about Mac. Why on earth would you use the close event as a minimize event?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Mac: Show all window event

Post by Shardik »

J. Baker wrote:That's one thing that has annoyed me about Mac. Why on earth would you use the close event as a minimize event?
I agree with you (being a new Mac user coming from Windows and Linux).
Therefore try this much shorter version which terminates the application
on clicking the red close symbol and minimizes the window to the dock
on clicking the yellow symbol. Nevertheless you only need to click the
dock icon once to restore the window to its previous expanded position.
Without the Apple Event Handler you would have to do a right click onto
the dock icon to open the context menu and have to do a second click
onto the window title to restore the window... :wink:

Code: Select all

EnableExplicit

ImportC ""
  CollapseWindow(WindowRef.L, Collapse.L)
EndImport

#kAEReopenApplication = 'rapp'
#kCoreEventClass = 'aevt'

Structure EventTypeSpec
  EventClass.L
  EventKind.L
EndStructure

ProcedureC DockClickHandler()
  CollapseWindow(WindowID(0), #False)
EndProcedure

OpenWindow(0, 270, 100, 320, 120, "Click on dock icon to restore window", #PB_Window_MinimizeGadget)

; ----- Intercept click on icon in dock
AEInstallEventHandler_(#kCoreEventClass, #kAEReopenApplication, @DockClickHandler(), 0, #False)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Mac: Show all window event

Post by Polo »

J. Baker wrote:That's one thing that has annoyed me about Mac. Why on earth would you use the close event as a minimize event?
That's the idea of closing the window doesnt mean closing the app. Not sure I like it but well, it's the way it works on Mac and I want my app to behave like it should!

Shardik, your code sample doesn't behave like other Mac apps, it should hide the window, not minimise it in the dock :)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Mac: Show all window event

Post by Shardik »

Polo wrote:
J. Baker wrote:That's one thing that has annoyed me about Mac. Why on earth would you use the close event as a minimize event?
That's the idea of closing the window doesnt mean closing the app. Not sure I like it but well, it's the way it works on Mac and I want my app to behave like it should!

Shardik, your code sample doesn't behave like other Mac apps, it should hide the window, not minimise it in the dock :)
Gaetan, now you have the proof that I am still a Mac noob... :lol:

You are right in that on closing a normal app via the red closing button the
app is hidden. I interpreted the small dot under the app's dock icon in the
way that it has been minimized. But on a right click on the dock icon we
receive two different context menus depending whether the app has been
closed or minimized... :oops:

But my example from my first posting has already all you need: you only have
to change the CollapseWindow() against PB's HideWindow() and your app hides
on closing the window and is becoming visible on a click onto the app's dock
icon:

Code: Select all

EnableExplicit

#kAEReopenApplication = 'rapp'
#kCoreEventClass = 'aevt'
#kEventClassWindow = 'wind'
#kEventWindowClose = 72

Structure EventTypeSpec
  EventClass.L
  EventKind.L
EndStructure

ProcedureC WindowCloseHandler(*NextEventHandler, Event.L, UserData.L)
  HideWindow(0, #True)
EndProcedure

ProcedureC DockClickHandler()
  HideWindow(0, #False)
EndProcedure

Dim EventTypes.EventTypeSpec(0)

OpenWindow(0, 270, 100, 300, 120, "On WindowClose hide window")

; ----- Intercept closing of window
EventTypes(0)\EventClass = #kEventClassWindow
EventTypes(0)\EventKind = #kEventWindowClose
InstallEventHandler_(GetWindowEventTarget_(WindowID(0)), @WindowCloseHandler(), 1, @EventTypes(), WindowID(0), 0)

; ----- Intercept click on icon in dock
AEInstallEventHandler_(#kCoreEventClass, #kAEReopenApplication, @DockClickHandler(), 0, #False)

Repeat
  If WaitWindowEvent() = #PB_Event_Menu
    If EventMenu() = -1
      Break
    EndIf
  EndIf
ForEver
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Mac: Show all window event

Post by Polo »

Shardik wrote:
Polo wrote:
J. Baker wrote:That's one thing that has annoyed me about Mac. Why on earth would you use the close event as a minimize event?
That's the idea of closing the window doesnt mean closing the app. Not sure I like it but well, it's the way it works on Mac and I want my app to behave like it should!

Shardik, your code sample doesn't behave like other Mac apps, it should hide the window, not minimise it in the dock :)
Gaetan, now you have the proof that I am still a Mac noob... :lol:
You just proved you weren't with your last example, EXACTLY what I was looking for! I hope there'll be a native way to do this in PB, as even if this is specific to MacOS, I think it's really important in order to make a native fell app :)
Post Reply