diff --git a/Sources/StableBar/Bar/WidgetAnimation.swift b/Sources/StableBar/Bar/WidgetAnimation.swift index 1993085e..eacf6713 100644 --- a/Sources/StableBar/Bar/WidgetAnimation.swift +++ b/Sources/StableBar/Bar/WidgetAnimation.swift @@ -180,6 +180,7 @@ struct WorkspaceConfiguration: Decodable, Sendable { var layoutGlyphs: [String: String]? var hideApps: [String]? var hideWorkspaces: [String]? + var order: [String]? var showEmpty: Bool? var showApps: Bool? var windows: Bool? @@ -197,6 +198,7 @@ struct WorkspaceConfiguration: Decodable, Sendable { case indicator, indicatorStyle, style, focusedStyle, visibleStyle, emptyStyle, appStyle, focusedAppStyle, visibleAppStyle case focusedWindowStyle case appGlyphs, layoutGlyphs, hideApps, hideWorkspaces, showEmpty, showApps, windows, titleLength, magnification, badges + case order } init(from decoder: Decoder) throws { @@ -207,6 +209,7 @@ struct WorkspaceConfiguration: Decodable, Sendable { layoutGlyphs = try values.decodeIfPresent([String: String].self, forKey: .layoutGlyphs) hideApps = try values.decodeIfPresent([String].self, forKey: .hideApps) hideWorkspaces = try values.decodeIfPresent([String].self, forKey: .hideWorkspaces) + order = try values.decodeIfPresent([String].self, forKey: .order) showEmpty = try values.decodeIfPresent(Bool.self, forKey: .showEmpty) showApps = try values.decodeIfPresent(Bool.self, forKey: .showApps) windows = try values.decodeIfPresent(Bool.self, forKey: .windows) diff --git a/Sources/StableBar/Settings/BarInspector.swift b/Sources/StableBar/Settings/BarInspector.swift index 85c862cf..2dfd3781 100644 --- a/Sources/StableBar/Settings/BarInspector.swift +++ b/Sources/StableBar/Settings/BarInspector.swift @@ -519,6 +519,12 @@ struct BarInspector: View { for: widget.id) } } + SettingRow(title: "Workspace order", hint: "Names in the order to show, separated by commas. Others follow in the source's order.") { + NameListField(title: "Workspace order", prompt: "Z, X, C, V, B, N, M", names: widget.workspace?.order ?? []) { + editor.set(["workspace": .object(["order": $0.isEmpty ? .null : .array($0.map(ConfigurationValue.string))])], + for: widget.id) + } + } if widget.workspace?.windows == true { let length = Binding(get: { Double(widget.workspace?.titleLength ?? 0) }, set: { editor.set(["workspace": .object(["titleLength": .number($0)])], for: widget.id) diff --git a/Sources/StableBar/Workspaces/WorkspaceLayout.swift b/Sources/StableBar/Workspaces/WorkspaceLayout.swift index c7ec8365..ae9b7d31 100644 --- a/Sources/StableBar/Workspaces/WorkspaceLayout.swift +++ b/Sources/StableBar/Workspaces/WorkspaceLayout.swift @@ -6,15 +6,23 @@ struct WorkspaceFilter: Hashable { let showEmpty: Bool var hideApps: [String] = [] var hideWorkspaces: [String] = [] + var order: [String] = [] func apply(to workspaces: [Workspace]) -> [Workspace] { - workspaces.compactMap { workspace in + let shown: [Workspace] = workspaces.compactMap { workspace -> Workspace? in guard shows(workspace) else { return nil } var workspace = workspace workspace.apps.removeAll(where: hides) workspace.windows.removeAll { hides($0.app) } return workspace } + guard !order.isEmpty else { return shown } + var seen = Set() + let listed = order.compactMap { name -> Workspace? in + guard seen.insert(name).inserted else { return nil } + return shown.first { $0.name == name } + } + return listed + shown.filter { !seen.contains($0.name) } } /// Whether no workspace is shown, without filtering their apps and windows. diff --git a/Sources/StableBar/Workspaces/WorkspaceView.swift b/Sources/StableBar/Workspaces/WorkspaceView.swift index 4fdf2699..978d54cc 100644 --- a/Sources/StableBar/Workspaces/WorkspaceView.swift +++ b/Sources/StableBar/Workspaces/WorkspaceView.swift @@ -24,7 +24,8 @@ struct WorkspaceView: View { let workspaces = workspaceStore.workspaces(on: monitor, showEmpty: configuration?.showEmpty == true || (workspaceStore.source.supportsWindowMoves && workspaceStore.windowDrag.active != nil), - hideApps: configuration?.hideApps ?? [], hideWorkspaces: configuration?.hideWorkspaces ?? []) + hideApps: configuration?.hideApps ?? [], hideWorkspaces: configuration?.hideWorkspaces ?? [], + order: configuration?.order ?? []) let indicator = configuration?.indicator ?? .background HStack(spacing: appearance.spacing) { ForEach(workspaces) { workspace in diff --git a/Sources/StableBar/Workspaces/Workspaces.swift b/Sources/StableBar/Workspaces/Workspaces.swift index f4853c9c..c977f58a 100644 --- a/Sources/StableBar/Workspaces/Workspaces.swift +++ b/Sources/StableBar/Workspaces/Workspaces.swift @@ -278,8 +278,8 @@ final class WorkspaceStore { self.workspaceNotificationCenter = workspaceNotificationCenter } - func workspaces(on monitor: Int, showEmpty: Bool, hideApps: [String] = [], hideWorkspaces: [String] = []) -> [Workspace] { - WorkspaceFilter(monitor: monitor, showEmpty: showEmpty, hideApps: hideApps, hideWorkspaces: hideWorkspaces).apply(to: workspaces) + func workspaces(on monitor: Int, showEmpty: Bool, hideApps: [String] = [], hideWorkspaces: [String] = [], order: [String] = []) -> [Workspace] { + WorkspaceFilter(monitor: monitor, showEmpty: showEmpty, hideApps: hideApps, hideWorkspaces: hideWorkspaces, order: order).apply(to: workspaces) } /// Retain the same subscription while either workspaces or presentation needs it. diff --git a/Tests/StableBarTests/WidgetTests.swift b/Tests/StableBarTests/WidgetTests.swift index 00ed0a43..a8ea5b3c 100644 --- a/Tests/StableBarTests/WidgetTests.swift +++ b/Tests/StableBarTests/WidgetTests.swift @@ -694,6 +694,17 @@ private func calls(_ log: URL) -> [Substring] { #expect(workspaceStore.workspaces(on: 1, showEmpty: true, hideApps: hidden).map(\.name) == ["1", "2", "S", "T"]) } +@Test @MainActor func workspaceOrderPutsNamedWorkspacesFirst() { + let workspaceStore = WorkspaceStore() + workspaceStore.workspaces = ["Z", "X", "C", "V", "B", "N", "M"].map { + Workspace(name: $0, isFocused: false, isVisible: false, monitor: 1) + } + let keyboard = ["Z", "X", "C", "V", "B", "N", "M"] + #expect(workspaceStore.workspaces(on: 1, showEmpty: true, order: keyboard).map(\.name) == keyboard) + #expect(workspaceStore.workspaces(on: 1, showEmpty: true, order: ["M", "C", "Q"]).map(\.name) + == ["M", "C", "Z", "X", "V", "B", "N"]) +} + @Test @MainActor func workspaceRowGrowsWhenAnEmptyWorkspaceAppears() throws { let workspaceStore = WorkspaceStore() workspaceStore.workspaces = [ diff --git a/docs/widgets.md b/docs/widgets.md index 28896bd4..b9734e4f 100644 --- a/docs/widgets.md +++ b/docs/widgets.md @@ -218,6 +218,7 @@ A `workspaces` widget (also accepted as `aerospace`) accepts `workspace` with: - `showEmpty` and `showApps`: control empty workspaces and app icons. - `hideApps`: app bundle IDs or app names the widget leaves out, matched like `appGlyphs` keys, so `Adobe Photoshop*` hides every Photoshop version. Hidden apps leave the per-app and `windows` views, and a workspace whose only apps are hidden counts as empty, so `showEmpty: false` hides it unless it's visible on a display. For example, `"workspace": { "hideApps": ["com.1password.1password", "Raycast"] }`. - `hideWorkspaces`: workspace names the widget leaves out, unless that workspace is focused, such as `["S", "scratch"]`. In **Settings → Widgets**, select the workspace widget and list both in **Hide apps** and **Hide workspaces**, separated by commas. +- `order`: workspace names in the order the cells show them, such as `["Z", "X", "C", "V", "B", "N", "M"]`. Workspaces the list doesn't name follow in the source's order, and names that aren't present are skipped, like [`widgetOrder`](configuration.md#theme-tokens-and-automatic-profiles). It matches a workspace's name, as `hideWorkspaces` does, which with OmniWM or Rift can differ from the label the cell shows. In **Settings → Widgets**, select the workspace widget and list them in **Workspace order**, separated by commas. Without it, workspaces keep the source's order (alphabetical with AeroSpace). - `windows`: when `true`, shows one item per window instead of one per app. Each item keeps its app icon or `appGlyphs` glyph. Clicking it focuses that window; clicking the workspace number or cell padding switches workspace. An item can outlive its window: AeroSpace, for one, doesn't announce a window leaving every workspace. When OmniWM can't find the window of an app that is running, clicking its item opens that app instead, as described in [OmniWM workspaces](#omniwm-workspaces). Otherwise, when clicking an item, or dropping it on another workspace, fails with AeroSpace, yabai, OmniWM, Rift or KiwiDesk, the widget reads the workspaces again and drops the item without an error if its window is gone. The error shows while the window is still listed, or when the workspaces can't be read. `showApps: false` hides these items too. - `focusedWindowStyle`: the icon of the focused window, or in per-app mode of the app it belongs to. Off unless set: `{}` turns it on with the default look, a background reaching 3 pt past the icon on each side and 2 pt above and below, in the cell's text color at low opacity and with the cell's corner radius. With `magnification`, it hugs the icon while the pointer is over the widget. In **Settings → Widgets**, select the workspace widget and turn on **Mark the focused window**. AeroSpace, OmniWM, Rift, KiwiDesk and yabai report which window has focus; with yabai, register `window_focused` so the highlight follows focus. `background`, `cornerRadius`, `borderColor` and `borderWidth` change it, `paddingHorizontal` and `paddingVertical` set how far it reaches past the icon, `foreground`, `fontSize`, `appIcons` and the icon `width` and `height` apply to its icon or glyph over `appStyle`, and its `states` follow the cell: `focused`, `visible`, `empty`, mode and `hover`. `workspaceDefaults` accepts it too, so a theme can set it. For example, `"workspace": { "windows": true, "focusedWindowStyle": { "background": "#ffffff33", "cornerRadius": 6 } }`. - `titleLength`: maximum characters of each window title when `windows` is enabled. Omit it or use `0` for icons only. For example, `"workspace": { "windows": true, "titleLength": 24 }`. The full title is available on hover. Titles refresh with workspace and window events; yabai users can also register `window_title_changed`. diff --git a/scenes/workspace-order-bar.json b/scenes/workspace-order-bar.json new file mode 100644 index 00000000..7d32802b --- /dev/null +++ b/scenes/workspace-order-bar.json @@ -0,0 +1,21 @@ +{ + "$schema": "../scene.schema.json", + "configuration": { + "workspaceSource": "aerospace", + "widgets": [ + { "id": "order", "type": "workspaces", "position": "left", + "workspace": { "order": ["Z", "X", "C", "V", "B", "N"], "showEmpty": true } } + ] + }, + "workspaces": [ + { "workspace": "C", "workspace-is-focused": false, "workspace-is-visible": false, "monitor-appkit-nsscreen-screens-id": 1 }, + { "workspace": "V", "workspace-is-focused": false, "workspace-is-visible": false, "monitor-appkit-nsscreen-screens-id": 1 }, + { "workspace": "X", "workspace-is-focused": true, "workspace-is-visible": true, "monitor-appkit-nsscreen-screens-id": 1 }, + { "workspace": "N", "workspace-is-focused": false, "workspace-is-visible": false, "monitor-appkit-nsscreen-screens-id": 1 }, + { "workspace": "B", "workspace-is-focused": false, "workspace-is-visible": false, "monitor-appkit-nsscreen-screens-id": 1 }, + { "workspace": "Z", "workspace-is-focused": false, "workspace-is-visible": false, "monitor-appkit-nsscreen-screens-id": 1 } + ], + "appearances": ["dark"], + "draw": "bar", + "width": 700 +} diff --git a/stablebar.schema.json b/stablebar.schema.json index 5c113791..4f609fd7 100644 --- a/stablebar.schema.json +++ b/stablebar.schema.json @@ -1587,6 +1587,10 @@ "$ref": "#/definitions/strings", "description": "Workspace names to leave out unless focused." }, + "order": { + "$ref": "#/definitions/strings", + "description": "Workspace names in the order the cells show them. Workspaces it doesn't name follow in the source's order." + }, "showEmpty": { "$ref": "#/definitions/boolean" },