Files
byre/mac/Sources/Byre/Platform.swift
T

111 lines
2.4 KiB
Swift

import SwiftUI
#if os(macOS)
import AppKit
typealias PlatformImage = NSImage
typealias PlatformColor = NSColor
typealias PlatformFont = NSFont
typealias PlatformView = NSView
let touchOnly = false
let deviceKind = "Mac"
extension Image {
init(platformImage: PlatformImage) {
self.init(nsImage: platformImage)
}
}
extension PlatformColor {
static func dynamic(light: NSColor, dark: NSColor) -> NSColor {
NSColor(name: nil) { appearance in
appearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua ? dark : light
}
}
}
@MainActor
func copyToClipboard(_ text: String) {
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(text, forType: .string)
}
@MainActor
func openExternal(_ url: URL) {
NSWorkspace.shared.open(url)
}
let appBecameActive = NSApplication.didBecomeActiveNotification
@MainActor
func appIsActive() -> Bool {
NSApp.isActive
}
#else
import UIKit
typealias PlatformImage = UIImage
typealias PlatformColor = UIColor
typealias PlatformFont = UIFont
typealias PlatformView = UIView
let touchOnly = true
let deviceKind = "iPhone"
struct CheckToggleStyle: ToggleStyle {
func makeBody(configuration: Configuration) -> some View {
Button {
configuration.isOn.toggle()
} label: {
HStack(spacing: 8) {
Image(systemName: configuration.isOn ? "checkmark.square.fill" : "square")
.foregroundStyle(configuration.isOn ? Palette.accent : Palette.muted)
configuration.label
.foregroundStyle(Palette.text)
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
}
}
extension ToggleStyle where Self == CheckToggleStyle {
static var checkbox: CheckToggleStyle {
CheckToggleStyle()
}
}
extension Image {
init(platformImage: PlatformImage) {
self.init(uiImage: platformImage)
}
}
extension PlatformColor {
static func dynamic(light: UIColor, dark: UIColor) -> UIColor {
UIColor { traits in
traits.userInterfaceStyle == .dark ? dark : light
}
}
}
@MainActor
func copyToClipboard(_ text: String) {
UIPasteboard.general.string = text
}
@MainActor
func openExternal(_ url: URL) {
UIApplication.shared.open(url)
}
let appBecameActive = UIApplication.didBecomeActiveNotification
@MainActor
func appIsActive() -> Bool {
UIApplication.shared.applicationState == .active
}
#endif