Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect focus when activating application #5

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion FlashSpace/Core/FocusedWindowTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ final class FocusedWindowTracker {
// Activate the workspace if it's not already active
if workspaceManager.activeWorkspace[workspace.display]?.id != workspace.id {
print("\n\nFound workspace for app: \(workspace.name)")
workspaceManager.activateWorkspace(workspace)
workspaceManager.activateWorkspace(workspace, setFocus: false)
app.focus()
}
}
}
14 changes: 8 additions & 6 deletions FlashSpace/Core/WorkspaceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ final class WorkspaceManager {
.store(in: &cancellables)
}

func activateWorkspace(_ workspace: Workspace) {
func activateWorkspace(_ workspace: Workspace, setFocus: Bool) {
print("\n\nWORKSPACE: \(workspace.name)")
print("----")

lastWorkspaceActivation = Date()
activeWorkspace[workspace.display] = workspace
showApps(in: workspace)
showApps(in: workspace, setFocus: setFocus)
hideApps(in: workspace)

// Some apps may not hide properly,
// so we hide apps in the workspace after a short delay
hideAgainSubject.send(workspace)
}

private func showApps(in workspace: Workspace) {
private func showApps(in workspace: Workspace, setFocus: Bool) {
let regularApps = NSWorkspace.shared.runningApplications
.filter { $0.activationPolicy == .regular }
let appsToShow = regularApps
Expand All @@ -54,9 +54,11 @@ final class WorkspaceManager {
app.unhide()
}

appsToShow
.first { $0.localizedName == workspace.apps.last }?
.focus()
if setFocus {
appsToShow
.first { $0.localizedName == workspace.apps.last }?
.focus()
}
}

private func hideApps(in workspace: Workspace) {
Expand Down
4 changes: 2 additions & 2 deletions FlashSpace/Utils/HotKeysManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class HotKeysManager {
guard let updatedWorkspace = self?.workspaceRepository.workspaces
.first(where: { $0.id == workspace.id }) else { return true }

self?.workspaceManager.activateWorkspace(updatedWorkspace)
self?.workspaceManager.activateWorkspace(updatedWorkspace, setFocus: true)
return true
}

Expand All @@ -80,7 +80,7 @@ final class HotKeysManager {
guard let updatedWorkspace = self?.workspaceRepository.workspaces
.first(where: { $0.id == workspace.id }) else { return true }

self?.workspaceManager.activateWorkspace(updatedWorkspace)
self?.workspaceManager.activateWorkspace(updatedWorkspace, setFocus: false)
NotificationCenter.default.post(name: .newAppAssigned, object: nil)
return true
}
Expand Down