vChewing-macOS/Source/Modules/AppDelegate.swift

139 lines
5.6 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 Shared
import Uninstaller
import UpdateSputnik
@objc(AppDelegate)
public class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDelegate {
private var folderMonitor = FolderMonitor(
url: URL(fileURLWithPath: LMMgr.dataFolderPath(isDefaultFolder: false))
)
}
// MARK: - Private Functions
extension AppDelegate {
private func reloadOnFolderChangeHappens() {
// 100ms 使使
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
if PrefMgr.shared.shouldAutoReloadUserDataFiles { LMMgr.initUserLangModels() }
}
}
}
// MARK: - Public Functions
extension AppDelegate {
public func userNotificationCenter(_: NSUserNotificationCenter, shouldPresent _: NSUserNotification) -> Bool {
true
}
public 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.loadCassetteData()
LMMgr.initUserLangModels()
self.folderMonitor.folderDidChange = { [weak self] in
self?.reloadOnFolderChangeHappens()
}
if LMMgr.userDataFolderExists { self.folderMonitor.startMonitoring() }
}
PrefMgr.shared.fixOddPreferences()
// 使
if PrefMgr.shared.checkUpdateAutomatically {
UpdateSputnik.shared.checkForUpdate(forced: false, url: kUpdateInfoSourceURL)
}
}
public 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() }
}
public func selfUninstall() {
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()
NSApp.activate(ignoringOtherApps: true)
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)
)
}
}
///
/// - Returns: MiB
@discardableResult public func checkMemoryUsage() -> Double {
guard let currentMemorySizeInBytes = NSApplication.memoryFootprint else { return 0 }
let currentMemorySize: Double = (Double(currentMemorySizeInBytes) / 1024 / 1024).rounded(toPlaces: 1)
switch currentMemorySize {
case 512...:
vCLog("WARNING: EXCESSIVE MEMORY FOOTPRINT (\(currentMemorySize)MB).")
let userNotification = NSUserNotification()
userNotification.title = NSLocalizedString("vChewing", comment: "")
userNotification.informativeText = NSLocalizedString(
"vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size.",
comment: ""
)
NSUserNotificationCenter.default.deliver(userNotification)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
NSApp.terminate(self)
}
default: break
}
return currentMemorySize
}
// New About Window
@IBAction public func about(_: Any) {
CtlAboutWindow.show()
NSApp.activate(ignoringOtherApps: true)
}
}