Skip to content

Commit

Permalink
Common Superclass for Floating Window and Panel and other changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
n0shake committed Apr 9, 2016
1 parent 0cfebbb commit 5f73b4d
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 300 deletions.
1 change: 0 additions & 1 deletion Clocker/Appearance Tab/CLAppearanceViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@

@interface CLAppearanceViewController : NSViewController


@end
121 changes: 70 additions & 51 deletions Clocker/Appearance Tab/CLAppearanceViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "PanelController.h"
#import "CommonStrings.h"
#import "CLTimezoneData.h"
#import "CLFloatingWindowController.h"

@interface CLAppearanceViewController ()
@property (weak) IBOutlet NSSegmentedControl *timeFormat;
Expand Down Expand Up @@ -42,12 +43,16 @@ - (IBAction)timeFormatSelectionChanged:(id)sender

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:timeFormat.selectedSegment] forKey:CL24hourFormatSelectedKey];

[self refreshMainTableview];
[self refreshMainTableview:YES andUpdateFloatingWindow:YES];
}

- (IBAction)themeChanged:(id)sender
{
NSSegmentedControl *themeSegment = (NSSegmentedControl *)sender;

//Get the current display mode
[self refreshMainTableview:NO andUpdateFloatingWindow:YES];

ApplicationDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
PanelController *panelController = appDelegate.panelController;
[panelController.backgroundView setNeedsDisplay:YES];
Expand All @@ -71,71 +76,80 @@ - (IBAction)themeChanged:(id)sender

}

- (IBAction)displayModeChanged:(id)sender
{
NSSegmentedControl *modeSegment = (NSSegmentedControl *)sender;
ApplicationDelegate *sharedDelege = (ApplicationDelegate*)[NSApplication sharedApplication].delegate;

if (modeSegment.selectedSegment == 1)
{
sharedDelege.floatingWindow = [CLFloatingWindowController sharedFloatingWindow];
[sharedDelege.floatingWindow showWindow:nil];
[NSApp activateIgnoringOtherApps:YES];
}
else
{
sharedDelege.floatingWindow = [CLFloatingWindowController sharedFloatingWindow];
[sharedDelege.floatingWindow.window close];
[sharedDelege.panelController updateDefaultPreferences];
}
}


- (IBAction)changeRelativeDayDisplay:(id)sender
{
NSSegmentedControl *relativeDayControl = (NSSegmentedControl*) sender;
NSNumber *selectedIndex = [NSNumber numberWithInteger:relativeDayControl.selectedSegment];
[[NSUserDefaults standardUserDefaults] setObject:selectedIndex forKey:CLRelativeDateKey];
[self refreshMainTableview];
[self refreshMainTableview:YES andUpdateFloatingWindow:YES];
}


- (void)refreshMainTableview
- (void)refreshMainTableview:(BOOL)panel andUpdateFloatingWindow:(BOOL)value
{
dispatch_async(dispatch_get_main_queue(), ^{
ApplicationDelegate *appDelegate = [[NSApplication sharedApplication] delegate];

PanelController *panelController = appDelegate.panelController;

[panelController updateDefaultPreferences];

[panelController.mainTableview reloadData];

[appDelegate.menubarController shouldIconBeUpdated:YES];
if (panel)
{
ApplicationDelegate *appDelegate = [[NSApplication sharedApplication] delegate];

PanelController *panelController = appDelegate.panelController;

[panelController updateDefaultPreferences];

[panelController.mainTableview reloadData];

[appDelegate.menubarController shouldIconBeUpdated:YES];
}

if (value)
{
//Get the current display mode
NSNumber *displayMode = [[NSUserDefaults standardUserDefaults] objectForKey:CLShowAppInForeground];

if (displayMode.integerValue == 1)
{
//Get the Floating window instance
for (NSWindow *window in [NSApplication sharedApplication].windows)
{
if ([window.windowController isKindOfClass:[CLFloatingWindowController class]])
{
CLFloatingWindowController *currentInstance = (CLFloatingWindowController *)window.windowController;
[currentInstance.mainTableview reloadData];

//Only one instance where we need to update panel color and in that instance we pass panel as NO

if (!panel)
{
[currentInstance updatePanelColor];
}
}
}
}
}
});
}

- (NSImage *)imageWithSubviewsWithTextField:(NSTextField *)textField
{
NSSize mySize = textField.bounds.size;
NSSize imgSize = NSMakeSize( mySize.width, mySize.height );

NSBitmapImageRep *bir = [textField bitmapImageRepForCachingDisplayInRect:[textField bounds]];
[bir setSize:imgSize];
[textField cacheDisplayInRect:[textField bounds] toBitmapImageRep:bir];

NSImage* image = [[NSImage alloc]initWithSize:imgSize];
[image addRepresentation:bir];
return image;

}

- (NSImage *)textWithTextField:(NSTextField *)textField
{
NSString *myString = textField.stringValue;
unsigned char *string = (unsigned char *) [myString UTF8String];
NSSize mySize = NSMakeSize(50,100); //or measure the string

NSBitmapImageRep *bir = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:&string
pixelsWide:mySize.width pixelsHigh:mySize.height
bitsPerSample:8
samplesPerPixel:3 // or 4 with alpha
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:0
bytesPerRow:0 // 0 == determine automatically
bitsPerPixel:0]; // 0 == determine automatically

//draw text using -(void)drawInRect:(NSRect)aRect withAttributes:(NSDictionary *)attributes

NSImage* image = [[NSImage alloc]initWithSize:mySize];
[image addRepresentation:bir];
return image;
}

- (IBAction)changeMenuBarDisplayPreferences:(id)sender
{
NSSegmentedControl *segmentedControl = (NSSegmentedControl *)sender;
Expand All @@ -147,6 +161,11 @@ - (IBAction)changeMenuBarDisplayPreferences:(id)sender
[userDefaults setObject:shouldCityBeShown forKey:@"shouldCityBeShown"];
}

- (IBAction)showFutureSlider:(id)sender
{
//Get the current display mode
[self refreshMainTableview:NO andUpdateFloatingWindow:YES];
}


@end
32 changes: 32 additions & 0 deletions Clocker/CLParentPanelController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// CLParentPanelController.h
// Clocker
//
// Created by Abhishek Banthia on 4/4/16.
//
//

#import <Cocoa/Cocoa.h>
#import "CLOneWindowController.h"

@interface CLParentPanelController : NSWindowController<NSWindowDelegate, NSTableViewDataSource, NSTableViewDelegate>


@property (nonatomic, strong) NSMutableArray *defaultPreferences;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, assign) NSInteger futureSliderValue;
@property (nonatomic) BOOL showReviewCell;

@property (weak) IBOutlet NSButton *shutdownButton;
@property (weak) IBOutlet NSButton *preferencesButton;
@property (weak) IBOutlet NSSlider *futureSlider;
@property (weak) IBOutlet NSTableView *mainTableview;
@property (weak) IBOutlet NSLayoutConstraint *scrollViewHeight;
@property (nonatomic, strong) CLOneWindowController *oneWindow;

- (void)updateDefaultPreferences;
- (void)showOptions:(BOOL)value;
- (void)removeContextHelpForSlider;
- (void)updatePanelColor;

@end
165 changes: 165 additions & 0 deletions Clocker/CLParentPanelController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//
// CLParentPanelController.m
// Clocker
//
// Created by Abhishek Banthia on 4/4/16.
//
//

#import "CLParentPanelController.h"
#import "CLRatingCellView.h"
#import "CLTimezoneData.h"
#import "CommonStrings.h"
#import "CLOneWindowController.h"

@interface CLParentPanelController ()

@end

@implementation CLParentPanelController

- (void)awakeFromNib
{
[super awakeFromNib];

if ([[[NSUserDefaults standardUserDefaults] objectForKey:CLThemeKey] isKindOfClass:[NSString class]]) {
[[NSUserDefaults standardUserDefaults] setObject:@0 forKey:CLThemeKey];
}

NSNumber *theme = [[NSUserDefaults standardUserDefaults] objectForKey:CLThemeKey];

if (theme.integerValue == 1)
{
self.shutdownButton.image = [NSImage imageNamed:@"PowerIcon-White"];
self.preferencesButton.image = [NSImage imageNamed:@"Settings-White"];
}
else
{
self.shutdownButton.image = [NSImage imageNamed:@"PowerIcon"];
self.preferencesButton.image = [NSImage imageNamed:NSImageNameActionTemplate];
}

self.mainTableview.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone;


}

- (void) updateDefaultPreferences
{

NSArray *defaultZones = [[NSUserDefaults standardUserDefaults] objectForKey:CLDefaultPreferenceKey];

self.defaultPreferences = self.defaultPreferences == nil ? [[NSMutableArray alloc] initWithArray:defaultZones] : [NSMutableArray arrayWithArray:defaultZones];

self.scrollViewHeight.constant = self.showReviewCell ?
(self.defaultPreferences.count+1)*55+40 : self.defaultPreferences.count*55 + 30;

if (self.defaultPreferences.count == 1) {
self.futureSlider.hidden = YES;
}
else
{
self.futureSlider.hidden = NO;
}

[self updatePanelColor];
}

- (void)updatePanelColor
{
NSNumber *theme = [[NSUserDefaults standardUserDefaults] objectForKey:CLThemeKey];
if (theme.integerValue == 1)
{
[self.mainTableview setBackgroundColor:[NSColor blackColor]];
self.window.alphaValue = 0.90;
}
else
{
[self.mainTableview setBackgroundColor:[NSColor whiteColor]];
self.window.alphaValue = 1;
}
}


- (void)showOptions:(BOOL)value
{

if (self.defaultPreferences.count == 0)
{
value = YES;
}

dispatch_async(dispatch_get_main_queue(), ^{
self.shutdownButton.hidden = !value;
self.preferencesButton.hidden = !value;

});

}


- (IBAction)sliderMoved:(id)sender
{
NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *newDate = [currentCalendar dateByAddingUnit:NSCalendarUnitMinute
value:self.futureSliderValue
toDate:[NSDate date]
options:kNilOptions];

self.dateFormatter.dateStyle = kCFDateFormatterNoStyle;
self.dateFormatter.timeStyle = kCFDateFormatterShortStyle;

NSString *relativeDate = [currentCalendar isDateInToday:newDate] ? @"Today" : @"Tomorrow";

NSString *helper = [self.dateFormatter stringFromDate:newDate];

NSHelpManager *helpManager = [NSHelpManager sharedHelpManager];

NSPoint pointInScreen = [NSEvent mouseLocation];
pointInScreen.y -= 5;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@", relativeDate, helper]];
[NSHelpManager setContextHelpModeActive:YES];
[helpManager setContextHelp:attributedString forObject:self.futureSlider];
[helpManager showContextHelpForObject:self.futureSlider locationHint:pointInScreen];

[self.mainTableview reloadData];
}

- (void)removeContextHelpForSlider
{
NSEvent *newEvent = [NSEvent mouseEventWithType:NSLeftMouseDown
location:self.window.mouseLocationOutsideOfEventStream
modifierFlags:0
timestamp:0
windowNumber:self.window.windowNumber
context:self.window.graphicsContext
eventNumber:0
clickCount:1
pressure:0];
[NSApp postEvent:newEvent atStart:NO];
newEvent = [NSEvent mouseEventWithType:NSLeftMouseUp
location:self.window.mouseLocationOutsideOfEventStream
modifierFlags:0
timestamp:0
windowNumber:self.window.windowNumber
context:self.window.graphicsContext
eventNumber:0
clickCount:1
pressure:0];

[NSApp postEvent:newEvent atStart:NO];
}

#pragma mark -
#pragma mark Preferences Target-Action
#pragma mark -

- (IBAction)openPreferences:(id)sender
{
self.oneWindow = [CLOneWindowController sharedWindow];
[self.oneWindow showWindow:nil];
[NSApp activateIgnoringOtherApps:YES];

}

@end
2 changes: 2 additions & 0 deletions Clocker/Onboarding/CLOnboardingWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ - (void)windowDidLoad {
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}



+ (instancetype)sharedWindow
{
if (sharedOnboardingWindow == nil)
Expand Down
Loading

0 comments on commit 5f73b4d

Please sign in to comment.