Skip to content

Commit

Permalink
Fix traymenu update issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Navonil Majumder authored and Navonil Majumder committed Nov 11, 2024
1 parent 1229230 commit 3370eea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
14 changes: 1 addition & 13 deletions Sources/AppBundle/MenuBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,7 @@ public func menuBar(viewModel: TrayMenuModel) -> some Scene {
Divider()
if viewModel.isEnabled {
Text("Workspaces:")
ForEach(Workspace.all) { (workspace: Workspace) in
Button {
refreshSession { _ = workspace.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))
}
}
}
ForEach(viewModel.workspaceStatus) { $0.button }
Divider()
}
Button(viewModel.isEnabled ? "Disable" : "Enable") {
Expand Down
20 changes: 20 additions & 0 deletions Sources/AppBundle/TrayMenuModel.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import AppKit
import Common
import SwiftUI

struct WorkspaceButton: Identifiable {
let id = UUID()
let button: Button<Toggle<Text>>
}

public class TrayMenuModel: ObservableObject {
public static let shared = TrayMenuModel()
Expand All @@ -9,6 +15,7 @@ public class TrayMenuModel: ObservableObject {
@Published var trayText: String = ""
/// Is "layouting" enabled
@Published var isEnabled: Bool = true
@Published var workspaceStatus: [WorkspaceButton] = []
}

func updateTrayText() {
Expand All @@ -19,4 +26,17 @@ func updateTrayText() {
($0.activeWorkspace == focus.workspace && sortedMonitors.count > 1 ? "*" : "") + $0.activeWorkspace.name
}
.joined(separator: "")
TrayMenuModel.shared.workspaceStatus = Workspace.all.map { (workspace: Workspace) in
WorkspaceButton(button: Button {
refreshSession { _ = workspace.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))
}
})
}
}

0 comments on commit 3370eea

Please sign in to comment.