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

Commit

Permalink
Add hack to really activate the app
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Apr 26, 2024
1 parent 6061aff commit 7168a49
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Aware.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
0347D59F2BAFB97300C5741E /* BackgroundTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0347D59E2BAFB97300C5741E /* BackgroundTask.swift */; };
035820952BDB11880099E707 /* View+NSStatusItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035820942BDB11880099E707 /* View+NSStatusItem.swift */; platformFilters = (macos, ); };
035820972BDB122B0099E707 /* View+NSWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035820962BDB122B0099E707 /* View+NSWindow.swift */; platformFilters = (macos, ); };
035820992BDB24EC0099E707 /* NSApplication+Activate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035820982BDB24EC0099E707 /* NSApplication+Activate.swift */; platformFilters = (macos, ); };
036569CE2B9A40C8003D3DCA /* MenuBarTimelineView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 036569CD2B9A40C8003D3DCA /* MenuBarTimelineView.swift */; platformFilters = (macos, ); };
036DA9B52B7AF52E0066B4B2 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 036DA9B42B7AF52E0066B4B2 /* App.swift */; };
036EBD1B1C1408C200121D0B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 036EBD1A1C1408C200121D0B /* Assets.xcassets */; };
Expand Down Expand Up @@ -72,6 +73,7 @@
0347D59E2BAFB97300C5741E /* BackgroundTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundTask.swift; sourceTree = "<group>"; };
035820942BDB11880099E707 /* View+NSStatusItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+NSStatusItem.swift"; sourceTree = "<group>"; };
035820962BDB122B0099E707 /* View+NSWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+NSWindow.swift"; sourceTree = "<group>"; };
035820982BDB24EC0099E707 /* NSApplication+Activate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSApplication+Activate.swift"; sourceTree = "<group>"; };
036569CD2B9A40C8003D3DCA /* MenuBarTimelineView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarTimelineView.swift; sourceTree = "<group>"; };
036DA9B42B7AF52E0066B4B2 /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
036EBD151C1408C200121D0B /* Aware.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Aware.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -166,6 +168,7 @@
037195352B804E4C00B807ED /* ActivityMonitor.swift */,
0381B4982B808A5A002213F6 /* MenuBar.swift */,
036569CD2B9A40C8003D3DCA /* MenuBarTimelineView.swift */,
035820982BDB24EC0099E707 /* NSApplication+Activate.swift */,
0341CB692B9C2CC800CC0C96 /* NSEvent+AsyncStream.swift */,
032E60AD2BD74BEF008BB2B4 /* SettingsView.swift */,
035820942BDB11880099E707 /* View+NSStatusItem.swift */,
Expand Down Expand Up @@ -326,6 +329,7 @@
037195362B804E4C00B807ED /* ActivityMonitor.swift in Sources */,
0381B4992B808A5A002213F6 /* MenuBar.swift in Sources */,
036569CE2B9A40C8003D3DCA /* MenuBarTimelineView.swift in Sources */,
035820992BDB24EC0099E707 /* NSApplication+Activate.swift in Sources */,
0341CB6A2B9C2CC800CC0C96 /* NSEvent+AsyncStream.swift in Sources */,
032E60AE2BD74BEF008BB2B4 /* SettingsView.swift in Sources */,
035820952BDB11880099E707 /* View+NSStatusItem.swift in Sources */,
Expand Down
30 changes: 30 additions & 0 deletions Aware/macOS/NSApplication+Activate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// NSApplication+Activate.swift
// Aware
//
// Created by Joshua Peek on 4/25/24.
//

import AppKit
import OSLog

private nonisolated(unsafe) let logger = Logger(
subsystem: "com.awaremac.Aware", category: "NSApp+Activate"
)

extension NSApplication {
func activateAggressively() {
let start: ContinuousClock.Instant = .now
let deadline: ContinuousClock.Instant = start.advanced(by: .seconds(1))

Task(priority: .high) {
while isActive == false && deadline > .now && !Task.isCancelled {
activate()
await Task.yield()
}

assert(isActive, "expected app to be active")
logger.debug("\(self) application took \(.now - start) to activate")
}
}
}
7 changes: 7 additions & 0 deletions Aware/macOS/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ struct SettingsView: View {
.padding()
.frame(width: 350)
.bindWindow($window)
.onChange(of: windowIsVisible) { oldValue, newValue in
logger.debug("Window visibility change: \(oldValue) -> \(newValue)")

if oldValue == false && newValue == true {
NSApp.activateAggressively()
}
}
.task(id: window) {
guard let window else {
assertionFailure("no window is set")
Expand Down

0 comments on commit 7168a49

Please sign in to comment.