Skip to content

Commit

Permalink
Use Swift 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun authored and Hyunje Jun committed Oct 3, 2016
1 parent e4b4521 commit c6e0ea0
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 83 deletions.
6 changes: 6 additions & 0 deletions kawa.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,11 @@
TargetAttributes = {
CC6D8EC71B654099005682C0 = {
CreatedOnToolsVersion = 6.4;
LastSwiftMigration = 0800;
};
CC6D8ED91B654099005682C0 = {
CreatedOnToolsVersion = 6.4;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -396,6 +398,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "net.noraesae.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = Kawa;
SWIFT_OBJC_BRIDGING_HEADER = kawa/BridgingHeader.h;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -413,6 +416,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "net.noraesae.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = Kawa;
SWIFT_OBJC_BRIDGING_HEADER = kawa/BridgingHeader.h;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -432,6 +436,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "net.noraesae.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -447,6 +452,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "net.noraesae.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
12 changes: 6 additions & 6 deletions kawa/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var justLaunched: Bool = true
var launchedForTheFirstTime: Bool = Settings.get(Settings.launchedForTheFirstTime, withDefaultValue: true)

func applicationDidFinishLaunching(aNotification: NSNotification) {
func applicationDidFinishLaunching(_ aNotification: Notification) {
InputSourceManager.initialize()
preferenceWindowController = instantiatePreferenceWindowController()
StatusBar.initWithPreferenceWindowController(preferenceWindowController)
Expand All @@ -27,10 +27,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func instantiatePreferenceWindowController() -> PreferenceWindowController {
let storyboard = NSStoryboard(name: "Main", bundle: nil)
return storyboard.instantiateControllerWithIdentifier("Preference") as! PreferenceWindowController
return storyboard.instantiateController(withIdentifier: "Preference") as! PreferenceWindowController
}

func applicationDidBecomeActive(notification: NSNotification) {
func applicationDidBecomeActive(_ notification: Notification) {
if !justLaunched || launchedForTheFirstTime {
preferenceWindowController.showAndActivate(self)
}
Expand All @@ -40,14 +40,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

func applicationWillTerminate(aNotification: NSNotification) {
func applicationWillTerminate(_ aNotification: Notification) {
}

@IBAction func showPreferences(sender: AnyObject?) {
@IBAction func showPreferences(_ sender: AnyObject?) {
preferenceWindowController.showAndActivate(self)
}

@IBAction func hidePreferences(sender: AnyObject?) {
@IBAction func hidePreferences(_ sender: AnyObject?) {
preferenceWindowController.close()
}
}
12 changes: 6 additions & 6 deletions kawa/HyperlinkTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
import Cocoa

class HyperlinkTextField: NSTextField {
func setURL(url: NSURL) {
func setURL(_ url: URL) {
self.allowsEditingTextAttributes = true
self.selectable = true
self.isSelectable = true
self.attributedStringValue = linkString(stringValue, url: url)
}

override func resetCursorRects() {
self.addCursorRect(self.bounds, cursor: NSCursor.pointingHandCursor())
self.addCursorRect(self.bounds, cursor: NSCursor.pointingHand())
}

func linkString(text: String, url: NSURL) -> NSMutableAttributedString {
func linkString(_ text: String, url: URL) -> NSMutableAttributedString {
let attrString = NSMutableAttributedString(string: text)
let range = NSRange(location: 0, length: attrString.length)
attrString.beginEditing()
attrString.addAttribute(NSLinkAttributeName, value: url.absoluteString, range: range)
attrString.addAttribute(NSFontAttributeName, value: font!, range: range)
attrString.addAttribute(NSForegroundColorAttributeName, value: NSColor.blueColor(), range: range)
attrString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue as AnyObject, range: range)
attrString.addAttribute(NSForegroundColorAttributeName, value: NSColor.blue, range: range)
attrString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue as AnyObject, range: range)
attrString.endEditing()
return attrString
}
Expand Down
54 changes: 27 additions & 27 deletions kawa/InputSourceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import Carbon
import Cocoa

class InputSource: Equatable {
static func getProperty<T>(source: TISInputSource, _ key: CFString) -> T? {
static func getProperty<T>(_ source: TISInputSource, _ key: CFString) -> T? {
let cfType = TISGetInputSourceProperty(source, key)
if (cfType != nil) {
return Unmanaged<AnyObject>.fromOpaque(COpaquePointer(cfType)).takeUnretainedValue() as? T
return Unmanaged<AnyObject>.fromOpaque(OpaquePointer(cfType)!).takeUnretainedValue() as? T
} else {
return nil
}
}

static func isProperInputSource(source: TISInputSource) -> Bool {
static func isProperInputSource(_ source: TISInputSource) -> Bool {
let category: String = getProperty(source, kTISPropertyInputSourceCategory)!
let selectable: Bool = getProperty(source, kTISPropertyInputSourceIsSelectCapable)!
return category == (kTISCategoryKeyboardInputSource as String) && selectable
Expand All @@ -35,17 +35,17 @@ class InputSource: Equatable {
self.id = InputSource.getProperty(tisInputSource, kTISPropertyInputSourceID)!
self.name = InputSource.getProperty(tisInputSource, kTISPropertyLocalizedName)!

let imageURL: NSURL? = InputSource.getProperty(tisInputSource, kTISPropertyIconImageURL)
let imageURL: URL? = InputSource.getProperty(tisInputSource, kTISPropertyIconImageURL)
if imageURL != nil {
self.icon = NSImage(contentsOfURL: getRetinaImageURL(imageURL!))
self.icon = NSImage(contentsOf: getRetinaImageURL(imageURL!))
if self.icon == nil {
self.icon = NSImage(contentsOfURL: getTiffImageURL(imageURL!))
self.icon = NSImage(contentsOf: getTiffImageURL(imageURL!))
if self.icon == nil {
self.icon = NSImage(contentsOfURL: imageURL!)
self.icon = NSImage(contentsOf: imageURL!)
}
}
} else {
let iconRef: IconRef? = COpaquePointer(TISGetInputSourceProperty(tisInputSource, kTISPropertyIconRef))
let iconRef: IconRef? = OpaquePointer(TISGetInputSourceProperty(tisInputSource, kTISPropertyIconRef))
if iconRef != nil {
self.icon = NSImage(iconRef: iconRef!)
}
Expand All @@ -67,16 +67,16 @@ class InputSource: Equatable {
TISSelectInputSource(tisInputSource)
}

func getRetinaImageURL(path: NSURL) -> NSURL {
var components = path.pathComponents!
func getRetinaImageURL(_ path: URL) -> URL {
var components = path.pathComponents
let filename: String = components.removeLast()
let ext: String = path.pathExtension!
let retinaFilename = filename.stringByReplacingOccurrencesOfString("." + ext, withString: "@2x." + ext)
return NSURL.fileURLWithPathComponents(components + [retinaFilename])!
let ext: String = path.pathExtension
let retinaFilename = filename.replacingOccurrences(of: "." + ext, with: "@2x." + ext)
return URL.fileURL(withPathComponents: components + [retinaFilename])!
}

func getTiffImageURL(path: NSURL) -> NSURL {
return path.URLByDeletingPathExtension!.URLByAppendingPathExtension("tiff")
func getTiffImageURL(_ path: URL) -> URL {
return path.deletingPathExtension().appendingPathExtension("tiff")
}
}

Expand All @@ -94,13 +94,13 @@ class InputSourceManager {

inputSources = inputSourceList.filter(InputSource.isProperInputSource)
.map {
(let tisInputSource) -> InputSource in
(tisInputSource) -> InputSource in
return InputSource(tisInputSource: tisInputSource)
}
}

static func previousOf(inputSource: InputSource) -> InputSource? {
if let idx = inputSources.indexOf(inputSource) {
static func previousOf(_ inputSource: InputSource) -> InputSource? {
if let idx = inputSources.index(of: inputSource) {
let previousIdx = idx == 0 ? idx + inputSources.count - 1 : idx - 1
return inputSources[previousIdx]
} else {
Expand All @@ -109,18 +109,18 @@ class InputSourceManager {
}

static func selectNext() {
let src = CGEventSourceCreate(CGEventSourceStateID.HIDSystemState)!
let src = CGEventSource(stateID: CGEventSourceStateID.hidSystemState)!

let down = CGEventCreateKeyboardEvent(src, CGKeyCode(kVK_Space), true)!
let up = CGEventCreateKeyboardEvent(src, CGKeyCode(kVK_Space), false)!
let down = CGEvent(keyboardEventSource: src, virtualKey: CGKeyCode(kVK_Space), keyDown: true)!
let up = CGEvent(keyboardEventSource: src, virtualKey: CGKeyCode(kVK_Space), keyDown: false)!

let flag = CGEventFlags(rawValue: CGEventFlags.MaskAlternate.rawValue | CGEventFlags.MaskCommand.rawValue)!
CGEventSetFlags(down, flag);
CGEventSetFlags(up, flag);
let flag = CGEventFlags(rawValue: CGEventFlags.maskAlternate.rawValue | CGEventFlags.maskCommand.rawValue)
down.flags = flag;
up.flags = flag;

let loc = CGEventTapLocation.CGHIDEventTap
let loc = CGEventTapLocation.cghidEventTap

CGEventPost(loc, down)
CGEventPost(loc, up)
down.post(tap: loc)
up.post(tap: loc)
}
}
18 changes: 9 additions & 9 deletions kawa/LaunchOnStartup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
import Foundation

class LaunchOnStartup {
static func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItemRef?, lastReference: LSSharedFileListItemRef?) {
let appUrl = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath)
static func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItem?, lastReference: LSSharedFileListItem?) {
let appUrl = URL(fileURLWithPath: Bundle.main.bundlePath)
let loginItemsRef = LSSharedFileListCreate(
nil,
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
nil
).takeRetainedValue() as LSSharedFileListRef?
).takeRetainedValue() as LSSharedFileList?
if loginItemsRef != nil {
let loginItems = LSSharedFileListCopySnapshot(loginItemsRef, nil).takeRetainedValue() as Array

if loginItems.count == 0 {
return (nil, kLSSharedFileListItemBeforeFirst.takeRetainedValue())
}

let lastItemRef: LSSharedFileListItemRef = loginItems.last as! LSSharedFileListItemRef
let lastItemRef: LSSharedFileListItem = loginItems.last as! LSSharedFileListItem

for currentItemRef in loginItems as! [LSSharedFileListItemRef] {
for currentItemRef in loginItems as! [LSSharedFileListItem] {
if let itemUrl = LSSharedFileListItemCopyResolvedURL(currentItemRef, 0, nil) {
if (itemUrl.takeRetainedValue() as NSURL).isEqual(appUrl) {
if (itemUrl.takeRetainedValue() as URL) == appUrl {
return (currentItemRef, lastItemRef)
}
}
Expand All @@ -38,17 +38,17 @@ class LaunchOnStartup {
return (nil, nil)
}

static func setLaunchAtStartup(shouldLaunch: Bool) {
static func setLaunchAtStartup(_ shouldLaunch: Bool) {
let itemReferences = itemReferencesInLoginItems()
let alreadyExists = (itemReferences.existingReference != nil)
let loginItemsRef = LSSharedFileListCreate(
nil,
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
nil
).takeRetainedValue() as LSSharedFileListRef?
).takeRetainedValue() as LSSharedFileList?
if loginItemsRef != nil {
if !alreadyExists && shouldLaunch {
if let appUrl : CFURLRef = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) {
if let appUrl : CFURL = URL(fileURLWithPath: Bundle.main.bundlePath) as CFURL? {
LSSharedFileListInsertItemURL(
loginItemsRef,
itemReferences.lastReference,
Expand Down
10 changes: 5 additions & 5 deletions kawa/PreferenceWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
import Cocoa

class PreferenceWindowController: NSWindowController, NSWindowDelegate {
func showAndActivate(sender: AnyObject?) {
func showAndActivate(_ sender: AnyObject?) {
self.showWindow(sender)
self.window?.makeKeyAndOrderFront(sender)
NSApp.activateIgnoringOtherApps(true)
NSApp.activate(ignoringOtherApps: true)
}

func windowWillClose(notification: NSNotification) {
func windowWillClose(_ notification: Notification) {
deactivate()
}

func deactivate() {
// focus an application owning the menu bar
let workspace = NSWorkspace.sharedWorkspace()
workspace.menuBarOwningApplication?.activateWithOptions(NSApplicationActivationOptions.ActivateIgnoringOtherApps)
let workspace = NSWorkspace.shared()
workspace.menuBarOwningApplication?.activate(options: NSApplicationActivationOptions.activateIgnoringOtherApps)
}
}
10 changes: 5 additions & 5 deletions kawa/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
import Cocoa

class Settings {
static let defaults = NSUserDefaults.standardUserDefaults()
static let defaults = UserDefaults.standard

static let showMenubarIcon = "show-menubar-icon"
static let launchOnStartup = "launch-on-startup"
static let useAdvancedSwitchMethod = "use-advanced-switch-method"
static let launchedForTheFirstTime = "launched-for-the-first-time"

static func get<T>(key: String, withDefaultValue: T) -> T {
let val: T? = defaults.objectForKey(key) as? T
static func get<T>(_ key: String, withDefaultValue: T) -> T {
let val: T? = defaults.object(forKey: key) as? T
if val != nil {
return val!
} else {
return withDefaultValue
}
}

static func set<T>(key: String, toValue: T) {
defaults.setObject((toValue as! AnyObject), forKey: key)
static func set<T>(_ key: String, toValue: T) {
defaults.set((toValue as AnyObject), forKey: key)
defaults.synchronize()
}
}
12 changes: 6 additions & 6 deletions kawa/ShortcutCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ class ShortcutCellView: NSTableCellView {
var inputSource: InputSource?
var shortcutKey: String?

func setInputSource(inputSource: InputSource) {
func setInputSource(_ inputSource: InputSource) {
self.inputSource = inputSource
shortcutKey = inputSource.id.stringByReplacingOccurrencesOfString(".", withString: "-")
shortcutKey = inputSource.id.replacingOccurrences(of: ".", with: "-")
shortcutView.associatedUserDefaultsKey = shortcutKey!
shortcutView.shortcutValueChange = self.shortcutValueDidChanged
MASShortcutBinder.sharedBinder().bindShortcutWithDefaultsKey(shortcutKey!, toAction: selectInput)
MASShortcutBinder.shared().bindShortcut(withDefaultsKey: shortcutKey!, toAction: selectInput)
}

func shortcutValueDidChanged(sender: MASShortcutView!) {
func shortcutValueDidChanged(_ sender: MASShortcutView!) {
if sender.shortcutValue == nil {
resetShortcutBinder()
}
}

func resetShortcutBinder() {
MASShortcutBinder.sharedBinder().breakBindingWithDefaultsKey(shortcutKey!)
MASShortcutBinder.sharedBinder().bindShortcutWithDefaultsKey(shortcutKey!, toAction: selectInput)
MASShortcutBinder.shared().breakBinding(withDefaultsKey: shortcutKey!)
MASShortcutBinder.shared().bindShortcut(withDefaultsKey: shortcutKey!, toAction: selectInput)
}

func selectInput() {
Expand Down
12 changes: 6 additions & 6 deletions kawa/ShortcutTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import Cocoa

class ShortcutTableView: NSTableView, NSTableViewDataSource, NSTableViewDelegate {
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
func numberOfRows(in tableView: NSTableView) -> Int {
return InputSourceManager.inputSources.count
}

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let identifier = tableColumn?.identifier
let inputSource = InputSourceManager.inputSources[row]

Expand All @@ -26,15 +26,15 @@ class ShortcutTableView: NSTableView, NSTableViewDataSource, NSTableViewDelegate
return nil
}

func createKeyboardCellView(tableView: NSTableView, _ inputSource: InputSource) -> NSTableCellView? {
let cell = tableView.makeViewWithIdentifier("KeyboardCellView", owner: self) as? NSTableCellView
func createKeyboardCellView(_ tableView: NSTableView, _ inputSource: InputSource) -> NSTableCellView? {
let cell = tableView.make(withIdentifier: "KeyboardCellView", owner: self) as? NSTableCellView
cell!.textField?.stringValue = inputSource.name
cell!.imageView?.image = inputSource.icon
return cell
}

func createShorcutCellView(tableView: NSTableView, _ inputSource: InputSource) -> ShortcutCellView? {
let cell = tableView.makeViewWithIdentifier("ShortcutCellView", owner: self) as? ShortcutCellView
func createShorcutCellView(_ tableView: NSTableView, _ inputSource: InputSource) -> ShortcutCellView? {
let cell = tableView.make(withIdentifier: "ShortcutCellView", owner: self) as? ShortcutCellView
cell?.setInputSource(inputSource)
return cell
}
Expand Down
Loading

0 comments on commit c6e0ea0

Please sign in to comment.