Alternative to Deactivate?

Hi folks. Using Mint linux 20.3 and Xojo 2019r1.
I have a problem that seems like it would have a simple solution, but I’m not seeing it. All I want to do is have my app do window1.visible = False when I click anywhere except the app window, conditional on a particular property.
For example, in the Deactivate event on Window1, I have this: (“keepopen” is the property, integer, value 1 or 0 – on or off)

if keepopen  = 1 then  (ie: keepopen = active)
Window1.visible = True
else
Window1.visible = False
end if

This works, but behaves strangely. There is a popupmenu in Window1, and if keepopen=0 (window becomes visible=False when the mouse clicks outside it), then I am unable to open the popupmenu because clicking it also causes the window to deactivate.
I’ve also tried using System.MouseX and System.MouseY to target any mouse coordinates less than the window height & width, but to no avail (app window is positioned at bottom-right of the screen).

Am I missing something, or is there another way to accomplish what I want? Any/all help is much appreciated.

Sorry to trouble you, folks. Apparently I wasn’t using System.MouseX and Y properly before…it works as desired now.
In my Window1 Deactivate event, I now do this:

if keepopen = 0 then
  if System.MouseX < Window1.Left  OR System.MouseY < Window1.top then
    Window1.visible = False
  end if
end if

(Remember, Window1 is positioned at screen bottom-right)