InteractiveHoverStyle.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import AppKit
  2. import SwiftUI
  3. // MARK: - Theme Tokens
  4. extension AppTheme {
  5. static let hoverAnimation: Animation = .easeOut(duration: 0.15)
  6. static let hoverPressScale: CGFloat = 0.98
  7. static let hoverLiftScale: CGFloat = 1.012
  8. }
  9. // MARK: - Button Styles
  10. struct AppPlainButtonStyle: ButtonStyle {
  11. @State private var isHovered = false
  12. func makeBody(configuration: Configuration) -> some View {
  13. configuration.label
  14. .scaleEffect(
  15. configuration.isPressed ? AppTheme.hoverPressScale : (isHovered ? AppTheme.hoverLiftScale : 1)
  16. )
  17. .brightness(configuration.isPressed ? -0.04 : (isHovered ? 0.05 : 0))
  18. .animation(AppTheme.hoverAnimation, value: isHovered)
  19. .animation(AppTheme.hoverAnimation, value: configuration.isPressed)
  20. .onHover { isHovered = $0 }
  21. .hoverCursor()
  22. }
  23. }
  24. struct AppSecondaryButtonStyle: ButtonStyle {
  25. @State private var isHovered = false
  26. func makeBody(configuration: Configuration) -> some View {
  27. configuration.label
  28. .foregroundStyle(AppTheme.textSecondary)
  29. .padding(.horizontal, 12)
  30. .padding(.vertical, 8)
  31. .background(secondaryBackground(isPressed: configuration.isPressed))
  32. .clipShape(RoundedRectangle(cornerRadius: 8))
  33. .overlay(
  34. RoundedRectangle(cornerRadius: 8)
  35. .stroke(
  36. isHovered || configuration.isPressed
  37. ? AppTheme.hoverBorder
  38. : AppTheme.cardBorder,
  39. lineWidth: 1
  40. )
  41. )
  42. .scaleEffect(configuration.isPressed ? AppTheme.hoverPressScale : (isHovered ? AppTheme.hoverLiftScale : 1))
  43. .animation(AppTheme.hoverAnimation, value: isHovered)
  44. .animation(AppTheme.hoverAnimation, value: configuration.isPressed)
  45. .onHover { isHovered = $0 }
  46. .hoverCursor()
  47. }
  48. private func secondaryBackground(isPressed: Bool) -> Color {
  49. if isPressed {
  50. return AppTheme.cardBackground.opacity(0.55)
  51. }
  52. if isHovered {
  53. return AppTheme.cardBackground.opacity(0.85)
  54. }
  55. return AppTheme.cardBackground
  56. }
  57. }
  58. struct AppPrimaryButtonStyle: ButtonStyle {
  59. @State private var isHovered = false
  60. func makeBody(configuration: Configuration) -> some View {
  61. configuration.label
  62. .brightness(configuration.isPressed ? -0.08 : (isHovered ? 0.08 : 0))
  63. .scaleEffect(configuration.isPressed ? AppTheme.hoverPressScale : (isHovered ? AppTheme.hoverLiftScale : 1))
  64. .animation(AppTheme.hoverAnimation, value: isHovered)
  65. .animation(AppTheme.hoverAnimation, value: configuration.isPressed)
  66. .onHover { isHovered = $0 }
  67. .hoverCursor()
  68. }
  69. }
  70. struct AppTextLinkButtonStyle: ButtonStyle {
  71. @State private var isHovered = false
  72. func makeBody(configuration: Configuration) -> some View {
  73. configuration.label
  74. .foregroundStyle(isHovered ? AppTheme.textPrimary : AppTheme.textSecondary)
  75. .underline(isHovered || configuration.isPressed)
  76. .animation(AppTheme.hoverAnimation, value: isHovered)
  77. .animation(AppTheme.hoverAnimation, value: configuration.isPressed)
  78. .onHover { isHovered = $0 }
  79. .hoverCursor()
  80. }
  81. }
  82. // MARK: - View Modifiers
  83. private struct HoverCursorModifier: ViewModifier {
  84. @State private var isHovered = false
  85. func body(content: Content) -> some View {
  86. content
  87. .onHover { hovering in
  88. isHovered = hovering
  89. if hovering {
  90. NSCursor.pointingHand.push()
  91. } else {
  92. NSCursor.pop()
  93. }
  94. }
  95. }
  96. }
  97. private struct HoverOverlayModifier: ViewModifier {
  98. let cornerRadius: CGFloat
  99. var isEnabled: Bool = true
  100. @State private var isHovered = false
  101. func body(content: Content) -> some View {
  102. content
  103. .overlay {
  104. if isEnabled && isHovered {
  105. RoundedRectangle(cornerRadius: cornerRadius)
  106. .fill(AppTheme.hoverFill)
  107. .allowsHitTesting(false)
  108. }
  109. }
  110. .animation(AppTheme.hoverAnimation, value: isHovered)
  111. .onHover { isHovered = $0 }
  112. }
  113. }
  114. private struct HoverCardModifier: ViewModifier {
  115. let cornerRadius: CGFloat
  116. var isSelected: Bool = false
  117. var isEnabled: Bool = true
  118. @State private var isHovered = false
  119. func body(content: Content) -> some View {
  120. content
  121. .overlay {
  122. if isEnabled && isHovered {
  123. RoundedRectangle(cornerRadius: cornerRadius)
  124. .fill(isSelected ? AppTheme.selectedRowFill : AppTheme.hoverFill)
  125. .allowsHitTesting(false)
  126. }
  127. }
  128. .overlay {
  129. if isEnabled && isHovered {
  130. RoundedRectangle(cornerRadius: cornerRadius)
  131. .stroke(AppTheme.hoverBorder, lineWidth: 1)
  132. .allowsHitTesting(false)
  133. }
  134. }
  135. .scaleEffect(isEnabled && isHovered ? AppTheme.hoverLiftScale : 1)
  136. .animation(AppTheme.hoverAnimation, value: isHovered)
  137. .onHover { isHovered = $0 }
  138. }
  139. }
  140. private struct SidebarRowHoverModifier: ViewModifier {
  141. let isSelected: Bool
  142. let showsCardBackground: Bool
  143. let selectedBorderColor: Color
  144. @State private var isHovered = false
  145. func body(content: Content) -> some View {
  146. content
  147. .background(sidebarRowBackground)
  148. .animation(AppTheme.hoverAnimation, value: isHovered)
  149. .onHover { isHovered = $0 }
  150. }
  151. @ViewBuilder
  152. private var sidebarRowBackground: some View {
  153. if showsCardBackground && isSelected {
  154. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  155. .fill(AppTheme.quickAccessBackground)
  156. .overlay(
  157. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  158. .stroke(selectedBorderColor, lineWidth: 1)
  159. )
  160. } else if isSelected {
  161. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  162. .fill(AppTheme.selectedRowFill)
  163. } else if isHovered {
  164. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  165. .fill(AppTheme.hoverFill)
  166. }
  167. }
  168. }
  169. // MARK: - View Extensions
  170. extension View {
  171. func hoverCursor(_ enabled: Bool = true) -> some View {
  172. modifier(HoverCursorEnabledModifier(enabled: enabled))
  173. }
  174. func hoverOverlay(cornerRadius: CGFloat, isEnabled: Bool = true) -> some View {
  175. modifier(HoverOverlayModifier(cornerRadius: cornerRadius, isEnabled: isEnabled))
  176. }
  177. func hoverCard(cornerRadius: CGFloat, isSelected: Bool = false, isEnabled: Bool = true) -> some View {
  178. modifier(HoverCardModifier(cornerRadius: cornerRadius, isSelected: isSelected, isEnabled: isEnabled))
  179. }
  180. func sidebarRowHover(
  181. isSelected: Bool,
  182. showsCardBackground: Bool = false,
  183. selectedBorderColor: Color = .clear
  184. ) -> some View {
  185. modifier(
  186. SidebarRowHoverModifier(
  187. isSelected: isSelected,
  188. showsCardBackground: showsCardBackground,
  189. selectedBorderColor: selectedBorderColor
  190. )
  191. )
  192. }
  193. }
  194. private struct HoverCursorEnabledModifier: ViewModifier {
  195. let enabled: Bool
  196. func body(content: Content) -> some View {
  197. if enabled {
  198. content.modifier(HoverCursorModifier())
  199. } else {
  200. content
  201. }
  202. }
  203. }