Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Wrap up open/dismiss behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Apr 6, 2024
1 parent f085b8b commit bd7de96
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
42 changes: 38 additions & 4 deletions Aware/Shared/WindowAction+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ private nonisolated(unsafe) let logger = Logger(
extension OpenWindowAction {
/// Opens a window that’s associated with the specified identifier and waits until the window actually opens.
/// Yeah... the default behavior opens the window background but doesn't actually wait till it happens.
@MainActor func andWait(id: String) async throws {
@MainActor
func andWait(id: String) async throws {
try await withTimeout(.seconds(1)) { @MainActor in
let clock = ContinuousClock()
let started = clock.now
Expand All @@ -32,9 +33,9 @@ extension OpenWindowAction {

let duration = .now - started
if duration < .seconds(1) {
logger.debug("Window took \(duration) to open")
logger.debug("Window took \(duration, privacy: .public) to open")
} else {
logger.error("Window took \(duration) to open")
logger.error("Window took \(duration, privacy: .public) to open")
}

let newCount = UIApplication.shared.connectedScenes.count
Expand All @@ -46,7 +47,8 @@ extension OpenWindowAction {
extension DismissWindowAction {
/// Dismisses the window that’s associated with the specified identifier and waits until the window actually opens.
/// Yeah... the default behavior closes the window background but doesn't actually wait till it happens.
@MainActor func andWait(id: String) async throws {
@MainActor
func andWait(id: String) async throws {
try await withTimeout(.seconds(1)) { @MainActor in
let clock = ContinuousClock()
let started = clock.now
Expand All @@ -69,6 +71,38 @@ extension DismissWindowAction {
assert(oldCount - 1 == newCount, "expected 1 window to close")
}
}

@MainActor
func withFallbackAndWait(id: String, openWindow: OpenWindowAction, openID: String) async throws {
assert(id != openID, "dismiss and open IDs should be different")

let openWindowCount = UIApplication.shared.connectedScenes.count
logger.debug("\(openWindowCount) open windows")

// If multiple windows are open, dismissWindow SHOULD work
if openWindowCount > 1 {
logger.info("Dismissing \(id, privacy: .public) window")
try await andWait(id: id)
} else {
// If this is the last window, we need to open the fallback window
logger.info("Opening \(openID, privacy: .public) window")
try await openWindow.andWait(id: openID)

logger.info("Dismissing \(id, privacy: .public) window")
try await andWait(id: id)
}
}

@MainActor
func withFallback(id: String, openWindow: OpenWindowAction, openID: String) {
Task<Void, Never> {
do {
try await self.withFallbackAndWait(id: id, openWindow: openWindow, openID: openID)
} catch {
logger.error("Failed to dismiss \(id, privacy: .public) window: \(error, privacy: .public)")
}
}
}
}

#endif
22 changes: 1 addition & 21 deletions Aware/visionOS/LogWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,7 @@ struct LogWindow: Scene {
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Dismiss", systemImage: "xmark") {
Task<Void, Never> {
do {
let openWindowCount = UIApplication.shared.connectedScenes.count
logger.debug("\(openWindowCount) open windows")

// If multiple windows are open, dismissWindow SHOULD work
if openWindowCount > 1 {
logger.info("Dismissing Logs window")
try await dismissWindow.andWait(id: "logs")

} else {
// If this is the last window open, we need to re-open the main window
logger.warning("Re-opening Timer window")
try await openWindow.andWait(id: "timer")
logger.info("Dismissing Logs window")
try await dismissWindow.andWait(id: "logs")
}
} catch {
logger.error("Failed to dismiss Logs window: \(error, privacy: .public)")
}
}
dismissWindow.withFallback(id: "logs", openWindow: openWindow, openID: "timer")
}
.labelStyle(.iconOnly)
}
Expand Down

0 comments on commit bd7de96

Please sign in to comment.