SwiftExtension // Add String.charDescriptions: [String], etc. (#249)
* SwiftExtension // Add String.charDescriptions: [String]. * SwiftExtension // Including certain SwiftUI extensions.
This commit is contained in:
parent
f8c722ce8e
commit
3c88b810bc
|
@ -136,11 +136,11 @@ public struct AppProperty<Value> {
|
|||
}
|
||||
}
|
||||
|
||||
// MARK: - String Extension
|
||||
// MARK: - String RegReplace Extension
|
||||
|
||||
// Ref: https://stackoverflow.com/a/40993403/4162914 && https://stackoverflow.com/a/71291137/4162914
|
||||
extension String {
|
||||
public mutating func regReplace(pattern: String, replaceWith: String = "") {
|
||||
// Ref: https://stackoverflow.com/a/40993403/4162914 && https://stackoverflow.com/a/71291137/4162914
|
||||
do {
|
||||
let regex = try NSRegularExpression(
|
||||
pattern: pattern, options: [.caseInsensitive, .anchorsMatchLines]
|
||||
|
@ -152,3 +152,14 @@ extension String {
|
|||
} catch { return }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - String CharName Extension
|
||||
|
||||
extension String {
|
||||
public var charDescriptions: [String] {
|
||||
flatMap(\.unicodeScalars).compactMap {
|
||||
let theName: String = $0.properties.name ?? ""
|
||||
return String(format: "U+%02X %@", $0.value, theName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
|
||||
// ====================
|
||||
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
|
||||
// ... with NTL restriction stating that:
|
||||
// No trademark license is granted to use the trade names, trademarks, service
|
||||
// marks, or product names of Contributor, except as required to fulfill notice
|
||||
// requirements defined in MIT License.
|
||||
|
||||
import SwiftUI
|
||||
|
||||
// MARK: - Add "onChange" support.
|
||||
|
||||
// Ref: https://mjeld.com/swiftui-macos-10-15-toggle-onchange/
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
extension Binding {
|
||||
public func onChange(_ action: @escaping () -> Void) -> Binding {
|
||||
Binding(
|
||||
get: {
|
||||
wrappedValue
|
||||
},
|
||||
set: { newValue in
|
||||
wrappedValue = newValue
|
||||
action()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Add ".tooltip" support.
|
||||
|
||||
// Ref: https://stackoverflow.com/a/63217861
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
@available(macOS, obsoleted: 11)
|
||||
struct Tooltip: NSViewRepresentable {
|
||||
let tooltip: String
|
||||
|
||||
func makeNSView(context _: NSViewRepresentableContext<Tooltip>) -> NSView {
|
||||
let view = NSView()
|
||||
view.toolTip = tooltip
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
func updateNSView(_: NSView, context _: NSViewRepresentableContext<Tooltip>) {}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
@available(macOS, obsoleted: 11)
|
||||
extension View {
|
||||
public func help(_ tooltip: String) -> some View {
|
||||
overlay(Tooltip(tooltip: tooltip))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Windows Aero in Swift UI
|
||||
|
||||
// Ref: https://stackoverflow.com/questions/62461957
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
public struct VisualEffectView: NSViewRepresentable {
|
||||
let material: NSVisualEffectView.Material
|
||||
let blendingMode: NSVisualEffectView.BlendingMode
|
||||
public init(material: NSVisualEffectView.Material, blendingMode: NSVisualEffectView.BlendingMode) {
|
||||
self.material = material
|
||||
self.blendingMode = blendingMode
|
||||
}
|
||||
|
||||
public func makeNSView(context _: Context) -> NSVisualEffectView {
|
||||
let visualEffectView = NSVisualEffectView()
|
||||
visualEffectView.material = material
|
||||
visualEffectView.blendingMode = blendingMode
|
||||
visualEffectView.state = NSVisualEffectView.State.active
|
||||
return visualEffectView
|
||||
}
|
||||
|
||||
public func updateNSView(_ visualEffectView: NSVisualEffectView, context _: Context) {
|
||||
visualEffectView.material = material
|
||||
visualEffectView.blendingMode = blendingMode
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@
|
|||
// requirements defined in MIT License.
|
||||
|
||||
import SSPreferences
|
||||
import SwiftExtension
|
||||
import SwiftUI
|
||||
|
||||
extension NSImage {
|
||||
|
@ -127,72 +128,3 @@ class CtlPrefUI {
|
|||
)
|
||||
static let shared = CtlPrefUI()
|
||||
}
|
||||
|
||||
// MARK: - Add "onChange" support.
|
||||
|
||||
// Ref: https://mjeld.com/swiftui-macos-10-15-toggle-onchange/
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
extension Binding {
|
||||
public func onChange(_ action: @escaping () -> Void) -> Binding {
|
||||
Binding(
|
||||
get: {
|
||||
wrappedValue
|
||||
},
|
||||
set: { newValue in
|
||||
wrappedValue = newValue
|
||||
action()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Add ".tooltip" support.
|
||||
|
||||
// Ref: https://stackoverflow.com/a/63217861
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
@available(macOS, obsoleted: 11)
|
||||
struct Tooltip: NSViewRepresentable {
|
||||
let tooltip: String
|
||||
|
||||
func makeNSView(context _: NSViewRepresentableContext<Tooltip>) -> NSView {
|
||||
let view = NSView()
|
||||
view.toolTip = tooltip
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
func updateNSView(_: NSView, context _: NSViewRepresentableContext<Tooltip>) {}
|
||||
}
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
@available(macOS, obsoleted: 11)
|
||||
extension View {
|
||||
public func help(_ tooltip: String) -> some View {
|
||||
overlay(Tooltip(tooltip: tooltip))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Windows Aero in Swift UI
|
||||
|
||||
// Ref: https://stackoverflow.com/questions/62461957
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
struct VisualEffectView: NSViewRepresentable {
|
||||
let material: NSVisualEffectView.Material
|
||||
let blendingMode: NSVisualEffectView.BlendingMode
|
||||
|
||||
func makeNSView(context _: Context) -> NSVisualEffectView {
|
||||
let visualEffectView = NSVisualEffectView()
|
||||
visualEffectView.material = material
|
||||
visualEffectView.blendingMode = blendingMode
|
||||
visualEffectView.state = NSVisualEffectView.State.active
|
||||
return visualEffectView
|
||||
}
|
||||
|
||||
func updateNSView(_ visualEffectView: NSVisualEffectView, context _: Context) {
|
||||
visualEffectView.material = material
|
||||
visualEffectView.blendingMode = blendingMode
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
import BookmarkManager
|
||||
import SSPreferences
|
||||
import Shared
|
||||
import SwiftExtension
|
||||
import SwiftUI
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
import SSPreferences
|
||||
import Shared
|
||||
import SwiftExtension
|
||||
import SwiftUI
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
import BookmarkManager
|
||||
import SSPreferences
|
||||
import Shared
|
||||
import SwiftExtension
|
||||
import SwiftUI
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
import SSPreferences
|
||||
import Shared
|
||||
import SwiftExtension
|
||||
import SwiftUI
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
import SSPreferences
|
||||
import Shared
|
||||
import SwiftExtension
|
||||
import SwiftUI
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
import IMKUtils
|
||||
import SSPreferences
|
||||
import Shared
|
||||
import SwiftExtension
|
||||
import SwiftUI
|
||||
|
||||
@available(macOS 10.15, *)
|
||||
|
|
Loading…
Reference in New Issue