-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppDelegate.swift
77 lines (69 loc) · 3.22 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* VarnamIME is a user-configurable phonetic Input Method Engine for Mac OS X.
* Copyright (C) 2018 Ranganath Atreya
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
import InputMethodKit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
private (set) var server: IMKServer!
private (set) var candidatesWindow: IMKCandidates!
private var _languageConfig: [LanguageConfig]?
private var _systemTrayMenu: NSMenu?
var systemTrayMenu: NSMenu! { get {
let config = VarnamConfig()
if config.languageConfig == _languageConfig, let menu = _systemTrayMenu {
return menu
}
_languageConfig = config.languageConfig
let systemTrayMenu = NSMenu(title: "VarnamIME")
Logger.log.debug("Adding Installed Scripts to Menu")
for entry in _languageConfig!.filter({ $0.isEnabled }) {
let item = NSMenuItem(title: entry.language, action: #selector(VarnamController.menuItemSelected), keyEquivalent: "")
if let flags = entry.shortcutModifiers, let key = entry.shortcutKey {
item.keyEquivalentModifierMask = NSEvent.ModifierFlags(rawValue: flags)
item.keyEquivalent = item.keyEquivalentModifierMask.contains(.shift) ? key : key.lowercased()
}
if entry.identifier == config.schemeID {
item.state = .on
}
item.representedObject = entry.identifier
systemTrayMenu.addItem(item)
}
_systemTrayMenu = autoreleasepool { systemTrayMenu }
return _systemTrayMenu
}}
func applicationDidFinishLaunching(_ aNotification: Notification) {
for arg in CommandLine.arguments {
if arg == "-import" {
importVLF()
exit(0)
}
}
guard let connectionName = Bundle.main.infoDictionary?["InputMethodConnectionName"] as? String else {
fatalError("Unable to get Connection Name from Info dictionary!")
}
guard let bundleId = Bundle.main.bundleIdentifier else {
fatalError("Unable to obtain bundle identifier!")
}
guard let server = IMKServer(name: connectionName, bundleIdentifier: bundleId) else {
fatalError("Unable to init IMKServer for connection name: \(connectionName) and bundle id: \(bundleId)")
}
Logger.log.debug("Initialized IMK Server: \(server.bundle().bundleIdentifier ?? "nil")")
self.server = server
// Panel type is the orientation. Default: Vertical
// Use kIMKSingleRowSteppingCandidatePanel for horizontal
candidatesWindow = IMKCandidates(server: server, panelType: kIMKSingleColumnScrollingCandidatePanel)
candidatesWindow.setAttributes([IMKCandidatesSendServerKeyEventFirst: NSNumber(booleanLiteral: true)])
}
func applicationWillTerminate(_ aNotification: Notification) {
Logger.log.debug("Comitting all editing before terminating")
server.commitComposition(self)
}
func importVLF() {
Varnam.importAllVLFInAssets()
}
}