Skip to content

Instantly share code, notes, and snippets.

@OskarGroth
Created December 18, 2021 11:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OskarGroth/a7d921cd4137dc713d49549216d6ebd0 to your computer and use it in GitHub Desktop.
Save OskarGroth/a7d921cd4137dc713d49549216d6ebd0 to your computer and use it in GitHub Desktop.
struct ContentView: View {
@State var selected: Bool = false
var body: some View {
Color.clear
.frame(width: 900, height: 400)
.toolbar {
ToolbarItem {
Button(action: {}, label: {
Image(systemName: "sidebar.leading")
})
}
ToolbarItem {
ToolbarButton(action: { print("action") }) {
Image(systemName: "sidebar.leading")
}
}
}
}
}
struct ToolbarButton<Content: View>: View {
@Environment(\.colorScheme) var colorScheme
let action: (() -> Void)
let content: () -> Content
@State private var mouseDown = false
@State private var isHovered = false
var body: some View {
content()
.foregroundColor(Color(.controlTextColor))
.font(.system(size: 17, weight: .regular))
.opacity(foregroundOpacity())
.padding(.bottom, 1)
.padding([.leading, .trailing], 8)
.frame(height: 28)
.background(
Color(.unemphasizedSelectedContentBackgroundColor).opacity(backgroundOpacity())
)
.blendMode(colorScheme == .dark ? .plusLighter : .plusDarker)
.onHover {
isHovered = $0
}
.onTapGesture {
action()
}
.onLongPressGesture(minimumDuration: 2, pressing: {
mouseDown = $0
}, perform: { })
.cornerRadius(6)
}
func foregroundOpacity() -> Double {
if mouseDown {
return 1
}
return colorScheme == .dark ? 0.45 : 0.65
}
func backgroundOpacity() -> Double {
if mouseDown {
return colorScheme == .dark ? 0.3 : 0.75
} else if isHovered {
return colorScheme == .dark ? 0.15 : 0.4
}
return 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment