Skip to content

Commit 3ff09eb

Browse files
committed
Merge SPAppController (part of #2789)
1 parent 2f0099e commit 3ff09eb

14 files changed

+261
-426
lines changed

Source/SPAppController.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
@class SPAboutController;
3838
@class SPDatabaseDocument;
3939
@class SPBundleEditorController;
40+
@class SPWindowController;
4041

4142
@interface SPAppController : NSObject <FRFeedbackReporterDelegate, NSApplicationDelegate, NSOpenSavePanelDelegate, NSFileManagerDelegate>
4243
{
@@ -116,4 +117,25 @@
116117
- (void)addHTMLOutputController:(id)controller;
117118
- (void)removeHTMLOutputController:(id)controller;
118119

120+
#pragma mark - SPAppleScriptSupport
121+
122+
- (NSArray *)orderedDocuments;
123+
- (void)insertInOrderedDocuments:(SPDatabaseDocument *)doc;
124+
- (NSArray *)orderedWindows;
125+
- (id)handleQuitScriptCommand:(NSScriptCommand *)command;
126+
- (id)handleOpenScriptCommand:(NSScriptCommand *)command;
127+
128+
#pragma mark - SPWindowManagement
129+
130+
- (IBAction)newWindow:(id)sender;
131+
- (IBAction)newTab:(id)sender;
132+
- (IBAction)duplicateTab:(id)sender;
133+
134+
- (SPWindowController *)newWindow;
135+
- (SPDatabaseDocument *)makeNewConnectionTabOrWindow;
136+
- (SPWindowController *)frontController;
137+
138+
- (NSWindow *)frontDocumentWindow;
139+
- (void)tabDragStarted:(id)sender;
140+
119141
@end

Source/SPAppController.m

Lines changed: 239 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
#import "SPFavoritesController.h"
4848
#import "SPEditorTokens.h"
4949
#import "SPBundleCommandRunner.h"
50-
#import "SPWindowManagement.h"
5150
#import "SPCopyTable.h"
5251
#import "SPSyntaxParser.h"
5352
#import "SPOSInfo.h"
53+
#import "SPPrintController.h"
5454

5555
#import <PSMTabBar/PSMTabBarControl.h>
5656
#import <Sparkle/Sparkle.h>
@@ -2329,6 +2329,244 @@ - (void)_copyDefaultThemes
23292329
}
23302330
}
23312331

2332+
#pragma mark - SPAppleScriptSupport
2333+
2334+
/**
2335+
* Is needed to interact with AppleScript for set/get internal SP variables
2336+
*/
2337+
- (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key
2338+
{
2339+
NSLog(@"Not yet implemented.");
2340+
2341+
return NO;
2342+
}
2343+
2344+
/**
2345+
* AppleScript call to get the available documents.
2346+
*/
2347+
- (NSArray *)orderedDocuments
2348+
{
2349+
NSMutableArray *orderedDocuments = [NSMutableArray array];
2350+
2351+
for (NSWindow *aWindow in [self orderedWindows])
2352+
{
2353+
if ([[aWindow windowController] isMemberOfClass:[SPWindowController class]]) {
2354+
[orderedDocuments addObjectsFromArray:[[aWindow windowController] documents]];
2355+
}
2356+
}
2357+
2358+
return orderedDocuments;
2359+
}
2360+
2361+
/**
2362+
* AppleScript support for 'make new document'.
2363+
*
2364+
* TODO: following tab support this has been disabled - need to discuss reimplmenting vs syntax.
2365+
*/
2366+
- (void)insertInOrderedDocuments:(SPDatabaseDocument *)doc
2367+
{
2368+
[self newWindow:self];
2369+
2370+
// Set autoconnection if appropriate
2371+
if ([[NSUserDefaults standardUserDefaults] boolForKey:SPAutoConnectToDefault]) {
2372+
[[self frontDocument] connect];
2373+
}
2374+
}
2375+
2376+
/**
2377+
* AppleScript call to get the available windows.
2378+
*/
2379+
- (NSArray *)orderedWindows
2380+
{
2381+
return [NSApp orderedWindows];
2382+
}
2383+
2384+
/**
2385+
* AppleScript handler to quit Sequel Pro
2386+
*
2387+
* This handler is required to allow termination via the Dock or AppleScript event after activating it using AppleScript
2388+
*/
2389+
- (id)handleQuitScriptCommand:(NSScriptCommand *)command
2390+
{
2391+
[NSApp terminate:self];
2392+
2393+
return nil;
2394+
}
2395+
2396+
/**
2397+
* AppleScript open handler
2398+
*
2399+
* This handler is required to catch the 'open' command if no argument was passed which would cause a crash.
2400+
*/
2401+
- (id)handleOpenScriptCommand:(NSScriptCommand *)command
2402+
{
2403+
return nil;
2404+
}
2405+
2406+
/**
2407+
* AppleScript print handler
2408+
*
2409+
* This handler prints the active view.
2410+
*/
2411+
- (id)handlePrintScriptCommand:(NSScriptCommand *)command
2412+
{
2413+
SPDatabaseDocument *frontDoc = [self frontDocument];
2414+
2415+
if (frontDoc && ![frontDoc isWorking] && ![[frontDoc connectionID] isEqualToString:@"_"]) {
2416+
[frontDoc startPrintDocumentOperation];
2417+
}
2418+
2419+
return nil;
2420+
}
2421+
2422+
#pragma mark - SPWindowManagement
2423+
2424+
- (IBAction)newWindow:(id)sender
2425+
{
2426+
[self newWindow];
2427+
}
2428+
2429+
/**
2430+
* Create a new window, containing a single tab.
2431+
*/
2432+
- (SPWindowController *)newWindow
2433+
{
2434+
static NSPoint cascadeLocation = {.x = 0, .y = 0};
2435+
2436+
// Create a new window controller, and set up a new connection view within it.
2437+
SPWindowController *newWindowController = [[SPWindowController alloc] initWithWindowNibName:@"MainWindow"];
2438+
NSWindow *newWindow = [newWindowController window];
2439+
2440+
// Cascading defaults to on - retrieve the window origin automatically assigned by cascading,
2441+
// and convert to a top left point.
2442+
NSPoint topLeftPoint = [newWindow frame].origin;
2443+
topLeftPoint.y += [newWindow frame].size.height;
2444+
2445+
// The first window should use autosaving; subsequent windows should cascade.
2446+
// So attempt to set the frame autosave name; this will succeed for the very
2447+
// first window, and fail for others.
2448+
BOOL usedAutosave = [newWindow setFrameAutosaveName:@"DBView"];
2449+
2450+
if (!usedAutosave) {
2451+
[newWindow setFrameUsingName:@"DBView"];
2452+
}
2453+
2454+
// Add the connection view
2455+
[newWindowController addNewConnection];
2456+
2457+
// Cascade according to the statically stored cascade location.
2458+
cascadeLocation = [newWindow cascadeTopLeftFromPoint:cascadeLocation];
2459+
2460+
// Set the window controller as the window's delegate
2461+
[newWindow setDelegate:newWindowController];
2462+
2463+
// Show the window, and perform frontmost tasks again once the window has drawn
2464+
[newWindowController showWindow:self];
2465+
[[newWindowController selectedTableDocument] didBecomeActiveTabInWindow];
2466+
2467+
return newWindowController;
2468+
}
2469+
2470+
/**
2471+
* Create a new tab in the frontmost window.
2472+
*/
2473+
- (IBAction)newTab:(id)sender
2474+
{
2475+
SPWindowController *frontController = [self frontController];
2476+
2477+
// If no window was found, create a new one
2478+
if (!frontController) {
2479+
[self newWindow:self];
2480+
}
2481+
else {
2482+
if ([[frontController window] isMiniaturized]) {
2483+
[[frontController window] deminiaturize:self];
2484+
}
2485+
2486+
[frontController addNewConnection:self];
2487+
}
2488+
}
2489+
2490+
- (SPDatabaseDocument *)makeNewConnectionTabOrWindow
2491+
{
2492+
SPWindowController *frontController = [self frontController];
2493+
2494+
SPDatabaseDocument *frontDocument;
2495+
// If no window was found or the front most window has no tabs, create a new one
2496+
if (!frontController || [[frontController valueForKeyPath:@"tabView"] numberOfTabViewItems] == 1) {
2497+
frontController = [self newWindow];
2498+
frontDocument = [frontController selectedTableDocument];
2499+
}
2500+
// Open the spf file in a new tab if the tab bar is visible
2501+
else {
2502+
if ([[frontController window] isMiniaturized]) [[frontController window] deminiaturize:self];
2503+
frontDocument = [frontController addNewConnection];
2504+
}
2505+
2506+
return frontDocument;
2507+
}
2508+
2509+
/**
2510+
* Duplicate the current connection tab
2511+
*/
2512+
- (IBAction)duplicateTab:(id)sender
2513+
{
2514+
SPDatabaseDocument *theFrontDocument = [self frontDocument];
2515+
2516+
if (!theFrontDocument) return [self newTab:sender];
2517+
2518+
// Add a new tab to the window
2519+
if ([[self frontDocumentWindow] isMiniaturized]) {
2520+
[[self frontDocumentWindow] deminiaturize:self];
2521+
}
2522+
2523+
SPDatabaseDocument *newConnection = [[self frontController] addNewConnection];
2524+
2525+
// Get the state of the previously-frontmost document
2526+
NSDictionary *allStateDetails = @{
2527+
@"connection" : @YES,
2528+
@"history" : @YES,
2529+
@"session" : @YES,
2530+
@"query" : @YES,
2531+
@"password" : @YES
2532+
};
2533+
2534+
NSMutableDictionary *frontState = [NSMutableDictionary dictionaryWithDictionary:[theFrontDocument stateIncludingDetails:allStateDetails]];
2535+
2536+
// Ensure it's set to autoconnect
2537+
[frontState setObject:@YES forKey:@"auto_connect"];
2538+
2539+
// Set the connection on the new tab
2540+
[newConnection setState:frontState];
2541+
}
2542+
2543+
/**
2544+
* Retrieve the frontmost document window; returns nil if not found.
2545+
*/
2546+
- (NSWindow *)frontDocumentWindow
2547+
{
2548+
return [[self frontController] window];
2549+
}
2550+
2551+
- (SPWindowController *)frontController
2552+
{
2553+
for (NSWindow *aWindow in [NSApp orderedWindows]) {
2554+
id ctr = [aWindow windowController];
2555+
if ([ctr isMemberOfClass:[SPWindowController class]]) {
2556+
return ctr;
2557+
}
2558+
}
2559+
return nil;
2560+
}
2561+
2562+
/**
2563+
* When tab drags start, bring all the windows in front of other applications.
2564+
*/
2565+
- (void)tabDragStarted:(id)sender
2566+
{
2567+
[NSApp arrangeInFront:self];
2568+
}
2569+
23322570
#pragma mark -
23332571

23342572
- (void)dealloc

Source/SPAppleScriptSupport.h

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)