Use of transition effects when minimizing/restoring windows

Mac OSX specific forum
User avatar
Shardik
Addict
Addict
Posts: 1991
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Use of transition effects when minimizing/restoring windows

Post by Shardik »

The MacOS Carbon API offers several window transition effects for
minimizing a window to the dock or for restoring it. The following
example demonstrates how to achieve this. Because for some
transition effects a parent window is required, I have drawn the
transition window into a parent window just to be able to demonstrate
all transition effects together. And you can't combine all transition
actions with each other: if you use a move action for minimizing, you
also have to use move to restore the window.

You have to chose a minimizing and a restoring effect which may be
different (with the exeption of the slide/move action). Then click onto
the "Minimize window" button to see the chosen minimizing effect. As
a further goodie I have implemented a DockClickHandler. Left click onto
the dock icon of this program to see the chosen restoring effect...

Code: Select all

EnableExplicit

ImportC ""
  TransitionWindow(WindowRef.L, TransitionEffect.L, TransitionAction.L, *Rect)
  TransitionWindowAndParent(ChildWindowRef.L, ParentWindowRef.L, TransitionEffect.L, TransitionAction.L, *Rect)
EndImport

#kAEReopenApplication = 'rapp'
#kCoreEventClass = 'aevt'
#kWindowCollapseBoxAttribute = 1 << 3

; ----- Window Transition Action Constants
#kWindowShowTransitionAction   = 1
#kWindowHideTransitionAction   = 2
#kWindowMoveTransitionAction   = 3
#kWindowResizeTransitionAction = 4

; ----- Window Transition Effect Constants
#kWindowZoomTransitionEffect = 1
#kWindowSheetTransitionEffect = 2
#kWindowSlideTransitionEffect = 3
#kWindowFadeTransitionEffect = 4
#kWindowGenieTransitionEffect = 5

Structure Rect
  Top.W
  Left.W
  Bottom.W
  Right.W
EndStructure

Procedure TransferWindow(MinimizeWindow.I)
  Protected FirstOptionBoxID.I
  Protected Rect.Rect
  Protected TransitionAction.L
  Protected TransitionEffect.L

  Rect\Left = WindowX(0) + 20
  Rect\Top = WindowY(0) + 30
  Rect\Right = WindowX(0) + WindowWidth(0) - 20
  Rect\Bottom = WindowY(0) + WindowHeight(0) + 10

  If MinimizeWindow
    FirstOptionBoxID = 1
    TransitionAction = #kWindowHideTransitionAction
  Else
    FirstOptionBoxID = 7
    TransitionAction = #kWindowShowTransitionAction
  EndIf

  If GetGadgetState(FirstOptionBoxID)
    TransitionAction = #kWindowMoveTransitionAction
    TransitionEffect = #kWindowSlideTransitionEffect
  ElseIf GetGadgetState(FirstOptionBoxID + 1)
    TransitionEffect = #kWindowZoomTransitionEffect
  ElseIf GetGadgetState(FirstOptionBoxID + 2)
    TransitionEffect = #kWindowSheetTransitionEffect
    Rect\Left - 20
    Rect\Top - 30
  ElseIf GetGadgetState(FirstOptionBoxID + 3)
    TransitionEffect = #kWindowFadeTransitionEffect
  Else
    TransitionEffect = #kWindowGenieTransitionEffect
  EndIf

  Select TransitionEffect
    Case #kWindowZoomTransitionEffect, #kWindowFadeTransitionEffect
      TransitionWindow(WindowID(1), TransitionEffect, TransitionAction, 0)
    Default
      TransitionWindowAndParent(WindowID(1), WindowID(0), TransitionEffect, TransitionAction, @Rect)
  EndSelect
  
  If MinimizeWindow
    ChangeWindowAttributes_(WindowID(1), #kWindowCollapseBoxAttribute, 0)
    SetWindowState(1, #PB_Window_Minimize)
  Else
    SetWindowState(1, #PB_Window_Normal)
    ChangeWindowAttributes_(WindowID(1), 0, #kWindowCollapseBoxAttribute)
    SetActiveWindow(1)
  EndIf
EndProcedure

Procedure EnableSlideOption(MoveTransition.I)
  Protected i.I

  If MoveTransition
    For i = 1 To 5
      DisableGadget(i, #False)
    Next i

    DisableGadget(7, #False)

    For i = 8 To 11
      DisableGadget(i, #True)
    Next i

    SetGadgetState(1, #True)
    SetGadgetState(7, #True)
  Else
    For i = 1 To 5
      DisableGadget(i, #False)
    Next i

    DisableGadget(7, #True)

    For i = 8 To 11
      DisableGadget(i, #False)
    Next i

    If GetGadgetState(7)
      SetGadgetState(8, #True)
    EndIf
  EndIf
EndProcedure

ProcedureC DockClickHandler()
  TransferWindow(#False)
EndProcedure

OpenWindow(0, 80, 80, 290, 400, "Parent window")
OpenWindow(1, 100, 110, 250, 360, "Window transition effects", #PB_Window_SystemMenu, WindowID(0))

Frame3DGadget(0, 10, 10, WindowWidth(1) - 20, 145, "Window minimizing effects")
OptionGadget(1, 20, 30, GadgetWidth(0) - 40, 20, "Slide to dock (PB default)")
OptionGadget(2, 20, 55, GadgetWidth(0) - 40, 20, "Zoom")
OptionGadget(3, 20, 80, GadgetWidth(0) - 40, 20, "Move up")
OptionGadget(4, 20, 105, GadgetWidth(0) - 40, 20, "Fade out")
OptionGadget(5, 20, 130, GadgetWidth(0) - 40, 20, "Move to left and back")
SetGadgetState(1, #True)

Frame3DGadget(6, 10, 170, WindowWidth(1) - 20, 145, "Window restoring effects")
OptionGadget(7, 20, 190, GadgetWidth(0) - 40, 20, "Slide to dock (PB default)")
OptionGadget(8, 20, 215, GadgetWidth(0) - 40, 20, "Zoom")
OptionGadget(9, 20, 240, GadgetWidth(0) - 40, 20, "Move down")
OptionGadget(10, 20, 265, GadgetWidth(0) - 40, 20, "Fade in")
OptionGadget(11, 20, 290, GadgetWidth(0) - 40, 20, "Move to left and back")
SetGadgetState(7, #True)

ButtonGadget(12, 50, 325, 150, 20, "Minimize window")

EnableSlideOption(#True)

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

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget() 
        Case 1, 7
          EnableSlideOption(#True)
        Case 2 To 5, 8 To 11
          EnableSlideOption(#False)
        Case 12
          If EventType() = #PB_EventType_LeftClick
            TransferWindow(#True)
          EndIf
      EndSelect
  EndSelect
ForEver
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Use of transition effects when minimizing/restoring wind

Post by srod »

Very nice. :)

Thanks.
I may look like a mule, but I'm not a complete ass.
mestnyi
Addict
Addict
Posts: 1001
Joined: Mon Nov 25, 2013 6:41 am

Re: Use of transition effects when minimizing/restoring windows

Post by mestnyi »

macOS Big Sur v.11.6.4 Purebasic 573 not work.
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Use of transition effects when minimizing/restoring windows

Post by Rinzwind »

mestnyi wrote: Fri Apr 01, 2022 12:05 am macOS Big Sur v.11.6.4 Purebasic 573 not work.
That's to be expected since the post was talking about Carbon which has been replaced for years by Cocoa.
Post Reply