OSDN Git Service

HMBridgeViewControllerをSwiftに変換した
authormasakih <masakih@users.sourceforge.jp>
Fri, 30 Dec 2016 07:32:24 +0000 (16:32 +0900)
committermasakih <masakih@users.sourceforge.jp>
Fri, 30 Dec 2016 07:32:24 +0000 (16:32 +0900)
KCD.xcodeproj/project.pbxproj
KCD/HMBridgeViewController.h [deleted file]
KCD/HMBridgeViewController.m [deleted file]
KCD/HMBridgeViewController.swift [new file with mode: 0644]
KCD/KCD-Bridging-Header.h

index d28a839..7e5beaf 100644 (file)
                F4A9E25A1C2700CF0006E4BC /* HMRealPowerUpCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4A9E2591C2700CF0006E4BC /* HMRealPowerUpCommand.m */; };
                F4A9E7FB1D045BC100F91746 /* HMScreenshotModel.m in Sources */ = {isa = PBXBuildFile; fileRef = F4A9E7F61D045BC100F91746 /* HMScreenshotModel.m */; };
                F4A9E7FD1D045BC100F91746 /* HMTiledImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4A9E7FA1D045BC100F91746 /* HMTiledImageView.m */; };
-               F4A9E8001D045C0E00F91746 /* HMBridgeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F4A9E7FF1D045C0E00F91746 /* HMBridgeViewController.m */; };
                F4A9E8031D045C1E00F91746 /* HMImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4A9E8021D045C1E00F91746 /* HMImageView.m */; };
                F4A9E80C1D045C4200F91746 /* HMScreenshotListViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F4A9E80B1D045C4200F91746 /* HMScreenshotListViewController.m */; };
                F4AA05FB192B92C800844688 /* Temporary.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = F4AA05F9192B92C700844688 /* Temporary.xcdatamodeld */; };
                F4E5FF041E1518200026868C /* HMScreenshotDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E5FF031E1518200026868C /* HMScreenshotDetailViewController.swift */; };
                F4E5FF061E1523900026868C /* HMScreenshotCollectionViewItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E5FF051E1523900026868C /* HMScreenshotCollectionViewItem.swift */; };
                F4E5FF081E1542780026868C /* HMScreenshotEditorViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E5FF071E1542780026868C /* HMScreenshotEditorViewController.swift */; };
+               F4E5FF0A1E15CE3D0026868C /* HMBridgeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E5FF091E15CE3D0026868C /* HMBridgeViewController.swift */; };
                F4E7802818DBD2A00011BC4C /* HMGetShipCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E7802718DBD2A00011BC4C /* HMGetShipCommand.m */; };
                F4E7802B18DC3A820011BC4C /* HMLocalDataStore.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E7802A18DC3A820011BC4C /* HMLocalDataStore.m */; };
                F4E7802E18DC3AF00011BC4C /* LocalData.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = F4E7802C18DC3AF00011BC4C /* LocalData.xcdatamodeld */; };
                F4A9E7F61D045BC100F91746 /* HMScreenshotModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMScreenshotModel.m; sourceTree = "<group>"; };
                F4A9E7F91D045BC100F91746 /* HMTiledImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMTiledImageView.h; sourceTree = "<group>"; };
                F4A9E7FA1D045BC100F91746 /* HMTiledImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMTiledImageView.m; sourceTree = "<group>"; };
-               F4A9E7FE1D045C0E00F91746 /* HMBridgeViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMBridgeViewController.h; sourceTree = "<group>"; };
-               F4A9E7FF1D045C0E00F91746 /* HMBridgeViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMBridgeViewController.m; sourceTree = "<group>"; };
                F4A9E8011D045C1E00F91746 /* HMImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMImageView.h; sourceTree = "<group>"; };
                F4A9E8021D045C1E00F91746 /* HMImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMImageView.m; sourceTree = "<group>"; };
                F4A9E80A1D045C4200F91746 /* HMScreenshotListViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMScreenshotListViewController.h; sourceTree = "<group>"; };
                F4E5FF031E1518200026868C /* HMScreenshotDetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMScreenshotDetailViewController.swift; sourceTree = "<group>"; };
                F4E5FF051E1523900026868C /* HMScreenshotCollectionViewItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMScreenshotCollectionViewItem.swift; sourceTree = "<group>"; };
                F4E5FF071E1542780026868C /* HMScreenshotEditorViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMScreenshotEditorViewController.swift; sourceTree = "<group>"; };
+               F4E5FF091E15CE3D0026868C /* HMBridgeViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMBridgeViewController.swift; sourceTree = "<group>"; };
                F4E7802618DBD2A00011BC4C /* HMGetShipCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMGetShipCommand.h; sourceTree = "<group>"; };
                F4E7802718DBD2A00011BC4C /* HMGetShipCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMGetShipCommand.m; sourceTree = "<group>"; };
                F4E7802918DC3A820011BC4C /* HMLocalDataStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMLocalDataStore.h; sourceTree = "<group>"; };
                                F414656D1DAB855700D68C97 /* HMCollectionView.m */,
                                F4E5FF051E1523900026868C /* HMScreenshotCollectionViewItem.swift */,
                                F41465671DAB852E00D68C97 /* HMScreenshotCollectionViewItem.xib */,
-                               F4A9E7FE1D045C0E00F91746 /* HMBridgeViewController.h */,
-                               F4A9E7FF1D045C0E00F91746 /* HMBridgeViewController.m */,
+                               F4E5FF091E15CE3D0026868C /* HMBridgeViewController.swift */,
                                F4E5FF031E1518200026868C /* HMScreenshotDetailViewController.swift */,
                                F4649FD01DBC52600097AEBF /* HMScreenshotDetailViewController.xib */,
                                F4A9E8011D045C1E00F91746 /* HMImageView.h */,
                                F431E9791DF3CD1B00FB56AE /* HMActinKindTransformer.m in Sources */,
                                F45F3BE11E067A870009434E /* HMHistoryItemCleaner.swift in Sources */,
                                F426802B194D0F4800B2C27F /* HMMemberShip2Command.m in Sources */,
-                               F4A9E8001D045C0E00F91746 /* HMBridgeViewController.m in Sources */,
                                F4AA06101931869E00844688 /* HMBattleResultCommand.m in Sources */,
                                F4B37EF019FADFC9002F371A /* HMIgnoreZeroTransformer.m in Sources */,
                                F4AC136F1E0963DB00851147 /* HMProgressPanel.swift in Sources */,
                                F4C3957418CDDFE500A1E918 /* HMKenzoDockStatus.m in Sources */,
                                F471784D1B6FA18800F5E59D /* HMKCResource.m in Sources */,
                                F4F9F87D1C6B5E4A00D0EE76 /* HMKCMasterSType+CoreDataProperties.m in Sources */,
+                               F4E5FF0A1E15CE3D0026868C /* HMBridgeViewController.swift in Sources */,
                                F414656E1DAB855700D68C97 /* HMCollectionView.m in Sources */,
                                F4C6687D1C545E3800601AFB /* HMMapWindowController.m in Sources */,
                                F4AA060D1930E69200844688 /* HMMidnightBattleCommand.m in Sources */,
diff --git a/KCD/HMBridgeViewController.h b/KCD/HMBridgeViewController.h
deleted file mode 100644 (file)
index 3c658e7..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-//
-//  HMBridgeViewController.h
-//  testScreenshotForKCD
-//
-//  Created by Hori,Masaki on 2016/03/30.
-//  Copyright © 2016年 Hori,Masaki. All rights reserved.
-//
-
-#import <Cocoa/Cocoa.h>
-
-#import "HMScreenshotInformation.h"
-
-@interface HMBridgeViewController : NSViewController <NSSharingServicePickerTouchBarItemDelegate, NSSharingServiceDelegate>
-@property (nonatomic, strong) IBOutlet NSArrayController *arrayController;
-
-// for sharing service
-@property (readonly) NSRect contentRect;
-
-@property (readonly) BOOL appendKanColleTag;
-@property (readonly) NSString *tagString;
-
-- (IBAction)share:(id)sender;
-@end
diff --git a/KCD/HMBridgeViewController.m b/KCD/HMBridgeViewController.m
deleted file mode 100644 (file)
index df02dc6..0000000
+++ /dev/null
@@ -1,170 +0,0 @@
-//
-//  HMBridgeViewController.m
-//  testScreenshotForKCD
-//
-//  Created by Hori,Masaki on 2016/03/30.
-//  Copyright © 2016年 Hori,Masaki. All rights reserved.
-//
-
-#import "HMBridgeViewController.h"
-#import "HMScreenshotModel.h"
-
-#import "HMUserDefaults.h"
-
-
-@interface HMBridgeViewController () <NSSharingServicePickerDelegate>
-
-@property (nonatomic, copy) NSString *tagString;
-
-@end
-
-@implementation HMBridgeViewController
-
-- (instancetype)initWithCoder:(NSCoder *)coder
-{
-       self =  [super initWithCoder:coder];
-       if(self) {
-               NSString *tag = NSLocalizedString(@"kancolle", @"kancolle twitter hash tag");
-               if(tag) {
-                       _tagString = [NSString stringWithFormat:@"#%@", tag];
-               } else {
-                       _tagString = @"";
-               }
-//             _useMask = HMStandardDefaults.useMask;
-       }
-       
-       return self;
-}
-
-- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
-{
-    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
-    if(self) {
-        NSString *tag = NSLocalizedString(@"kancolle", @"kancolle twitter hash tag");
-        if(tag) {
-            _tagString = [NSString stringWithFormat:@"#%@", tag];
-        } else {
-            _tagString = @"";
-        }
-//             _useMask = HMStandardDefaults.useMask;
-    }
-    return self;
-}
-
-- (void)setRepresentedObject:(id)representedObject
-{
-       [super setRepresentedObject:representedObject];
-       
-       if([representedObject isKindOfClass:[HMScreenshotModel class]]) {
-               HMScreenshotModel *model = representedObject;
-               
-               [self.arrayController bind:NSContentArrayBinding
-                                                 toObject:model
-                                          withKeyPath:@"screenshots"
-                                                  options:nil];
-               [self.arrayController bind:NSSortDescriptorsBinding
-                                                 toObject:model
-                                          withKeyPath:@"sortDescriptors"
-                                                  options:nil];
-               [self.arrayController bind:NSSelectionIndexesBinding
-                                                 toObject:model
-                                          withKeyPath:@"selectedIndexes"
-                                                  options:nil];
-               [self.arrayController bind:NSFilterPredicateBinding
-                                                 toObject:model
-                                          withKeyPath:@"filterPredicate"
-                                                  options:nil];
-       }
-}
-
--(void)dealloc
-{
-       [self.arrayController unbind:NSContentArrayBinding];
-       [self.arrayController unbind:NSSortDescriptorsBinding];
-       [self.arrayController unbind:NSSelectionIndexesBinding];
-       [self.arrayController unbind:NSFilterPredicateBinding];
-       
-       self.representedObject = nil;
-}
-
-- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(nullable id)sender
-{
-       if([sender respondsToSelector:@selector(setAction:)]) {
-               [sender setAction:nil];
-       }
-       
-       NSViewController *v = segue.destinationController;
-       v.representedObject = self.representedObject;
-}
-
-- (BOOL)appendKanColleTag
-{
-       return HMStandardDefaults.appendKanColleTag;
-}
-
-
-- (NSArray *)itemsForShareingServicePicker
-{
-    NSArray<HMScreenshotInformation *> *informations = [self.arrayController.selectedObjects copy];
-    NSMutableArray<NSString *> *paths = [NSMutableArray array];
-    for(HMScreenshotInformation *info in informations) {
-        [paths addObject:info.path];
-    }
-    NSMutableArray *items = [NSMutableArray array];
-    for(NSString *path in paths) {
-        NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
-        if(image) [items addObject:image];
-    }
-    
-    NSString *tags = nil;
-    if(self.appendKanColleTag) {
-        tags = self.tagString;
-        tags = [@"\n" stringByAppendingString:tags];
-    }
-    if(tags) {
-        [items addObject:tags];
-    }
-    
-    return items;
-}
-- (IBAction)share:(id)sender
-{
-       NSArray *items = [self itemsForShareingServicePicker];
-       
-       NSSharingServicePicker *picker = [[NSSharingServicePicker alloc] initWithItems:items];
-       picker.delegate = self;
-       [picker showRelativeToRect:[sender bounds]
-                                               ofView:sender
-                                preferredEdge:NSMinXEdge];
-}
-
-- (NSArray *)itemsForSharingServicePickerTouchBarItem:(NSSharingServicePickerTouchBarItem *)pickerTouchBarItem
-{
-    return [self itemsForShareingServicePicker];
-}
-
-#pragma mark - NSSharingServiceDelegate NSSharingServicePickerDelegate
-- (id <NSSharingServiceDelegate>)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker delegateForSharingService:(NSSharingService *)sharingService
-{
-       return self;
-}
-
-- (NSRect)sharingService:(NSSharingService *)sharingService sourceFrameOnScreenForShareItem:(id<NSPasteboardWriting>)item
-{
-       if([item isKindOfClass:[NSString class]]) return NSZeroRect;
-       
-       NSRect frame = self.contentRect;
-       return [self.view.window convertRectToScreen:frame];
-}
-- (NSImage *)sharingService:(NSSharingService *)sharingService transitionImageForShareItem:(id<NSPasteboardWriting>)item contentRect:(NSRect *)contentRect
-{
-       if([item isKindOfClass:[NSImage class]]) return (NSImage *)item;
-       
-       return nil;
-}
-- (NSWindow *)sharingService:(NSSharingService *)sharingService sourceWindowForShareItems:(NSArray *)items sharingContentScope:(NSSharingContentScope *)sharingContentScope
-{
-       return self.view.window;
-}
-
-@end
diff --git a/KCD/HMBridgeViewController.swift b/KCD/HMBridgeViewController.swift
new file mode 100644 (file)
index 0000000..d99eb9a
--- /dev/null
@@ -0,0 +1,102 @@
+//
+//  HMBridgeViewController.swift
+//  KCD
+//
+//  Created by Hori,Masaki on 2016/12/30.
+//  Copyright © 2016年 Hori,Masaki. All rights reserved.
+//
+
+import Cocoa
+
+protocol ScreenshotSharingProvider {
+    var contentRect: NSRect { get }
+}
+
+class HMBridgeViewController: NSViewController {
+    
+    deinit {
+        [NSContentArrayBinding, NSSortDescriptorsBinding,
+         NSSelectionIndexesBinding, NSFilterPredicateBinding]
+            .forEach { arrayController.unbind($0) }
+    }
+    
+    @IBOutlet var arrayController: NSArrayController!
+    
+    override var representedObject: Any? {
+        didSet {
+            guard let representedObject = representedObject else { return }
+            [(NSContentArrayBinding, "screenshots"),
+             (NSSortDescriptorsBinding, "sortDescriptors"),
+             (NSSelectionIndexesBinding, "selectedIndexes"),
+             (NSFilterPredicateBinding, "filterPredicate")]
+                .forEach {
+                    arrayController.bind($0.0, to: representedObject, withKeyPath: $0.1, options: nil)
+            }
+        }
+    }
+    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
+        (sender as? NSControl)?.action = nil
+        (segue.destinationController as? NSViewController)?.representedObject = representedObject
+    }
+}
+
+extension HMBridgeViewController: NSSharingServicePickerDelegate, ScreenshotSharingProvider {
+    var contentRect: NSRect {
+        fatalError("Must implemente")
+    }
+    private var appendKanColleTag: Bool {
+        get {
+            return HMStandardDefaults.appendKanColleTag as Bool
+        }
+    }
+    private var tagString: String? {
+        let tag = NSLocalizedString("kancolle", comment: "kancolle twitter hash tag")
+        if tag == "" { return "" }
+        return "#\(tag)"
+    }
+    @IBAction func share(_ sender: AnyObject?) {
+        guard let view = sender as? NSView else { return }
+        let picker = NSSharingServicePicker(items: itemsForShareingServicePicker())
+        picker.delegate = self
+        picker.show(relativeTo: view.bounds, of: view, preferredEdge: .minX)
+    }
+    
+    fileprivate func itemsForShareingServicePicker() -> [AnyObject] {
+        guard let informations = arrayController.selectedObjects as? [HMScreenshotInformation] else { return [] }
+        let images: [NSImage] = informations
+            .map { $0.path }
+            .flatMap { (path) -> NSImage? in
+                guard let path = path else { return nil }
+                guard let image = NSImage(contentsOfFile: path) else { return nil }
+                return image
+        }
+        if !appendKanColleTag { return images }
+        if let tag = tagString {
+            return ["\n\(tag)" as AnyObject] + images
+        }
+        return images
+    }
+}
+extension HMBridgeViewController: NSSharingServiceDelegate {
+    func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, delegateFor sharingService: NSSharingService) -> NSSharingServiceDelegate? {
+        return self
+    }
+    
+    func sharingService(_ sharingService: NSSharingService, sourceFrameOnScreenForShareItem item: Any) -> NSRect {
+        guard let window = view.window else { return NSZeroRect }
+        return window.convertToScreen(contentRect)
+    }
+    func sharingService(_ sharingService: NSSharingService, transitionImageForShareItem item: Any, contentRect: UnsafeMutablePointer<NSRect>) -> NSImage? {
+        return item as? NSImage
+    }
+    func sharingService(_ sharingService: NSSharingService, sourceWindowForShareItems items: [Any], sharingContentScope: UnsafeMutablePointer<NSSharingContentScope>) -> NSWindow? {
+        return view.window
+    }
+}
+
+@available(OSX 10.12.2, *)
+extension HMBridgeViewController: NSSharingServicePickerTouchBarItemDelegate {
+    func items(for pickerTouchBarItem: NSSharingServicePickerTouchBarItem) -> [Any] {
+        return itemsForShareingServicePicker()
+    }
+}
index 06afdff..68fef56 100644 (file)
@@ -12,8 +12,6 @@
 #import "HMDamageView.h"
 #import "HMStrengthenListItemCellView.h"
 
-#import "HMBridgeViewController.h"
-
 #import "HMScreenshotListWindowController.h"
 #import "HMScreenshotListViewController.h"