Skip to content

armgit / cra

Public Imported from GitHub
0

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.

The protected media toast stays until it is closed

Victor6ef5f1d8890b
diff --git a/Sources/Cra/UI/Windows/MainWindowController.swift b/Sources/Cra/UI/Windows/MainWindowController.swift
index 6ad1089..44901c7 100644
--- a/Sources/Cra/UI/Windows/MainWindowController.swift
+++ b/Sources/Cra/UI/Windows/MainWindowController.swift
@@ -2648,26 +2648,36 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM
return url?.isEmpty == false ? url : nil
}
- /// The first time a service plays in a WebKit page (docs/protected-media.md), once per service
- /// ever: "Spotify plays with Safari's engine", and Why? opens the Site Control Center, which
- /// says what that means. A moment after the page shows, so it isn't lost in the tab switch.
+ /// The first time a service plays in a WebKit page (docs/protected-media.md): "Spotify plays
+ /// with Safari's engine", with Why? (the Site Control Center says what that means). It stays
+ /// until its ✕ or Why? is pressed, so Cra knows it was seen; only then is the service told for
+ /// good. Shown once a session per service: one that went unanswered shows again next launch.
private func tellAboutProtectedMedia(_ service: String) {
let key = "protectedMediaToldServices"
- var told = UserDefaults.standard.stringArray(forKey: key) ?? []
- guard !told.contains(service), window?.isVisible == true else { return }
- told.append(service)
- UserDefaults.standard.set(told, forKey: key)
+ guard !(UserDefaults.standard.stringArray(forKey: key) ?? []).contains(service), !offeredProtectedMedia.contains(service),
+ window?.isVisible == true else { return }
+ offeredProtectedMedia.insert(service)
+ let told = {
+ let list = UserDefaults.standard.stringArray(forKey: key) ?? []
+ if !list.contains(service) { UserDefaults.standard.set(list + [service], forKey: key) }
+ }
+ // A moment after the page shows, so it isn't lost in the tab switch.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) { [weak self] in
- guard let self, let id = self.browsers.visibleTabID, self.sidebarUI.navState(for: id).protectedMedia == service else {
- // Gone before it could be said: next time.
- UserDefaults.standard.set((UserDefaults.standard.stringArray(forKey: key) ?? []).filter { $0 != service }, forKey: key)
+ guard let self, let window = self.window, let id = self.browsers.visibleTabID,
+ self.sidebarUI.navState(for: id).protectedMedia == service else {
+ self?.offeredProtectedMedia.remove(service)
return
}
- self.showToast("\(service) plays with Safari's engine",
- action: ShinyToast.Action(title: "Why?") { [weak self] in self?.showSiteControlsFromTheirButton() })
+ let page = self.shell.webContainer.convert(self.shell.webContainer.bounds, to: nil)
+ self.toast.show("\(service) plays with Safari's engine",
+ action: ShinyToast.Action(title: "Why?") { [weak self] in self?.showSiteControlsFromTheirButton() },
+ page: page, in: window, theme: self.focusedSpace.theme, onClose: told)
}
}
+ /// Services whose toast was offered this session (they show once a session until answered).
+ private var offeredProtectedMedia: Set<String> = []
+
/// Arc's toast at the top right of the page (ShinyToast): Cra's confirmations.
func showToast(_ text: String, action: ShinyToast.Action? = nil) {
guard let window else { return }
@@ -2802,6 +2812,11 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM
if commandBar.isOpen { positionCommandPanel() }
floatingSidebar?.reposition()
for p in peeks { p.repositionPeek() }
+ // A toast that stays (the protected media one) follows the page's corner, once laid out.
+ DispatchQueue.main.async { [weak self] in
+ guard let self, let window = self.window else { return }
+ self.toast.follow(page: self.shell.webContainer.convert(self.shell.webContainer.bounds, to: nil), in: window)
+ }
saveFrame()
}
diff --git a/Sources/Cra/UI/Windows/ShinyToast.swift b/Sources/Cra/UI/Windows/ShinyToast.swift
index a093761..454280e 100644
--- a/Sources/Cra/UI/Windows/ShinyToast.swift
+++ b/Sources/Cra/UI/Windows/ShinyToast.swift
@@ -61,6 +61,13 @@ final class ShinyToast {
private let button = CALayer()
private let buttonLabel = CATextLayer()
private let buttonGlyph = CALayer()
+ /// A toast that stays until it is closed: an ✕ at its end, a round wash like the text button's.
+ private let closeButton = CALayer()
+ private let closeGlyph = CALayer()
+ private var persistent = false
+ private var onClose: (() -> Void)?
+ static let closeSize: CGFloat = 26
+ static let closeGap: CGFloat = 6
private var hide: DispatchWorkItem?
private var action: Action?
/// The text colour: the button is a wash of it.
@@ -94,6 +101,10 @@ final class ShinyToast {
buttonGlyph.contentsGravity = .center
button.addSublayer(buttonGlyph)
pill.addSublayer(button)
+ closeButton.cornerRadius = Self.closeSize / 2
+ closeGlyph.contentsGravity = .center
+ closeButton.addSublayer(closeGlyph)
+ pill.addSublayer(closeButton)
view.layer?.addSublayer(pill)
view.onHover = { [weak self] inside in self?.hovered(inside) }
view.onClick = { [weak self] point in self?.clicked(at: point) }
@@ -101,15 +112,30 @@ final class ShinyToast {
var isShown: Bool { panel.parent != nil }
+ /// A toast that stays (onClose) keeps to the page's top right as the window resizes, sidebar
+ /// toggles included; one that goes on its time isn't moved.
+ func follow(page: NSRect, in window: NSWindow) {
+ guard isShown, persistent, panel.parent === window else { return }
+ let size = pill.bounds.size
+ let local = NSRect(x: page.maxX - Self.trailingInset - size.width, y: page.maxY - Self.topInset - size.height,
+ width: size.width, height: size.height)
+ panel.setFrame(window.convertToScreen(local.insetBy(dx: -Self.margin, dy: -Self.margin)), display: false)
+ }
+
/// Shows `text` at the top right of `page` (window coordinates) over `window`'s pages.
typealias Colors = (top: NSColor, bottom: NSColor, outline: NSColor, text: NSColor)
- func show(_ text: String, action: Action? = nil, page: NSRect, in window: NSWindow, theme: SpaceTheme) {
- show(text, action: action, page: page, in: window, colors: Self.colors(theme))
+ /// `onClose`: the toast stays until its ✕ is pressed (or its action runs), and this is told
+ /// then; something Cra has to be sure the user saw. Without it, the toast goes on its time.
+ func show(_ text: String, action: Action? = nil, page: NSRect, in window: NSWindow, theme: SpaceTheme, onClose: (() -> Void)? = nil) {
+ show(text, action: action, page: page, in: window, colors: Self.colors(theme), onClose: onClose)
}
- func show(_ text: String, action: Action? = nil, page: NSRect, in window: NSWindow, colors: Colors) {
+ func show(_ text: String, action: Action? = nil, page: NSRect, in window: NSWindow, colors: Colors, onClose: (() -> Void)? = nil) {
self.action = action
+ persistent = onClose != nil
+ self.onClose = onClose
+ let closeRoom = persistent ? Self.closeGap + Self.closeSize : 0
let glyph = action?.symbol
let textWidth = ceil((text as NSString).size(withAttributes: [.font: Self.font]).width)
let buttonWidth = glyph != nil ? Self.glyphButton.width
@@ -117,6 +143,7 @@ final class ShinyToast {
let height = glyph != nil ? Self.glyphHeight : Self.height
let width = glyph != nil ? Self.glyphPadding + textWidth + Self.glyphGap + Self.glyphButton.width + Self.glyphPadding
: textWidth + 2 * Self.padding + (buttonWidth.map { $0 + Self.buttonGap - Self.padding + (Self.height - Self.buttonHeight) / 2 } ?? 0)
+ + closeRoom
let local = NSRect(x: page.maxX - Self.trailingInset - width, y: page.maxY - Self.topInset - height,
width: width, height: height)
panel.setFrame(window.convertToScreen(local.insetBy(dx: -Self.margin, dy: -Self.margin)), display: false)
@@ -157,7 +184,7 @@ final class ShinyToast {
button.cornerRadius = Self.buttonHeight / 2
button.borderWidth = 0
let inset = (Self.height - Self.buttonHeight) / 2
- button.frame = CGRect(x: width - inset - buttonWidth, y: inset, width: buttonWidth, height: Self.buttonHeight)
+ button.frame = CGRect(x: width - inset - closeRoom - buttonWidth, y: inset, width: buttonWidth, height: Self.buttonHeight)
buttonTint = colors.text
button.backgroundColor = colors.text.withAlphaComponent(0.1).cgColor
buttonLabel.foregroundColor = colors.text.cgColor
@@ -166,9 +193,21 @@ final class ShinyToast {
let buttonLine = ceil(Self.buttonFont.ascender - Self.buttonFont.descender)
buttonLabel.frame = CGRect(x: 0, y: (Self.buttonHeight - buttonLine) / 2, width: buttonWidth, height: buttonLine)
}
+ closeButton.isHidden = !persistent
+ if persistent {
+ let s = Self.closeSize
+ closeButton.frame = CGRect(x: width - (height - s) / 2 - s, y: (height - s) / 2, width: s, height: s)
+ closeButton.backgroundColor = colors.text.withAlphaComponent(0.1).cgColor
+ let config = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold).applying(.init(paletteColors: [colors.text]))
+ closeGlyph.contents = NSImage(systemSymbolName: "xmark", accessibilityDescription: "Close")?.withSymbolConfiguration(config)
+ closeGlyph.contentsScale = scale
+ closeGlyph.frame = CGRect(x: 0, y: 0, width: s, height: s)
+ buttonTint = colors.text
+ }
CATransaction.commit()
- panel.ignoresMouseEvents = action == nil
- view.hitRect = action == nil ? .zero : NSRect(x: Self.margin, y: Self.margin, width: width, height: height)
+ let clickable = action != nil || persistent
+ panel.ignoresMouseEvents = !clickable
+ view.hitRect = !clickable ? .zero : NSRect(x: Self.margin, y: Self.margin, width: width, height: height)
if !isShown {
// Opening the tab's page holds the main thread for ~0.1 s, so the toast waits for the
// next turn, invisible: started now, its first frame showed it most of the way in.
@@ -186,6 +225,8 @@ final class ShinyToast {
private func scheduleHide() {
hide?.cancel()
+ // A toast that has to be seen stays until it is closed.
+ guard !persistent else { hide = nil; return }
let work = DispatchWorkItem { [weak self] in self?.dismiss() }
hide = work
DispatchQueue.main.asyncAfter(deadline: .now() + Self.visibleFor, execute: work)
@@ -193,7 +234,14 @@ final class ShinyToast {
/// The pointer on a toast with a button holds it up; leaving starts its time again.
private func hovered(_ inside: Bool) {
- guard isShown, action != nil else { return }
+ guard isShown, action != nil || persistent else { return }
+ if persistent {
+ CATransaction.begin()
+ CATransaction.setDisableActions(true)
+ if action?.symbol == nil { button.backgroundColor = buttonTint.withAlphaComponent(inside ? 0.16 : 0.1).cgColor }
+ CATransaction.commit()
+ return
+ }
if inside {
hide?.cancel()
} else if hide?.isCancelled != false {
@@ -207,8 +255,15 @@ final class ShinyToast {
/// `point` in the content view: the button runs its action and the toast goes.
private func clicked(at point: NSPoint) {
- guard let action, isShown else { return }
+ guard isShown else { return }
let local = NSPoint(x: point.x - Self.margin, y: point.y - Self.margin)
+ if persistent, closeButton.frame.insetBy(dx: -4, dy: -4).contains(local) {
+ let closed = onClose
+ dismiss()
+ closed?()
+ return
+ }
+ guard let action else { return }
guard button.frame.insetBy(dx: -4, dy: -4).contains(local) else { return }
if let anchored = action.anchored, let root = view.layer {
// The menu hangs from the button, the toast stays up under it (Arc), then goes on its time.
@@ -217,7 +272,10 @@ final class ShinyToast {
scheduleHide()
return
}
+ // Pressing the button shows it was seen, as the ✕ does.
+ let closed = persistent ? onClose : nil
dismiss()
+ closed?()
action.run()
}
@@ -259,6 +317,8 @@ final class ShinyToast {
func dismiss() {
hide?.cancel()
hide = nil
+ persistent = false
+ onClose = nil
guard isShown else { return }
panel.ignoresMouseEvents = true
CATransaction.begin()