Skip to content

Commit

Permalink
setSystemImage
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPodymov committed Dec 15, 2023
1 parent be356cf commit 01b76bd
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Sources/CommonAppleKit/CommonAppleKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@
}

public extension CAButton {
private static var states: [UIControl.State] {
[.normal, .selected, .highlighted, .disabled, .focused]
}

func setTitle(_ title: String) {
Self.states.forEach { state in
setTitle(title, for: state)
}
}

func setImage(_ image: CAImage) {
Self.states.forEach { state in
setImage(image, for: state)
}
}

func addTargetForPrimaryActionTriggered(
_ target: AnyObject,
action: Selector
Expand Down Expand Up @@ -115,10 +131,14 @@
}

public extension CAButton {
func setTitleForAllStates(_ title: String) {
func setTitle(_ title: String) {
self.title = title
}

func setImage(_ image: CAImage) {
self.image = image
}

func addTargetForPrimaryActionTriggered(
_ target: AnyObject,
action: Selector
Expand Down Expand Up @@ -174,3 +194,19 @@
}
}
#endif

public extension CAButton {
func setSystemImage(_ systemName: String) {
let image: CAImage?
if #available(macOS 11.0, iOS 13.0, tvOS 13.0, *) {
image = CAImage(systemName: systemName)
} else {
image = nil
}
if let image {
setImage(image)
} else {
setTitle(systemName)
}
}
}

0 comments on commit 01b76bd

Please sign in to comment.