|
| 1 | +// |
| 2 | +// Copyright © 2024 osy. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import SwiftUI |
| 18 | +import UniformTypeIdentifiers |
| 19 | + |
| 20 | +struct MacDeviceLabel<Title>: View where Title : StringProtocol { |
| 21 | + let title: Title |
| 22 | + let device: MacDevice |
| 23 | + |
| 24 | + init(_ title: Title, device macDevice: MacDevice) { |
| 25 | + self.title = title |
| 26 | + self.device = macDevice |
| 27 | + } |
| 28 | + |
| 29 | + var body: some View { |
| 30 | + Label(title, systemImage: device.symbolName) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// credits: https://adamdemasi.com/2023/04/15/mac-device-icon-by-device-class.html |
| 35 | + |
| 36 | +private extension UTTagClass { |
| 37 | + static let deviceModelCode = UTTagClass(rawValue: "com.apple.device-model-code") |
| 38 | +} |
| 39 | + |
| 40 | +private extension UTType { |
| 41 | + static let macBook = UTType("com.apple.mac.laptop") |
| 42 | + static let macBookWithNotch = UTType("com.apple.mac.notched-laptop") |
| 43 | + static let macMini = UTType("com.apple.macmini") |
| 44 | + static let macStudio = UTType("com.apple.macstudio") |
| 45 | + static let iMac = UTType("com.apple.imac") |
| 46 | + static let macPro = UTType("com.apple.macpro") |
| 47 | + static let macPro2013 = UTType("com.apple.macpro-cylinder") |
| 48 | + static let macPro2019 = UTType("com.apple.macpro-2019") |
| 49 | +} |
| 50 | + |
| 51 | +struct MacDevice { |
| 52 | + let model: String |
| 53 | + let symbolName: String |
| 54 | + |
| 55 | + #if os(macOS) |
| 56 | + static let current: Self = { |
| 57 | + let key = "hw.model" |
| 58 | + var size = size_t() |
| 59 | + sysctlbyname(key, nil, &size, nil, 0) |
| 60 | + let value = malloc(size) |
| 61 | + defer { |
| 62 | + value?.deallocate() |
| 63 | + } |
| 64 | + sysctlbyname(key, value, &size, nil, 0) |
| 65 | + guard let cChar = value?.bindMemory(to: CChar.self, capacity: size) else { |
| 66 | + return Self(model: "Unknown") |
| 67 | + } |
| 68 | + return Self(model: String(cString: cChar)) |
| 69 | + }() |
| 70 | + #endif |
| 71 | + |
| 72 | + init(model: String?) { |
| 73 | + self.model = model ?? "Unknown" |
| 74 | + self.symbolName = Self.symbolName(from: self.model) |
| 75 | + } |
| 76 | + |
| 77 | + private static func checkModel(_ model: String, conformsTo type: UTType?) -> Bool { |
| 78 | + guard let type else { |
| 79 | + return false |
| 80 | + } |
| 81 | + return UTType(tag: model, tagClass: .deviceModelCode, conformingTo: nil)?.conforms(to: type) ?? false |
| 82 | + } |
| 83 | + |
| 84 | + private static func symbolName(from model: String) -> String { |
| 85 | + if checkModel(model, conformsTo: .macBookWithNotch), |
| 86 | + #available(macOS 14, iOS 17, macCatalyst 17, tvOS 17, watchOS 10, *) { |
| 87 | + // macbook.gen2 was added with SF Symbols 5.0 (macOS Sonoma, 2023), but MacBooks with a notch |
| 88 | + // were released in 2021! |
| 89 | + return "macbook.gen2" |
| 90 | + } else if checkModel(model, conformsTo: .macBook) { |
| 91 | + return "laptopcomputer" |
| 92 | + } else if checkModel(model, conformsTo: .macMini) { |
| 93 | + return "macmini" |
| 94 | + } else if checkModel(model, conformsTo: .macStudio) { |
| 95 | + return "macstudio" |
| 96 | + } else if checkModel(model, conformsTo: .iMac) { |
| 97 | + return "desktopcomputer" |
| 98 | + } else if checkModel(model, conformsTo: .macPro2019) { |
| 99 | + return "macpro.gen3" |
| 100 | + } else if checkModel(model, conformsTo: .macPro2013) { |
| 101 | + return "macpro.gen2" |
| 102 | + } else if checkModel(model, conformsTo: .macPro) { |
| 103 | + return "macpro" |
| 104 | + } |
| 105 | + return "display" |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#Preview { |
| 110 | + MacDeviceLabel("MacBook", device: MacDevice(model: "Mac14,6")) |
| 111 | +} |
0 commit comments