Skip to content

Commit

Permalink
add tvOS support
Browse files Browse the repository at this point in the history
  • Loading branch information
xnth97 committed Mar 3, 2022
1 parent aee83ac commit 28a6843
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let package = Package(
platforms: [
.iOS(.v14),
.macOS(.v12),
.tvOS(.v15),
],
products: [
.library(
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# SymbolPicker

A simple and searchable SFSymbol Picker for SwiftUI
A simple and cross-platform SFSymbol Picker for SwiftUI

![](https://img.shields.io/badge/License-MIT-green)
![](https://img.shields.io/badge/Platform-iOS-blue)
![](https://img.shields.io/badge/Platform-iOS%20%7C%20macOS%20%7C%20tvOS-blue)

## Features

SymbolPicker provides a simple interface for picking a SFSymbol with search functionality that is backported to iOS 14. SymbolPicker is implemented with SwiftUI and is suggested to use within `sheet` (please see example below).
SymbolPicker provides a simple and cross-platform interface for picking a SFSymbol with search functionality that is backported to iOS 14. SymbolPicker is implemented with SwiftUI and supports iOS, macOS and tvOS platforms.

## Usage

### Requirements

* iOS 14.0+ / macOS 12.0+
* iOS 14.0+ / macOS 12.0+ / tvOS 15.0+
* Xcode 13.0+
* Swift 5.0+

Expand All @@ -29,6 +29,8 @@ dependencies: [

### Example

It is suggested to use SymbolPicker within a `sheet`.

```swift
import SwiftUI
import SymbolPicker
Expand Down Expand Up @@ -58,7 +60,7 @@ struct ContentView: View {
## TODO

- [ ] Categories support
- [ ] Multiplatform
- [x] Multiplatform
- [ ] Inline UI
- [ ] Codegen from latest SF Symbols

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"search_placeholder" = "Search";
"cancel" = "Cancel";
"sf_symbol_picker" = "SF Symbol Picker";
"sf_symbol_picker" = "Select a symbol";
"done" = "Done";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"search_placeholder" = "搜索";
"cancel" = "取消";
"sf_symbol_picker" = "选择系统符号";
"sf_symbol_picker" = "选择符号";
"done" = "确定";
29 changes: 24 additions & 5 deletions Sources/SymbolPicker/SymbolPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import SwiftUI
#endif

public struct SymbolPicker: View {

// MARK: - Static consts

private static let symbols: [String] = {
Expand All @@ -32,8 +33,10 @@ public struct SymbolPicker: View {
}()

private static var gridDimension: CGFloat {
#if os(iOS) || os(tvOS)
#if os(iOS)
return 64
#elseif os(tvOS)
return 128
#elseif os(macOS)
return 30
#else
Expand All @@ -42,8 +45,10 @@ public struct SymbolPicker: View {
}

private static var symbolSize: CGFloat {
#if os(iOS) || os(tvOS)
#if os(iOS)
return 24
#elseif os(tvOS)
return 48
#elseif os(macOS)
return 14
#else
Expand All @@ -52,8 +57,10 @@ public struct SymbolPicker: View {
}

private static var symbolCornerRadius: CGFloat {
#if os(iOS) || os(tvOS)
#if os(iOS)
return 8
#elseif os(tvOS)
return 12
#elseif os(macOS)
return 4
#else
Expand All @@ -77,7 +84,7 @@ public struct SymbolPicker: View {

@ViewBuilder
private var searchableSymbolGrid: some View {
#if os(iOS) || os(tvOS)
#if os(iOS)
if #available(iOS 15.0, *) {
symbolGrid
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
Expand All @@ -95,6 +102,9 @@ public struct SymbolPicker: View {
.padding()
}
}
#elseif os(tvOS)
symbolGrid
.searchable(text: $searchText, placement: .automatic)
#elseif os(macOS)
VStack(spacing: 10) {
TextField(LocalizedString("search_placeholder"), text: $searchText)
Expand All @@ -107,13 +117,16 @@ public struct SymbolPicker: View {
}

internal func dynamicColor(light: PlatformColor, dark: PlatformColor) -> Color {
#if os(iOS) || os(tvOS)
#if os(iOS)
let color = PlatformColor { $0.userInterfaceStyle == .dark ? dark : light }
if #available(iOS 15.0, *) {
return Color(uiColor: color)
} else {
return Color(color)
}
#elseif os(tvOS)
let color = PlatformColor { $0.userInterfaceStyle == .dark ? dark : light }
return Color(uiColor: color)
#elseif os(macOS)
let color = PlatformColor(name: nil) { $0.name == .darkAqua ? dark : light }
if #available(macOS 12.0, *) {
Expand Down Expand Up @@ -162,7 +175,11 @@ public struct SymbolPicker: View {
Image(systemName: thisSymbol)
.font(.system(size: Self.symbolSize))
.frame(maxWidth: .infinity, minHeight: Self.gridDimension)
#if !os(tvOS)
.background(Color.accentColor)
#else
.background(Color.gray.opacity(0.3))
#endif
.cornerRadius(Self.symbolCornerRadius)
.foregroundColor(.white)
} else {
Expand All @@ -187,7 +204,9 @@ public struct SymbolPicker: View {
secondarySystemBackground.edgesIgnoringSafeArea(.all)
searchableSymbolGrid
}
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(LocalizedString("cancel")) {
Expand Down

0 comments on commit 28a6843

Please sign in to comment.