diff --git a/Padbury Clock Revived.xcodeproj/project.pbxproj b/Padbury Clock Revived.xcodeproj/project.pbxproj index df01648..c0efe9b 100644 --- a/Padbury Clock Revived.xcodeproj/project.pbxproj +++ b/Padbury Clock Revived.xcodeproj/project.pbxproj @@ -286,7 +286,7 @@ "@loader_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 1.1.1; + MARKETING_VERSION = 1.1.2; PRODUCT_BUNDLE_IDENTIFIER = "com.hans.Padbury-Clock-Revived"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -312,7 +312,7 @@ "@loader_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 10.15; - MARKETING_VERSION = 1.1.1; + MARKETING_VERSION = 1.1.2; PRODUCT_BUNDLE_IDENTIFIER = "com.hans.Padbury-Clock-Revived"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; diff --git a/Padbury Clock Revived.xcodeproj/project.xcworkspace/xcuserdata/hans.xcuserdatad/UserInterfaceState.xcuserstate b/Padbury Clock Revived.xcodeproj/project.xcworkspace/xcuserdata/hans.xcuserdatad/UserInterfaceState.xcuserstate index 5f2eaf4..e130e9d 100644 Binary files a/Padbury Clock Revived.xcodeproj/project.xcworkspace/xcuserdata/hans.xcuserdatad/UserInterfaceState.xcuserstate and b/Padbury Clock Revived.xcodeproj/project.xcworkspace/xcuserdata/hans.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Padbury Clock Revived/ClockView.swift b/Padbury Clock Revived/ClockView.swift index 9cdce82..9c994c9 100644 --- a/Padbury Clock Revived/ClockView.swift +++ b/Padbury Clock Revived/ClockView.swift @@ -83,7 +83,7 @@ final class ClockView: ScreenSaverView { // of a better system please make a pull request or write me an email! // >>> contact.kamik423@gmail.com <<< let fontSizeScaleFactor : CGFloat - switch preferences.font { + switch preferences.fontFamily { case .sanFrancisco, .neueHelvetica: fontSizeScaleFactor = 0.20 case .sanFranciscoMono: diff --git a/Padbury Clock Revived/ConfigureSheet.xib b/Padbury Clock Revived/ConfigureSheet.xib index 9fdfeda..7c7be67 100644 --- a/Padbury Clock Revived/ConfigureSheet.xib +++ b/Padbury Clock Revived/ConfigureSheet.xib @@ -13,6 +13,7 @@ + @@ -33,7 +34,7 @@ - + @@ -48,7 +49,7 @@ - + @@ -58,7 +59,7 @@ - + @@ -204,10 +215,12 @@ + + diff --git a/Padbury Clock Revived/ConfigureSheetController.swift b/Padbury Clock Revived/ConfigureSheetController.swift index 5e615bf..be9ec63 100644 --- a/Padbury Clock Revived/ConfigureSheetController.swift +++ b/Padbury Clock Revived/ConfigureSheetController.swift @@ -22,6 +22,7 @@ class ConfigureSheetController: NSObject { @IBOutlet var showTimeSeparatorsCheckbox: NSButton! @IBOutlet var fontSelector: NSPopUpButton! @IBOutlet var fontWeightSelector: NSPopUpButton! + @IBOutlet var plainFontsOnlyCheckbox: NSButton! @IBOutlet var mainScreenCheckbox: NSButton! override init() { @@ -51,23 +52,32 @@ class ConfigureSheetController: NSObject { twentyfourHoursCheckbox.state = preferences.useAmPm ? .off : .on showSecondsCheckbox.state = preferences.showSeconds ? .on : .off showTimeSeparatorsCheckbox.state = preferences.showTimeSeparators ? .on : .off - fontSelector.selectItem(withTitle: preferences.font.name) + fontSelector.selectItem(withTitle: preferences.fontFamily.name) mainScreenCheckbox.state = preferences.mainScreenOnly ? .on : .off + plainFontsOnlyCheckbox.state = preferences.plainFontsOnly ? .on : .off // Remove all options from the font selector and add the ones corresponding to the fonts fontSelector.removeAllItems() fontSelector.addItems(withTitles: SupportedFont.allCases.map { $0.name }) - fontSelector.selectItem(withTitle: preferences.font.name) + fontSelector.selectItem(withTitle: preferences.fontFamily.name) + // Apply font style + for (index, font) in SupportedFont.allCases.enumerated() { + fontSelector.item(at: index)?.attributedTitle = NSAttributedString(string: font.name, attributes: [.font: NSFont(name: font.fontFamilyName, size: NSFont.systemFontSize) ?? NSFont.menuFont(ofSize: NSFont.systemFontSize)]) + } // Remove all the options from the font weight selector // and add the ones corresponding to the current font fontWeightSelector.removeAllItems() - fontWeightSelector.addItems(withTitles: preferences.font.availableWeights.map({ $0.name })) + fontWeightSelector.addItems(withTitles: preferences.fontFamily.availableWeights) // Select the correct item // If the weight is not available for this font select the first option - fontWeightSelector.selectItem(at: preferences.font.availableWeights.firstIndex(where: { $0.name == preferences.fontWeight.name }) ?? 0 ) + fontWeightSelector.selectItem(at: preferences.fontFamily.availableWeights.firstIndex(of: preferences.styleName) ?? 0) + // Preview font + for (index, typeName) in preferences.fontFamily.availableWeights.enumerated() { + fontWeightSelector.item(at: index)?.attributedTitle = NSAttributedString(string: typeName, attributes: [.font: NSFont(name: preferences.fontFamily.postscriptName(for: typeName) ?? "", size: NSFont.systemFontSize) ?? NSFont.menuFont(ofSize: NSFont.systemFontSize)]) + } // Store said option back to the settings in case it changed - preferences.fontWeight = NSFont.Weight.from(name: fontWeightSelector.selectedItem?.title ?? "") + preferences.styleName = fontWeightSelector.selectedItem?.title ?? "" // Trigger update of the preview window ClockView.shared?.setup(force: true) @@ -84,9 +94,10 @@ class ConfigureSheetController: NSObject { preferences.useAmPm = twentyfourHoursCheckbox.state == .off preferences.showSeconds = showSecondsCheckbox.state == .on preferences.showTimeSeparators = showTimeSeparatorsCheckbox.state == .on - preferences.font = SupportedFont.named(fontSelector.selectedItem?.title ?? "") - preferences.fontWeight = NSFont.Weight.from(name: fontWeightSelector.selectedItem?.title ?? "") + preferences.fontFamily = SupportedFont.named(fontSelector.selectedItem?.title ?? "") + preferences.styleName = fontWeightSelector.selectedItem?.title ?? "" preferences.mainScreenOnly = mainScreenCheckbox.state == .on + preferences.plainFontsOnly = plainFontsOnlyCheckbox.state == .on // Update the options. Font weight might have changed. self.setup() diff --git a/Padbury Clock Revived/Preferences.swift b/Padbury Clock Revived/Preferences.swift index 236aea9..3519ebb 100644 --- a/Padbury Clock Revived/Preferences.swift +++ b/Padbury Clock Revived/Preferences.swift @@ -21,69 +21,38 @@ class Preferences: NSObject { Preferences.shared = self } - var font: SupportedFont { + var fontFamily: SupportedFont { // Which font should be used - get { return SupportedFont.named(defaults.value(forKey: "Font") as? String ?? "") } + get { return SupportedFont.named(defaults.value(forKey: "FontFamily") as? String ?? "") } set { - defaults.setValue(newValue.name, forKey: "Font") + defaults.setValue(newValue.name, forKey: "FontFamily") defaults.synchronize() } } func nsFont(ofSize fontSize: CGFloat) -> NSFont { // The NSFont to use with correct weight and size set - let fallback = NSFont.monospacedDigitSystemFont(ofSize: fontSize, weight: fontWeight) - switch font { - case .sanFrancisco: - // Default system font - return .monospacedDigitSystemFont(ofSize: fontSize, weight: fontWeight) - case .sanFranciscoMono: - // Monospace default system font - return .monospacedSystemFont(ofSize: fontSize, weight: fontWeight) - case .newYork: - // Serif default system font - let descriptor = NSFont.monospacedDigitSystemFont(ofSize: fontSize, weight: fontWeight).fontDescriptor - return NSFont(descriptor: descriptor.withDesign(.serif) ?? descriptor, size: 0.0) ?? fallback - case .neueHelvetica: - // Neue Helvetica - // Get the name of the font file to load - let fontName: String - switch fontWeight { - case .ultraLight: - fontName = "Helvetica Neue UltraLight" - case .thin: - fontName = "Helvetica Neue Thin" - case .light: - fontName = "Helvetica Neue Light" - case .regular: - fontName = "Helvetica Neue" - case .medium: - fontName = "Helvetica Neue Medium" - case .bold: - fontName = "Helvetica Neue Bold" - default: - fontName = "Helvetica Neue" - } - // Load the font - guard var font = NSFont(name: fontName, size: fontSize) else { return fallback } - // Apply TrueType stylistic sets to get proportional numbers and the raised colon. - let fontAttributes: [NSFontDescriptor.AttributeName: Any] = [ - .featureSettings: [ - [ - // Proportional Numbers - NSFontDescriptor.FeatureKey.typeIdentifier: 6, - NSFontDescriptor.FeatureKey.selectorIdentifier: 1 - ], - [ - // Alternate Punctuation (rounded, raised colon) - NSFontDescriptor.FeatureKey.typeIdentifier: 17, - NSFontDescriptor.FeatureKey.selectorIdentifier: 1 - ] - ] - ] - // Apply the attributes - font = NSFont(descriptor: font.fontDescriptor.addingAttributes(fontAttributes), size: 0.0) ?? font - return font + let fallback = NSFont.monospacedDigitSystemFont(ofSize: fontSize, weight: .regular) + + // Load the font + guard var font = NSFont(name: fontFamily.postscriptName(for: styleName) ?? "", size: fontSize) else { return fallback } + // Apply TrueType stylistic sets to get proportional numbers. + var featureSettings: [[NSFontDescriptor.FeatureKey: Int]] = [[.typeIdentifier: kNumberSpacingType, .selectorIdentifier: kMonospacedNumbersSelector]] + if fontFamily == .neueHelvetica { + // Alternate Punctuation (rounded, raised colon) + featureSettings.append([.typeIdentifier: kCharacterAlternativesType, .selectorIdentifier: 1]) + } + // Apply the attributes + font = NSFont(descriptor: font.fontDescriptor.addingAttributes([.featureSettings: featureSettings]), size: 0.0) ?? font + return font + } + + var plainFontsOnly: Bool { + // Only use Regular Width Roman Fonts + get { return (defaults.value(forKey: "PlainFontsOnly") as? Bool) ?? true } + set { + defaults.setValue(newValue, forKey: "PlainFontsOnly") + defaults.synchronize() } } @@ -123,11 +92,11 @@ class Preferences: NSObject { } } - var fontWeight: NSFont.Weight { + var styleName: String { // The font weight to be used - get { return NSFont.Weight.from(name: (defaults.value(forKey: "fontWeight") as? String) ?? "Ultra Light") } + get { return defaults.value(forKey: "styleName") as? String ?? "UltraLight" } set { - defaults.setValue(newValue.name, forKey: "fontWeight") + defaults.setValue(newValue, forKey: "styleName") defaults.synchronize() } } @@ -174,60 +143,32 @@ enum SupportedFont: String, CaseIterable { } } - static func named(_ name: String) -> SupportedFont { - // Get the font from the name - SupportedFont.allCases.first(where: { $0.name == name }) ?? .sanFrancisco - } - - var availableWeights: [NSFont.Weight] { - // List of available font weights for each font + var fontFamilyName: String { + // The name of the font family ".AppleSystemUIFontUltraLight" switch self { case .sanFrancisco: - return [.ultraLight, .thin, .light, .regular, .medium, .semibold, .bold, .heavy, .black] + return ".AppleSystemUIFont" case .sanFranciscoMono: - return [.light, .regular, .medium, .semibold, .bold, .heavy, .black] + return ".AppleSystemUIFontMonospaced" case .newYork: - return [.regular, .medium, .semibold, .bold, .heavy, .black] + return ".AppleSystemUIFontSerif" case .neueHelvetica: - return [.ultraLight, .thin, .light, .regular, .medium, .bold] + return "Helvetica Neue" } } -} - -// MARK: - NSFont.Weight Names - -extension NSFont.Weight { - var name: String { - // Names for font weights - get { - switch self { - case .ultraLight: return "Ultra Light" - case .thin: return "Thin" - case .light: return "Light" - case .regular: return "Regular" - case .medium: return "Medium" - case .semibold: return "Semibold" - case .bold: return "Bold" - case .heavy: return "Heavy" - case .black: return "Black" - default: return "Regular" - } - } + + static func named(_ name: String) -> SupportedFont { + // Get the font from the name + SupportedFont.allCases.first(where: { $0.name == name }) ?? .sanFrancisco } - - static func from(name: String) -> NSFont.Weight { - // Font weight from name - switch name { - case "Ultra Light": return .ultraLight - case "Thin": return .thin - case "Light": return .light - case "Regular": return .regular - case "Medium": return .medium - case "Semibold": return .semibold - case "Bold": return .bold - case "Heavy": return .heavy - case "Black": return .black - default: return .regular - } + + var availableWeights: [String] { + // List of available font weights for each font + return (NSFontManager.shared.availableMembers(ofFontFamily: self.fontFamilyName)?.filter({ !(Preferences.shared?.plainFontsOnly ?? true) || Int(truncating: $0[3] as? NSNumber ?? 0)&0b1000001 == 0 }).map({ $0[1] as? String ?? "Error" })) ?? [] + } + + func postscriptName(for styleName: String) -> String? { + // Get + return NSFontManager.shared.availableMembers(ofFontFamily: self.fontFamilyName)?.first(where: { $0[1] as? String == styleName })?[0] as? String } }