Skip to content

Commit

Permalink
[common] Fix build on Xcode 12; fix Mac build
Browse files Browse the repository at this point in the history
  • Loading branch information
kirb committed Mar 10, 2021
1 parent 2f919bd commit a70e063
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 22 deletions.
37 changes: 25 additions & 12 deletions Common/Controllers/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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

Expand All @@ -52,7 +53,7 @@ public enum KeyboardButtonStyle: Int {
"theme": "kirb",
"bellHUD": true,
"bellVibrate": true,
"bellSound": false
"bellSound": true
]
preferences.register(defaults: defaults)

Expand All @@ -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 {
Expand Down Expand Up @@ -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() {
Expand All @@ -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
}
Expand All @@ -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
}

}
Expand Down
2 changes: 1 addition & 1 deletion Common/Global.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions Common/UI/CrossPlatformUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions Common/VT100/VT100ColorMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions Common/VT100/VT100ColorMap.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1120"
LastUpgradeVersion = "1140"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -14,9 +14,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CFBB966022B546DA00585BE6"
BlueprintIdentifier = "CF71BC2022BB61E200AFB1E1"
BuildableName = "NewTerm.app"
BlueprintName = "NewTerm (iOS)"
BlueprintName = "NewTerm (macOS)"
ReferencedContainer = "container:NewTerm.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand Down Expand Up @@ -44,9 +44,9 @@
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CFBB966022B546DA00585BE6"
BlueprintIdentifier = "CF71BC2022BB61E200AFB1E1"
BuildableName = "NewTerm.app"
BlueprintName = "NewTerm (iOS)"
BlueprintName = "NewTerm (macOS)"
ReferencedContainer = "container:NewTerm.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
Expand All @@ -61,9 +61,9 @@
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CFBB966022B546DA00585BE6"
BlueprintIdentifier = "CF71BC2022BB61E200AFB1E1"
BuildableName = "NewTerm.app"
BlueprintName = "NewTerm (iOS)"
BlueprintName = "NewTerm (macOS)"
ReferencedContainer = "container:NewTerm.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
Expand Down
2 changes: 2 additions & 0 deletions iOS/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIUserInterfaceStyle</key>
<string>Dark</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion iOS/UI/Keyboard/KeyboardPopupToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion iOS/UI/Preferences/PreferencesRootController.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// Copyright (c) 2015 HASHBANG Productions. All rights reserved.
//

@import NewTermCommon;
#import "PreferencesRootController.h"
#import <NewTerm-Swift.h>

#if LINK_CEPHEI
#import "../prefs/HBNTPreferencesRootListController.h"
Expand Down
19 changes: 19 additions & 0 deletions prefs/HBNTTerminalSampleTableCell.x
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions prefs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a70e063

Please sign in to comment.