InteractiveHoverStyle.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 AppDestructiveButtonStyle: ButtonStyle {
  59. private static let destructiveColor = Color(hex: 0xEF4444)
  60. @State private var isHovered = false
  61. func makeBody(configuration: Configuration) -> some View {
  62. configuration.label
  63. .foregroundStyle(Self.destructiveColor)
  64. .padding(.horizontal, 12)
  65. .padding(.vertical, 8)
  66. .background(destructiveBackground(isPressed: configuration.isPressed))
  67. .clipShape(RoundedRectangle(cornerRadius: 8))
  68. .overlay(
  69. RoundedRectangle(cornerRadius: 8)
  70. .stroke(
  71. isHovered || configuration.isPressed
  72. ? Self.destructiveColor.opacity(0.55)
  73. : Self.destructiveColor.opacity(0.25),
  74. lineWidth: 1
  75. )
  76. )
  77. .scaleEffect(configuration.isPressed ? AppTheme.hoverPressScale : (isHovered ? AppTheme.hoverLiftScale : 1))
  78. .animation(AppTheme.hoverAnimation, value: isHovered)
  79. .animation(AppTheme.hoverAnimation, value: configuration.isPressed)
  80. .onHover { isHovered = $0 }
  81. .hoverCursor()
  82. }
  83. private func destructiveBackground(isPressed: Bool) -> Color {
  84. if isPressed {
  85. return Self.destructiveColor.opacity(0.18)
  86. }
  87. if isHovered {
  88. return Self.destructiveColor.opacity(0.12)
  89. }
  90. return Self.destructiveColor.opacity(0.08)
  91. }
  92. }
  93. struct AppPrimaryButtonStyle: ButtonStyle {
  94. @State private var isHovered = false
  95. func makeBody(configuration: Configuration) -> some View {
  96. configuration.label
  97. .brightness(configuration.isPressed ? -0.08 : (isHovered ? 0.08 : 0))
  98. .scaleEffect(configuration.isPressed ? AppTheme.hoverPressScale : (isHovered ? AppTheme.hoverLiftScale : 1))
  99. .animation(AppTheme.hoverAnimation, value: isHovered)
  100. .animation(AppTheme.hoverAnimation, value: configuration.isPressed)
  101. .onHover { isHovered = $0 }
  102. .hoverCursor()
  103. }
  104. }
  105. struct AppTextLinkButtonStyle: ButtonStyle {
  106. @State private var isHovered = false
  107. func makeBody(configuration: Configuration) -> some View {
  108. configuration.label
  109. .foregroundStyle(isHovered ? AppTheme.textPrimary : AppTheme.textSecondary)
  110. .underline(isHovered || configuration.isPressed)
  111. .animation(AppTheme.hoverAnimation, value: isHovered)
  112. .animation(AppTheme.hoverAnimation, value: configuration.isPressed)
  113. .onHover { isHovered = $0 }
  114. .hoverCursor()
  115. }
  116. }
  117. // MARK: - View Modifiers
  118. private struct HoverCursorModifier: ViewModifier {
  119. @State private var isHovered = false
  120. func body(content: Content) -> some View {
  121. content
  122. .onHover { hovering in
  123. isHovered = hovering
  124. if hovering {
  125. NSCursor.pointingHand.push()
  126. } else {
  127. NSCursor.pop()
  128. }
  129. }
  130. }
  131. }
  132. private struct HoverOverlayModifier: ViewModifier {
  133. let cornerRadius: CGFloat
  134. var isEnabled: Bool = true
  135. @State private var isHovered = false
  136. func body(content: Content) -> some View {
  137. content
  138. .overlay {
  139. if isEnabled && isHovered {
  140. RoundedRectangle(cornerRadius: cornerRadius)
  141. .fill(AppTheme.hoverFill)
  142. .allowsHitTesting(false)
  143. }
  144. }
  145. .animation(AppTheme.hoverAnimation, value: isHovered)
  146. .onHover { isHovered = $0 }
  147. }
  148. }
  149. private struct HoverCardModifier: ViewModifier {
  150. let cornerRadius: CGFloat
  151. var isSelected: Bool = false
  152. var isEnabled: Bool = true
  153. @State private var isHovered = false
  154. func body(content: Content) -> some View {
  155. content
  156. .overlay {
  157. if isEnabled && isHovered {
  158. RoundedRectangle(cornerRadius: cornerRadius)
  159. .fill(isSelected ? AppTheme.selectedRowFill : AppTheme.hoverFill)
  160. .allowsHitTesting(false)
  161. }
  162. }
  163. .overlay {
  164. if isEnabled && isHovered {
  165. RoundedRectangle(cornerRadius: cornerRadius)
  166. .stroke(AppTheme.hoverBorder, lineWidth: 1)
  167. .allowsHitTesting(false)
  168. }
  169. }
  170. .scaleEffect(isEnabled && isHovered ? AppTheme.hoverLiftScale : 1)
  171. .animation(AppTheme.hoverAnimation, value: isHovered)
  172. .onHover { isHovered = $0 }
  173. }
  174. }
  175. private struct SidebarRowHoverModifier: ViewModifier {
  176. let isSelected: Bool
  177. var selectedBorderColor: Color = AppTheme.accentPurple.opacity(0.4)
  178. @State private var isHovered = false
  179. func body(content: Content) -> some View {
  180. content
  181. .background(sidebarRowBackground)
  182. .animation(AppTheme.hoverAnimation, value: isHovered)
  183. .onHover { isHovered = $0 }
  184. }
  185. @ViewBuilder
  186. private var sidebarRowBackground: some View {
  187. if isSelected {
  188. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  189. .fill(AppTheme.quickAccessBackground)
  190. .overlay(
  191. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  192. .stroke(selectedBorderColor, lineWidth: 1)
  193. )
  194. } else if isHovered {
  195. RoundedRectangle(cornerRadius: AppTheme.cornerRadiusSmall)
  196. .fill(AppTheme.hoverFill)
  197. }
  198. }
  199. }
  200. // MARK: - View Extensions
  201. extension View {
  202. func hoverCursor(_ enabled: Bool = true) -> some View {
  203. modifier(HoverCursorEnabledModifier(enabled: enabled))
  204. }
  205. func hoverOverlay(cornerRadius: CGFloat, isEnabled: Bool = true) -> some View {
  206. modifier(HoverOverlayModifier(cornerRadius: cornerRadius, isEnabled: isEnabled))
  207. }
  208. func hoverCard(cornerRadius: CGFloat, isSelected: Bool = false, isEnabled: Bool = true) -> some View {
  209. modifier(HoverCardModifier(cornerRadius: cornerRadius, isSelected: isSelected, isEnabled: isEnabled))
  210. }
  211. func sidebarRowHover(
  212. isSelected: Bool,
  213. selectedBorderColor: Color = AppTheme.accentPurple.opacity(0.4)
  214. ) -> some View {
  215. modifier(
  216. SidebarRowHoverModifier(
  217. isSelected: isSelected,
  218. selectedBorderColor: selectedBorderColor
  219. )
  220. )
  221. }
  222. }
  223. private struct HoverCursorEnabledModifier: ViewModifier {
  224. let enabled: Bool
  225. func body(content: Content) -> some View {
  226. if enabled {
  227. content.modifier(HoverCursorModifier())
  228. } else {
  229. content
  230. }
  231. }
  232. }