import SwiftUI import AppKit // TabBarView draws the tab bar. The view is a SwiftUI view. The strip hosts // the view in an NSHostingView. The view observes the StripViewModel. See // spec section 7.9. The view uses system colors only. The view supports light // mode and dark mode. struct TabBarView: View { let model: StripViewModel @State private var hoveredID: CGWindowID? var body: some View { let windows = model.store.frontmostWindows let appIcon = model.appIcon() let appName = model.appName() HStack(spacing: 0) { // The app icon is on the left of all tabs. iconView(appIcon) .frame(width: 26) if windows.isEmpty { // Empty state: the app name in muted text. Text(appName) .font(.system(size: 12)) .foregroundStyle(.secondary) .padding(.leading, 4) Spacer(minLength: 0) } else { tabRow(windows) } } .padding(.horizontal, 4) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading) } @ViewBuilder private func iconView(_ icon: NSImage?) -> some View { if let icon = icon { Image(nsImage: icon) .resizable() .frame(width: 16, height: 16) } else { Image(systemName: "app") .frame(width: 16, height: 16) } } @ViewBuilder private func tabRow(_ windows: [WindowInfo]) -> some View { let activeIndex = windows.firstIndex { $0.id == model.activeWindowID } let layout = TabLayout.compute(availableWidth: model.availableWidth, windowCount: windows.count, activeIndex: activeIndex) HStack(spacing: 3) { ForEach(layout.visibleIndices, id: \.self) { index in tabView(windows[index], width: layout.tabWidth) } if layout.showsOverflow { overflowMenu(windows, indices: layout.overflowIndices) } Spacer(minLength: 0) } .padding(.leading, 4) } private func tabView(_ window: WindowInfo, width: CGFloat) -> some View { let isActive = window.id == model.activeWindowID let isHovered = window.id == hoveredID return HStack(spacing: 3) { if window.isOnCurrentSpace == false { Image(systemName: "desktopcomputer") .font(.system(size: 9)) .foregroundStyle(.secondary) } Text(window.title) .font(.system(size: 11)) .italic(window.isMinimized) .lineLimit(1) .truncationMode(.middle) .foregroundStyle(textStyle(active: isActive, minimized: window.isMinimized)) } .padding(.horizontal, 6) .frame(width: width, height: 22) .background(background(active: isActive, hovered: isHovered)) .clipShape(RoundedRectangle(cornerRadius: 5)) .help(window.title) .contentShape(Rectangle()) .onTapGesture { model.onActivate?(window.id) } .onHover { inside in hoveredID = inside ? window.id : (hoveredID == window.id ? nil : hoveredID) } .contextMenu { Button("Minimize") { model.onMinimize?(window.id) } Button("Close") { model.onClose?(window.id) } } } private func overflowMenu(_ windows: [WindowInfo], indices: [Int]) -> some View { Menu("+\(indices.count)") { ForEach(indices, id: \.self) { index in let window = windows[index] Button(action: { model.onActivate?(window.id) }) { Text(prefix(window) + window.title) } } } .menuStyle(.borderlessButton) .frame(width: 36, height: 22) .fixedSize() } // prefix returns a text marker for the overflow menu. The marker shows the // state of the window. private func prefix(_ window: WindowInfo) -> String { if window.isMinimized { return "— " } if window.isOnCurrentSpace == false { return "□ " } return "" } private func textStyle(active: Bool, minimized: Bool) -> HierarchicalShapeStyle { if active { return .primary } if minimized { return .tertiary } return .secondary } @ViewBuilder private func background(active: Bool, hovered: Bool) -> some View { if active { Color.accentColor.opacity(0.25) } else if hovered { Color.primary.opacity(0.08) } else { Color.clear } } }