SnosMe / electron-overlay-window

Creating overlays is easy like never before
MIT License
201 stars 38 forks source link

Added MacOS support #17

Closed hsource closed 1 year ago

hsource commented 3 years ago

Motivation

I wanted to get awakened_poe_trade working on Mac, and this was the biggest barrier, since no binaries existed for this on Mac.

PS: thanks a lot for the great demo app! I don't think developing this would've been possible without that

Fix

Created Mac interface for overlay_window.h - for the most part, this is structured similarly to the Windows/Linux code, with a few exceptions:

  1. Mac doesn't allow windows to be attached/reparented to those of other applications, so the overlay window has to stay within the separate app. To make this not look weird, we hide the window whenever the target window isn't on top
  2. Mac doesn't give a global way to listen to windows becoming the foreground window. We instead do 2 things:
    • Attach to the foreground window and detect when it gets deactivated or destroyed. When it does that, attach to the next foreground window
    • When attaching observers, we poll for 5 seconds since the observers seem to take a while to take effect

Side changes

Testing

Tested by:

  1. Run yarn demo:electron
  2. Open an empty, untitled TextEdit window. If you need to change the sample window, edit attachByTitle in the electron-demo file to attach to a window with a different title

Tested

Resizing, focusing

Testing

Fullscreen

https://user-images.githubusercontent.com/2937410/119969235-29ddc780-bf63-11eb-90d0-d80e57dbf5c5.mp4

hsource commented 3 years ago

I pushed a new version so that it handles renames properly like the other OS's, and added a call to refocus the target after we call .show() so that it doesn't take an extra click to focus the original app.

SnosMe commented 3 years ago

Will review it soon, a little busy these weeks.

Not familiar with Mac API, can you explain why addChildWindow won't work here?

hsource commented 3 years ago

Mac has a bit of a stricter permission model for windows - different applications don't have access to the NSWindow* for other applications. Because of that, we just have to make do with what the Accessibility API lets us do.

This Stackoverflow question has more: addChild my window to other application's window

SnosMe commented 3 years ago

Depends on #20 , so that the behavior (always on top) is the same for all OS.

hsource commented 3 years ago

Depends on #20 , so that the behavior (always on top) is the same for all OS.

Right on time! I just rebased on top of that branch and it looks like really minimal changes are needed.

paragbaxi commented 2 years ago

Tried to run https://github.com/hsource/electron-overlay-window/releases/tag/v3.0.0-macos.2 but no window appeared. Did allow iTerm.app to control computer.

❯ yarn demo:electron
yarn run v1.22.10
$ node-gyp build && yarn build-ts && electron dist/demo/electron-demo.js
gyp info it worked if it ends with ok
gyp info using node-gyp@7.1.2
gyp info using node@16.8.0 | darwin | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Nothing to be done for `all'.
gyp info ok
$ tsc
(node:26592) electron: The default of nativeWindowOpen is deprecated and will be changing from false to true in Electron 15.  See https://github.com/electron/electron/issues/28511 for more information.
(Use `Electron --trace-warnings ...` to show where the warning was created)
(node:26592) electron: The default of nativeWindowOpen is deprecated and will be changing from false to true in Electron 15.  See https://github.com/electron/electron/issues/28511 for more information.

How can I get this working?

hsource commented 2 years ago

How can I get this working?

You just need to open a TextEdit window with an untitled document. It should automatically bind to that. I updated the instructions!

paragbaxi commented 2 years ago

Works!

kraj011 commented 2 years ago

Hey! Will this ever be merged into master? Thanks!

jamninetyfive commented 2 years ago

any update?