No Events

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

No Events

Post by mk-soft »

In Mac OS come no events from the timer or post event when the window is resized or in the menu is active.
Is it a system constraint or a bug.

On Windows or Linux, the event is not blocked.

Example

Code: Select all

;-TOP

Enumeration ;Window
  #Main
EndEnumeration

Enumeration ; Menu
  #Menu
EndEnumeration

Enumeration ; MenuItems
  #MenuExit
EndEnumeration
  
Enumeration ; Gadgets
  #List
EndEnumeration

Enumeration ; Statusbar
  #Status
EndEnumeration

Enumeration ; Timer
  #Timer
EndEnumeration

; Global Variable
Global exit

; ***************************************************************************************

; Functions
Procedure UpdateWindow()
  
  Protected x, y, dx, dy, menu, status
  
  menu = MenuHeight()
  If IsStatusBar(#Status)
    status = StatusBarHeight(#Status)
  Else
    status = 0
  EndIf
  x = 0
  y = 0
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - menu - status
  ResizeGadget(#List, x, y, dx, dy)
  
EndProcedure

; ***************************************************************************************

Procedure Logs(Text.s)
    
  Protected sTemp.s, c
  
    sTemp = FormatDate("%YYYY/%MM/%DD %HH.%II.%SS;", Date())
    sTemp + Text
    AddGadgetItem(#List, -1, sTemp)
    c = CountGadgetItems(#List)
    If c > 1000
      RemoveGadgetItem(#List, 0)
      c - 1
    EndIf
    c - 1
    SetGadgetState(#List, c)
    SetGadgetState(#List, -1)
  
EndProcedure


; ***************************************************************************************

Procedure EventTimerHandler()
  
  Static c
  
  c + 1
  Logs("Conter " + Str(c))
  
EndProcedure

; ***************************************************************************************

Procedure Main()
  
  Protected event, style, dx, dy
  
  style = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget
  dx = 800
  dy = 600
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, dx, dy, "Main", style)
    
    ; Menu
    CreateMenu(#Menu, WindowID(#Main))
    MenuTitle("&File")
    MenuItem(#MenuExit, "Ex&it")
    
    ; Gadgets
    ListViewGadget(#List, 0, 0, dx, dy)
    
    ; Statusbar
    CreateStatusBar(#Status, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; For Mac
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      ; Enable Fullscreen
      Protected NewCollectionBehaviour
      NewCollectionBehaviour = CocoaMessage(0, WindowID(#Main), "collectionBehavior") | $80
      CocoaMessage(0, WindowID(#Main), "setCollectionBehavior:", NewCollectionBehaviour)
      ; Mac default menu´s
      If Not IsMenu(#Menu)
        CreateMenu(#Menu, WindowID(#Main))
      EndIf
      MenuItem(#PB_Menu_About, "")
      MenuItem(#PB_Menu_Preferences, "")
    CompilerEndIf
    
    UpdateWindow()
    
    AddWindowTimer(#Main, #Timer, 1000)
    
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow())
    BindEvent(#PB_Event_Timer, @EventTimerHandler())
    
    ; Main Loop
    Repeat
      event = WaitWindowEvent()
      Select event
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                MessageRequester("Info", "Testing of Modul Logging")
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                exit = #True
                
            CompilerEndIf
              
            Case #MenuExit
              exit = #True
              
          EndSelect
          
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              exit = #True
              
          EndSelect
          
      EndSelect
      
    Until exit
    
  EndIf
  
EndProcedure : Main()

End
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: No Events

Post by wilbert »

mk-soft wrote:In Mac OS come no events from the timer or post event when the window is resized or in the menu is active.
Is it a system constraint or a bug.
It depends on what runloop mode(s) are used to monitor events.
kCFRunLoopDefaultMode does not detect events while resizing, kCFRunLoopCommonModes usually does as far as I can tell.
If not, NSEventTrackingRunLoopMode can be added to the common modes.

Here's an OSX example of a timer that keeps updating while resizing

Code: Select all

ImportC ""
  CFAbsoluteTimeGetCurrent .d()
  CFRunLoopAddCommonMode (rl, mode)
  CFRunLoopAddTimer (rl, timer, mode)
  CFRunLoopGetCurrent ()
  CFRunLoopGetMain ()
  CFRunLoopTimerGetNextFireDate .d(timer)
  CFRunLoopRemoveTimer (rl, timer, mode)
  CFRunLoopTimerCreate (allocator, fireDate.d, interval.d, flags, order, callout, *context)
  dlsym (handle, symbol.p-utf8)
EndImport

Global *NSEventTrackingRunLoopMode.Integer = dlsym(-2, "NSEventTrackingRunLoopMode")
Global *NSModalPanelRunLoopMode.Integer = dlsym(-2, "NSModalPanelRunLoopMode")
Global *kCFRunLoopCommonModes.Integer = dlsym(-2, "kCFRunLoopCommonModes")


; Timer callback

ProcedureC MyTimerCallback(timer, *info)
  AddGadgetItem(0, 0, StrD(CFRunLoopTimerGetNextFireDate(timer), 1))
EndProcedure


; Window resize callback

Procedure Window0_Resize()
  ResizeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
EndProcedure


; Main

OpenWindow(0, 0, 0, 400, 300, "Runloop Timer Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)

ListIconGadget(0, 10, 10, 380, 280, "Timer", 140)
BindEvent(#PB_Event_SizeWindow, @Window0_Resize())

runLoop = CFRunLoopGetCurrent()
CFRunLoopAddCommonMode(runLoop, *NSEventTrackingRunLoopMode\i)

myTimer = CFRunLoopTimerCreate(#Null, CFAbsoluteTimeGetCurrent(), 0.1, 0, 0, @MyTimerCallback(), 0) 
CFRunLoopAddTimer(runLoop, myTimer, *kCFRunLoopCommonModes\i)

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: No Events

Post by mk-soft »

Thank you,
for timer events a good alternative.

For PostEvent Fed must have a look, because they are handled internally.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: No Events

Post by wilbert »

mk-soft wrote:For PostEvent Fred must have a look, because they are handled internally.
I think you are right. Fred knows how the PB event handler is coded.
I don't know if PostEvent really posts an event into the event stream of the OS or is something only the PB functions can pick up.
Windows (x64)
Raspberry Pi OS (Arm64)
Rinzwind
Enthusiast
Enthusiast
Posts: 690
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: No Events

Post by Rinzwind »

Is Fred aware of this?

PostEvent and timer won't work when menu is active (and at least 1 other situation too). Can break logic for someone who uses PB timers and thinks they always 'tick'. Any workaround for postevent?
Post Reply