jslegendre/AfloatX

Feature Request : Focus follows mouse

Opened this issue · 1 comments

Feature Request : Focus follows mouse

I found the implementation method of focus follows mouse in Afloat:

When I was using Afloat the other day (not AloatX), Finder and some other applications have experienced the problem of infinitely crashing and being totally unresponsive after mouseclick/mouseenter. After some trials & experiments, I finally managed to get a crash report. I found that the reason why afloat crashed was precisely because of Focus follows Mouse feature...

↑ the specific reason for the crash is: focus follows mouse will trigger a semaphore_ wait_continue event, which failed to get a valid response, or it was put into a dead loop.
【here is a short log just before semaphore crashes. It can be seen that the improper handling of semaphore by Afloat when using the "AfloatStorage" class causes the main thread deadlock. If you open the activity monitor, you can find that the deadlock thread has experienced many context switches. But I don't know how to fix it.

  11  -[Afloat mouseEntered:] + 246 (Afloat + 17718) [0x110ba1536]
  11  +[AfloatStorage globalValueForKey:] + 230 (Afloat + 30902) [0x110ba48b6]
  11  +[AfloatStorage _withGlobalStorage:] + 361 (Afloat + 30345) [0x110ba4689]

Then, I also found the implementation method of focus follows mouse in afloat: it is implemented by overwriting sendevent (MouseEntered) method.
The use of overwriting sendevent function is described in the official document, as follows:

https://developer.apple.com/documentation/appkit/nsapplication/1428359-sendevent
original documentation:“You rarely invoke sendEvent(:) directly, although you might want to override this method to perform some action on every event. sendEvent(:) messages are sent from the main event loop (the run() method).”

↑ dear author Jslegendre, if you have the idea of implementing focus follows mouse, consider this method as a small hint~

—————————————— ↓ Same stuff, but In Chinese ——————————————
我在使用afloat(不是afloatX)的时候,finder以及其他应用发生了无限闪退的问题。经过一番折腾,我终于设法获取了Crach Report,我发现afloat崩溃的原因正是由于Focus follows mouse……

↑具体崩溃的原因是:Focus follows mouse会触发一个 semaphore_wait_continue 事件,这个事件未能得到响应。
【这里是semaphore崩溃之前的一小段log。可以看出,Afloat在使用“AfloatStorage”类的时候,对semaphore的不当处理造成了主线程死锁。如果打开activity monitor,可以发现发生死锁的线程都经历了很多次context switch。但具体如何修复我也不是很懂。

  11  -[Afloat mouseEntered:] + 246 (Afloat + 17718) [0x110ba1536]
  11  +[AfloatStorage globalValueForKey:] + 230 (Afloat + 30902) [0x110ba48b6]
  11  +[AfloatStorage _withGlobalStorage:] + 361 (Afloat + 30345) [0x110ba4689]

然后,我还发现了afloat中的Focus follows mouse的实现方法:通过overwrite sendevent(MouseEntered)事件实现。
这种覆盖函数的实现方式在官方文档中有所说明,如下:
https://developer.apple.com/documentation/appkit/nsapplication/1428359-sendevent
原文:“You rarely invoke sendEvent(:) directly, although you might want to override this method to perform some action on every event. sendEvent(:) messages are sent from the main event loop (the run() method).”

↑如果作者您有实现Focus follows mouse的想法,就当我这个方法是一个小提示吧~