Skip to content
Closed
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
2 changes: 2 additions & 0 deletions Sources/AppBundle/ui/ExperimentalUISettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum MenuBarStyle: String, CaseIterable, Identifiable, Equatable, Hashable {
case squares
case i3
case i3Ordered
case i3OrderedWithAppIcons
var id: String { rawValue }
var title: String {
switch self {
Expand All @@ -30,6 +31,7 @@ enum MenuBarStyle: String, CaseIterable, Identifiable, Equatable, Hashable {
case .squares: "Square images"
case .i3: "i3 style grouped"
case .i3Ordered: "i3 style ordered"
case .i3OrderedWithAppIcons: "i3 style ordered + app icons"
}
}
}
Expand Down
64 changes: 48 additions & 16 deletions Sources/AppBundle/ui/MenuBarLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,9 @@ struct MenuBarLabel: View {
otherWorkspaces(with: workspaces)
}
case .i3Ordered:
let modeItem = viewModel.trayItems.first { $0.type == .mode }
if let modeItem {
itemView(for: modeItem)
modeSeparator(with: .monospaced)
}
let orderedWorkspaces = viewModel.workspaces.filter { !$0.isEffectivelyEmpty || $0.isVisible }
ForEach(orderedWorkspaces, id: \.name) { item in
let trayItem = TrayItem(
type: .workspace,
name: item.name,
isActive: item.isFocused,
hasFullscreenWindows: item.hasFullscreenWindows,
)
itemView(for: trayItem)
.opacity(item.isVisible ? 1 : 0.5)
}
orderedWorkspacesView(showApps: false)
case .i3OrderedWithAppIcons:
orderedWorkspacesView(showApps: true)
}
}
}
Expand Down Expand Up @@ -168,6 +155,51 @@ struct MenuBarLabel: View {
}
}
}

private func orderedWorkspacesView(showApps: Bool) -> some View {
let modeItem = viewModel.trayItems.first { $0.type == .mode }
let orderedWorkspaces = viewModel.workspaces.filter { !$0.isEffectivelyEmpty || $0.isVisible }
return Group {
if let modeItem {
itemView(for: modeItem)
modeSeparator(with: .monospaced)
}
ForEach(orderedWorkspaces, id: \.name) { ws in
let trayItem = TrayItem(
type: .workspace,
name: ws.name,
isActive: ws.isFocused,
hasFullscreenWindows: ws.hasFullscreenWindows,
)
itemView(for: trayItem)
.opacity(ws.isVisible ? 1 : 0.5)
if showApps {
let limit = style != nil ? (ws.isFocused ? 1 : 0) : ws.apps.count
ForEach(ws.apps.prefix(limit)) { app in
appIconView(for: app)
.opacity(ws.isFocused && app.isFocused ? 1 : 0.5)
}
}
}
}
}

@ViewBuilder
private func appIconView(for app: AppViewModel) -> some View {
let icon = Image(nsImage: app.icon)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: itemSize, height: itemSize)
if app.isFocused {
icon.clipShape(RoundedRectangle(cornerRadius: itemCornerRadius, style: .continuous))
.overlay {
RoundedRectangle(cornerRadius: itemCornerRadius, style: .continuous)
.strokeBorder(finalColor, lineWidth: 1)
}
} else {
icon
}
}
}

extension String {
Expand Down
50 changes: 50 additions & 0 deletions Sources/AppBundle/ui/TrayMenuModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,28 @@ public final class TrayMenuModel: ObservableObject {
default: ""
}
let hasFullscreenWindows = $0.allLeafWindowsRecursive.contains { $0.isFullscreen }
let appViewModels: [AppViewModel]
if TrayMenuModel.shared.experimentalUISettings.displayStyle == .i3OrderedWithAppIcons {
let focusedWindowId = focus.windowOrNil?.windowId
appViewModels = $0.allLeafWindowsRecursive.map { window in
AppViewModel(
windowId: window.windowId,
name: window.app.name ?? "Unknown",
icon: resolveAppIcon(for: window),
isFocused: window.windowId == focusedWindowId,
)
}
} else {
appViewModels = []
}
return WorkspaceViewModel(
name: $0.name,
suffix: suffix,
isFocused: focus.workspace == $0,
isEffectivelyEmpty: $0.isEffectivelyEmpty,
isVisible: $0.isVisible,
hasFullscreenWindows: hasFullscreenWindows,
apps: appViewModels,
)
}
var items = sortedMonitors.map {
Expand Down Expand Up @@ -69,6 +84,23 @@ struct WorkspaceViewModel: Hashable {
let isEffectivelyEmpty: Bool
let isVisible: Bool
let hasFullscreenWindows: Bool
let apps: [AppViewModel]

func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}

struct AppViewModel: Identifiable, Equatable {
let windowId: UInt32
let name: String
let icon: NSImage
let isFocused: Bool
var id: UInt32 { windowId }

static func == (lhs: AppViewModel, rhs: AppViewModel) -> Bool {
lhs.windowId == rhs.windowId && lhs.isFocused == rhs.isFocused
}
}

enum TrayItemType: String, Hashable {
Expand Down Expand Up @@ -108,3 +140,21 @@ struct TrayItem: Hashable, Identifiable {
return type.rawValue + name
}
}

@MainActor
private func resolveAppIcon(for window: Window) -> NSImage {
if let macApp = window.app as? MacApp {
if let icon = macApp.nsApp.icon {
return icon
}
if let bundlePath = macApp.bundlePath {
return NSWorkspace.shared.icon(forFile: bundlePath)
}
if let bundleId = macApp.rawAppBundleId,
let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleId)
{
return NSWorkspace.shared.icon(forFile: url.path)
}
}
return NSImage(named: NSImage.applicationIconName) ?? NSImage()
}
Loading