Permalink
Browse files

refactor: Swift 3

  • Loading branch information...
1 parent 52513db commit 7d704e7d47ad120887a2c9468ff0a95d16308326 @uetchy uetchy committed Sep 20, 2016
@@ -14,10 +14,10 @@ var loginItem = NSMenuItem()
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
- var statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(NSVariableStatusItemLength))
+ var statusItem = NSStatusBar.system().statusItem(withLength: CGFloat(NSVariableStatusItemLength))
- func applicationDidFinishLaunching(aNotification: NSNotification) {
+ func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
_ = KeyEvent()
@@ -27,30 +27,30 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.statusItem.highlightMode = true
self.statusItem.menu = menu
- loginItem = menu.addItemWithTitle("ログイン時に開く", action: #selector(AppDelegate.launch(_:)), keyEquivalent: "")!
+ loginItem = menu.addItem(withTitle: "ログイン時に開く", action: #selector(AppDelegate.launch(_:)), keyEquivalent: "")
loginItem.state = applicationIsInStartUpItems() ? 1 : 0
- menu.addItem(NSMenuItem.separatorItem())
+ menu.addItem(NSMenuItem.separator())
- menu.addItemWithTitle("About ⌘英かな 1.0.1", action: #selector(AppDelegate.open(_:)), keyEquivalent: "")
- menu.addItemWithTitle("Quit", action: #selector(AppDelegate.quit(_:)), keyEquivalent: "")
+ menu.addItem(withTitle: "About ⌘英かな 1.0.1", action: #selector(AppDelegate.open(_:)), keyEquivalent: "")
+ menu.addItem(withTitle: "Quit", action: #selector(AppDelegate.quit(_:)), keyEquivalent: "")
}
- func applicationWillTerminate(aNotification: NSNotification) {
+ func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
- @IBAction func open(sender: NSButton) {
- if let checkURL = NSURL(string: "https://ei-kana.appspot.com") {
- if NSWorkspace.sharedWorkspace().openURL(checkURL) {
+ @IBAction func open(_ sender: NSButton) {
+ if let checkURL = URL(string: "https://ei-kana.appspot.com") {
+ if NSWorkspace.shared().open(checkURL) {
print("url successfully opened")
}
} else {
print("invalid url")
}
}
- @IBAction func launch(sender: NSButton) {
+ @IBAction func launch(_ sender: NSButton) {
if sender.state == 0 {
sender.state = 1
addLaunchAtStartup()
@@ -61,7 +61,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}
- @IBAction func quit(sender: NSButton) {
- NSApplication.sharedApplication().terminate(self)
+ @IBAction func quit(_ sender: NSButton) {
+ NSApplication.shared().terminate(self)
}
}
@@ -15,18 +15,18 @@ class KeyEvent: NSObject {
super.init()
let checkOptionPrompt = kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString
- let options: CFDictionary = [checkOptionPrompt: true]
+ let options: CFDictionary = [checkOptionPrompt: true] as NSDictionary
if !AXIsProcessTrustedWithOptions(options) {
// アクセシビリティに設定されていない場合、設定されるまでループで待つ
- NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(KeyEvent.watchAXIsProcess(_:)), userInfo: nil, repeats: true)
+ Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(KeyEvent.watchAXIsProcess(_:)), userInfo: nil, repeats: true)
} else {
self.watch()
}
}
- func watchAXIsProcess(timer: NSTimer) {
+ func watchAXIsProcess(_ timer: Timer) {
if AXIsProcessTrusted() {
timer.invalidate()
print("アクセシビリティに設定されました")
@@ -40,50 +40,50 @@ class KeyEvent: NSObject {
func watch () {
let masks = [
- NSEventMask.KeyDownMask,
- NSEventMask.KeyUpMask,
- NSEventMask.LeftMouseDownMask,
- NSEventMask.LeftMouseUpMask,
- NSEventMask.RightMouseDownMask,
- NSEventMask.RightMouseUpMask,
- NSEventMask.OtherMouseDownMask,
- NSEventMask.OtherMouseUpMask,
- NSEventMask.ScrollWheelMask
+ NSEventMask.keyDown,
+ NSEventMask.keyUp,
+ NSEventMask.leftMouseDown,
+ NSEventMask.leftMouseUp,
+ NSEventMask.rightMouseDown,
+ NSEventMask.rightMouseUp,
+ NSEventMask.otherMouseDown,
+ NSEventMask.otherMouseUp,
+ NSEventMask.scrollWheel
// NSEventMask.MouseMovedMask,
]
let handler = {(evt: NSEvent!) -> Void in
self.keyCode = nil
}
for mask in masks {
- NSEvent.addGlobalMonitorForEventsMatchingMask(mask, handler: handler)
+ NSEvent.addGlobalMonitorForEvents(matching: mask, handler: handler)
}
- NSEvent.addGlobalMonitorForEventsMatchingMask(NSEventMask.FlagsChangedMask, handler: {(evevt: NSEvent!) -> Void in
+ NSEvent.addGlobalMonitorForEvents(matching: NSEventMask.flagsChanged, handler: {(evevt: NSEvent!) -> Void in
if evevt.keyCode == 55 { // 右コマンドキー
- if evevt.modifierFlags.contains(.CommandKeyMask) {
+ if evevt.modifierFlags.contains(.command) {
self.keyCode = 55
}
else if self.keyCode == 55 {
print("英数")
- let loc = CGEventTapLocation.CGHIDEventTap
+ let loc = CGEventTapLocation.cghidEventTap
- CGEventPost(loc, CGEventCreateKeyboardEvent(nil, 102, true))
- CGEventPost(loc, CGEventCreateKeyboardEvent(nil, 102, false))
+ CGEvent(keyboardEventSource: nil, virtualKey: 102, keyDown: true)?.post(tap: loc)
+ CGEvent(keyboardEventSource: nil, virtualKey: 102, keyDown: false)?.post(tap: loc)
}
}
else if evevt.keyCode == 54 { // 左コマンドキー
- if evevt.modifierFlags.contains(.CommandKeyMask) {
+ if evevt.modifierFlags.contains(.command) {
self.keyCode = 54
}
else if self.keyCode == 54 {
print("かな")
- let loc = CGEventTapLocation.CGHIDEventTap
+ let loc = CGEventTapLocation.cghidEventTap
- CGEventPost(loc, CGEventCreateKeyboardEvent(nil, 104, true))
- CGEventPost(loc, CGEventCreateKeyboardEvent(nil, 104, false))
+ CGEvent(keyboardEventSource: nil, virtualKey: 104, keyDown: true)?.post(tap: loc)
+ CGEvent(keyboardEventSource: nil, virtualKey: 104, keyDown: false)?.post(tap: loc)
}
}
else {
@@ -12,11 +12,10 @@ class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
-
// Do any additional setup after loading the view.
}
- override var representedObject: AnyObject? {
+ override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
@@ -18,27 +18,27 @@ func applicationIsInStartUpItems() -> Bool {
return (itemReferencesInLoginItems().existingReference != nil)
}
-func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItemRef?, lastReference: LSSharedFileListItemRef?) {
- let itemUrl : UnsafeMutablePointer<Unmanaged<CFURL>?> = UnsafeMutablePointer<Unmanaged<CFURL>?>.alloc(1)
+func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItem?, lastReference: LSSharedFileListItem?) {
+ let itemUrl : UnsafeMutablePointer<Unmanaged<CFURL>?> = UnsafeMutablePointer<Unmanaged<CFURL>?>.allocate(capacity: 1)
- if let appUrl : NSURL = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) {
+ if let appUrl : URL = URL(fileURLWithPath: Bundle.main.bundlePath) {
let loginItemsRef = LSSharedFileListCreate(
nil,
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
nil
- ).takeRetainedValue() as LSSharedFileListRef?
+ ).takeRetainedValue() as LSSharedFileList?
if loginItemsRef != nil {
let loginItems: NSArray = LSSharedFileListCopySnapshot(loginItemsRef, nil).takeRetainedValue() as NSArray
// print("There are \(loginItems.count) login items")
- let lastItemRef: LSSharedFileListItemRef = loginItems.lastObject as! LSSharedFileListItemRef
+ let lastItemRef: LSSharedFileListItem = loginItems.lastObject as! LSSharedFileListItem
for i in 0 ..< loginItems.count {
- let currentItemRef: LSSharedFileListItemRef = loginItems.objectAtIndex(i) as! LSSharedFileListItemRef
+ let currentItemRef: LSSharedFileListItem = loginItems.object(at: i) as! LSSharedFileListItem
if LSSharedFileListItemResolve(currentItemRef, 0, itemUrl, nil) == noErr {
- if let urlRef: NSURL = itemUrl.memory?.takeRetainedValue() {
+ if let urlRef: URL = itemUrl.pointee?.takeRetainedValue() as URL? {
// print("URL Ref: \(urlRef.lastPathComponent)")
- if urlRef.isEqual(appUrl) {
+ if urlRef == appUrl {
return (currentItemRef, lastItemRef)
}
}
@@ -59,10 +59,10 @@ func addLaunchAtStartup() {
nil,
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
nil
- ).takeRetainedValue() as LSSharedFileListRef?
+ ).takeRetainedValue() as LSSharedFileList?
- if let appUrl : CFURLRef = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) {
+ if let appUrl : CFURL = URL(fileURLWithPath: Bundle.main.bundlePath) as CFURL? {
LSSharedFileListInsertItemURL(
loginItemsRef,
itemReferences.lastReference,
@@ -83,7 +83,7 @@ func removeLaunchAtStartup() {
nil,
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
nil
- ).takeRetainedValue() as LSSharedFileListRef?
+ ).takeRetainedValue() as LSSharedFileList?
if let itemRef = itemReferences.existingReference {
LSSharedFileListItemRemove(loginItemsRef,itemRef);
@@ -7,7 +7,6 @@
//
import XCTest
-@testable import StatusBarApp
class StatusBarAppTests: XCTestCase {
@@ -28,7 +27,7 @@ class StatusBarAppTests: XCTestCase {
func testPerformanceExample() {
// This is an example of a performance test case.
- self.measureBlock {
+ self.measure {
// Put the code you want to measure the time of here.
}
}
@@ -146,11 +146,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
- LastUpgradeCheck = 0730;
+ LastUpgradeCheck = 0800;
ORGANIZATIONNAME = eikana;
TargetAttributes = {
629428161D387503001BD4E9 = {
CreatedOnToolsVersion = 7.3;
+ LastSwiftMigration = 0800;
SystemCapabilities = {
com.apple.Sandbox = {
enabled = 0;
@@ -159,6 +160,7 @@
};
629428271D387503001BD4E9 = {
CreatedOnToolsVersion = 7.3;
+ LastSwiftMigration = 0800;
TestTargetID = 629428161D387503001BD4E9;
};
};
@@ -257,8 +259,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
+ CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+ CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
@@ -302,8 +306,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
+ CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+ CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
@@ -322,6 +328,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
+ SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
@@ -330,10 +337,11 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
COMBINE_HIDPI_IMAGES = YES;
- INFOPLIST_FILE = StatusBarApp/Info.plist;
+ INFOPLIST_FILE = "cmd-eikana/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.appspot.ei-kana.CommandEiKana";
PRODUCT_NAME = "⌘英かな";
+ SWIFT_VERSION = 3.0;
};
name = Debug;
};
@@ -342,10 +350,11 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
COMBINE_HIDPI_IMAGES = YES;
- INFOPLIST_FILE = StatusBarApp/Info.plist;
+ INFOPLIST_FILE = "cmd-eikana/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.appspot.ei-kana.CommandEiKana";
PRODUCT_NAME = "⌘英かな";
+ SWIFT_VERSION = 3.0;
};
name = Release;
};
@@ -354,11 +363,11 @@
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
COMBINE_HIDPI_IMAGES = YES;
- INFOPLIST_FILE = StatusBarAppTests/Info.plist;
+ INFOPLIST_FILE = "cmd-eikanaTests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = test1.StatusBarAppTests;
PRODUCT_NAME = "⌘英かな";
- TEST_HOST = "$(BUILT_PRODUCTS_DIR)/⌘英かな.app/Contents/MacOS/⌘英かな";
+ SWIFT_VERSION = 3.0;
};
name = Debug;
};
@@ -367,11 +376,11 @@
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
COMBINE_HIDPI_IMAGES = YES;
- INFOPLIST_FILE = StatusBarAppTests/Info.plist;
+ INFOPLIST_FILE = "cmd-eikanaTests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = test1.StatusBarAppTests;
PRODUCT_NAME = "⌘英かな";
- TEST_HOST = "$(BUILT_PRODUCTS_DIR)/⌘英かな.app/Contents/MacOS/⌘英かな";
+ SWIFT_VERSION = 3.0;
};
name = Release;
};

0 comments on commit 7d704e7

Please sign in to comment.