From a70e063addd5660b40258006c33d33e0e07fd8ef Mon Sep 17 00:00:00 2001 From: Adam Demasi Date: Wed, 10 Mar 2021 19:57:56 +1030 Subject: [PATCH] [common] Fix build on Xcode 12; fix Mac build --- Common/Controllers/Preferences.swift | 37 +++++++++++++------ Common/Global.swift | 2 +- Common/UI/CrossPlatformUI.swift | 2 + Common/VT100/VT100ColorMap.h | 5 +++ Common/VT100/VT100ColorMap.m | 6 +++ .../xcschemes/NewTerm (macOS).xcscheme | 14 +++---- iOS/Supporting Files/Info.plist | 2 + iOS/UI/Keyboard/KeyboardPopupToolbar.swift | 2 +- .../Preferences/PreferencesRootController.m | 2 +- prefs/HBNTTerminalSampleTableCell.x | 19 ++++++++++ prefs/Makefile | 1 + 11 files changed, 70 insertions(+), 22 deletions(-) diff --git a/Common/Controllers/Preferences.swift b/Common/Controllers/Preferences.swift index 3408a29..941af32 100644 --- a/Common/Controllers/Preferences.swift +++ b/Common/Controllers/Preferences.swift @@ -13,10 +13,11 @@ import UIKit #endif public enum KeyboardButtonStyle: Int { - case text = 0, icons = 1 + case text, icons } -@objc public class Preferences: NSObject { +@objc(Preferences) +public class Preferences: NSObject { @objc public static let didChangeNotification = Notification.Name(rawValue: "NewTermPreferencesDidChangeNotification") @@ -28,7 +29,7 @@ public enum KeyboardButtonStyle: Int { let themesPlist = NSDictionary(contentsOf: Bundle.main.url(forResource: "Themes", withExtension: "plist")!)! public var fontMetrics: FontMetrics! - public var colorMap: VT100ColorMap! + @objc public var colorMap: VT100ColorMap! private var kvoContext = 0 @@ -52,7 +53,7 @@ public enum KeyboardButtonStyle: Int { "theme": "kirb", "bellHUD": true, "bellVibrate": true, - "bellSound": false + "bellSound": true ] preferences.register(defaults: defaults) @@ -62,7 +63,7 @@ public enum KeyboardButtonStyle: Int { for key in defaults.keys { preferences.addObserver(self, forKeyPath: key, options: [], context: &kvoContext) } - preferencesUpdated(notification: nil) + preferencesUpdated(fromNotification: false) } public var fontName: String { @@ -108,25 +109,33 @@ public enum KeyboardButtonStyle: Int { set { preferences.set(newValue, forKey: "lastVersion") } } + #if os(iOS) @available(iOS 13, *) @objc public var userInterfaceStyle: UIUserInterfaceStyle { return colorMap.userInterfaceStyle } + #elseif os(macOS) + @objc public var appearanceStyle: NSAppearance.Name { + return colorMap.appearanceStyle + } + #endif // MARK: - Callbacks override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if context == &kvoContext { - preferencesUpdated(notification: nil) + preferencesUpdated(fromNotification: true) } else { super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context) } } - @objc func preferencesUpdated(notification: Notification?) { + func preferencesUpdated(fromNotification: Bool) { fontMetricsChanged() colorMapChanged() - NotificationCenter.default.post(name: Preferences.didChangeNotification, object: nil) + if fromNotification { + NotificationCenter.default.post(name: Preferences.didChangeNotification, object: nil) + } } private func fontMetricsChanged() { @@ -148,11 +157,11 @@ public enum KeyboardButtonStyle: Int { } if regularFont == nil || boldFont == nil { - NSLog("font %@ size %f could not be initialised", fontName, fontSize) + NSLog("Font %@ size %.1f could not be initialised", fontName, fontSize) if #available(iOS 13.0, macOS 10.15, *) { fontName = "SF Mono" } else { - fontName = "Courier" + fontName = "Menlo" } return } @@ -164,12 +173,16 @@ public enum KeyboardButtonStyle: Int { // if the theme doesn’t exist… how did we get here? force it to the default, which will call // this method again guard let theme = themesPlist[themeName] as? [String: Any] else { - NSLog("theme %@ doesn’t exist", themeName) - themeName = "kirb" + NSLog("Theme %@ doesn’t exist", themeName) + themeName = "Basic" return } colorMap = VT100ColorMap(dictionary: theme) + + #if os(macOS) + NSApp.appearance = NSAppearance(named: colorMap.appearanceStyle) + #endif } } diff --git a/Common/Global.swift b/Common/Global.swift index c1cfa58..6b31d5b 100644 --- a/Common/Global.swift +++ b/Common/Global.swift @@ -17,7 +17,7 @@ public let isBigDevice: Bool = { case .phone, .carPlay, .unspecified: return false - case .pad, .tv: + case .pad, .tv, .mac: return true @unknown default: diff --git a/Common/UI/CrossPlatformUI.swift b/Common/UI/CrossPlatformUI.swift index e1e0d22..d7843e9 100644 --- a/Common/UI/CrossPlatformUI.swift +++ b/Common/UI/CrossPlatformUI.swift @@ -10,6 +10,8 @@ import AppKit public typealias Color = NSColor public typealias Font = NSFont +public typealias UIFont = NSFont +public typealias UIFontDescriptor = NSFontDescriptor #else import UIKit diff --git a/Common/VT100/VT100ColorMap.h b/Common/VT100/VT100ColorMap.h index dead46b..7911fcf 100644 --- a/Common/VT100/VT100ColorMap.h +++ b/Common/VT100/VT100ColorMap.h @@ -15,7 +15,12 @@ @property (nonatomic, retain, readonly) Color *backgroundCursor; @property (nonatomic, readonly) BOOL isDark; + +#if TARGET_OS_IPHONE @property (nonatomic, readonly) UIUserInterfaceStyle userInterfaceStyle; +#else +@property (nonatomic, readonly) NSAppearanceName appearanceStyle; +#endif - (instancetype)init; - (instancetype)initWithDictionary:(NSDictionary *)dictionary; diff --git a/Common/VT100/VT100ColorMap.m b/Common/VT100/VT100ColorMap.m index c8323fe..928c855 100644 --- a/Common/VT100/VT100ColorMap.m +++ b/Common/VT100/VT100ColorMap.m @@ -132,8 +132,14 @@ - (Color *)colorAtIndex:(unsigned int)index { } } +#if TARGET_OS_IPHONE - (UIUserInterfaceStyle)userInterfaceStyle { return _isDark ? UIUserInterfaceStyleDark : UIUserInterfaceStyleLight; } +#else +- (NSAppearanceName)appearanceStyle { + return _isDark ? NSAppearanceNameDarkAqua : NSAppearanceNameAqua; +} +#endif @end diff --git a/NewTerm.xcodeproj/xcshareddata/xcschemes/NewTerm (macOS).xcscheme b/NewTerm.xcodeproj/xcshareddata/xcschemes/NewTerm (macOS).xcscheme index 6c766aa..fb2af6e 100644 --- a/NewTerm.xcodeproj/xcshareddata/xcschemes/NewTerm (macOS).xcscheme +++ b/NewTerm.xcodeproj/xcshareddata/xcschemes/NewTerm (macOS).xcscheme @@ -1,6 +1,6 @@ @@ -44,9 +44,9 @@ runnableDebuggingMode = "0"> @@ -61,9 +61,9 @@ runnableDebuggingMode = "0"> diff --git a/iOS/Supporting Files/Info.plist b/iOS/Supporting Files/Info.plist index 0f70910..68b7192 100644 --- a/iOS/Supporting Files/Info.plist +++ b/iOS/Supporting Files/Info.plist @@ -62,5 +62,7 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight + UIUserInterfaceStyle + Dark diff --git a/iOS/UI/Keyboard/KeyboardPopupToolbar.swift b/iOS/UI/Keyboard/KeyboardPopupToolbar.swift index 5ad811a..62f7a69 100644 --- a/iOS/UI/Keyboard/KeyboardPopupToolbar.swift +++ b/iOS/UI/Keyboard/KeyboardPopupToolbar.swift @@ -48,7 +48,7 @@ class KeyboardPopupToolbar: UIView { homeEndSpacerView.addCompactConstraint("self.width = 0", metrics: nil, views: nil) pageUpDownSpacerView.addCompactConstraint("self.width = 0", metrics: nil, views: nil) deleteSpacerView.addCompactConstraint("self.width <= max", metrics: [ - "max": CGFloat.greatestFiniteMagnitude + "max": 1000 // TODO: ??? What was this for? ], views: nil) buttons = [ diff --git a/iOS/UI/Preferences/PreferencesRootController.m b/iOS/UI/Preferences/PreferencesRootController.m index dc26b30..9424f40 100644 --- a/iOS/UI/Preferences/PreferencesRootController.m +++ b/iOS/UI/Preferences/PreferencesRootController.m @@ -6,8 +6,8 @@ // Copyright (c) 2015 HASHBANG Productions. All rights reserved. // +@import NewTermCommon; #import "PreferencesRootController.h" -#import #if LINK_CEPHEI #import "../prefs/HBNTPreferencesRootListController.h" diff --git a/prefs/HBNTTerminalSampleTableCell.x b/prefs/HBNTTerminalSampleTableCell.x index bd98418..2c3d879 100644 --- a/prefs/HBNTTerminalSampleTableCell.x +++ b/prefs/HBNTTerminalSampleTableCell.x @@ -1,5 +1,15 @@ #import "HBNTTerminalSampleTableCell.h" +@interface VT100ColorMap : NSObject +@property (nonatomic, strong) UIColor *background; +@end + +@interface Preferences : NSObject ++ (instancetype)shared; ++ (NSNotificationName)didChangeNotification; +- (VT100ColorMap *)colorMap; +@end + @interface TerminalSampleView : UIView @end @@ -11,14 +21,23 @@ if (self) { self.textLabel.hidden = YES; + self.selectedBackgroundView = [[UIView alloc] init]; TerminalSampleView *sampleView = [[%c(TerminalSampleView) alloc] initWithFrame:self.contentView.bounds]; sampleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; sampleView.userInteractionEnabled = NO; [self.contentView addSubview:sampleView]; + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferencesUpdated) name:[%c(Preferences) didChangeNotification] object:nil]; + [self preferencesUpdated]; } return self; } +- (void)preferencesUpdated { + Preferences *preferences = [%c(Preferences) shared]; + self.backgroundColor = preferences.colorMap.background; +} + @end diff --git a/prefs/Makefile b/prefs/Makefile index 267bc40..5847f41 100644 --- a/prefs/Makefile +++ b/prefs/Makefile @@ -5,5 +5,6 @@ NewTermPreferences_FILES = $(wildcard *.[xm]) NewTermPreferences_INSTALL_PATH = /Applications/NewTerm.app/PreferenceBundles NewTermPreferences_PRIVATE_FRAMEWORKS = Preferences NewTermPreferences_EXTRA_FRAMEWORKS = Cephei CepheiPrefs +NewTermPreferences_CFLAGS = -fobjc-arc include $(THEOS_MAKE_PATH)/bundle.mk