From f243b7ae34e1b3bcafbf3d63f588413ee39b7b90 Mon Sep 17 00:00:00 2001 From: smithdude Date: Thu, 1 Oct 2020 11:20:29 +0900 Subject: [PATCH] update for macos --- Package.swift | 2 +- Sources/ClockTimePicker/ClockHand.swift | 20 +++++++++------- Sources/ClockTimePicker/ClockLooks.swift | 21 ++++++++++++---- Sources/ClockTimePicker/ClockPickerView.swift | 24 ++++++++++--------- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/Package.swift b/Package.swift index dca3a9d..afd474d 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.1 +// swift-tools-version:5.2 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription diff --git a/Sources/ClockTimePicker/ClockHand.swift b/Sources/ClockTimePicker/ClockHand.swift index 4380d0f..044e5ff 100644 --- a/Sources/ClockTimePicker/ClockHand.swift +++ b/Sources/ClockTimePicker/ClockHand.swift @@ -27,7 +27,9 @@ public struct ClockHand: View { @State var handleColor = Color.primary @State var handleRimColor = Color.primary - let impact = UIImpactFeedbackGenerator(style: .light) + #if os(iOS) + let impact = UIImpactFeedbackGenerator(style: .light) + #endif @State private var prevHour = 0 @@ -70,7 +72,7 @@ public struct ClockHand: View { } } - var dragHandle: some View { + public var dragHandle: some View { return ZStack { Circle().overlay( Circle().stroke(self.handleRimColor, lineWidth: self.options.handleRimWidth) @@ -79,7 +81,7 @@ public struct ClockHand: View { } } - func startHandPos(_ geom: GeometryProxy) { + public func startHandPos(_ geom: GeometryProxy) { // the looks handColor = handType == .hour ? options.hourHandColor : options.minuteHandColor handWidth = handType == .hour ? options.hourHandWidth : options.minuteHandWidth @@ -108,7 +110,7 @@ public struct ClockHand: View { } } - func updateClockHour(_ angleDeg: Float) { + public func updateClockHour(_ angleDeg: Float) { // normalise to 0-360 let theAngle = normalise(angleDeg) // 1 hour is 30 deg @@ -127,10 +129,12 @@ public struct ClockHand: View { clockDate = newDate } adjustClockDate() - if options.impactFeedbackOn && Calendar.current.component(.hour, from: clockDate) != prevHour { - prevHour = Calendar.current.component(.hour, from: clockDate) - impact.impactOccurred() - } + #if os(iOS) + if options.impactFeedbackOn && Calendar.current.component(.hour, from: clockDate) != prevHour { + prevHour = Calendar.current.component(.hour, from: clockDate) + impact.impactOccurred() + } + #endif } private func updateClockMinute(_ angleDeg: Float) { diff --git a/Sources/ClockTimePicker/ClockLooks.swift b/Sources/ClockTimePicker/ClockLooks.swift index c900cc7..720823c 100644 --- a/Sources/ClockTimePicker/ClockLooks.swift +++ b/Sources/ClockTimePicker/ClockLooks.swift @@ -8,6 +8,11 @@ import Foundation import SwiftUI +#if os(iOS) + import UIKit +#elseif os(OSX) + import AppKit +#endif public class ClockLooks : ObservableObject { @@ -59,12 +64,18 @@ public class ClockLooks : ObservableObject { // the text showing the hour and minutes at the center of the clock @Published public var centerTextFont: Font = Font.custom("Helvetica", size: 20) @Published public var centerBackgroundColor: Color = Color.primary - @Published public var centerForegroundColor: Color = Color(UIColor.systemBackground) - // AM:PM - @Published public var ampmSelectedColor: UIColor = UIColor.white - @Published public var ampmNormalColor: UIColor = UIColor.black - @Published public var ampmTintColor: UIColor = UIColor.black + #if os(iOS) + // AM:PM + @Published public var ampmSelectedColor: UIColor = UIColor.white + @Published public var ampmNormalColor: UIColor = UIColor.black + @Published public var ampmTintColor: UIColor = UIColor.black + + @Published public var centerForegroundColor: Color = Color(.systemBackground) + #else + @Published public var centerForegroundColor: Color = Color(NSColor.windowBackgroundColor) + #endif + // x and y offset from clock center, of the AM:PM picker as the proportion of the clock size @Published public var ampmPickerXoffset = CGFloat(0.0) @Published public var ampmPickerYoffset = CGFloat(0.06) diff --git a/Sources/ClockTimePicker/ClockPickerView.swift b/Sources/ClockTimePicker/ClockPickerView.swift index 8dd0a46..4f08122 100644 --- a/Sources/ClockTimePicker/ClockPickerView.swift +++ b/Sources/ClockTimePicker/ClockPickerView.swift @@ -31,10 +31,12 @@ public struct ClockPickerView : View { public init(date: Binding, options: ClockLooks = ClockLooks()) { self._clockDate = date self.options = options - // to control the AM:PM picker colors - UISegmentedControl.appearance().selectedSegmentTintColor = self.options.ampmTintColor - UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: self.options.ampmSelectedColor], for: .selected) - UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: self.options.ampmNormalColor], for: .normal) + // to control the AM:PM picker colors in ios + #if os(iOS) + UISegmentedControl.appearance().selectedSegmentTintColor = self.options.ampmTintColor + UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: self.options.ampmSelectedColor], for: .selected) + UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: self.options.ampmNormalColor], for: .normal) + #endif } public var body: some View { @@ -53,7 +55,7 @@ public struct ClockPickerView : View { } } - var clockWithHands: some View { + public var clockWithHands: some View { Group { ClockFace(period: self.$period, options: self.options) ClockHand(clockDate: self.$clockDate, handType: .hour, period: self.$period, options: self.options) @@ -62,7 +64,7 @@ public struct ClockPickerView : View { } } - var clockWithoutHands: some View { + public var clockWithoutHands: some View { Group { ClockNoHands(clockDate: self.$clockDate, period: self.$period, options: self.options, handType: self.$hand) HStack { @@ -73,7 +75,7 @@ public struct ClockPickerView : View { } } - var periodPicker: some View { + public var periodPicker: some View { Picker(selection: Binding( get: { self.period }, set: { self.adjustClockDate($0) }), label: Text("")) { ForEach(0.. String { + public func asHours() -> String { var theHours = Calendar.current.component(.hour, from: clockDate) if period == 1 { if theHours < 12 { @@ -92,18 +94,18 @@ public struct ClockPickerView : View { return String(theHours) } - func asMinutes() -> String { + public func asMinutes() -> String { formatter.dateFormat = "mm" return formatter.string(from: clockDate) } - func asText() -> String { + public func asText() -> String { formatter.dateFormat = "HH:mm" return formatter.string(from: clockDate) } // change the hour according to the period setting - func adjustClockDate(_ value: Int) { + public func adjustClockDate(_ value: Int) { self.period = value let theMinutes = Calendar.current.component(.minute, from: clockDate) var theHours = Calendar.current.component(.hour, from: clockDate)