Skip to content

Commit 7442ee3

Browse files
committed
refactor: initializing of InputSource from TISInputSource
1 parent 778c6c2 commit 7442ee3

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

Sources/InputSourceManager/InputSource.swift

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import Carbon
3+
import SwiftUI
34

45
protocol TISInputSourceConvertible {
56
init?(tisInputSource: TISInputSource)
@@ -62,57 +63,38 @@ public struct InputSource {
6263

6364
extension InputSource: TISInputSourceConvertible {
6465
init?(tisInputSource: TISInputSource) {
65-
guard let pointer = TISGetInputSourceProperty(tisInputSource, kTISPropertyInputSourceID),
66-
let id = Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as? String else {
66+
guard let id = tisInputSource.getProperty(for: kTISPropertyInputSourceID) as? String else {
6767
return nil
6868
}
6969

70-
guard let pointer = TISGetInputSourceProperty(tisInputSource, kTISPropertyLocalizedName),
71-
let name = Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as? String else {
70+
guard let name = tisInputSource.getProperty(for: kTISPropertyLocalizedName) as? String else {
7271
return nil
7372
}
7473

75-
guard let pointer = TISGetInputSourceProperty(tisInputSource, kTISPropertyInputSourceCategory),
76-
let category = Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as? String else {
74+
guard let category = tisInputSource.getProperty(for: kTISPropertyInputSourceCategory) as? String else {
7775
return nil
7876
}
7977

80-
guard let pointer = TISGetInputSourceProperty(tisInputSource, kTISPropertyInputSourceIsSelectCapable),
81-
let isSelectable = Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as? Bool else {
78+
guard let isSelectable = tisInputSource.getProperty(for: kTISPropertyInputSourceIsSelectCapable) as? Bool else {
8279
return nil
8380
}
8481

85-
guard let pointer = TISGetInputSourceProperty(tisInputSource, kTISPropertyInputSourceIsEnableCapable),
86-
let isEnableable = Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as? Bool else {
82+
guard let isEnableable = tisInputSource.getProperty(for: kTISPropertyInputSourceIsEnableCapable) as? Bool else {
8783
return nil
8884
}
8985

90-
guard let pointer = TISGetInputSourceProperty(tisInputSource, kTISPropertyInputSourceIsSelected),
91-
let isSelected = Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as? Bool else {
86+
guard let isSelected = tisInputSource.getProperty(for: kTISPropertyInputSourceIsSelected) as? Bool else {
9287
return nil
9388
}
9489

95-
guard let pointer = TISGetInputSourceProperty(tisInputSource, kTISPropertyInputSourceIsEnabled),
96-
let isEnabled = Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as? Bool else {
90+
guard let isEnabled = tisInputSource.getProperty(for: kTISPropertyInputSourceIsEnabled) as? Bool else {
9791
return nil
9892
}
9993

100-
guard let pointer = TISGetInputSourceProperty(tisInputSource, kTISPropertyInputSourceLanguages),
101-
let sourceLanguages = Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as? [String] else {
94+
guard let sourceLanguages = tisInputSource.getProperty(for: kTISPropertyInputSourceLanguages) as? [String] else {
10295
return nil
10396
}
10497

105-
var url: URL?
106-
if let pointer = TISGetInputSourceProperty(tisInputSource, kTISPropertyIconImageURL),
107-
let iconImageURL = Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue() as? URL? {
108-
url = iconImageURL
109-
}
110-
111-
var iconRef: IconRef?
112-
if let retIconRef = OpaquePointer(TISGetInputSourceProperty(tisInputSource, kTISPropertyIconRef)) as IconRef? {
113-
iconRef = retIconRef
114-
}
115-
11698
self.id = id
11799
self.localizedName = name
118100
self.category = category
@@ -121,8 +103,8 @@ extension InputSource: TISInputSourceConvertible {
121103
self.isSelected = isSelected
122104
self.isEnabled = isEnabled
123105
self.sourceLanguages = sourceLanguages
124-
self.iconImageURL = url
125-
self.iconRef = iconRef
106+
self.iconImageURL = tisInputSource.getProperty(for: kTISPropertyIconImageURL) as? URL
107+
self.iconRef = OpaquePointer(TISGetInputSourceProperty(tisInputSource, kTISPropertyIconRef)) as IconRef?
126108
}
127109

128110

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Foundation
2+
import Carbon
3+
4+
extension TISInputSource {
5+
func getProperty(for key: CFString) -> Any? {
6+
guard let pointer = TISGetInputSourceProperty(self, key) else {
7+
return nil
8+
}
9+
10+
return Unmanaged<AnyObject>.fromOpaque(pointer).takeUnretainedValue()
11+
}
12+
}

0 commit comments

Comments
 (0)