Skip to content

Commit

Permalink
Merge pull request #936 from Sequel-Ace/windowUpdates
Browse files Browse the repository at this point in the history
Move NSWindowDelegate to Swift #infra
  • Loading branch information
Kaspik committed Mar 4, 2021
2 parents 57f0bbf + 1228549 commit 574bad7
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 126 deletions.
6 changes: 3 additions & 3 deletions .github/pull_request_template.md
Expand Up @@ -13,7 +13,7 @@ Please use one of these hashtags for your PR title:
-

## Closes following issues:
- Closes
- Closes:

## Tested:
- Processors:
Expand All @@ -25,12 +25,12 @@ Please use one of these hashtags for your PR title:
- [ ] 10.14.x (Mojave)
- [ ] 10.15.x (Catalina)
- [ ] 11.x (Big Sur)
- Xcode Version:
- Localizations:
- [ ] English
- [ ] Spanish
- [ ] Other (please specify)
- Xcode Version:

## Screenshots:


## Additional notes:
1 change: 1 addition & 0 deletions Frameworks/QueryKit/QueryKit.xcodeproj/project.pbxproj
Expand Up @@ -365,6 +365,7 @@
hasScannedForEncodings = 1;
knownRegions = (
en,
Base,
);
mainGroup = 0867D691FE84028FC02AAC07 /* QueryKit */;
productRefGroup = 034768DFFF38A50411DB9C8B /* Products */;
Expand Down
2 changes: 1 addition & 1 deletion Resources/Localization/en.lproj/Localizable.strings
Expand Up @@ -3295,4 +3295,4 @@
"No Newer Release Available" = "No Newer Release Available";

/* Info alert message when there are Newer Releases Available */
"You are currently running the latest release." = "You are currently running the latest release."'
"You are currently running the latest release." = "You are currently running the latest release.";
3 changes: 3 additions & 0 deletions Source/Controllers/SPAppController.h
Expand Up @@ -29,6 +29,9 @@
//
// More info at <https://github.com/sequelpro/sequelpro>

#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>

@class SPPreferenceController;
@class SPAboutController;
@class SPDatabaseDocument;
Expand Down
8 changes: 3 additions & 5 deletions Source/Controllers/Window/SPWindowController.h
Expand Up @@ -34,16 +34,14 @@
@class SPDatabaseDocument;
@protocol SPWindowControllerDelegate;

@interface SPWindowController : NSWindowController <NSWindowDelegate>
@interface SPWindowController : NSWindowController
{
NSClipView *titleBarLineHidingView;

NSMenuItem *closeWindowMenuItem;
NSMenuItem *closeTabMenuItem;

NSMutableArray *managedDatabaseConnections;
}

@property (nonatomic, strong) NSMenuItem *closeWindowMenuItem;
@property (nonatomic, strong) NSMenuItem *closeTabMenuItem;
@property (nonatomic, weak) id<SPWindowControllerDelegate> delegate;
@property (nonatomic, strong) IBOutlet NSTabView *tabView;
@property (nonatomic, strong) IBOutlet PSMTabBarControl *tabBarControl;
Expand Down
111 changes: 3 additions & 108 deletions Source/Controllers/Window/SPWindowController.m
Expand Up @@ -66,7 +66,7 @@ - (void)awakeFromNib {
[super awakeFromNib];

[self setupAppearance];
// [self setupConstraints]; FIXME: causes a crash
[self setupConstraints];

[self _switchOutSelectedTableDocument:nil];

Expand All @@ -85,8 +85,8 @@ - (void)awakeFromNib {

// Retrieve references to the 'Close Window' and 'Close Tab' menus. These are updated as window focus changes.
NSMenu *mainMenu = [NSApp mainMenu];
closeWindowMenuItem = [[[mainMenu itemWithTag:SPMainMenuFile] submenu] itemWithTag:SPMainMenuFileClose];
closeTabMenuItem = [[[mainMenu itemWithTag:SPMainMenuFile] submenu] itemWithTag:SPMainMenuFileCloseTab];
_closeWindowMenuItem = [[[mainMenu itemWithTag:SPMainMenuFile] submenu] itemWithTag:SPMainMenuFileClose];
_closeTabMenuItem = [[[mainMenu itemWithTag:SPMainMenuFile] submenu] itemWithTag:SPMainMenuFileCloseTab];

// Register for drag start and stop notifications - used to show/hide tab bars
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
Expand Down Expand Up @@ -483,111 +483,6 @@ - (void)_selectedTableDocumentDeallocd:(NSNotification *)notification
[self _switchOutSelectedTableDocument:nil];
}

#pragma mark - SPWindowControllerDelegate

/**
* Determine whether the window is permitted to close.
* Go through the tabs in this window, and ask the database connection view
* in each one if it can be closed, returning YES only if all can be closed.
*/
- (BOOL)windowShouldClose:(NSWindow *)sender {
for (NSTabViewItem *eachItem in [self.tabView tabViewItems]) {
SPDatabaseDocument *eachDocument = [eachItem databaseDocument];

if (![eachDocument parentTabShouldClose]) {
return NO;
}
}

// Remove global session data if the last window of a session will be closed
if ([SPAppDelegate sessionURL] && [[SPAppDelegate windowControllers] count] == 1) {
[SPAppDelegate setSessionURL:nil];
[SPAppDelegate setSpfSessionDocData:nil];
}
[self.delegate windowControllerDidClose:self];
return YES;
}

/**
* When the window does close, close all tabs.
*/
- (void)windowWillClose:(NSNotification *)notification
{
SPLog(@"windowWillClose, notification: %@", notification);
for (NSTabViewItem *eachItem in [self.tabView tabViewItems])
{
[self.tabView removeTabViewItem:eachItem];
}
}

/**
* When the window becomes key, inform the selected tab and
* update menu items.
*/
- (void)windowDidBecomeKey:(NSNotification *)notification
{
[self.selectedTableDocument tabDidBecomeKey];

// Update the "Close window" item
[closeWindowMenuItem setTitle:NSLocalizedString(@"Close Window", @"Close Window menu item")];
[closeWindowMenuItem setKeyEquivalentModifierMask:(NSEventModifierFlagCommand | NSEventModifierFlagShift)];

// Ensure the "Close tab" item is enabled and has the standard shortcut
[closeTabMenuItem setEnabled:YES];
[closeTabMenuItem setKeyEquivalent:@"w"];
[closeTabMenuItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
}

/**
* When the window resigns key, update menu items.
*/
- (void)windowDidResignKey:(NSNotification *)notification
{

// Disable the "Close tab" menu item
[closeTabMenuItem setEnabled:NO];
[closeTabMenuItem setKeyEquivalent:@""];

// Update the "Close window" item to show only "Close"
[closeWindowMenuItem setTitle:NSLocalizedString(@"Close", @"Close menu item")];
[closeWindowMenuItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
}

/**
* Observe changes in main window status to update drawing state to match
*/
- (void)windowDidBecomeMain:(NSNotification *)notification
{
}

- (void)windowDidResignMain:(NSNotification *)notification
{
}

/**
* If the window is resized, notify all the tabs.
*/
- (void)windowDidResize:(NSNotification *)notification
{
for (NSTabViewItem *eachItem in [self.tabView tabViewItems]) {
[[eachItem databaseDocument] tabDidResize];
}
}

/**
* If the window is entering fullscreen, update the front tab's titlebar status view visibility.
*/
- (void)windowWillEnterFullScreen:(NSNotification *)notification
{
}

/**
* If the window exits fullscreen, update the front tab's titlebar status view visibility.
*/
- (void)windowDidExitFullScreen:(NSNotification *)notification
{
}

#pragma mark -
#pragma mark Tab view delegate methods

Expand Down
78 changes: 69 additions & 9 deletions Source/Controllers/Window/SPWindowController.swift
Expand Up @@ -11,19 +11,79 @@ import SnapKit

extension SPWindowController {
@objc func setupAppearance() {
// Here should ahppen all UI / layout setups
// Here should ahppen all UI / layout setups in the future ocne we remove .xib
}

@objc func setupConstraints() {
tabBarControl.snp.makeConstraints {
$0.top.equalToSuperview()
$0.leading.trailing.equalToSuperview()
$0.height.equalTo(25)
// Here we will set constraints in the future once we remove .xib, for now, commented out as it crashes
// tabBarControl.snp.makeConstraints {
// $0.top.equalToSuperview()
// $0.leading.trailing.equalToSuperview()
// $0.height.equalTo(25)
// }
// tabView.snp.makeConstraints {
// $0.top.equalTo(tabBarControl.snp.bottom)
// $0.leading.trailing.equalToSuperview()
// $0.bottom.equalToSuperview()
// }
}
}

extension SPWindowController: NSWindowDelegate {
/// Determine whether the window is permitted to close.
/// Go through the tabs in this window, and ask the database connection view in each one if it can be closed, returning YES only if all can be closed.
/// - Parameter sender: NSWindow instance
/// - Returns: true or false
public func windowShouldClose(_ sender: NSWindow) -> Bool {
for tabItem in tabView.tabViewItems {
guard let document = tabItem.databaseDocument else {
continue
}
if !document.parentTabShouldClose() {
return false
}
}

if let appDelegate = NSApp.delegate as? SPAppController, appDelegate.sessionURL() != nil, appDelegate.windowControllers.count == 1 {
appDelegate.setSessionURL(nil)
appDelegate.setSpfSessionDocData(nil)
}
tabView.snp.makeConstraints {
$0.top.equalTo(tabBarControl.snp.bottom)
$0.leading.trailing.equalToSuperview()
$0.bottom.equalToSuperview()
delegate.windowControllerDidClose(self)
return true
}

public func windowWillClose(_ notification: Notification) {
tabView.tabViewItems.forEach {
tabView.removeTabViewItem($0)
}
}

public func windowDidBecomeKey(_ notification: Notification) {
selectedTableDocument.tabDidBecomeKey()

// Update close tab
closeTabMenuItem.isEnabled = true
closeTabMenuItem.keyEquivalent = "w"
closeTabMenuItem.keyEquivalentModifierMask = .command

// Update the "Close" item to show "Close window"
closeWindowMenuItem.title = NSLocalizedString("Close Window", comment: "Close Window menu item")
closeWindowMenuItem.keyEquivalentModifierMask = [.command, .shift]
}

public func windowDidResignKey(_ notification: Notification) {
// Update close tab
closeTabMenuItem.isEnabled = true
closeTabMenuItem.keyEquivalentModifierMask = [.command, .shift]

// Update the "Close window" item to show only "Close"
closeWindowMenuItem.title = NSLocalizedString("Close", comment: "Close menu item")
closeWindowMenuItem.keyEquivalentModifierMask = .command
}

public func windowDidResize(_ notification: Notification) {
tabView.tabViewItems.forEach {
$0.databaseDocument?.tabDidResize()
}
}
}
1 change: 1 addition & 0 deletions Source/Sequel-Ace-Bridging-Header.h
Expand Up @@ -27,6 +27,7 @@
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "SPAppController.h"
#import "SPWindowController.h"
#import "SPDatabaseDocument.h"
#import "PSMTabBarControl.h"
Expand Down

0 comments on commit 574bad7

Please sign in to comment.