mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
feat: added buttons for import/export application settings (#1837)
This commit is contained in:
@@ -62,4 +62,19 @@ public class Store {
|
||||
self.defaults.removeObject(forKey: key)
|
||||
}
|
||||
}
|
||||
|
||||
public func export(to url: URL) {
|
||||
guard let id = Bundle.main.bundleIdentifier,
|
||||
let dicitionary = self.defaults.persistentDomain(forName: id) else { return }
|
||||
NSDictionary(dictionary: dicitionary).write(to: url, atomically: true)
|
||||
}
|
||||
|
||||
public func `import`(from url: URL) {
|
||||
guard let id = Bundle.main.bundleIdentifier, let dict = NSDictionary(contentsOf: url) as? [String: Any] else { return }
|
||||
self.defaults.setPersistentDomain(dict, forName: id)
|
||||
if let path = Bundle.main.resourceURL?.deletingLastPathComponent().deletingLastPathComponent().absoluteString {
|
||||
asyncShell("/usr/bin/open \(path)")
|
||||
NSApp.terminate(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,6 +298,18 @@ class ApplicationSettings: NSStackView {
|
||||
grid.setContentHuggingPriority(.defaultHigh, for: .horizontal)
|
||||
grid.setContentHuggingPriority(.defaultHigh, for: .vertical)
|
||||
|
||||
let importBtn: NSButton = NSButton()
|
||||
importBtn.title = localizedString("Import")
|
||||
importBtn.bezelStyle = .rounded
|
||||
importBtn.target = self
|
||||
importBtn.action = #selector(self.importSettings)
|
||||
|
||||
let exportBtn: NSButton = NSButton()
|
||||
exportBtn.title = localizedString("Export")
|
||||
exportBtn.bezelStyle = .rounded
|
||||
exportBtn.target = self
|
||||
exportBtn.action = #selector(self.exportSettings)
|
||||
|
||||
let resetBtn: NSButton = NSButton()
|
||||
resetBtn.title = localizedString("Reset")
|
||||
resetBtn.bezelStyle = .rounded
|
||||
@@ -307,8 +319,10 @@ class ApplicationSettings: NSStackView {
|
||||
|
||||
grid.addRow(with: [
|
||||
self.titleView(localizedString("Settings")),
|
||||
resetBtn
|
||||
importBtn
|
||||
])
|
||||
grid.addRow(with: [NSGridCell.emptyContentView, exportBtn])
|
||||
grid.addRow(with: [NSGridCell.emptyContentView, resetBtn])
|
||||
|
||||
view.addArrangedSubview(grid)
|
||||
|
||||
@@ -434,6 +448,30 @@ class ApplicationSettings: NSStackView {
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func importSettings(_ sender: NSObject) {
|
||||
let panel = NSOpenPanel()
|
||||
panel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow)))
|
||||
panel.begin { (result) in
|
||||
guard result.rawValue == NSApplication.ModalResponse.OK.rawValue else { return }
|
||||
if let url = panel.url {
|
||||
Store.shared.import(from: url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func exportSettings(_ sender: NSObject) {
|
||||
let panel = NSSavePanel()
|
||||
panel.nameFieldStringValue = "Stats.plist"
|
||||
panel.showsTagField = false
|
||||
panel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow)))
|
||||
panel.begin { (result) in
|
||||
guard result.rawValue == NSApplication.ModalResponse.OK.rawValue else { return }
|
||||
if let url = panel.url {
|
||||
Store.shared.export(to: url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func resetSettings(_ sender: NSObject) {
|
||||
let alert = NSAlert()
|
||||
alert.messageText = localizedString("Reset settings")
|
||||
|
||||
Reference in New Issue
Block a user