macOSのスクリーンショットの保存場所を変更するメニュバーエクストラ
修订版 | b8533da3c4d5cba93e257d1048779c33cb36e0ab (tree) |
---|---|
时间 | 2018-03-10 17:55:41 |
作者 | masakih <masakih@user...> |
Commiter | masakih |
ActionListenerを削除
@@ -11,7 +11,6 @@ | ||
11 | 11 | F40EB6BF1EA1A55700FEE444 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = F40EB6BD1EA1A55700FEE444 /* Localizable.strings */; }; |
12 | 12 | F40EB6C81EA1CD7A00FEE444 /* ImageTypeItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = F40EB6C71EA1CD7A00FEE444 /* ImageTypeItem.swift */; }; |
13 | 13 | F40EB6CA1EA1D7B500FEE444 /* ImageType.plist in Resources */ = {isa = PBXBuildFile; fileRef = F40EB6C91EA1D7B500FEE444 /* ImageType.plist */; }; |
14 | - F40EB6D01EA26BEC00FEE444 /* ActionListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = F40EB6CF1EA26BEC00FEE444 /* ActionListener.swift */; }; | |
15 | 14 | F40EB6D21EA2F28100FEE444 /* Screenshot.swift in Sources */ = {isa = PBXBuildFile; fileRef = F40EB6D11EA2F28100FEE444 /* Screenshot.swift */; }; |
16 | 15 | F414DD031EABAB7300111DA7 /* UserDefaultsExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F414DD021EABAB7300111DA7 /* UserDefaultsExtension.swift */; }; |
17 | 16 | F42539E01EACCEDE0083EB9D /* UserDefaultsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = F42539DF1EACCEDE0083EB9D /* UserDefaultsTest.swift */; }; |
@@ -44,7 +43,6 @@ | ||
44 | 43 | F40EB6C61EA1A94200FEE444 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/InfoPlist.strings; sourceTree = "<group>"; }; |
45 | 44 | F40EB6C71EA1CD7A00FEE444 /* ImageTypeItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageTypeItem.swift; sourceTree = "<group>"; }; |
46 | 45 | F40EB6C91EA1D7B500FEE444 /* ImageType.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = ImageType.plist; sourceTree = "<group>"; }; |
47 | - F40EB6CF1EA26BEC00FEE444 /* ActionListener.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionListener.swift; sourceTree = "<group>"; }; | |
48 | 46 | F40EB6D11EA2F28100FEE444 /* Screenshot.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Screenshot.swift; sourceTree = "<group>"; }; |
49 | 47 | F414DD021EABAB7300111DA7 /* UserDefaultsExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserDefaultsExtension.swift; sourceTree = "<group>"; }; |
50 | 48 | F42539DF1EACCEDE0083EB9D /* UserDefaultsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserDefaultsTest.swift; sourceTree = "<group>"; }; |
@@ -113,7 +111,6 @@ | ||
113 | 111 | F47B07111EA10A3E006711D9 /* ChooseFolderItem.swift */, |
114 | 112 | F40EB6C71EA1CD7A00FEE444 /* ImageTypeItem.swift */, |
115 | 113 | F47B07171EA10A88006711D9 /* QuitItem.swift */, |
116 | - F40EB6CF1EA26BEC00FEE444 /* ActionListener.swift */, | |
117 | 114 | F4D346931EA644170031AFB3 /* LimitedArray.swift */, |
118 | 115 | F47B06EE1E9A5DF9006711D9 /* Assets.xcassets */, |
119 | 116 | F47B06F31E9A5DF9006711D9 /* Info.plist */, |
@@ -244,7 +241,6 @@ | ||
244 | 241 | F47B070C1E9A65FA006711D9 /* StatusItem.swift in Sources */, |
245 | 242 | F47B06EB1E9A5DF9006711D9 /* AppDelegate.swift in Sources */, |
246 | 243 | F47B07101EA10A1A006711D9 /* FolderItem.swift in Sources */, |
247 | - F40EB6D01EA26BEC00FEE444 /* ActionListener.swift in Sources */, | |
248 | 244 | F40EB6D21EA2F28100FEE444 /* Screenshot.swift in Sources */, |
249 | 245 | F47B07181EA10A88006711D9 /* QuitItem.swift in Sources */, |
250 | 246 | F40EB6C81EA1CD7A00FEE444 /* ImageTypeItem.swift in Sources */, |
@@ -1,35 +0,0 @@ | ||
1 | -// | |
2 | -// ActionListener.swift | |
3 | -// GoInto | |
4 | -// | |
5 | -// Created by Hori,Masaki on 2017/04/15. | |
6 | -// Copyright © 2017年 Hori,Masaki. All rights reserved. | |
7 | -// | |
8 | - | |
9 | -import Cocoa | |
10 | - | |
11 | -/// El Capitan以前はaction targetがNSObjectを継承していなければならないため、targetとして利用する | |
12 | -/// | |
13 | -/// extension ActionListener { | |
14 | -/// @IBAction func action(_ sender: Any?) { | |
15 | -/// guard let owner = owner as? Responder? else { return } | |
16 | -/// owner.operate() | |
17 | -/// } | |
18 | -/// } | |
19 | -/// | |
20 | -/// class Responder { | |
21 | -/// let listener = ActionListener() | |
22 | -/// let actionSender: AnyObject | |
23 | -/// | |
24 | -/// init() { | |
25 | -/// actionSender.action = #selector(ActionListener.action(_:)) | |
26 | -/// actionSender.target = listener | |
27 | -/// listener.owner = self | |
28 | -/// } | |
29 | -/// func operate() { | |
30 | -/// // do something | |
31 | -/// } | |
32 | -/// } | |
33 | -class ActionListener: NSObject { | |
34 | - weak var owner: AnyObject? | |
35 | -} |
@@ -8,9 +8,18 @@ | ||
8 | 8 | |
9 | 9 | import Cocoa |
10 | 10 | |
11 | -extension ActionListener { | |
11 | +class ChooseFolderItem: StatusItem { | |
12 | + let menuItem = NSMenuItem() | |
13 | + let urlSelector: (URL) -> Void | |
14 | + | |
15 | + init(_ handler: @escaping ((URL) -> Void)) { | |
16 | + urlSelector = handler | |
17 | + menuItem.title = NSLocalizedString("Choose Folder", comment: "Choose Folder MenuItem") | |
18 | + menuItem.action = #selector(selectFolder(_:)) | |
19 | + menuItem.target = self | |
20 | + } | |
21 | + | |
12 | 22 | @IBAction func selectFolder(_ sender: Any?) { |
13 | - guard let owner = owner as? ChooseFolderItem else { return } | |
14 | 23 | let panel = NSOpenPanel() |
15 | 24 | panel.canChooseDirectories = true |
16 | 25 | panel.allowsMultipleSelection = false |
@@ -23,20 +32,6 @@ extension ActionListener { | ||
23 | 32 | NSApplication.shared.activate(ignoringOtherApps: true) |
24 | 33 | guard panel.runModal() == NSApplication.ModalResponse(NSFileHandlingPanelOKButton), |
25 | 34 | let url = panel.directoryURL else { return } |
26 | - owner.urlSelector(url) | |
27 | - } | |
28 | -} | |
29 | - | |
30 | -class ChooseFolderItem: StatusItem { | |
31 | - let menuItem = NSMenuItem() | |
32 | - let urlSelector: (URL) -> Void | |
33 | - private let listener = ActionListener() | |
34 | - | |
35 | - init(_ handler: @escaping ((URL) -> Void)) { | |
36 | - urlSelector = handler | |
37 | - menuItem.title = NSLocalizedString("Choose Folder", comment: "Choose Folder MenuItem") | |
38 | - menuItem.action = #selector(ActionListener.selectFolder(_:)) | |
39 | - menuItem.target = listener | |
40 | - listener.owner = self | |
35 | + urlSelector(url) | |
41 | 36 | } |
42 | 37 | } |
@@ -8,17 +8,9 @@ | ||
8 | 8 | |
9 | 9 | import Cocoa |
10 | 10 | |
11 | -extension ActionListener { | |
12 | - @IBAction func changeFolder(_ sender: Any?) { | |
13 | - guard let owner = owner as? FolderItem else { return } | |
14 | - owner.set() | |
15 | - } | |
16 | -} | |
17 | - | |
18 | 11 | final class FolderItem: StatusItem { |
19 | 12 | let url: URL |
20 | 13 | let menuItem = NSMenuItem() |
21 | - private let listener = ActionListener() | |
22 | 14 | |
23 | 15 | init(_ url: URL) { |
24 | 16 | self.url = url |
@@ -32,9 +24,8 @@ final class FolderItem: StatusItem { | ||
32 | 24 | |
33 | 25 | let work = NSWorkspace.shared |
34 | 26 | menuItem.image = fitSize(work.icon(forFile: url.path)) |
35 | - menuItem.action = #selector(ActionListener.changeFolder(_:)) | |
36 | - menuItem.target = listener | |
37 | - listener.owner = self | |
27 | + menuItem.action = #selector(changeFolder(_:)) | |
28 | + menuItem.target = self | |
38 | 29 | } |
39 | 30 | deinit { |
40 | 31 | remove() |
@@ -55,6 +46,10 @@ final class FolderItem: StatusItem { | ||
55 | 46 | func update(_ url: URL) { |
56 | 47 | menuItem.state = (self.url == url ? .on : .off) |
57 | 48 | } |
49 | + | |
50 | + @IBAction func changeFolder(_ sender: Any?) { | |
51 | + set() | |
52 | + } | |
58 | 53 | } |
59 | 54 | |
60 | 55 | func fitSize(_ image: NSImage) -> NSImage { |
@@ -8,16 +8,6 @@ | ||
8 | 8 | |
9 | 9 | import Cocoa |
10 | 10 | |
11 | -extension ActionListener { | |
12 | - @IBAction func selectType(_ sender: Any?) { | |
13 | - guard let owner = owner as? ImageTypeItem, | |
14 | - let item = sender as? NSMenuItem, | |
15 | - let typeName = item.representedObject as? String | |
16 | - else { return } | |
17 | - owner.set(typeName) | |
18 | - } | |
19 | -} | |
20 | - | |
21 | 11 | private func loadImageTypes() -> [String] { |
22 | 12 | guard let url = Bundle.main.url(forResource: "ImageType", withExtension: "plist"), |
23 | 13 | let array = NSArray(contentsOf: url) |
@@ -28,11 +18,8 @@ private func loadImageTypes() -> [String] { | ||
28 | 18 | class ImageTypeItem: StatusItem { |
29 | 19 | let menuItem = NSMenuItem() |
30 | 20 | private let supportTypes = loadImageTypes() |
31 | - private let listener = ActionListener() | |
32 | 21 | |
33 | - init() { | |
34 | - listener.owner = self | |
35 | - | |
22 | + init() { | |
36 | 23 | menuItem.title = NSLocalizedString("Image Type", comment: "Image Type MenuItem") |
37 | 24 | |
38 | 25 | let ws = NSWorkspace.shared |
@@ -43,8 +30,8 @@ class ImageTypeItem: StatusItem { | ||
43 | 30 | .map { |
44 | 31 | let item = NSMenuItem() |
45 | 32 | item.title = ws.localizedDescription(forType: $0) ?? "Never Use Default Value" |
46 | - item.action = #selector(ActionListener.selectType(_:)) | |
47 | - item.target = listener | |
33 | + item.action = #selector(selectType(_:)) | |
34 | + item.target = self | |
48 | 35 | item.representedObject = ws.preferredFilenameExtension(forType: $0) |
49 | 36 | return item |
50 | 37 | } |
@@ -68,4 +55,11 @@ class ImageTypeItem: StatusItem { | ||
68 | 55 | Screenshot.shared.type = typeName |
69 | 56 | } |
70 | 57 | } |
58 | + | |
59 | + @IBAction func selectType(_ sender: Any?) { | |
60 | + guard let item = sender as? NSMenuItem, | |
61 | + let typeName = item.representedObject as? String | |
62 | + else { return } | |
63 | + set(typeName) | |
64 | + } | |
71 | 65 | } |
@@ -8,20 +8,17 @@ | ||
8 | 8 | |
9 | 9 | import Cocoa |
10 | 10 | |
11 | -extension ActionListener { | |
12 | - @IBAction func quit(_ sender: Any?) { | |
13 | - NSApplication.shared.terminate(nil) | |
14 | - } | |
15 | -} | |
16 | - | |
17 | 11 | class QuitItem: StatusItem { |
18 | 12 | let menuItem = NSMenuItem() |
19 | - private let listener = ActionListener() | |
20 | 13 | |
21 | 14 | init() { |
22 | 15 | let format = NSLocalizedString("Quit %@", comment: "Quit Menu Item") |
23 | 16 | menuItem.title = String(format: format, AppDelegate.appName) |
24 | - menuItem.action = #selector(ActionListener.quit(_:)) | |
25 | - menuItem.target = listener | |
17 | + menuItem.action = #selector(quit(_:)) | |
18 | + menuItem.target = self | |
19 | + } | |
20 | + | |
21 | + @IBAction func quit(_ sender: Any?) { | |
22 | + NSApplication.shared.terminate(nil) | |
26 | 23 | } |
27 | 24 | } |