Skip to content

Commit

Permalink
Make custom buttons accept first mouse input
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Feb 18, 2024
1 parent 93f2c01 commit c8df752
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion Ice/UI/Styles/CustomButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ struct CustomButtonStyle: PrimitiveButtonStyle {
func updateNSView(_: NSView, context: Context) { }
}

/// Custom view that ensures that the button accepts
/// the first mouse input.
private struct FirstMouseOverlay: NSViewRepresentable {
private class Represented: NSView {
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
return true
}
}

func makeNSView(context: Context) -> NSView {
return Represented()
}

func updateNSView(_: NSView, context: Context) { }
}

/// Custom shape that draws a rounded rectangle with some of
/// its sides flattened according to the given button shape.
private struct ClipShape: Shape {
Expand Down Expand Up @@ -69,10 +85,15 @@ struct CustomButtonStyle: PrimitiveButtonStyle {
.opacity(customButtonConfiguration.isHighlighted ? 0.2 : 0)
.blendMode(.overlay)
.background(isPressed ? .tertiary : .quaternary)
.background { MouseDownInterceptor() }
.background {
MouseDownInterceptor()
}
.clipShape(ClipShape(cornerRadius: 5, shape: customButtonConfiguration.shape))
.opacity(customButtonConfiguration.bezelOpacity)
}
.overlay {
FirstMouseOverlay()
}
.simultaneousGesture(
DragGesture(minimumDistance: 0)
.onChanged { value in
Expand Down

0 comments on commit c8df752

Please sign in to comment.