Jotley / app / macOS / Views / CustomTooltip.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | |
| 4 | // MARK: - Tooltip Content View |
| 5 | struct TooltipContentView: View { |
| 6 | let text: String |
| 7 | |
| 8 | var body: some View { |
| 9 | Text(text) |
| 10 | .font(.system(size: 11, weight: .medium)) |
| 11 | .foregroundColor(.white) |
| 12 | .padding(.horizontal, 8) |
| 13 | .padding(.vertical, 4) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | // MARK: - Floating Tooltip Modifier |
| 18 | struct FloatingTooltip: ViewModifier { |
| 19 | let text: String |
| 20 | @State private var isHovering = false |
| 21 | @State private var showTooltip = false |
| 22 | @State private var hoverWorkItem: DispatchWorkItem? |
| 23 | |
| 24 | func body(content: Content) -> some View { |
| 25 | content |
| 26 | .onHover { hovering in |
| 27 | isHovering = hovering |
| 28 | |
| 29 | // Cancel any pending show/hide operations |
| 30 | hoverWorkItem?.cancel() |
| 31 | |
| 32 | if hovering { |
| 33 | // Create a new work item to show tooltip after delay |
| 34 | let workItem = DispatchWorkItem { |
| 35 | if isHovering { |
| 36 | withAnimation(.easeInOut(duration: 0.12)) { |
| 37 | showTooltip = true |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | hoverWorkItem = workItem |
| 42 | DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: workItem) |
| 43 | } else { |
| 44 | // Hide immediately on mouse leave |
| 45 | withAnimation(.easeInOut(duration: 0.08)) { |
| 46 | showTooltip = false |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | .overlay { |
| 51 | if showTooltip { |
| 52 | TooltipContentView(text: text) |
| 53 | .fixedSize() |
| 54 | .padding(.vertical, 4) |
| 55 | .padding(.horizontal, 2) |
| 56 | .background( |
| 57 | RoundedRectangle(cornerRadius: 6, style: .continuous) |
| 58 | .fill(Color(red: 0.15, green: 0.15, blue: 0.15)) |
| 59 | ) |
| 60 | .offset(y: -34) |
| 61 | .allowsHitTesting(false) |
| 62 | .transition(.opacity) |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | extension View { |
| 69 | func floatingTooltip(_ text: String) -> some View { |
| 70 | self.modifier(FloatingTooltip(text: text)) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Add this extension to disable clipping |