Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Blink/SpaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ class SpaceController: UIViewController, LayoutInsetsProvider {

@objc private func _setupAppearance() {
self.view.tintColor = .cyan

guard !BLKDefaults.isAutoThemeToggleEnabled() else {
overrideUserInterfaceStyle = .unspecified
return
}

switch BLKDefaults.keyboardStyle() {
case .light:
overrideUserInterfaceStyle = .light
Expand Down
23 changes: 22 additions & 1 deletion Blink/TermController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class TermController: UIViewController {

params.fontSize = BLKDefaults.selectedFontSize()?.intValue ?? 16
params.fontName = BLKDefaults.selectedFontName()
params.themeName = BLKDefaults.selectedThemeName()
params.themeName = BLKDefaults.currentThemeNameForAppearance()
params.enableBold = BLKDefaults.enableBold()
params.boldAsBright = BLKDefaults.isBoldAsBright()
params.viewSize = .zero
Expand Down Expand Up @@ -293,6 +293,17 @@ class TermController: UIViewController {
selector: #selector(_relayout),
name: NSNotification.Name(rawValue: LayoutManagerBottomInsetDidUpdate), object: nil)

NotificationCenter.default.addObserver(
self,
selector: #selector(_themeChanged(_:)),
name: NSNotification.Name(rawValue: BKAppearanceChanged), object: nil)

}

@objc private func _themeChanged(_ notification: Notification) {
let themeName = notification.object as? String ?? BLKDefaults.currentThemeNameForAppearance()
_termView.applyTheme(themeName)
_sessionParams.themeName = themeName
}

@objc func _relayout() {
Expand Down Expand Up @@ -333,6 +344,16 @@ class TermController: UIViewController {
_sessionParams.viewSize = view.bounds.size
}

public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle,
BLKDefaults.isAutoThemeToggleEnabled() {
let themeName = BLKDefaults.currentThemeNameForAppearance()
_termView.applyTheme(themeName)
_sessionParams.themeName = themeName
}
}

@objc public func terminate() {
NotificationCenter.default.post(name: .deviceTerminated, object: nil, userInfo: ["device": _termDevice])
_proxyView.destroyControlledView()
Expand Down
2 changes: 1 addition & 1 deletion Blink/TermView.m
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ - (WKUserScript *)_termInitScriptWith:(MCPParams *)params;
}

- (void)applyTheme:(NSString *)themeName {
NSString *themeContent = [[BKTheme withName: themeName ?: [BLKDefaults selectedThemeName]] content];
NSString *themeContent = [[BKTheme withName:themeName ?: [BLKDefaults selectedThemeName]] content];
if (themeContent) {
NSString *script = [NSString stringWithFormat:@"(function(){%@})();", themeContent];
[_webView evaluateJavaScript:script completionHandler:nil];
Expand Down
53 changes: 53 additions & 0 deletions BlinkTests/AutoThemeToggleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import XCTest
@testable import Blink

class AutoThemeToggleTests: XCTestCase {

override func setUp() {
super.setUp()
BLKDefaults.loadDefaults()
}

override func tearDown() {
BLKDefaults.setAutoThemeToggleEnabled(false)
BLKDefaults.setLightThemeName("Default")
BLKDefaults.setDarkThemeName("Default")
BLKDefaults.saveDefaults()
super.tearDown()
}

func testAutoThemeToggleDefaults() {
XCTAssertFalse(BLKDefaults.isAutoThemeToggleEnabled())
XCTAssertEqual(BLKDefaults.selectedLightThemeName(), "Default")
XCTAssertEqual(BLKDefaults.selectedDarkThemeName(), "Default")
}

func testEnablingAutoThemeToggle() {
BLKDefaults.setAutoThemeToggleEnabled(true)
XCTAssertTrue(BLKDefaults.isAutoThemeToggleEnabled())
}

func testSettingLightAndDarkThemes() {
BLKDefaults.setLightThemeName("Light")
BLKDefaults.setDarkThemeName("Dark")

XCTAssertEqual(BLKDefaults.selectedLightThemeName(), "Light")
XCTAssertEqual(BLKDefaults.selectedDarkThemeName(), "Dark")
}

func testCurrentThemeNameForAppearanceWithAutoToggleDisabled() {
BLKDefaults.setAutoThemeToggleEnabled(false)
BLKDefaults.setThemeName("Custom")

let currentTheme = BLKDefaults.currentThemeNameForAppearance()
XCTAssertEqual(currentTheme, "Custom")
}

func testThemeApplicationPostsNotification() {
let expectation = self.expectation(forNotification: NSNotification.Name(rawValue: BKAppearanceChanged), object: nil, handler: nil)

BLKDefaults.applyCurrentTheme()

waitForExpectations(timeout: 1.0, handler: nil)
}
}
12 changes: 12 additions & 0 deletions Settings/Model/BLKDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ typedef NS_ENUM(NSInteger, BKSnippetDefaultLocation) {
@property (nonatomic) BKSnippetDefaultLocation snippetsDefaultLocation;
@property (nonatomic, strong) NSString *scratchLanguageMode;

@property (nonatomic) BOOL autoThemeToggleEnabled;
@property (nonatomic, strong) NSString *lightThemeName;
@property (nonatomic, strong) NSString *darkThemeName;

+ (void)loadDefaults;
+ (BOOL)saveDefaults;
+ (void)setCursorBlink:(BOOL)state;
Expand Down Expand Up @@ -144,6 +148,14 @@ typedef NS_ENUM(NSInteger, BKSnippetDefaultLocation) {
+ (void)setScratchLanguageMode:(NSString *)mode;
+ (NSString *)scratchLanguageMode;

+ (void)setAutoThemeToggleEnabled:(BOOL)enabled;
+ (BOOL)isAutoThemeToggleEnabled;
+ (void)setLightThemeName:(NSString *)themeName;
+ (NSString *)selectedLightThemeName;
+ (void)setDarkThemeName:(NSString *)themeName;
+ (NSString *)selectedDarkThemeName;
+ (NSString *)currentThemeNameForAppearance;
+ (void)applyCurrentTheme;

+ (void)applyExternalScreenCompensation:(BKOverscanCompensation)value;
@end
53 changes: 52 additions & 1 deletion Settings/Model/BLKDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ - (id)initWithCoder:(NSCoder *)coder
_dontUseBlinkSnippetsIndex = [coder decodeBoolForKey:@"dontUseBlinkSnippetsIndex"];
_snippetsDefaultLocation = [coder decodeIntegerForKey:@"snippetsDefaultLocation"];
_scratchLanguageMode = [coder decodeObjectOfClass:[NSString class] forKey:@"scratchLanguageMode"];
_autoThemeToggleEnabled = [coder decodeBoolForKey:@"autoThemeToggleEnabled"];
_lightThemeName = [coder decodeObjectOfClasses:strings forKey:@"lightThemeName"];
_darkThemeName = [coder decodeObjectOfClasses:strings forKey:@"darkThemeName"];

return self;
}
Expand Down Expand Up @@ -133,6 +136,9 @@ - (void)encodeWithCoder:(NSCoder *)encoder
[encoder encodeBool:_dontUseBlinkSnippetsIndex forKey:@"dontUseBlinkSnippetsIndex"];
[encoder encodeInteger:_snippetsDefaultLocation forKey:@"snippetsDefaultLocation"];
[encoder encodeObject:_scratchLanguageMode forKey:@"scratchLanguageMode"];
[encoder encodeBool:_autoThemeToggleEnabled forKey:@"autoThemeToggleEnabled"];
[encoder encodeObject:_lightThemeName forKey:@"lightThemeName"];
[encoder encodeObject:_darkThemeName forKey:@"darkThemeName"];

}

Expand Down Expand Up @@ -218,7 +224,13 @@ + (void)loadDefaults {
if (!defaults.themeName) {
[defaults setThemeName:@"Default"];
}

if (!defaults.lightThemeName) {
[defaults setLightThemeName:@"Default"];
}
if (!defaults.darkThemeName) {
[defaults setDarkThemeName:@"Default"];
}

if (!defaults.fontSize) {
#if TARGET_OS_MACCATALYST
[defaults setFontSize:[NSNumber numberWithInt:22]];
Expand Down Expand Up @@ -497,4 +509,43 @@ + (void)applyExternalScreenCompensation:(BKOverscanCompensation)value {

}

+ (void)setAutoThemeToggleEnabled:(BOOL)enabled {
defaults.autoThemeToggleEnabled = enabled;
}

+ (BOOL)isAutoThemeToggleEnabled {
return defaults.autoThemeToggleEnabled;
}

+ (void)setLightThemeName:(NSString *)themeName {
defaults.lightThemeName = themeName;
}

+ (NSString *)selectedLightThemeName {
return defaults.lightThemeName;
}

+ (void)setDarkThemeName:(NSString *)themeName {
defaults.darkThemeName = themeName;
}

+ (NSString *)selectedDarkThemeName {
return defaults.darkThemeName;
}

+ (NSString *)currentThemeNameForAppearance {
if (!defaults.autoThemeToggleEnabled) {
return defaults.themeName;
}
UIUserInterfaceStyle style = UITraitCollection.currentTraitCollection.userInterfaceStyle;
return (style == UIUserInterfaceStyleDark)
? (defaults.darkThemeName ?: defaults.themeName)
: (defaults.lightThemeName ?: defaults.themeName);
}

+ (void)applyCurrentTheme {
NSString *themeName = [self currentThemeNameForAppearance];
[[NSNotificationCenter defaultCenter] postNotificationName:BKAppearanceChanged object:themeName];
}

@end
Loading