Skip to content

Commit

Permalink
Fix that menuBar shows incorrect workspace
Browse files Browse the repository at this point in the history
Apparently, this bug is only reproducible in macOS 15 Sequoia

#586
#678
  • Loading branch information
nikitabobko committed Nov 27, 2024
1 parent ba19c7c commit d172dfd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
12 changes: 4 additions & 8 deletions Sources/AppBundle/MenuBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/AppBundle/TrayMenuModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
3 changes: 1 addition & 2 deletions Sources/AppBundle/tree/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d172dfd

Please sign in to comment.