vChewing-macOS/Source/Modules/AppDelegate.swift

157 lines
6.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// (c) 2011 and onwards The OpenVanilla Project (MIT License).
// All possible vChewing-specific modifications are of:
// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import FolderMonitor
import Uninstaller
import UpdateSputnik
@objc(AppDelegate)
class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
private func reloadOnFolderChangeHappens() {
// 100ms 使使
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
if PrefMgr.shared.shouldAutoReloadUserDataFiles {
LMMgr.initUserLangModels()
}
}
}
public let updateSputnik = UpdateSputnik()
private var ctlClientListMgrInstance: ctlClientListMgr?
private var ctlPrefWindowInstance: ctlPrefWindow?
private var ctlAboutWindowInstance: ctlAboutWindow? // New About Window
public var folderMonitor = FolderMonitor(
url: URL(fileURLWithPath: LMMgr.dataFolderPath(isDefaultFolder: false))
)
private var currentAlertType: String = ""
func userNotificationCenter(_: NSUserNotificationCenter, shouldPresent _: NSUserNotification) -> Bool {
true
}
func applicationDidFinishLaunching(_: Notification) {
NSUserNotificationCenter.default.delegate = self
// 使
if PrefMgr.shared.failureFlagForUOMObservation {
LMMgr.clearUserOverrideModelData(.imeModeCHS)
LMMgr.clearUserOverrideModelData(.imeModeCHT)
PrefMgr.shared.failureFlagForUOMObservation = false
let userNotification = NSUserNotification()
userNotification.title = NSLocalizedString("vChewing", comment: "")
userNotification.informativeText =
"\(NSLocalizedString("vChewing crashed while handling previously loaded UOM observation data. These data files are cleaned now to ensure the usability.", comment: ""))"
userNotification.soundName = NSUserNotificationDefaultSoundName
NSUserNotificationCenter.default.deliver(userNotification)
}
if !PrefMgr.shared.onlyLoadFactoryLangModelsIfNeeded { LMMgr.loadDataModelsOnAppDelegate() }
DispatchQueue.main.async {
LMMgr.initUserLangModels()
self.folderMonitor.folderDidChange = { [weak self] in
self?.reloadOnFolderChangeHappens()
}
if LMMgr.userDataFolderExists {
self.folderMonitor.startMonitoring()
}
}
PrefMgr.shared.fixOddPreferences()
//
updateSputnik.varkUpdateInfoPageURLKey = "UpdateInfoSite"
updateSputnik.varkUpdateCheckDateKeyPrevious = "PreviousUpdateCheckDate"
updateSputnik.varkUpdateCheckDateKeyNext = "NextUpdateCheckDate"
updateSputnik.varkUpdateCheckInterval = 114_514
updateSputnik.varCheckUpdateAutomatically = "ChecvarkUpdateAutomatically"
// 使
if PrefMgr.shared.checkUpdateAutomatically {
updateSputnik.checkForUpdate(forced: false, url: kUpdateInfoSourceURL)
}
}
func updateDirectoryMonitorPath() {
folderMonitor.stopMonitoring()
folderMonitor = FolderMonitor(
url: URL(fileURLWithPath: LMMgr.dataFolderPath(isDefaultFolder: false))
)
folderMonitor.folderDidChange = { [weak self] in
self?.reloadOnFolderChangeHappens()
}
if LMMgr.userDataFolderExists {
folderMonitor.startMonitoring()
}
}
func showClientListMgr() {
if ctlClientListMgrInstance == nil {
ctlClientListMgrInstance = ctlClientListMgr.init(windowNibName: "frmClientListMgr")
}
ctlClientListMgrInstance?.window?.center()
ctlClientListMgrInstance?.window?.orderFrontRegardless() //
ctlClientListMgrInstance?.window?.level = .statusBar
ctlClientListMgrInstance?.window?.titlebarAppearsTransparent = true
NSApp.setActivationPolicy(.accessory)
}
func showPreferences() {
if ctlPrefWindowInstance == nil {
ctlPrefWindowInstance = ctlPrefWindow.init(windowNibName: "frmPrefWindow")
}
ctlPrefWindowInstance?.window?.center()
ctlPrefWindowInstance?.window?.orderFrontRegardless() //
ctlPrefWindowInstance?.window?.level = .statusBar
ctlPrefWindowInstance?.window?.titlebarAppearsTransparent = true
NSApp.setActivationPolicy(.accessory)
}
// New About Window
func showAbout() {
if ctlAboutWindowInstance == nil {
ctlAboutWindowInstance = ctlAboutWindow.init(windowNibName: "frmAboutWindow")
}
ctlAboutWindowInstance?.window?.center()
ctlAboutWindowInstance?.window?.orderFrontRegardless() //
ctlAboutWindowInstance?.window?.level = .statusBar
ctlAboutWindowInstance?.window?.titlebarAppearsTransparent = true
NSApp.setActivationPolicy(.accessory)
}
func selfUninstall() {
currentAlertType = "Uninstall"
let content = String(
format: NSLocalizedString(
"This will remove vChewing Input Method from this user account, requiring your confirmation.",
comment: ""
))
let alert = NSAlert()
alert.messageText = NSLocalizedString("Uninstallation", comment: "")
alert.informativeText = content
alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
alert.addButton(withTitle: NSLocalizedString("Not Now", comment: ""))
let result = alert.runModal()
if result == NSApplication.ModalResponse.alertFirstButtonReturn {
NSWorkspace.shared.openFile(
LMMgr.dataFolderPath(isDefaultFolder: true), withApplication: "Finder"
)
Uninstaller.uninstall(
isSudo: false, selfKill: true, defaultDataFolderPath: LMMgr.dataFolderPath(isDefaultFolder: true)
)
}
NSApp.setActivationPolicy(.accessory)
}
// New About Window
@IBAction func about(_: Any) {
(NSApp.delegate as? AppDelegate)?.showAbout()
NSApplication.shared.activate(ignoringOtherApps: true)
}
}