Skip to content

Commit

Permalink
set background color and status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Melbye committed Nov 13, 2018
1 parent 3bcdaf2 commit 70416e5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
5 changes: 5 additions & 0 deletions app/AboutViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ - (void)viewDidLoad {
[self _updatePreferenceUI];
}

- (void)dealloc
{
[self _removeObservers];
}

- (void)_addObservers {
UserPreferences *prefs = [UserPreferences shared];
NSKeyValueObservingOptions opts = NSKeyValueObservingOptionNew;
Expand Down
2 changes: 1 addition & 1 deletion app/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="FQi-r8-odu" customClass="TerminalView">
<rect key="frame" x="0.0" y="20" width="694" height="748"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<connections>
<outlet property="controlKey" destination="W9v-cj-FWz" id="HgQ-u7-5IL"/>
<outlet property="inputAccessoryView" destination="GyN-ob-WFz" id="mVD-kz-BQc"/>
Expand Down
29 changes: 27 additions & 2 deletions app/TerminalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "AppDelegate.h"
#import "TerminalView.h"
#import "ArrowBarButton.h"
#import "UserPreferences.h"

@interface TerminalViewController () <UIGestureRecognizerDelegate>

Expand Down Expand Up @@ -53,9 +54,11 @@ - (void)viewDidLoad {
selector:@selector(ishExited:)
name:ISHExitedNotification
object:nil];

[self.termView registerExternalKeyboardNotificationsToNotificationCenter:center];

self.view.backgroundColor = ThemeBackgroundColor([UserPreferences shared].theme);
[[UserPreferences shared] addObserver:self forKeyPath:@"theme" options:NSKeyValueObservingOptionNew context:nil];

[self.termView registerExternalKeyboardNotificationsToNotificationCenter:center];
if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
[self.bar removeArrangedSubview:self.hideKeyboardButton];
[self.hideKeyboardButton removeFromSuperview];
Expand All @@ -67,6 +70,28 @@ - (void)viewDidLoad {
}
}

- (void)dealloc
{
@try {
[[UserPreferences shared] removeObserver:self forKeyPath:@"theme"];
} @catch (NSException * __unused exception) {}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == [UserPreferences shared]) {
[UIView animateWithDuration:0.1 animations:^{
self.view.backgroundColor = ThemeBackgroundColor([UserPreferences shared].theme);
}];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

- (UIStatusBarStyle)preferredStatusBarStyle {
return ThemeStatusBar([UserPreferences shared].theme);
}

- (BOOL)prefersStatusBarHidden {
BOOL isIPhoneX = UIApplication.sharedApplication.delegate.window.safeAreaInsets.top > 20;
return !isIPhoneX;
Expand Down
1 change: 1 addition & 0 deletions app/UserPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef NS_ENUM(NSInteger, UserPreferenceTheme) {

extern UIColor *ThemeBackgroundColor(UserPreferenceTheme theme);
extern UIColor *ThemeForegroundColor(UserPreferenceTheme theme);
extern UIStatusBarStyle ThemeStatusBar(UserPreferenceTheme theme);
extern NSString *ThemeName(UserPreferenceTheme theme);

NS_ASSUME_NONNULL_BEGIN
Expand Down
35 changes: 26 additions & 9 deletions app/UserPreferences.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
}
}

UIStatusBarStyle ThemeStatusBar(UserPreferenceTheme theme) {
switch (theme) {
case UserPreferenceThemeLight:
return UIStatusBarStyleDefault;
case UserPreferenceThemeDark:
return UIStatusBarStyleLightContent;
case UserPreferenceThemeCount:
assert("invalid theme");
return UIStatusBarStyleDefault;
}
}

NSString *ThemeName(UserPreferenceTheme theme) {
switch (theme) {
case UserPreferenceThemeLight:
Expand Down Expand Up @@ -113,17 +125,22 @@ - (NSString *)JSONDictionary
return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:0 error:nil] encoding:NSUTF8StringEncoding];
}

- (NSString *)_hexFromUIColor:(UIColor *)color {
-(NSString *)_hexFromUIColor:(UIColor *)color
{
const CGFloat *components = CGColorGetComponents(color.CGColor);
size_t count = CGColorGetNumberOfComponents(color.CGColor);

if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
color = [UIColor colorWithRed:components[30] green:components[141] blue:components[13] alpha:components[1]];
if(count == 2){
return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
lroundf(components[0] * 255.0),
lroundf(components[0] * 255.0),
lroundf(components[0] * 255.0)];
} else {
return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
lroundf(components[0] * 255.0),
lroundf(components[1] * 255.0),
lroundf(components[2] * 255.0)];
}
if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
return [NSString stringWithFormat:@"#FFFFFF"];
}
return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(color.CGColor))[0]*255.0), (int)((CGColorGetComponents(color.CGColor))[1]*255.0), (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];

}

@end

0 comments on commit 70416e5

Please sign in to comment.