From 1fcf6730652d509e78de4b80888772083f5b124a Mon Sep 17 00:00:00 2001
From: Ariver <shanghai3168@gmail.com>
Date: Thu, 18 Jun 2026 22:04:51 +0800
Subject: [PATCH] Refine Quick Switch app hover visuals

---
 C1.source/Sources/Aligner/QuickSwitchRootView.swift |  183 +++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 175 insertions(+), 8 deletions(-)

diff --git a/C1.source/Sources/Aligner/QuickSwitchRootView.swift b/C1.source/Sources/Aligner/QuickSwitchRootView.swift
index 9b47c99..4797b32 100644
--- a/C1.source/Sources/Aligner/QuickSwitchRootView.swift
+++ b/C1.source/Sources/Aligner/QuickSwitchRootView.swift
@@ -241,6 +241,7 @@
         let item: QuickSwitchAppShelfItemViewModel
         let containerLayer: CALayer
         let backgroundLayer: CALayer
+        let indexLayer: CATextLayer
         let iconLayer: CALayer
         let labelLayer: CATextLayer
         let badgeLayer: CATextLayer
@@ -299,6 +300,10 @@
         static let normalGap: CGFloat = 16
         static let minGap: CGFloat = 8
         static let maxRows = 2
+        static let indexFontSize: CGFloat = 16
+        static let compressedIndexFontSize: CGFloat = 14
+        static let indexWidth: CGFloat = 18
+        static let indexHeight: CGFloat = 24
     }
 
     private enum RootLayoutMetrics {
@@ -751,11 +756,38 @@
         apply(viewModel: viewModel, preservingInteractionState: true, animatedProjection: true)
     }
 
+    func hoverNextAppGroupIndex(cycleForward: Bool = true) -> Bool {
+        guard !appShelfItems.isEmpty else {
+            return false
+        }
+
+        let orderedIndexes = appShelfItems.map(\.item.appGroupIndex)
+        let sortedIndexes = orderedIndexes
+
+        var currentIndexPosition: Int?
+        if let currentHovered = effectiveHoveredAppGroupIndex {
+            currentIndexPosition = sortedIndexes.firstIndex(of: currentHovered)
+        }
+
+        let nextPosition = currentIndexPosition.map { current in
+            if cycleForward {
+                (current + 1) % sortedIndexes.count
+            } else {
+                (current - 1 + sortedIndexes.count) % sortedIndexes.count
+            }
+        } ?? 0
+
+        let nextAppGroupIndex = sortedIndexes[nextPosition]
+        hoverAppGroup(nextAppGroupIndex)
+        return true
+    }
+
     private func apply(
         viewModel: QuickSwitchViewModel?,
         preservingInteractionState: Bool,
         animatedProjection: Bool
     ) {
+        let preservedWindowThumbnails = preservingInteractionState ? cachedThumbnailsByWindowID() : [:]
         let transitionSnapshot = animatedProjection ? captureProjectionTransitionSnapshot() : nil
         if !animatedProjection {
             resetProjectionTransitionReport()
@@ -816,12 +848,35 @@
         rebuildSpaceLaneSegments()
         rebuildAppShelfItems()
         rebuildWaterfallColumns()
+
+        if preservingInteractionState {
+            restoreThumbnailsFromCache(preservedWindowThumbnails)
+        }
+
         needsLayout = true
         ensureCurrentSelectionVisible(horizontalIntent: .alignWithAppShelf(animated: false))
         needsLayout = true
         if animatedProjection, let transitionSnapshot {
             layoutSubtreeIfNeeded()
             applyProjectionTransitionAnimations(from: transitionSnapshot)
+        }
+    }
+
+    private func cachedThumbnailsByWindowID() -> [UInt32: Any] {
+        var snapshots: [UInt32: Any] = [:]
+
+        for card in waterfallCardsByWindowID().values {
+            snapshots[card.item.window.id] = card.thumbnailLayer.contents
+        }
+
+        return snapshots
+    }
+
+    private func restoreThumbnailsFromCache(_ cachedThumbnails: [UInt32: Any]) {
+        for card in waterfallCardsByWindowID().values {
+            if let thumbnail = cachedThumbnails[card.item.window.id] {
+                card.thumbnailLayer.contents = thumbnail
+            }
         }
     }
 
@@ -2337,7 +2392,7 @@
             item.containerLayer.removeFromSuperlayer()
         }
 
-        appShelfItems = (currentViewModel?.appShelf ?? []).map { item in
+        appShelfItems = (currentViewModel?.appShelf ?? []).enumerated().map { ordinal, item in
             let container = CALayer()
             container.name = item.app.name
 
@@ -2353,6 +2408,18 @@
             icon.contents = appIconProvider.icon(for: item.app)
             icon.cornerRadius = 12
             icon.masksToBounds = false
+
+            let indexLabel = makeTextLayer(
+                string: appShelfIndexText(forOrdinal: ordinal),
+                fontSize: AppShelfMetrics.indexFontSize,
+                weight: .regular,
+                color: appShelfIndexColor,
+                alignment: .center
+            )
+            indexLabel.font = appShelfIndexFont(size: AppShelfMetrics.indexFontSize)
+            indexLabel.fontSize = AppShelfMetrics.indexFontSize
+            indexLabel.truncationMode = .end
+            indexLabel.zPosition = 14
 
             let label = makeTextLayer(
                 string: item.app.name,
@@ -2378,6 +2445,7 @@
             let closeButton = makeCloseButtonLayers()
 
             container.addSublayer(background)
+            container.addSublayer(indexLabel)
             container.addSublayer(icon)
             container.addSublayer(label)
             container.addSublayer(badge)
@@ -2388,6 +2456,7 @@
                 item: item,
                 containerLayer: container,
                 backgroundLayer: background,
+                indexLayer: indexLabel,
                 iconLayer: icon,
                 labelLayer: label,
                 badgeLayer: badge,
@@ -3421,6 +3490,13 @@
             )
             : nil
         item.iconLayer.frame = CGRect(x: iconX, y: iconY, width: visualIconSize, height: visualIconSize)
+        layoutAppShelfIndexLayer(
+            item.indexLayer,
+            iconFrame: item.iconLayer.frame,
+            iconSize: visualIconSize,
+            itemBounds: bounds,
+            highlighted: showsSelectedVisual || isHovered
+        )
         item.labelLayer.contentsScale = backingScaleFactor
         item.labelLayer.frame = CGRect(x: 0, y: 0, width: bounds.width, height: 14)
         item.badgeLayer.string = badgeText
@@ -3446,6 +3522,67 @@
         )
         item.selectedIndicatorLayer.frame = .zero
         item.selectedIndicatorLayer.backgroundColor = NSColor.clear.cgColor
+    }
+
+    private var appShelfIndexColor: NSColor {
+        NSColor.labelColor.withAlphaComponent(0.48)
+    }
+
+    private var appShelfIndexHighlightedColor: NSColor {
+        NSColor.labelColor.withAlphaComponent(0.58)
+    }
+
+    private func appShelfIndexFont(size: CGFloat) -> NSFont {
+        for fontName in ["Bradley Hand ITC", "Noteworthy-Light", "Marker Felt", "Snell Roundhand"] {
+            if let font = NSFont(name: fontName, size: size) {
+                return font
+            }
+        }
+
+        return NSFont.monospacedSystemFont(ofSize: size, weight: .regular)
+    }
+
+    private func appShelfIndexText(forOrdinal ordinal: Int) -> String {
+        let symbols = Array("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ")
+        guard ordinal >= 0 else { return "" }
+        if ordinal < symbols.count {
+            return String(symbols[ordinal])
+        }
+
+        let overflowIndex = ordinal - symbols.count
+        let prefix = symbols[10 + (overflowIndex / 26) % 26]
+        let suffix = symbols[10 + overflowIndex % 26]
+        return "\(prefix)\(suffix)"
+    }
+
+    private func layoutAppShelfIndexLayer(
+        _ layer: CATextLayer,
+        iconFrame: CGRect,
+        iconSize: CGFloat,
+        itemBounds: CGRect,
+        highlighted: Bool
+    ) {
+        let fontSize = iconSize <= AppShelfMetrics.minIconSize + 1
+            ? AppShelfMetrics.compressedIndexFontSize
+            : AppShelfMetrics.indexFontSize
+        layer.contentsScale = backingScaleFactor
+        layer.font = appShelfIndexFont(size: fontSize)
+        layer.fontSize = fontSize
+        layer.foregroundColor = (highlighted ? appShelfIndexHighlightedColor : appShelfIndexColor).cgColor
+        layer.opacity = 1
+
+        let indexWidth = AppShelfMetrics.indexWidth
+        let indexHeight = AppShelfMetrics.indexHeight
+        let preferredX = iconFrame.minX - indexWidth - 2
+        let clampedX = max(-7, min(itemBounds.width - indexWidth, preferredX))
+        let preferredY = iconFrame.maxY - indexHeight + 6
+        let clampedY = max(0, min(itemBounds.height - indexHeight, preferredY))
+        layer.frame = CGRect(
+            x: clampedX,
+            y: clampedY,
+            width: indexWidth,
+            height: indexHeight
+        )
     }
 
     private func appShelfRows() -> [[AppShelfItemLayers]] {
@@ -3648,6 +3785,10 @@
         return item.item.windowCount > 1 ? "\(item.item.windowCount)" : ""
     }
 
+    private var waterfallAppHoverHeaderFillColor: NSColor {
+        NSColor.systemGray.withAlphaComponent(0.22)
+    }
+
     private var appShelfMaxScrollOffset: CGFloat {
         max(0, appShelfContentWidth - shelfLayer.bounds.width)
     }
@@ -3733,6 +3874,7 @@
             column.countLayer.isHidden = true
 
             for card in column.cards {
+                card.containerLayer.transform = CATransform3DIdentity
                 if let frame = layout.cardFramesByWindowID[card.item.window.id] {
                     card.containerLayer.frame = frame
                 } else {
@@ -3880,7 +4022,7 @@
             height: WaterfallMetrics.headerHeight
         )
         column.headerLayer.backgroundColor = (isHovered
-            ? viewModeToggleBlueColor
+            ? waterfallAppHoverHeaderFillColor
             : (isSpaceFocused
                 ? NSColor.systemFill.withAlphaComponent(0.10)
                 : NSColor.controlBackgroundColor.withAlphaComponent(0.52))
@@ -3891,14 +4033,14 @@
             width: max(1, bounds.width - WaterfallMetrics.columnHeaderTextInset * 2),
             height: 16
         )
-        column.appNameLayer.foregroundColor = (isHovered ? NSColor.white : NSColor.labelColor).cgColor
+        column.appNameLayer.foregroundColor = NSColor.labelColor.cgColor
         column.countLayer.frame = CGRect(
             x: bounds.width - 48,
             y: bounds.height - 24,
             width: 36,
             height: 16
         )
-        column.countLayer.foregroundColor = (isHovered ? NSColor.white : NSColor.secondaryLabelColor).cgColor
+        column.countLayer.foregroundColor = NSColor.secondaryLabelColor.cgColor
 
         let appGroupIndex = column.column.appGroupIndex
         let verticalOffset = clampedWaterfallColumnOffset(
@@ -3918,6 +4060,7 @@
             let y = firstCardTop
                 - WaterfallMetrics.cardHeight
                 - CGFloat(cardIndex) * (WaterfallMetrics.cardHeight + WaterfallMetrics.cardGap)
+            card.containerLayer.transform = CATransform3DIdentity
             card.containerLayer.frame = CGRect(
                 x: WaterfallMetrics.columnHorizontalPadding,
                 y: y,
@@ -3932,8 +4075,7 @@
         let bounds = card.containerLayer.bounds
         let isSelected = card.item.window.id == effectiveSelection?.windowID
         let isHovered = card.item.window.id == hoveredWindowID
-        let isAppLinked = isHorizontalMasonryMode
-            && card.item.appGroupIndex == appIconHoverLinkedAppGroupIndex
+        let isAppLinked = isWaterfallCardAppLinked(card)
         applyWaterfallCardVisualState(
             card,
             selected: shouldShowSelectedVisual(isSelected: isSelected, isHovered: isHovered),
@@ -4586,6 +4728,26 @@
         return card.item.primarySpaceID == activeSpaceFocusID
     }
 
+    private func isWaterfallCardAppLinked(_ card: WaterfallCardLayers) -> Bool {
+        guard card.item.appGroupIndex == appIconHoverLinkedAppGroupIndex else { return false }
+
+        if isHorizontalMasonryMode {
+            return true
+        }
+
+        return waterfallColumns
+            .first(where: { $0.column.appGroupIndex == card.item.appGroupIndex })?
+            .cards
+            .sorted { lhs, rhs in
+                if lhs.item.windowIndex != rhs.item.windowIndex {
+                    return lhs.item.windowIndex < rhs.item.windowIndex
+                }
+                return lhs.item.window.id < rhs.item.window.id
+            }
+            .first?
+            .item.window.id == card.item.window.id
+    }
+
     private func waterfallColumnWidth() -> CGFloat {
         guard waterfallColumns.count > 0 else { return WaterfallMetrics.columnWidth }
 
@@ -5040,6 +5202,12 @@
                 "frame": dictionary(from: item.containerLayer.frame),
                 "visibleFrame": dictionary(from: visibleFrame),
                 "iconFrame": dictionary(from: item.iconLayer.frame),
+                "indexText": item.indexLayer.string ?? "",
+                "indexFrame": dictionary(from: item.indexLayer.frame),
+                "indexVisible": !item.indexLayer.isHidden && item.indexLayer.opacity > 0,
+                "indexFontSize": Double(item.indexLayer.fontSize),
+                "indexOpacity": Double(item.indexLayer.opacity),
+                "indexColor": colorDictionary(from: item.indexLayer.foregroundColor),
                 "labelFrame": dictionary(from: item.labelLayer.frame),
                 "labelTruncationMode": "middle",
                 "badgeVisible": !item.badgeLayer.isHidden,
@@ -5315,8 +5483,7 @@
         var states: [String] = []
         let isSelected = card.item.window.id == effectiveSelection?.windowID
         let isHovered = card.item.window.id == hoveredWindowID
-        let isAppLinked = isHorizontalMasonryMode
-            && card.item.appGroupIndex == appIconHoverLinkedAppGroupIndex
+        let isAppLinked = isWaterfallCardAppLinked(card)
         if shouldShowSelectedVisual(isSelected: isSelected, isHovered: isHovered) {
             states.append("selected")
         } else if isSelected {

--
Gitblit v1.9.3