From d172dfd8a92f2d339f3d46a12a297e43e80768ca Mon Sep 17 00:00:00 2001 From: Nikita Bobko Date: Wed, 27 Nov 2024 23:00:47 +0100 Subject: [PATCH] Fix that menuBar shows incorrect workspace Apparently, this bug is only reproducible in macOS 15 Sequoia https://github.com/nikitabobko/AeroSpace/issues/586 https://github.com/nikitabobko/AeroSpace/issues/678 --- Sources/AppBundle/MenuBar.swift | 12 ++++-------- Sources/AppBundle/TrayMenuModel.swift | 11 +++++++++++ Sources/AppBundle/tree/Workspace.swift | 3 +-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Sources/AppBundle/MenuBar.swift b/Sources/AppBundle/MenuBar.swift index 6d727958..760489eb 100644 --- a/Sources/AppBundle/MenuBar.swift +++ b/Sources/AppBundle/MenuBar.swift @@ -12,16 +12,12 @@ public func menuBar(viewModel: TrayMenuModel) -> some Scene { Divider() if viewModel.isEnabled { Text("Workspaces:") - ForEach(Workspace.all) { (workspace: Workspace) in + ForEach(viewModel.workspaces, id: \.name) { workspace in Button { - refreshSession(screenIsDefinitelyUnlocked: true) { _ = workspace.focusWorkspace() } + refreshSession(screenIsDefinitelyUnlocked: true) { _ = Workspace.get(byName: workspace.name).focusWorkspace() } } label: { - Toggle(isOn: workspace == focus.workspace - ? Binding(get: { true }, set: { _, _ in }) - : Binding(get: { false }, set: { _, _ in })) - { - let monitor = workspace.isVisible || !workspace.isEffectivelyEmpty ? " - \(workspace.workspaceMonitor.name)" : "" - Text(workspace.name + monitor).font(.system(.body, design: .monospaced)) + Toggle(isOn: .constant(workspace.isFocused)) { + Text(workspace.name + workspace.suffix).font(.system(.body, design: .monospaced)) } } } diff --git a/Sources/AppBundle/TrayMenuModel.swift b/Sources/AppBundle/TrayMenuModel.swift index 7b6f9f49..40db0232 100644 --- a/Sources/AppBundle/TrayMenuModel.swift +++ b/Sources/AppBundle/TrayMenuModel.swift @@ -9,6 +9,7 @@ public class TrayMenuModel: ObservableObject { @Published var trayText: String = "" /// Is "layouting" enabled @Published var isEnabled: Bool = true + @Published var workspaces: [WorkspaceViewModel] = [] } func updateTrayText() { @@ -20,4 +21,14 @@ func updateTrayText() { ($0.activeWorkspace == focus.workspace && sortedMonitors.count > 1 ? "*" : "") + $0.activeWorkspace.name } .joined(separator: " │ ") + TrayMenuModel.shared.workspaces = Workspace.all.map { + let monitor = $0.isVisible || !$0.isEffectivelyEmpty ? " - \($0.workspaceMonitor.name)" : "" + return WorkspaceViewModel(name: $0.name, suffix: monitor, isFocused: focus.workspace == $0) + } +} + +struct WorkspaceViewModel { + let name: String + let suffix: String + let isFocused: Bool } diff --git a/Sources/AppBundle/tree/Workspace.swift b/Sources/AppBundle/tree/Workspace.swift index 6ed3a0f0..a3f0f5be 100644 --- a/Sources/AppBundle/tree/Workspace.swift +++ b/Sources/AppBundle/tree/Workspace.swift @@ -30,10 +30,9 @@ private func getStubWorkspace(forPoint point: CGPoint) -> Workspace { ?? errorT("Can't create empty workspace") } -class Workspace: TreeNode, NonLeafTreeNodeObject, Hashable, Identifiable, CustomStringConvertible, Comparable { +class Workspace: TreeNode, NonLeafTreeNodeObject, Hashable, CustomStringConvertible, Comparable { let name: String private let nameLogicalSegments: StringLogicalSegments - var id: String { name } // satisfy Identifiable /// `assignedMonitorPoint` must be interpreted only when the workspace is invisible fileprivate var assignedMonitorPoint: CGPoint? = nil