Skip to content

Commit

Permalink
🐛 [Mac] Fix a bug in multi-window applications that did not return to…
Browse files Browse the repository at this point in the history
… the correct window for pasting (#1253)
  • Loading branch information
guiyanakuang authored Jun 14, 2024
1 parent 41671bb commit 19d1d6d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class MacAppWindowManager(
}

private fun createMacAppInfo(info: String): MacAppInfo? {
val result = info.split(" ", limit = 2)
if (result.size > 1) {
val result = info.split("\n", limit = 2)
if (result.size == 2) {
val bundleIdentifier = result[0]
val localizedName = result[1]
return MacAppInfo(bundleIdentifier, localizedName)
Expand Down
17 changes: 10 additions & 7 deletions composeApp/src/desktopMain/swift/MacosApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ public func saveAppIcon(bundleIdentifier: UnsafePointer<CChar>, path: UnsafePoin
}

@_cdecl("bringToBack")
public func bringToBack(windowTitle: UnsafePointer<CChar>, appName: UnsafePointer<CChar>, toPaste: Bool, keyCodesPointer: UnsafePointer<Int32>, count: Int) {
public func bringToBack(
windowTitle: UnsafePointer<CChar>,
appName: UnsafePointer<CChar>,
toPaste: Bool,
keyCodesPointer: UnsafePointer<Int32>,
count: Int
) {
DispatchQueue.main.async {
let title = String(cString: windowTitle)
let windows = NSApplication.shared.windows
Expand All @@ -184,11 +190,8 @@ public func bringToBack(windowTitle: UnsafePointer<CChar>, appName: UnsafePointe
}
}

let appNameString = String(cString: appName)
let apps = NSRunningApplication.runningApplications(withBundleIdentifier: appNameString)
if let app = apps.first {
app.activate(options: [.activateIgnoringOtherApps])
if (toPaste) {
if (toPaste) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
simulatePasteCommand(keyCodesPointer: keyCodesPointer, count: count)
}
}
Expand All @@ -199,7 +202,7 @@ public func bringToBack(windowTitle: UnsafePointer<CChar>, appName: UnsafePointe
public func bringToFront(windowTitle: UnsafePointer<CChar>) -> UnsafePointer<CChar> {

let currentApp = NSWorkspace.shared.frontmostApplication
let currentAppInfo = "\(currentApp?.bundleIdentifier ?? "") \(currentApp?.localizedName ?? "")"
let currentAppInfo = "\(currentApp?.bundleIdentifier ?? "")\n\(currentApp?.localizedName ?? "")"

DispatchQueue.main.async {
let title = String(cString: windowTitle)
Expand Down

0 comments on commit 19d1d6d

Please sign in to comment.