From 0f62c995d361254447f1381fc2641eb1f7a37f1d Mon Sep 17 00:00:00 2001 From: masakih Date: Fri, 23 Dec 2016 18:33:40 +0900 Subject: [PATCH] =?utf8?q?HMExternalBrowserWindowController=E3=82=92Swift?= =?utf8?q?=E3=81=AB=E5=A4=89=E6=8F=9B=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- KCD.xcodeproj/project.pbxproj | 10 +- KCD/HMAppDelegate.m | 1 - KCD/HMBookmarkManager.h | 8 +- KCD/HMBrowserContentAdjuster.m | 3 +- KCD/HMExternalBrowserWindowController.h | 43 --- KCD/HMExternalBrowserWindowController.m | 415 ---------------------------- KCD/HMExternalBrowserWindowController.swift | 325 ++++++++++++++++++++++ KCD/KCD-Bridging-Header.h | 4 + 8 files changed, 338 insertions(+), 471 deletions(-) delete mode 100644 KCD/HMExternalBrowserWindowController.h delete mode 100644 KCD/HMExternalBrowserWindowController.m create mode 100644 KCD/HMExternalBrowserWindowController.swift diff --git a/KCD.xcodeproj/project.pbxproj b/KCD.xcodeproj/project.pbxproj index d63409cb..7b16b384 100644 --- a/KCD.xcodeproj/project.pbxproj +++ b/KCD.xcodeproj/project.pbxproj @@ -159,7 +159,6 @@ F4B1E30919C73B6A00805879 /* HMSuppliesView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4B1E30619C73B6A00805879 /* HMSuppliesView.m */; }; F4B1E30C19D1C06900805879 /* HMHokyuChargeCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4B1E30B19D1C06900805879 /* HMHokyuChargeCommand.m */; }; F4B1E30F19D1C9C400805879 /* HMApplySuppliesCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4B1E30E19D1C9C400805879 /* HMApplySuppliesCommand.m */; }; - F4B2A08F1A1F9C2100D2EF12 /* HMExternalBrowserWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = F4B2A08D1A1F9C2100D2EF12 /* HMExternalBrowserWindowController.m */; }; F4B37EF019FADFC9002F371A /* HMIgnoreZeroTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = F4B37EEF19FADFC9002F371A /* HMIgnoreZeroTransformer.m */; }; F4B37EF319FBBBED002F371A /* HMRemodelSlotCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4B37EF219FBBBED002F371A /* HMRemodelSlotCommand.m */; }; F4B37EF619FBE801002F371A /* HMRemodelSlotItemCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4B37EF519FBE801002F371A /* HMRemodelSlotItemCommand.m */; }; @@ -241,6 +240,7 @@ F4C86B921C1C445D00F260FE /* HMUpgradeShipExcludeColorTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = F4C86B911C1C445D00F260FE /* HMUpgradeShipExcludeColorTransformer.m */; }; F4D05BC61E0ABD6800688D66 /* HMSlotItemWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4D05BC51E0ABD6800688D66 /* HMSlotItemWindowController.swift */; }; F4D05BC81E0C0DA100688D66 /* HMAirBaseWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4D05BC71E0C0DA100688D66 /* HMAirBaseWindowController.swift */; }; + F4D05BCA1E0CADA100688D66 /* HMExternalBrowserWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4D05BC91E0CADA100688D66 /* HMExternalBrowserWindowController.swift */; }; F4D2CBC218AD0E1A000EBC59 /* HMMasterSTypeCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4D2CBC118AD0E1A000EBC59 /* HMMasterSTypeCommand.m */; }; F4D2CBC518AE2AE2000EBC59 /* HMCompositCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4D2CBC418AE2AE2000EBC59 /* HMCompositCommand.m */; }; F4D2CBC818B0555A000EBC59 /* HMKCManagedObject.m in Sources */ = {isa = PBXBuildFile; fileRef = F4D2CBC718B0555A000EBC59 /* HMKCManagedObject.m */; }; @@ -610,8 +610,6 @@ F4B1E30B19D1C06900805879 /* HMHokyuChargeCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMHokyuChargeCommand.m; sourceTree = ""; }; F4B1E30D19D1C9C400805879 /* HMApplySuppliesCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMApplySuppliesCommand.h; sourceTree = ""; }; F4B1E30E19D1C9C400805879 /* HMApplySuppliesCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMApplySuppliesCommand.m; sourceTree = ""; }; - F4B2A08C1A1F9C2100D2EF12 /* HMExternalBrowserWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMExternalBrowserWindowController.h; sourceTree = ""; }; - F4B2A08D1A1F9C2100D2EF12 /* HMExternalBrowserWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMExternalBrowserWindowController.m; sourceTree = ""; }; F4B37EEE19FADFC9002F371A /* HMIgnoreZeroTransformer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMIgnoreZeroTransformer.h; sourceTree = ""; }; F4B37EEF19FADFC9002F371A /* HMIgnoreZeroTransformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMIgnoreZeroTransformer.m; sourceTree = ""; }; F4B37EF119FBBBED002F371A /* HMRemodelSlotCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMRemodelSlotCommand.h; sourceTree = ""; }; @@ -713,6 +711,7 @@ F4C86B911C1C445D00F260FE /* HMUpgradeShipExcludeColorTransformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMUpgradeShipExcludeColorTransformer.m; sourceTree = ""; }; F4D05BC51E0ABD6800688D66 /* HMSlotItemWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMSlotItemWindowController.swift; sourceTree = ""; }; F4D05BC71E0C0DA100688D66 /* HMAirBaseWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMAirBaseWindowController.swift; sourceTree = ""; }; + F4D05BC91E0CADA100688D66 /* HMExternalBrowserWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMExternalBrowserWindowController.swift; sourceTree = ""; }; F4D2CBC018AD0E1A000EBC59 /* HMMasterSTypeCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMMasterSTypeCommand.h; sourceTree = ""; }; F4D2CBC118AD0E1A000EBC59 /* HMMasterSTypeCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMMasterSTypeCommand.m; sourceTree = ""; }; F4D2CBC318AE2AE2000EBC59 /* HMCompositCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMCompositCommand.h; sourceTree = ""; }; @@ -1318,8 +1317,7 @@ F4A3AD9619BDDE5500FDBF80 /* HMPreferencePanelController.xib */, F45F3BE21E06A60A0009434E /* HMUpgradableShipsWindowController.swift */, F4B37F0119FC8DCC002F371A /* HMUpgradableShipsWindowController.xib */, - F4B2A08C1A1F9C2100D2EF12 /* HMExternalBrowserWindowController.h */, - F4B2A08D1A1F9C2100D2EF12 /* HMExternalBrowserWindowController.m */, + F4D05BC91E0CADA100688D66 /* HMExternalBrowserWindowController.swift */, F4BEEFFE1B11A5A7004702B8 /* HMExternalBrowserWindowController.xib */, F4AC136E1E0963DB00851147 /* HMProgressPanel.swift */, F4C668651C54518F00601AFB /* HMProgressPanel.xib */, @@ -1883,7 +1881,6 @@ F495462E1B1894DD00E0D8D0 /* Bookmark.xcdatamodeld in Sources */, F4B414FB18B1FB6C00468DAF /* HMMasterMapCellCommand.m in Sources */, F4D6BCA91AA298FD00742FEB /* HMShipDetailViewController.m in Sources */, - F4B2A08F1A1F9C2100D2EF12 /* HMExternalBrowserWindowController.m in Sources */, F4B3DC2118DECF570011B4FC /* HMTSVSupport.m in Sources */, F4B1E30819C73B6A00805879 /* HMSuppliesCell.m in Sources */, F4666C4D1B1B3113001840DB /* HMBookmarkListTableView.m in Sources */, @@ -1993,6 +1990,7 @@ F4131ED8190FA08D004F7F9A /* HMDestroyItem2Command.m in Sources */, F48A705E1DE58A38005DFC0B /* HMKCAirBasePlaneInfo+CoreDataClass.m in Sources */, F4B3DC2718DF2F360011B4FC /* HMKenzoMark.m in Sources */, + F4D05BCA1E0CADA100688D66 /* HMExternalBrowserWindowController.swift in Sources */, F4F6E80F1B2C25D20008B66E /* HMStrengthenListItemCellView.m in Sources */, F424A7001BC8C10C0096C431 /* HMFailedCommand.m in Sources */, F49546261B1355EC00E0D8D0 /* HMBookmarkItem.m in Sources */, diff --git a/KCD/HMAppDelegate.m b/KCD/HMAppDelegate.m index 75a92b03..b9b4838d 100644 --- a/KCD/HMAppDelegate.m +++ b/KCD/HMAppDelegate.m @@ -16,7 +16,6 @@ #import "HMShipMasterDetailWindowController.h" -#import "HMExternalBrowserWindowController.h" #import "HMBrowserContentAdjuster.h" #import "HMFleetManager.h" diff --git a/KCD/HMBookmarkManager.h b/KCD/HMBookmarkManager.h index d59f282f..5a1b15e7 100644 --- a/KCD/HMBookmarkManager.h +++ b/KCD/HMBookmarkManager.h @@ -23,7 +23,7 @@ - (HMBookmarkDataStore *)editorStore; @end - -@interface NSObject (HMBookmarkMenu) -- (IBAction)selectBookmark:(id)sender; -@end +// +//@interface NSObject (HMBookmarkMenu) +//- (IBAction)selectBookmark:(id)sender; +//@end diff --git a/KCD/HMBrowserContentAdjuster.m b/KCD/HMBrowserContentAdjuster.m index a7740b6e..684305c1 100644 --- a/KCD/HMBrowserContentAdjuster.m +++ b/KCD/HMBrowserContentAdjuster.m @@ -8,8 +8,7 @@ #import "HMBrowserContentAdjuster.h" -#import "HMExternalBrowserWindowController.h" - +#import "KCD-Swift.h" @interface HMBrowserContentAdjuster () diff --git a/KCD/HMExternalBrowserWindowController.h b/KCD/HMExternalBrowserWindowController.h deleted file mode 100644 index dcdbbb94..00000000 --- a/KCD/HMExternalBrowserWindowController.h +++ /dev/null @@ -1,43 +0,0 @@ -// -// HMExternalBrowserWindowController.h -// KCD -// -// Created by Hori,Masaki on 2014/11/22. -// Copyright (c) 2014年 Hori,Masaki. All rights reserved. -// - -#import - -#import - - -@interface HMExternalBrowserWindowController : NSWindowController - -@property (nonatomic, strong) IBOutlet WebView *webView; - -@property (readonly) NSManagedObjectContext *managedObjectContext; - -@property (nonatomic) NSString *urlString; -@property (nonatomic) NSSize windowContentSize; -@property (readonly) NSRect contentVisibleRect; -@property BOOL canResize; -@property BOOL canScroll; -@property BOOL canMovePage; - -- (IBAction)clickGoBackSegment:(id)sender; - -- (IBAction)addBookmark:(id)sender; -- (IBAction)showBookmark:(id)sender; - -- (IBAction)selectBookmark:(id)sender; - -- (IBAction)scrollLeft:(id)sender; -- (IBAction)scrollRight:(id)sender; -- (IBAction)scrollUp:(id)sender; -- (IBAction)scrollDown:(id)sender; -- (IBAction)increaseWidth:(id)sender; -- (IBAction)decreaseWidth:(id)sender; -- (IBAction)increaseHeight:(id)sender; -- (IBAction)decreaseHeight:(id)sender; - -@end diff --git a/KCD/HMExternalBrowserWindowController.m b/KCD/HMExternalBrowserWindowController.m deleted file mode 100644 index a8011c8d..00000000 --- a/KCD/HMExternalBrowserWindowController.m +++ /dev/null @@ -1,415 +0,0 @@ -// -// HMExternalBrowserWindowController.m -// KCD -// -// Created by Hori,Masaki on 2014/11/22. -// Copyright (c) 2014年 Hori,Masaki. All rights reserved. -// - -#import "HMExternalBrowserWindowController.h" - -#import "HMAppDelegate.h" -#import "HMBookmarkManager.h" -#import "HMBookmarkListViewController.h" - -static void *WebViewContext = &WebViewContext; - - -@interface HMExternalBrowserWindowController () -@property (nonatomic, weak) IBOutlet NSSegmentedControl *goSegment; - -@property (nonatomic, weak) IBOutlet NSView *bookmarkListView; -@property (nonatomic, strong) HMBookmarkListViewController *bookmarkListViwController; - -@property (readwrite) NSRect contentVisibleRect; - -@property (nonatomic, weak) HMBookmarkItem *waitingBookmarkItem; - - -@property BOOL bookarkShowing; -@end - -@implementation HMExternalBrowserWindowController - -@synthesize canResize = _canResize; -@synthesize canScroll = _canScroll; - -- (id)init -{ - self = [super initWithWindowNibName:NSStringFromClass([self class])]; - return self; -} - -- (void)awakeFromNib -{ - // for Maverick - if(floor(NSAppKitVersionNumber) == NSAppKitVersionNumber10_9) { - self.webView.layerUsesCoreImageFilters = YES; - } -// self.webView.wantsLayer = YES; - - [self.webView addObserver:self - forKeyPath:@"canGoBack" - options:0 - context:WebViewContext]; - [self.webView addObserver:self - forKeyPath:@"canGoForward" - options:0 - context:WebViewContext]; - - HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate]; - [self.webView setApplicationNameForUserAgent:appDelegate.appNameForUserAgent]; - self.webView.frameLoadDelegate = self; - - self.canResize = YES; - self.canScroll = YES; - self.canMovePage = YES; - self.bookarkShowing = NO; -} - -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context -{ - if(context == WebViewContext) { - if([keyPath isEqualToString:@"canGoBack"]) { - [self.goSegment setEnabled:self.webView.canGoBack forSegment:0]; - } - if([keyPath isEqualToString:@"canGoForward"]) { - [self.goSegment setEnabled:self.webView.canGoForward forSegment:1]; - } - - - return; - } - - [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; -} - -- (NSManagedObjectContext *)managedObjectContext -{ - return [[HMBookmarkManager sharedManager] manageObjectContext]; -} - -static BOOL sameState(BOOL a, BOOL b) { - if(a && b) { - return YES; - } - if(!a && !b) { - return YES; - } - return NO; -} -- (void)setCanResize:(BOOL)canResize -{ - if(sameState(_canResize, canResize)) return; - - _canResize = canResize; - - NSUInteger styleMaks = self.window.styleMask; - if(canResize) { - styleMaks |= NSResizableWindowMask; - } else { - styleMaks &= ~NSResizableWindowMask; - } - self.window.styleMask = styleMaks; -} -- (BOOL)canResize -{ - return _canResize; -} -- (void)setCanScroll:(BOOL)canScroll -{ - if(sameState(_canScroll, canScroll)) return; - - _canScroll = canScroll; - - if(canScroll) { - [[[self.webView mainFrame] frameView] setAllowsScrolling:YES]; - } else { - [[[self.webView mainFrame] frameView] setAllowsScrolling:NO]; - } -} -- (BOOL)canScroll -{ - return _canScroll; -} - -- (NSString *)urlString -{ - return self.webView.mainFrameURL; -} -- (void)setContentVisibleRect:(NSRect)contentVisibleRect -{ - [self.webView.mainFrame.frameView.documentView scrollRectToVisible:contentVisibleRect]; -} -- (NSRect)contentVisibleRect -{ - return self.webView.mainFrame.frameView.documentView.visibleRect; -} - -- (void)setWindowContentSize:(NSSize)windowContentSize -{ - NSRect contentRect; - contentRect.origin = NSZeroPoint; - contentRect.size = windowContentSize; - - NSRect newFrame = [self.window frameRectForContentRect:contentRect]; - NSRect frame = self.window.frame; - newFrame.origin.x = NSMinX(frame); - newFrame.origin.y = NSMaxY(frame) - NSHeight(newFrame); - - [self.window setFrame:newFrame display:YES]; -} -- (NSSize)windowContentSize -{ - NSRect frame = self.window.frame; - NSRect contentRect = [self.window contentRectForFrameRect:frame]; - return contentRect.size; -} - - -- (void)setBookmark:(HMBookmarkItem *)bookmark -{ - if(!self.canMovePage) { - HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate]; - HMExternalBrowserWindowController *newController = [appDelegate createNewBrowser]; - [newController setBookmark:bookmark]; - return; - } - self.webView.mainFrameURL = bookmark.urlString; - self.windowContentSize = bookmark.windowContentSize; - self.canResize = bookmark.canResize; - self.canScroll = bookmark.canScroll; - self.waitingBookmarkItem = bookmark; -} -- (void)updateContentVisibleRect:(NSTimer *)timer -{ - HMBookmarkItem *item = [timer userInfo]; - self.contentVisibleRect = item.contentVisibleRect; -} -- (IBAction)selectBookmark:(id)sender -{ - HMBookmarkItem *item = [sender representedObject]; - if(!item) return; - - [self setBookmark:item]; -} - -- (IBAction)reloadContent:(id)sender -{ - [self.webView reload:self]; -} - -- (IBAction)goHome:(id)sender -{ - self.webView.mainFrameURL = @"http://www.dmm.com/"; -} -- (IBAction)clickGoBackSegment:(id)sender -{ - NSSegmentedCell *cell = self.goSegment.cell; - NSInteger tag = [cell tagForSegment:[cell selectedSegment]]; - switch (tag) { - case 0: - [self.webView goBack:self]; - break; - case 1: - [self.webView goForward:self]; - break; - default: - break; - } -} -- (IBAction)addBookmark:(id)sender -{ - HMBookmarkManager *bookmarkManager = [HMBookmarkManager sharedManager]; - HMBookmarkItem *bookmark = [bookmarkManager createNewBookmark]; - bookmark.name = self.window.title; - bookmark.urlString = self.webView.mainFrameURL; - bookmark.windowContentSize = self.windowContentSize; - bookmark.contentVisibleRect = self.contentVisibleRect; - bookmark.canResize = self.canResize; - bookmark.canScroll = self.canScroll; - bookmark.scrollDelay = 0.5; -} -- (BOOL)showsBookmarkList -{ - return self.webView.frame.origin.x > 0; -} -- (IBAction)showBookmark:(id)sender -{ - if(self.bookarkShowing) return; - self.bookarkShowing = YES; - - if(!self.bookmarkListViwController) { - self.bookmarkListViwController = [HMBookmarkListViewController new]; - self.bookmarkListView = self.bookmarkListViwController.view; - self.bookmarkListViwController.delegate = self; - - - NSRect frame = self.webView.frame; - frame.size.width = 200; - frame.origin.x = -200; - self.bookmarkListView.frame = frame; - NSView *view = self.webView.superview; - [view addSubview:self.bookmarkListView - positioned:NSWindowBelow - relativeTo:self.webView]; - } - - NSRect webViewFrame = self.webView.frame; - NSRect frame = self.bookmarkListView.frame; - frame.size.height = webViewFrame.size.height; - self.bookmarkListView.frame = frame; - - NSRect newFrame = webViewFrame; - - if([self showsBookmarkList]) { - frame.origin.x = -200; - newFrame.origin.x = 0; - newFrame.size.width = self.window.frame.size.width; - } else { - frame.origin.x = 0; - newFrame.origin.x = 200; - newFrame.size.width = self.window.frame.size.width - 200; - } - - - NSDictionary *webViewAnime = @{ - NSViewAnimationTargetKey : self.webView, - NSViewAnimationEndFrameKey : [NSValue valueWithRect:newFrame], - }; - NSDictionary *bookMarkAnime = @{ - NSViewAnimationTargetKey : self.bookmarkListView, - NSViewAnimationEndFrameKey : [NSValue valueWithRect:frame], - }; - NSAnimation *anime = [[NSViewAnimation alloc] initWithViewAnimations:@[webViewAnime, bookMarkAnime]]; - anime.delegate = self; - [anime startAnimation]; -} -- (void)animationDidEnd:(NSAnimation *)animation -{ - self.bookarkShowing = NO; -} - -- (BOOL)validateMenuItem:(NSMenuItem *)menuItem -{ - SEL action = [menuItem action]; - - if(action == @selector(addBookmark:)) { - return self.webView.mainFrameURL != nil; - } - if(action == @selector(showBookmark:)) { - if([self showsBookmarkList]) { - menuItem.title = NSLocalizedString(@"Hide Bookmark", @"Menu item title, Hide Bookmark"); - } else { - menuItem.title = NSLocalizedString(@"Show Bookmark", @"Menu item title, Show Bookmark"); - } - return YES; - } - if(action == @selector(selectBookmark:)) { - return YES; - } - if(action == @selector(reloadContent:)) { - return YES; - } - - return NO; -} - -- (IBAction)scrollLeft:(id)sender -{ - NSRect rect = self.contentVisibleRect; - rect.origin.x += 1; - self.contentVisibleRect = rect; -} -- (IBAction)scrollRight:(id)sender -{ - NSRect rect = self.contentVisibleRect; - rect.origin.x -= 1; - self.contentVisibleRect = rect; -} -- (IBAction)scrollUp:(id)sender -{ - NSRect rect = self.contentVisibleRect; - rect.origin.y += 1; - self.contentVisibleRect = rect; -} -- (IBAction)scrollDown:(id)sender -{ - NSRect rect = self.contentVisibleRect; - rect.origin.y -= 1; - self.contentVisibleRect = rect; -} -- (IBAction)increaseWidth:(id)sender -{ - NSRect frame = self.window.frame; - frame.size.width += 1; - [self.window setFrame:frame display:YES]; -} -- (IBAction)decreaseWidth:(id)sender -{ - NSRect frame = self.window.frame; - frame.size.width -= 1; - [self.window setFrame:frame display:YES]; -} -- (IBAction)increaseHeight:(id)sender -{ - NSRect frame = self.window.frame; - frame.size.height += 1; - frame.origin.y -= 1; - [self.window setFrame:frame display:YES]; -} -- (IBAction)decreaseHeight:(id)sender -{ - NSRect frame = self.window.frame; - frame.size.height -= 1; - frame.origin.y += 1; - [self.window setFrame:frame display:YES]; -} - - -- (void)swipeWithEvent:(NSEvent *)event -{ - if([event deltaX] > 0 && [self showsBookmarkList]) { - [self showBookmark:nil]; - } - if([event deltaX] < 0 && ![self showsBookmarkList]) { - [self showBookmark:nil]; - } -} - -- (void)didSelectBookmark:(HMBookmarkItem *)bookmark -{ - [self setBookmark:bookmark]; -} - - -#pragma mark - WebFrameLoadDelegate -- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame -{ - if(self.waitingBookmarkItem) { - [NSTimer scheduledTimerWithTimeInterval:self.waitingBookmarkItem.scrollDelay - target:self - selector:@selector(updateContentVisibleRect:) - userInfo:self.waitingBookmarkItem - repeats:NO]; - self.waitingBookmarkItem = nil; - } -} - -#pragma mark - WebPolicyDelegate -- (void)webView:(WebView *)webView -decidePolicyForNavigationAction:(NSDictionary *)actionInformation - request:(NSURLRequest *)request - frame:(WebFrame *)frame -decisionListener:(id)listener -{ - if(actionInformation && [actionInformation[WebActionNavigationTypeKey] integerValue] == WebNavigationTypeLinkClicked) { - if(self.canMovePage) { - [listener use]; - } - return; - } - [listener use]; -} - -@end diff --git a/KCD/HMExternalBrowserWindowController.swift b/KCD/HMExternalBrowserWindowController.swift new file mode 100644 index 00000000..dd0c822b --- /dev/null +++ b/KCD/HMExternalBrowserWindowController.swift @@ -0,0 +1,325 @@ +// +// HMExternalBrowserWindowController.swift +// KCD +// +// Created by Hori,Masaki on 2016/12/23. +// Copyright © 2016年 Hori,Masaki. All rights reserved. +// + +import Cocoa +import WebKit + +fileprivate extension Selector { + static let addBookmark = #selector(HMExternalBrowserWindowController.addBookmark(_:)) + static let showBookmark = #selector(HMExternalBrowserWindowController.showBookmark(_:)) + static let selectBookmark = #selector(HMExternalBrowserWindowController.selectBookmark(_:)) + static let reloadContent = #selector(HMExternalBrowserWindowController.reloadContent(_:)) + static let updateContentVisibleRect = #selector(HMExternalBrowserWindowController.updateContentVisibleRect(_:)) +} + +class HMExternalBrowserWindowController: NSWindowController { + + override var windowNibName: String! { + return "HMExternalBrowserWindowController" + } + + override func windowDidLoad() { + super.windowDidLoad() + + webView.addObserver(self, forKeyPath: "canGoBack", options: NSKeyValueObservingOptions(rawValue: 0), context: nil) + webView.addObserver(self, forKeyPath: "canGoForward", options: NSKeyValueObservingOptions(rawValue: 0), context: nil) + let appDelegate = NSApplication.shared().delegate as! HMAppDelegate + webView.applicationNameForUserAgent = appDelegate.appNameForUserAgent + webView.frameLoadDelegate = self + } + + @IBOutlet var webView: WebView! + @IBOutlet var goSegment: NSSegmentedControl! + @IBOutlet var bookmarkListView: NSView! + + let managedObjectContext: NSManagedObjectContext = HMBookmarkManager.shared().manageObjectContext + + var urlString: String? { + get { + return webView.mainFrameURL + } + } + var windowContentSize: NSSize { + get { + guard let window = window else { return NSZeroSize } + return window.contentRect(forFrameRect: window.frame).size + } + set { + guard let window = window else { return } + var contentRect: NSRect = NSZeroRect + contentRect.size = newValue + var newFrame = window.frameRect(forContentRect: contentRect) + let frame = window.frame + newFrame.origin.x = NSMinX(frame) + newFrame.origin.y = NSMaxY(frame) - NSHeight(newFrame) + window.setFrame(newFrame, display: true) + } + } + var contentVisibleRect: NSRect { + get { + return webView.mainFrame.frameView.documentView.visibleRect + } + set { + webView.mainFrame.frameView.documentView.scrollToVisible(newValue) + } + } + var canResize: Bool = true { + willSet { + guard let window = window else { return } + if canResize == newValue { return } + var style = window.styleMask.rawValue + if newValue { + style |= NSResizableWindowMask.rawValue + } else { + style &= ~NSResizableWindowMask.rawValue + } + window.styleMask = NSWindowStyleMask(rawValue: style) + } + } + var canScroll: Bool = true { + willSet { + guard let webView = webView else { return } + if canScroll == newValue { return } + if newValue { + webView.mainFrame.frameView.allowsScrolling = true + } else { + webView.mainFrame.frameView.allowsScrolling = false + } + } + } + var canMovePage: Bool = true + var bookmarkShowing: Bool = false + + lazy var bookmarkListViwController: HMBookmarkListViewController? = { + [unowned self] in + let controller = HMBookmarkListViewController() + self.bookmarkListView = controller.view + controller.delegate = self + + var frame = self.webView.frame + frame.size.width = 200 + frame.origin.x = -200 + self.bookmarkListView.frame = frame + self.webView.superview?.addSubview(self.bookmarkListView, positioned: .below, relativeTo: self.webView) + + return controller + }(); + var waitingBookmarkItem: HMBookmarkItem? + + override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { + if keyPath == "canGoBack" { + goSegment.setEnabled(webView.canGoBack, forSegment: 0) + return + } + if keyPath == "canGoForward" { + goSegment.setEnabled(webView.canGoForward, forSegment: 1) + return + } + + super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context) + } + + fileprivate func move(bookmark: HMBookmarkItem) { + if !canMovePage { + let appDelegate = NSApp.delegate as! HMAppDelegate + let newController = appDelegate.createNewBrowser() + newController?.move(bookmark: bookmark) + return + } + + webView.mainFrameURL = bookmark.urlString + windowContentSize = bookmark.windowContentSize + canResize = bookmark.canResize + canScroll = bookmark.canScroll + waitingBookmarkItem = bookmark + } + + @IBAction func selectBookmark(_ sender: AnyObject?) { + guard let item = sender?.representedObject as? HMBookmarkItem else { return } + move(bookmark: item) + } + @IBAction func reloadContent(_ sender: AnyObject?) { + webView.reload(nil) + } + @IBAction func goHome(_ sender: AnyObject?) { + webView.mainFrameURL = "http://www.dmm.com/" + } + @IBAction func clickGoBackSegment(_ sender: AnyObject?) { + guard let cell = goSegment.cell as? NSSegmentedCell else { return } + let tag = cell.tag(forSegment: cell.selectedSegment) + switch tag { + case 0: + webView.goBack(nil) + case 1: + webView.goForward(nil) + default: + break + } + } + @IBAction func addBookmark(_ sender: AnyObject?) { + guard let window = window else { return } + guard let bookmark = HMBookmarkManager.shared().createNewBookmark() else { return } + bookmark.name = window.title + bookmark.urlString = webView.mainFrameURL + bookmark.windowContentSize = windowContentSize + bookmark.contentVisibleRect = contentVisibleRect + bookmark.canResize = canResize + bookmark.canScroll = canScroll + bookmark.scrollDelay = 0.5 + } + fileprivate func showsBookmarkList() -> Bool { + return webView.frame.origin.x > 0 + } + @IBAction func showBookmark(_ sender: AnyObject?) { + guard let window = window else { return } + guard let _ = bookmarkListViwController else { return } + if bookmarkShowing { return } + bookmarkShowing = true + + var frame = bookmarkListView.frame + frame.size.height = webView.frame.size.height + bookmarkListView.frame = frame + + var newFrame = webView.frame + + if showsBookmarkList() { + frame.origin.x = -200 + newFrame.origin.x = 0 + newFrame.size.width = window.frame.size.width + } else { + frame.origin.x = 0 + newFrame.origin.x = 200 + newFrame.size.width = window.frame.size.width - 200 + } + + let webAnime: [String : Any] = [ + NSViewAnimationTargetKey: webView, + NSViewAnimationEndFrameKey: NSValue(rect: newFrame) + ] + let bookmarkAnime: [String : Any] = [ + NSViewAnimationTargetKey: bookmarkListView, + NSViewAnimationEndFrameKey: NSValue(rect: frame) + ] + let anime = NSViewAnimation(viewAnimations: [webAnime, bookmarkAnime]) + anime.delegate = self + anime.start() + } + + @IBAction func scrollLeft(_ sender: AnyObject?) { + var rect = contentVisibleRect + rect.origin.x += 1 + contentVisibleRect = rect + } + @IBAction func scrollRight(_ sender: AnyObject?) { + var rect = contentVisibleRect + rect.origin.x -= 1 + contentVisibleRect = rect + } + @IBAction func scrollUp(_ sender: AnyObject?) { + var rect = contentVisibleRect + rect.origin.y += 1 + contentVisibleRect = rect + } + @IBAction func scrollDown(_ sender: AnyObject?) { + var rect = contentVisibleRect + rect.origin.y -= 1 + contentVisibleRect = rect + } + @IBAction func increaseWidth(_ sender: AnyObject?) { + guard let window = window else { return } + var frame = window.frame + frame.size.width += 1 + window.setFrame(frame, display: true) + } + @IBAction func decreaseWidth(_ sender: AnyObject?) { + guard let window = window else { return } + var frame = window.frame + frame.size.width -= 1 + window.setFrame(frame, display: true) + } + @IBAction func increaseHeight(_ sender: AnyObject?) { + guard let window = window else { return } + var frame = window.frame + frame.size.height += 1 + frame.origin.y -= 1 + window.setFrame(frame, display: true) + } + @IBAction func decreaseHeight(_ sender: AnyObject?) { + guard let window = window else { return } + var frame = window.frame + frame.size.height -= 1 + frame.origin.y += 1 + window.setFrame(frame, display: true) + } + + override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { + guard let action = menuItem.action else { return false } + switch action { + case Selector.addBookmark: + return webView.mainFrameURL != nil + case Selector.showBookmark: + if showsBookmarkList() { + menuItem.title = NSLocalizedString("Hide Bookmark", comment: "Menu item title, Hide Bookmark") + } else { + menuItem.title = NSLocalizedString("Show Bookmark", comment: "Menu item title, Show Bookmark") + } + return true + case Selector.selectBookmark: + return true + case Selector.reloadContent: + return true + default: + return false + } + } + + override func swipe(with event: NSEvent) { + if event.deltaX > 0 && showsBookmarkList() { + showBookmark(nil) + } + if event.deltaX < 0 && !showsBookmarkList() { + showBookmark(nil) + } + } +} + +extension HMExternalBrowserWindowController: HMBookmarkListViewControllerDelegate { + func didSelectBookmark(_ bookmark: HMBookmarkItem) { + move(bookmark: bookmark) + } +} + +extension HMExternalBrowserWindowController: NSAnimationDelegate { + func animationDidEnd(_ animation: NSAnimation) { + bookmarkShowing = false + } +} + +extension HMExternalBrowserWindowController: WebFrameLoadDelegate, WebPolicyDelegate { + + func updateContentVisibleRect(_ timer: Timer) { + guard let item = timer.userInfo as? HMBookmarkItem else { return } + contentVisibleRect = item.contentVisibleRect + } + func webView(_ sender: WebView!, didFinishLoadFor frame: WebFrame!) { + if let waitingBookmarkItem = waitingBookmarkItem { + Timer.scheduledTimer(timeInterval: waitingBookmarkItem.scrollDelay, target: self, selector: .updateContentVisibleRect, userInfo: waitingBookmarkItem, repeats: false) + } + waitingBookmarkItem = nil + } + func webView(_ webView: WebView!, decidePolicyForNavigationAction actionInformation: [AnyHashable : Any]!, request: URLRequest!, frame: WebFrame!, decisionListener listener: WebPolicyDecisionListener!) { + if actionInformation?[WebActionNavigationTypeKey] as? WebNavigationType == .linkClicked { + if canMovePage { + listener.use() + } + return + } + listener.use() + } +} + diff --git a/KCD/KCD-Bridging-Header.h b/KCD/KCD-Bridging-Header.h index 1eaa51ab..8d5ca07d 100644 --- a/KCD/KCD-Bridging-Header.h +++ b/KCD/KCD-Bridging-Header.h @@ -6,8 +6,12 @@ #import "HMUserDefaults.h" +#import "HMBookmarkListViewController.h" + + #import "HMServerDataStore.h" #import "HMLocalDataStore.h" +#import "HMBookmarkManager.h" #import "HMKCShipObject+Extensions.h" -- 2.11.0