replaced initial nib with direct code call

This commit is contained in:
relikd
2018-08-15 20:29:04 +02:00
parent c4110cd160
commit aa173c59f8
10 changed files with 157 additions and 229 deletions

View File

@@ -21,10 +21,133 @@
// SOFTWARE.
#import "AppHook.h"
#import "PyHandler.h"
#import "BarMenu.h"
@interface AppHook()
@property (strong) BarMenu *barMenu;
@end
@implementation AppHook
- (instancetype)init {
self = [super init];
self.delegate = self;
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.barMenu = [BarMenu new];
[PyHandler prepare];
printf("up and running\n");
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
[PyHandler shutdown];
}
- (IBAction)closePreferences:(id)sender {
NSLog(@"closing in %@", sender);
}
#pragma mark - Core Data stack
@synthesize persistentContainer = _persistentContainer;
- (NSPersistentContainer *)persistentContainer {
// The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
@synchronized (self) {
if (_persistentContainer == nil) {
_persistentContainer = [[NSPersistentContainer alloc] initWithName:@"DBv1"];
[_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
if (error != nil) {
NSLog(@"Couldn't read NSPersistentContainer: %@, %@", error, error.userInfo);
abort();
}
}];
NSUndoManager *um = [[NSUndoManager alloc] init];
um.groupsByEvent = NO;
um.levelsOfUndo = 30;
_persistentContainer.viewContext.undoManager = um;
}
}
return _persistentContainer;
}
#pragma mark - Core Data Saving and Undo support
- (IBAction)saveAction:(id)sender {
// Performs the save action for the application, which is to send the save: message to the application's managed object context. Any encountered errors are presented to the user.
NSManagedObjectContext *context = self.persistentContainer.viewContext;
if (![context commitEditing]) {
NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd));
}
NSError *error = nil;
if (context.hasChanges && ![context save:&error]) {
// Customize this code block to include application-specific recovery steps.
[[NSApplication sharedApplication] presentError:error];
}
}
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window {
// Returns the NSUndoManager for the application. In this case, the manager returned is that of the managed object context for the application.
return self.persistentContainer.viewContext.undoManager;
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
// Save changes in the application's managed object context before the application terminates.
NSManagedObjectContext *context = self.persistentContainer.viewContext;
if (![context commitEditing]) {
NSLog(@"%@:%@ unable to commit editing to terminate", [self class], NSStringFromSelector(_cmd));
return NSTerminateCancel;
}
if (!context.hasChanges) {
return NSTerminateNow;
}
NSError *error = nil;
if (![context save:&error]) {
// Customize this code block to include application-specific recovery steps.
BOOL result = [sender presentError:error];
if (result) {
return NSTerminateCancel;
}
NSString *question = NSLocalizedString(@"Could not save changes while quitting. Quit anyway?", @"Quit without saves error question message");
NSString *info = NSLocalizedString(@"Quitting now will lose any changes you have made since the last successful save", @"Quit without saves error question info");
NSString *quitButton = NSLocalizedString(@"Quit anyway", @"Quit anyway button title");
NSString *cancelButton = NSLocalizedString(@"Cancel", @"Cancel button title");
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:question];
[alert setInformativeText:info];
[alert addButtonWithTitle:quitButton];
[alert addButtonWithTitle:cancelButton];
NSInteger answer = [alert runModal];
if (answer == NSAlertSecondButtonReturn) {
return NSTerminateCancel;
}
}
return NSTerminateNow;
}
#pragma mark - Event Handling, Forward Send Key Down Events
static NSEventModifierFlags fnKeyFlags = NSEventModifierFlagShift | NSEventModifierFlagControl | NSEventModifierFlagOption | NSEventModifierFlagCommand | NSEventModifierFlagFunction;
@implementation AppHook
- (void) sendEvent:(NSEvent *)event {
if ([event type] == NSEventTypeKeyDown) {
if (!event.characters || event.characters.length == 0) {
@@ -60,4 +183,5 @@ static NSEventModifierFlags fnKeyFlags = NSEventModifierFlagShift | NSEventModif
}
[super sendEvent:event];
}
@end