The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.
Picture in Picture stays over other apps' full-screen windows, as Arc's does (#206)
Victorc33b7361ce97
diff --git a/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift b/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swiftindex 2a3ef8f..1b481a3 100644--- a/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift+++ b/Sources/Cra/UI/PictureInPicture/PictureInPictureChrome.swift@@ -1,4 +1,5 @@import AppKit+import CraCEF/// What the hover controls do; set by BrowserManager for the tab whose video is out.struct PictureInPictureActions {@@ -97,6 +98,8 @@ final class PictureInPictureChrome {if window.responds(to: NSSelectorFromString("setPreventKeyWindow:")) {window.setValue(true, forKey: "preventKeyWindow")}+ // Over another app's full-screen Space too, like Arc's (#206). The overlay copies this below.+ CraCEFRuntime.floatOverFullScreenSpaces(window)if let cv = window.contentView {cv.wantsLayer = truecv.layer?.cornerRadius = Self.cornerRadius@@ -145,9 +148,13 @@ final class PictureInPictureChrome {/// Transparent window over Chromium's PiP window: swallows the pointer so/// Chrome's controls never show, and turns drags into moves and edge drags/// into aspect-locked resizes of the window underneath.-final class PictureInPictureOverlay: NSWindow {+///+/// A non-activating panel, like Arc's PiP: it stays on another app's full-screen+/// Space with Chromium's window (#206), and a click on it doesn't bring Cra+/// forward (which would leave that Space). Back to tab activates Cra itself.+final class PictureInPictureOverlay: NSPanel {init(over target: NSWindow) {- super.init(contentRect: target.frame, styleMask: [.borderless], backing: .buffered, defer: false)+ super.init(contentRect: target.frame, styleMask: [.borderless, .nonactivatingPanel], backing: .buffered, defer: false)isOpaque = falsebackgroundColor = .clearhasShadow = falsediff --git a/Sources/CraCEF/CraCEF.mm b/Sources/CraCEF/CraCEF.mmindex 10c509c..7312674 100644--- a/Sources/CraCEF/CraCEF.mm+++ b/Sources/CraCEF/CraCEF.mm@@ -233,6 +233,50 @@ static CraClient* AsCraClient(CefClient* c) {// and popups a plain titled window, which is how they are told apart here.static NSHashTable<NSWindow*>* g_extensionWindows = nil;+// NativeWidgetMacNSWindow's switch for acting as a non-activating panel (Widget::SetActivationIndependence).+@protocol CraActivationIndependentWindow+- (void)setActivationIndependence:(BOOL)independence;+@end++// Chromium makes a Picture in Picture window (a video's, or Document PiP's) an ordinary window that+// joins every desktop Space, so it went missing over another app's full-screen Space (GitHub #206).+// Only a non-activating panel joins that Space (#186); Arc's PiP is one (an NSPanel, AXSystemDialog).+// This is Chromium's own recipe for such windows (Glic's floating panel, the screen-share picker):+// activation independence, which makes NativeWidgetMacNSWindow answer _isNonactivatingPanel, and+// SetCanAppearInExistingFullscreenSpaces' collection behaviour.+static void FloatOverFullScreenSpaces(NSWindow* window) {+ if (!window) return;+ if ([window respondsToSelector:@selector(setActivationIndependence:)]) {+ [(id<CraActivationIndependentWindow>)window setActivationIndependence:YES];+ // It also turns canHide off; Chrome's PiP has always hidden with the app (⌘H), so it still does.+ window.canHide = YES;+ }+ // Chromium also makes every top-level window `.managed` (Document PiP is one), and a managed+ // window stays off another app's full-screen Space whatever else it says.+ NSWindowCollectionBehavior behavior = window.collectionBehavior;+ behavior &= ~(NSWindowCollectionBehaviorPrimary | NSWindowCollectionBehaviorFullScreenPrimary+ | NSWindowCollectionBehaviorManaged);+ behavior |= NSWindowCollectionBehaviorCanJoinAllSpaces | NSWindowCollectionBehaviorFullScreenAuxiliary;+ window.collectionBehavior = behavior;+ // Shown while a full-screen Space is in front (Document PiP, or a video's from a tab left for it),+ // the window only moves onto that Space once it is ordered again.+ if (window.isVisible) [window orderFrontRegardless];+}++// Document Picture in Picture's window comes from Chrome after the browser exists; it is+// usually there by the next turn of the run loop.+static void FloatDocumentPictureInPicture(CefRefPtr<CefBrowser> browser, int attempt) {+ NSView* view = (__bridge NSView*)browser->GetHost()->GetWindowHandle();+ if (NSWindow* window = view.window) {+ FloatOverFullScreenSpaces(window);+ return;+ }+ if (attempt >= 40) return;+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 50 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{+ FloatDocumentPictureInPicture(browser, attempt + 1);+ });+}+// Chrome shows a new tabbed window at the end of Navigate(), after this first navigation// has started, so ordering it out here is not enough: shown, it pulled a full-screen Cra// onto the desktop and back (GitHub #38). It is made unseeable instead: transparent, no@@ -1175,7 +1219,7 @@ public:/// Cra closed this page (-[CraBrowser close]). Kept here rather than read from the owner:/// the CraBrowser can be gone before Chromium asks.void SetCloseRequested() { close_requested_ = true; }- explicit CraClient(CraBrowser* owner) : owner_(owner) { CraClientRegistry().insert(static_cast<const void*>(static_cast<CefClient*>(this))); }+ explicit CraClient(CraBrowser* owner) : owner_(owner), ownerless_(owner == nil) { CraClientRegistry().insert(static_cast<const void*>(static_cast<CefClient*>(this))); }~CraClient() override { CraClientRegistry().erase(static_cast<const void*>(static_cast<CefClient*>(this))); }CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override { return this; }@@ -1210,6 +1254,7 @@ public:CEF_REQUIRE_UI_THREAD();g_liveBrowsers++;if (CraBrowser* o = owner_) [o adoptBrowser:browser];+ if (ownerless_) FloatDocumentPictureInPicture(browser, 0);}bool DoClose(CefRefPtr<CefBrowser> browser) override { return false; }@@ -1849,6 +1894,8 @@ private:// True while a prompt's answer runs (UI thread only).static inline bool answering_prompt_ = false;__weak CraBrowser* owner_;+ // Made without a CraBrowser: Document Picture in Picture's client (owner_ is weak, so it can't say).+ const bool ownerless_;bool close_requested_ = false;CefRefPtr<FaviconState> favicon_ = new FaviconState();CefRefPtr<CefResourceRequestHandler> blocker_ = new CraContentBlockingHandler();@@ -2694,6 +2741,8 @@ static void MarkProfileExitedCleanly(NSString* profileDir) {return NO;}++ (void)floatOverFullScreenSpaces:(NSWindow*)window { FloatOverFullScreenSpaces(window); }++ (void)runMessageLoop { CefRunMessageLoop(); }+ (void)quitMessageLoop { CraQuitMessageLoop(); }+ (void)shutdown {diff --git a/Sources/CraCEF/include/CraCEF.h b/Sources/CraCEF/include/CraCEF.hindex 566fcbd..bca69aa 100644--- a/Sources/CraCEF/include/CraCEF.h+++ b/Sources/CraCEF/include/CraCEF.h@@ -327,6 +327,9 @@ typedef NS_ENUM(NSInteger, CraDownloadState) {+ (BOOL)isExtensionWindow:(NSWindow *)window;/// YES for the Chrome-style window hosting one of Cra's own browsers.+ (BOOL)isPageWindow:(NSWindow *)window;+/// Lets one of Chromium's Picture in Picture windows show over another app's full-screen Space,+/// as Arc's non-activating PiP panel does (GitHub #206). Chromium only makes it join every desktop Space.++ (void)floatOverFullScreenSpaces:(NSWindow *)window;+ (NSString *)chromiumVersion;+ (NSString *)cefVersion;/// YES when the browser-level DevTools pipe is up (it needs fds 3/4 free at start-up).diff --git a/docs/arc-reverse-engineering.md b/docs/arc-reverse-engineering.mdindex 7a60aa2..75681c7 100644--- a/docs/arc-reverse-engineering.md+++ b/docs/arc-reverse-engineering.md@@ -685,8 +685,8 @@ area over the same zones (cursor rects only work in a key window, and the overlawhile the pointer is over the window Cra sets "SetsCursorInBackground" on its WindowServerconnection, so the resize cursors show with another app in front too (verified with `screencapture-C` over each edge and corner, Finder in front and Cra in front). A press with Cra in the background-already reached the overlay (it activates Cra and acceptsFirstMouse lets it through); without the-cursor it only looked dead. Drags are measured from each+already reached the overlay (acceptsFirstMouse lets it through; since #206 it no longer activates Cra);+without the cursor it only looked dead. Drags are measured from eachevent's own screen location; `cra.log` gets `pip: move`, `pip: resize <edge> from W×H`, `pip: resized<edge> W×H → W×H` (with "asked" when Chromium kept another size) and `pip: overlay has no parent window`.Swiping (4 Nov, reported on X): Arc's PiP follows two fingers on the trackpad and is thrown into a screen corner;@@ -697,6 +697,30 @@ follows the scroll deltas (direction from `isDirectionInvertedFromDevice`), thethe resting point with UIKit's 0.998/ms deceleration, the nearest corner of that screen's visible frame (10 ptin) is the target, and a critically damped spring (response 0.4 s) flies it there from the fingers' speed.AppKit's momentum events are ignored; a plain wheel moves nothing. `cra.log`: `pip: swipe to <corner> at N pt/s`.+Over another app's full-screen Space (6 Nov, GitHub #206):+- [O static] Arc's `PictureInPicture.PIPWindowController` subclasses `ARCUI.MovableWindowController`, and+ `ARCUI.MovableWindow` subclasses `NSPanel` (`otool -ov`, Arc 1.171). Its AXSystemDialog subrole (above) means+ `.nonactivatingPanel`, the same kind of window as Little Arc (see "Little Arc over a full-screen app").+ [U] Arc's collection behaviour, and Arc on screen over a full-screen app.+- [O, Chromium source] Chromium's video PiP (`VideoOverlayWindowViews::Create`) and Document PiP (`BrowserWidget`,+ TYPE_PICTURE_IN_PICTURE) are ordinary NativeWidgetMacNSWindows at kFloatingWindow with+ `visible_on_all_workspaces`, which is `.canJoinAllSpaces` only. Chromium floats Glic's panel and the+ screen-share picker over full-screen apps with `Widget::SetActivationIndependence` (NativeWidgetMacNSWindow+ `setActivationIndependence:`, which answers `_isNonactivatingPanel` and turns canHide off) plus+ `SetCanAppearInExistingFullscreenSpaces` (adds `.fullScreenAuxiliary`).+- [O, stand-in app + Spaces probe] With another app full screen, a background app's floating `.canJoinAllSpaces`+ window is missing from that Space. It is still missing with only `.fullScreenAuxiliary` added, or only+ `_isNonactivatingPanel`. With both it is there, and so is a borderless `.nonactivatingPanel` NSPanel with+ `.fullScreenAuxiliary`; canHide doesn't matter. `.managed` keeps it off whatever else is set, and Chromium makes+ every top-level window managed (Document PiP's had 37: canJoinAllSpaces, managed, participatesInCycle). Flags+ changed while the window is on screen take effect at the next Space switch. If the full-screen Space is already+ in front, they take effect only once the window is ordered again (`orderFrontRegardless`).+- Cra (`CraCEFRuntime.floatOverFullScreenSpaces`): Chromium's recipe without `.managed` on its video PiP window+ (from `PictureInPictureChrome.style`) and its Document PiP window (found from the ownerless CraClient's+ OnAfterCreated), with canHide kept on so ⌘H still hides it. The window is ordered again when visible, and the+ overlay is a borderless non-activating NSPanel. Measured on a test copy with a full-screen stand-in app in+ front: main had video and Document PiP off that Space. Now both are on it, so is a video PiP opened while that+ Space is in front, and a click and a drag on it keep the full-screen app frontmost and its Space active.One residual difference: Chrome paints its origin strip ("File on your device", top gradient)inside the compositor layer for about two seconds when the window first appears (present at1.5 s, gone at 2.5 s in quarter-second captures); it is drawn whether or not the window is