Skip to content

Commit

Permalink
Implement CJK layout workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyunje Jun committed Sep 15, 2017
1 parent 95be8f5 commit 195f3ad
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions kawa/InputSourceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ class InputSource: Equatable {
let tisInputSource: TISInputSource
let id: String
let name: String
let langs: Array<String>

var icon: NSImage? = nil

init(tisInputSource: TISInputSource) {
self.tisInputSource = tisInputSource
id = InputSource.getProperty(tisInputSource, kTISPropertyInputSourceID)!
name = InputSource.getProperty(tisInputSource, kTISPropertyLocalizedName)!
langs = InputSource.getProperty(tisInputSource, kTISPropertyInputSourceLanguages)!

let imageURL: URL? = InputSource.getProperty(tisInputSource, kTISPropertyIconImageURL)
if imageURL != nil {
Expand All @@ -54,14 +56,16 @@ class InputSource: Equatable {
}

func select() {
let langs = InputSource.getProperty(tisInputSource, kTISPropertyInputSourceLanguages)! as Array<String>
TISSelectInputSource(tisInputSource)

if langs.contains(where: { $0 == "ko" || $0 == "ja" || $0.hasPrefix("zh") }) {
// Workaround for TIS CJK layout bug:
// FIXME: when it's CJK, select English first and then return
print("CJK")
} else {
TISSelectInputSource(tisInputSource)
// when it's CJK, select English first and then return
if let english = InputSourceManager.englishSource() {
english.select()
Thread.sleep(forTimeInterval: 0.05)
InputSourceManager.selectPrevious()
}
}
}

Expand Down Expand Up @@ -96,19 +100,17 @@ class InputSourceManager {
}
}

static func tweak() {
selectPrevious()
Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(selectPrevious), userInfo: nil, repeats: false)
static func englishSource() -> InputSource? {
return inputSources.first(where: { $0.id == "com.apple.keylayout.US" })
}

@objc
static func selectPrevious() {
let src = CGEventSource(stateID: CGEventSourceStateID.hidSystemState)!

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.maskShift.rawValue | CGEventFlags.maskCommand.rawValue)
let flag = CGEventFlags(rawValue: CGEventFlags.maskAlternate.rawValue | CGEventFlags.maskCommand.rawValue)
down.flags = flag;
up.flags = flag;

Expand Down

0 comments on commit 195f3ad

Please sign in to comment.