|
7 | 7 |
|
8 | 8 | import SwiftUI |
9 | 9 |
|
| 10 | +@available(macOS 11.0, *) |
10 | 11 | public struct HoverView<Content>: View where Content: View { |
11 | 12 |
|
12 | 13 | // MARK: - Public Wrapped Properties |
13 | 14 |
|
14 | 15 | /// The content to display. |
15 | | - @ViewBuilder public let content: (Bool) -> Content |
| 16 | + @ViewBuilder public let content: (Bool, Bool) -> Content |
16 | 17 |
|
17 | 18 | /// Whether the view is hovered. |
18 | 19 | @State public var hover: Bool = false |
| 20 | + /// Whether the view is clicked. |
| 21 | + @State public var clicked: Bool = false |
| 22 | + |
| 23 | + // MARK: - Public Properties |
| 24 | + |
| 25 | + /// The action to perform on click. |
| 26 | + public let action: () -> Void |
19 | 27 |
|
20 | 28 | // MARK: - Public Body View |
21 | 29 |
|
22 | 30 | public var body: some View { |
23 | | - content(hover).onHover { hover = $0 } |
| 31 | + Button(action: action) { |
| 32 | + content(hover, clicked).onHover { hover = $0 } |
| 33 | + } |
| 34 | + .buttonStyle(HoverButtonStyle(pressed: $clicked)) |
24 | 35 | } |
25 | 36 |
|
26 | 37 | // MARK: - Public Initalizers |
27 | 38 |
|
28 | | - public init(content: @escaping (Bool) -> Content) { |
29 | | - self.content = content |
| 39 | + public init(action: @escaping () -> Void = {}, content c: @escaping (Bool, Bool) -> Content) { |
| 40 | + self.action = action |
| 41 | + self.content = c |
| 42 | + } |
| 43 | + |
| 44 | + public init(action: @escaping () -> Void = {}, content c: @escaping (Bool) -> Content) { |
| 45 | + self.action = action |
| 46 | + self.content = { (hover: Bool, click: Bool) in |
| 47 | + return c(hover) |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +@available(macOS 11.0, *) |
| 53 | +fileprivate struct HoverButtonStyle: ButtonStyle { |
| 54 | + let pressed: Binding<Bool> |
| 55 | + func makeBody(configuration: Configuration) -> some View { |
| 56 | + configuration.label.onChange(of: configuration.isPressed) { val in pressed.wrappedValue = val } |
30 | 57 | } |
31 | 58 | } |
0 commit comments