Skip to content

Commit

Permalink
Add keyboard shortcut modifier for standard button type
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Jan 20, 2025
1 parent a9c4746 commit 417bfd1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
7 changes: 4 additions & 3 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ SwiftUIKit makes its best effort to honor semver, but breaking changes can occur



## 5.0.4
## 5.1

### ✨ Features

* `Button+Standard` has support for a new `.search` type.

* `StandardButtonType` has been promoted to a top-level type.
* `View` has a new `StandardButtonType`-based `.keyboardShortcut` modifier.



## 5.0.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public extension Button {

/// Create a new ``StandardType``-based button.
init(
_ type: StandardType,
_ type: ButtonType,
_ title: LocalizedStringKey? = nil,
_ icon: Image? = nil,
bundle: Bundle? = nil,
Expand All @@ -25,22 +25,23 @@ public extension Button {
)
}
}

/// This enum defines standard button types and provides
/// standard localized texts and icons.
enum StandardType: String, CaseIterable, Identifiable {
case add, addFavorite, addToFavorites,
cancel, call, copy,
delete, deselect, done,
edit, email,
ok,
paste,
removeFavorite, removeFromFavorites,
search, select, share
}
}

public extension Button.StandardType {

/// This enum defines standard button types and provides
/// standard localized texts and icons.
public enum ButtonType: String, CaseIterable, Identifiable {
case add, addFavorite, addToFavorites,
cancel, call, copy,
delete, deselect, done,
edit, email,
ok,
paste,
removeFavorite, removeFromFavorites,
search, select, share
}

public extension ButtonType {

var id: String { rawValue }

Expand Down Expand Up @@ -72,13 +73,19 @@ public extension Button.StandardType {
}
}

var keyboardShortcut: String? {
var keyboardShortcut: KeyEquivalent? {
switch self {
case .search: "f"
default: nil
}
}

var keyboardShortcutModifier: EventModifiers? {
switch self {
default: .command
}
}

var role: ButtonRole? {
switch self {
case .cancel: .cancel
Expand Down Expand Up @@ -110,6 +117,25 @@ public extension Button.StandardType {
}
}
}

public extension View {

@ViewBuilder
func keyboardShortcut(
_ button: ButtonType
) -> some View {
if let shortcut = button.keyboardShortcut {
if let modifier = button.keyboardShortcutModifier {
self.keyboardShortcut(shortcut, modifiers: modifier)
} else {
self.keyboardShortcut(shortcut)
}
} else {
self
}
}
}

/*
#Preview {

Expand Down

0 comments on commit 417bfd1

Please sign in to comment.