Skip to content

Commit 76cef8d

Browse files
mobile-arnikitabobko
authored andcommitted
Fix MessageView window interaction and focus
closes #1545
1 parent 7a5939a commit 76cef8d

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Sources/AppBundle/tree/MacApp.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ final class MacApp: AbstractApp {
4343
// Otherwise, false positive ax notifications might trigger that lead to gcWindows
4444
if nsApp.bundleIdentifier == lockScreenAppBundleId { return nil }
4545
let pid = nsApp.processIdentifier
46+
// AX requests crash if you send them to yourself
47+
if pid == myPid { return nil }
4648

4749
while true {
4850
if let existing = allAppsMap[pid] { return existing }

Sources/AppBundle/ui/MessageView.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public func getMessageWindow(messageModel: MessageModel) -> some Scene {
77
SwiftUI.Window(messageModel.message?.title ?? aeroSpaceAppName, id: messageWindowId) {
88
MessageView(model: messageModel)
99
.onAppear {
10+
// Set activation policy; otherwise, AeroSpace windows won't be able to receive focus and accept keyborad input
11+
NSApp.setActivationPolicy(.accessory)
1012
NSApplication.shared.windows.forEach {
1113
if $0.identifier?.rawValue == messageWindowId {
1214
$0.level = .floating
@@ -23,6 +25,7 @@ public let messageWindowId = "\(aeroSpaceAppName).messageView"
2325
public struct MessageView: View {
2426
@StateObject private var model: MessageModel
2527
@Environment(\.dismiss) private var dismiss
28+
@FocusState var focus: Bool
2629

2730
public init(model: MessageModel) {
2831
self._model = .init(wrappedValue: model)
@@ -36,13 +39,20 @@ public struct MessageView: View {
3639
.font(.system(size: 48))
3740
Text("\(model.message?.description ?? "")")
3841
.padding(.horizontal)
42+
.focusable()
3943
}
4044
.padding()
4145
ScrollView {
4246
VStack(alignment: .leading) {
4347
HStack {
4448
TextEditor(text: .constant(model.message?.body ?? ""))
4549
.font(.system(size: 12).monospaced())
50+
.focused($focus)
51+
.onExitCommand {
52+
// Escape to remove focus from the TextEditor, and then you can hit Return to trigger the Close,
53+
// or use 'CMD + ,' for open config and 'CMD + R' for reload config (same as the menu shortcuts)
54+
focus = false
55+
}
4656
Spacer()
4757
}
4858
Spacer()
@@ -79,6 +89,9 @@ public struct MessageView: View {
7989
// If user closes the screen with the macOS native close (x) button and then the error is still the same, this window will not appear again
8090
model.message = nil
8191
}
92+
.onAppear {
93+
focus = true
94+
}
8295
}
8396
}
8497

Sources/AppBundle/util/appBundleUtil.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import os
55

66
let signposter = OSSignposter(subsystem: aeroSpaceAppId, category: .pointsOfInterest)
77

8+
let myPid = NSRunningApplication.current.processIdentifier
89
let lockScreenAppBundleId = "com.apple.loginwindow"
910
let AEROSPACE_WINDOW_ID = "AEROSPACE_WINDOW_ID" // env var
1011
let AEROSPACE_WORKSPACE = "AEROSPACE_WORKSPACE" // env var

0 commit comments

Comments
 (0)