Subject: Re: Interact directly on Emacs bug#28620: mouse drag event records wrong release window

From: Robert Weiner <[email-masked]>
Date: Thu, 12 Oct 2017 09:08:30 -0400

On Thu, Oct 12, 2017 at 4:05 AM, martin rudalics <[email-masked]> wrote:

> > So if I have 2 frames, f1 and f2, and a Chrome web browser window that is
> > atop f2, then if I drag from f1 into Chrome above f2, my drag release
> code
> > reports that the release window is in f2 rather than nil, as it should
> be.
> > I am on macOS which uses click to focus, so Emacs still gets the release
> > event since Chrome has not been selected with a click.
>
> I would call this a feature: f2 is probably the one meaningful target of
> your operation at that screen position.

​I think it is a feature that Emacs receives an event for this but a defect
that it can't distinguish when f2 is atop an external window or not and
thus whether the event was actually directed at f2 or not.

​​
>
> ​​
>
> ​​
> > Is there any way to deal with external window z-order layering such that
> ​​
> > one can tell within Emacs whether the topmost OS-level window at an
> ​​
> > absolute mouse position is an Emacs frame or not?
> ​​
>
> ​​
> Not really.  Compositing window managers on X no more allow to track the
> ​​
> visibility of windows reliably.  So while we can discern the visibility
> ​​
> of our own (window manager) windows based on what we store in their
> ​​
> asscociated frames' 'visible' slots, we can't do that for windows of
> ​​
> other applications.  And processing whatever else XGetWindowAttributes
> ​​
> returns for another application's window might not be trivial either.
>

​Just FYI, I am using the macOS window manager, not X, though as you note,
it is an issue there too.
The application-level window managers must have a z-ordering for all
windows in order to be able to select and raise windows when they are
behind others.  So you are saying that they don't publish this information
in any useful way that Emacs can obtain, right?

Part of the issue is that the macOS window manager uses click-to-focus, so
the release event of the drag does not switch focus to the application
whose window the release falls upon.  However, in drag-n-drop operations,
the window manager automatically switches focus to any compatible
application that the mouse moves over (after a delay) so that the right
application receives the drop (based on Z-order).

Mouse wheel events are also delivered to the topmost Z-order window without
either raising the window or switching focus.

So the window manager always knows where it should deliver at least two
kinds of events and maybe one of those types of events could be used to
help Emacs know whether a release event was over one of its frames or over
the window of an another application.

​
>
> ​​
> It should be poss
> ​​
> ​​
> ible to do what you want on Windows (where the debugger
> ​​
> also notifies you
> ​​
> when an Emacs frame is obscured) though.

​​
​​Well, one platform would be a good start.​  What would the pseudo-code
look like to check whether or not an Emacs frame was uppermost at the point
of mouse release?

Thanks,

Bob