|
6 | 6 | import AppKit |
7 | 7 | import Foundation |
8 | 8 |
|
9 | | -/// Available monospace fonts for the SQL editor |
10 | | -enum EditorFont: String, Codable, CaseIterable, Identifiable { |
11 | | - case systemMono = "System Mono" |
12 | | - case sfMono = "SF Mono" |
13 | | - case menlo = "Menlo" |
14 | | - case monaco = "Monaco" |
15 | | - case courierNew = "Courier New" |
16 | | - |
17 | | - var id: String { rawValue } |
18 | | - |
19 | | - var displayName: String { rawValue } |
20 | | - |
21 | | - /// Get the actual NSFont for this option |
22 | | - func font(size: CGFloat) -> NSFont { |
23 | | - switch self { |
24 | | - case .systemMono: |
| 9 | +internal struct FontFamilyOption: Equatable, Identifiable, Sendable { |
| 10 | + let id: String |
| 11 | + let displayName: String |
| 12 | +} |
| 13 | + |
| 14 | +internal enum EditorFontResolver { |
| 15 | + static let systemMonoId = "System Mono" |
| 16 | + |
| 17 | + static let availableMonospacedFamilies: [FontFamilyOption] = { |
| 18 | + var options: [FontFamilyOption] = [ |
| 19 | + FontFamilyOption(id: systemMonoId, displayName: systemMonoId) |
| 20 | + ] |
| 21 | + |
| 22 | + let familyNames = NSFontManager.shared.availableFontFamilies |
| 23 | + .filter { $0 != systemMonoId } |
| 24 | + .filter(isMonospacedFamily) |
| 25 | + .sorted { lhs, rhs in |
| 26 | + lhs.localizedCaseInsensitiveCompare(rhs) == .orderedAscending |
| 27 | + } |
| 28 | + |
| 29 | + var seen: Set<String> = [systemMonoId] |
| 30 | + for family in familyNames where !seen.contains(family) { |
| 31 | + seen.insert(family) |
| 32 | + options.append(FontFamilyOption(id: family, displayName: family)) |
| 33 | + } |
| 34 | + |
| 35 | + return options |
| 36 | + }() |
| 37 | + |
| 38 | + static func resolve(familyId: String, size: CGFloat) -> NSFont { |
| 39 | + guard familyId != systemMonoId else { |
25 | 40 | return NSFont.monospacedSystemFont(ofSize: size, weight: .regular) |
26 | | - case .sfMono: |
27 | | - return NSFont(name: "SFMono-Regular", size: size) |
28 | | - ?? NSFont.monospacedSystemFont(ofSize: size, weight: .regular) |
29 | | - case .menlo: |
30 | | - return NSFont(name: "Menlo", size: size) |
31 | | - ?? NSFont.monospacedSystemFont(ofSize: size, weight: .regular) |
32 | | - case .monaco: |
33 | | - return NSFont(name: "Monaco", size: size) |
34 | | - ?? NSFont.monospacedSystemFont(ofSize: size, weight: .regular) |
35 | | - case .courierNew: |
36 | | - return NSFont(name: "Courier New", size: size) |
37 | | - ?? NSFont.monospacedSystemFont(ofSize: size, weight: .regular) |
38 | 41 | } |
39 | | - } |
40 | 42 |
|
41 | | - /// Check if this font is available on the system |
42 | | - var isAvailable: Bool { |
43 | | - switch self { |
44 | | - case .systemMono: |
45 | | - return true |
46 | | - case .sfMono: |
47 | | - return NSFont(name: "SFMono-Regular", size: 12) != nil |
48 | | - case .menlo: |
49 | | - return NSFont(name: "Menlo", size: 12) != nil |
50 | | - case .monaco: |
51 | | - return NSFont(name: "Monaco", size: 12) != nil |
52 | | - case .courierNew: |
53 | | - return NSFont(name: "Courier New", size: 12) != nil |
| 43 | + let descriptor = NSFontDescriptor(fontAttributes: [.family: familyId]) |
| 44 | + if let font = NSFont(descriptor: descriptor, size: size), |
| 45 | + font.fontDescriptor.symbolicTraits.contains(.monoSpace) { |
| 46 | + return font |
54 | 47 | } |
| 48 | + |
| 49 | + return NSFont.monospacedSystemFont(ofSize: size, weight: .regular) |
| 50 | + } |
| 51 | + |
| 52 | + static func isAvailable(familyId: String) -> Bool { |
| 53 | + guard familyId != systemMonoId else { return true } |
| 54 | + return isMonospacedFamily(familyId) |
| 55 | + } |
| 56 | + |
| 57 | + private static func isMonospacedFamily(_ familyId: String) -> Bool { |
| 58 | + let descriptor = NSFontDescriptor(fontAttributes: [.family: familyId]) |
| 59 | + guard let font = NSFont(descriptor: descriptor, size: 12) else { return false } |
| 60 | + return font.fontDescriptor.symbolicTraits.contains(.monoSpace) |
55 | 61 | } |
56 | 62 | } |
57 | 63 |
|
|
0 commit comments