1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 02:54:31 +03:00

Add init(dict)

This commit is contained in:
Tae Won Ha 2017-02-28 22:04:12 +01:00
parent e4f7ceb6b1
commit 4535d90ecf
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
3 changed files with 228 additions and 141 deletions

View File

@ -52,10 +52,11 @@ class MainWindow: NSObject,
enum Tools: String {
static let all = Set([Tools.fileBrowser, Tools.openedFilesList, Tools.preview])
case fileBrowser = "com.qvacua.vimr.tools.file-browser"
case openedFilesList = "com.qvacua.vimr.tools.opened-files-list"
case preview = "com.qvacua.vimr.tools.preview"
}
enum OpenMode {

View File

@ -5,139 +5,13 @@
import Foundation
fileprivate class Keys {
static let openNewOnLaunch = "open-new-window-when-launching"
static let openNewOnReactivation = "open-new-window-on-reactivation"
static let useSnapshotUpdateChannel = "use-snapshot-update-channel"
class OpenQuickly {
static let key = "open-quickly"
static let ignorePatterns = "ignore-patterns"
}
class Appearance {
static let key = "appearance"
static let editorFontName = "editor-font-name"
static let editorFontSize = "editor-font-size"
static let editorLinespacing = "editor-linespacing"
static let editorUsesLigatures = "editor-uses-ligatures"
}
class MainWindow {
static let key = "main-window"
static let allToolsVisible = "is-all-tools-visible"
static let toolButtonsVisible = "is-tool-buttons-visible"
static let isShowHidden = "is-show-hidden"
}
class PreviewTool {
static let key = "preview-tool"
static let forwardSearchAutomatically = "is-forward-search-automatically"
static let reverseSearchAutomatically = "is-reverse-search-automatically"
static let refreshOnWrite = "is-refresh-on-write"
}
class WorkspaceTool {
static let key = "workspace-tool"
static let location = "location"
static let open = "is-visible"
static let dimension = "dimension"
}
}
protocol SerializableState {
init?(dict: [String: Any])
func dict() -> [String: Any]
}
extension AppState: SerializableState {
func dict() -> [String: Any] {
return [
Keys.openNewOnLaunch: self.openNewMainWindowOnLaunch,
Keys.openNewOnReactivation: self.openNewMainWindowOnReactivation,
Keys.useSnapshotUpdateChannel: self.useSnapshotUpdate,
Keys.OpenQuickly.key: self.openQuickly.dict(),
Keys.MainWindow.key: self.mainWindowTemplate.dict(),
]
}
}
extension OpenQuicklyWindow.State: SerializableState {
func dict() -> [String: Any] {
return [
Keys.OpenQuickly.ignorePatterns: FileItemIgnorePattern.toString(self.ignorePatterns)
]
}
}
extension AppearanceState: SerializableState {
func dict() -> [String: Any] {
return [
Keys.Appearance.editorFontName: self.font.fontName,
Keys.Appearance.editorFontSize: Float(self.font.pointSize),
Keys.Appearance.editorLinespacing: Float(self.linespacing),
Keys.Appearance.editorUsesLigatures: self.usesLigatures,
]
}
}
extension WorkspaceToolState: SerializableState {
func dict() -> [String: Any] {
return [
Keys.WorkspaceTool.location: PrefUtils.locationAsString(for: self.location),
Keys.WorkspaceTool.open: self.open,
Keys.WorkspaceTool.dimension: Float(self.dimension),
]
}
}
extension PreviewTool.State: SerializableState {
func dict() -> [String: Any] {
return [
Keys.PreviewTool.forwardSearchAutomatically: self.isForwardSearchAutomatically,
Keys.PreviewTool.reverseSearchAutomatically: self.isReverseSearchAutomatically,
Keys.PreviewTool.refreshOnWrite: self.isRefreshOnWrite,
]
}
}
extension MainWindow.State: SerializableState {
func dict() -> [String: Any] {
return [
Keys.MainWindow.allToolsVisible: self.isAllToolsVisible,
Keys.MainWindow.toolButtonsVisible: self.isToolButtonsVisible,
Keys.Appearance.key: self.appearance.dict(),
Keys.WorkspaceTool.key: Array(self.tools.keys.map { $0.rawValue })
.toDict { self.tools[MainWindow.Tools(rawValue: $0)!]!.dict() },
Keys.PreviewTool.key: self.previewTool.dict(),
Keys.MainWindow.isShowHidden: self.fileBrowserShowHidden,
]
}
}
class PrefUtils {
fileprivate static let whitespaceCharSet = CharacterSet.whitespaces
@ -248,3 +122,55 @@ class PrefUtils {
}
}
}
class Keys {
static let openNewOnLaunch = "open-new-window-when-launching"
static let openNewOnReactivation = "open-new-window-on-reactivation"
static let useSnapshotUpdateChannel = "use-snapshot-update-channel"
class OpenQuickly {
static let key = "open-quickly"
static let ignorePatterns = "ignore-patterns"
}
class Appearance {
static let key = "appearance"
static let editorFontName = "editor-font-name"
static let editorFontSize = "editor-font-size"
static let editorLinespacing = "editor-linespacing"
static let editorUsesLigatures = "editor-uses-ligatures"
}
class MainWindow {
static let key = "main-window"
static let allToolsVisible = "is-all-tools-visible"
static let toolButtonsVisible = "is-tool-buttons-visible"
static let isShowHidden = "is-show-hidden"
}
class PreviewTool {
static let key = "preview-tool"
static let forwardSearchAutomatically = "is-forward-search-automatically"
static let reverseSearchAutomatically = "is-reverse-search-automatically"
static let refreshOnWrite = "is-refresh-on-write"
}
class WorkspaceTool {
static let key = "workspace-tool"
static let location = "location"
static let open = "is-visible"
static let dimension = "dimension"
}
}

View File

@ -6,7 +6,7 @@
import Foundation
import RxSwift
struct AppState {
struct AppState: SerializableState {
static let `default` = AppState(mainWindow: MainWindow.State.default)
@ -31,11 +31,27 @@ struct AppState {
self.mainWindowTemplate = mainWindow
self.mainWindowTemplate.root = self.root
}
init?(dict: [String: Any]) {
return nil
}
func dict() -> [String: Any] {
return [
Keys.openNewOnLaunch: self.openNewMainWindowOnLaunch,
Keys.openNewOnReactivation: self.openNewMainWindowOnReactivation,
Keys.useSnapshotUpdateChannel: self.useSnapshotUpdate,
Keys.OpenQuickly.key: self.openQuickly.dict(),
Keys.MainWindow.key: self.mainWindowTemplate.dict(),
]
}
}
extension OpenQuicklyWindow {
struct State {
struct State: SerializableState {
static let `default` = State()
@ -45,6 +61,24 @@ extension OpenQuicklyWindow {
var ignoreToken = Token()
var open = false
init() {
}
init?(dict: [String: Any]) {
guard let patternsString = PrefUtils.string(from: dict, for: Keys.OpenQuickly.ignorePatterns) else {
return nil
}
self.ignorePatterns = FileItemIgnorePattern.from(string: patternsString)
}
func dict() -> [String: Any] {
return [
Keys.OpenQuickly.ignorePatterns: FileItemIgnorePattern.toString(self.ignorePatterns)
]
}
}
}
@ -86,18 +120,45 @@ struct PreviewState {
}
}
struct AppearanceState {
struct AppearanceState: SerializableState {
static let `default` = AppearanceState()
var font = NSFont.userFixedPitchFont(ofSize: 13)!
var linespacing: CGFloat = 1
var usesLigatures = false
init() {
}
init?(dict: [String: Any]) {
guard let editorFontName = dict[Keys.Appearance.editorFontName] as? String,
let fEditorFontSize = PrefUtils.float(from: dict, for: Keys.Appearance.editorFontSize),
let fEditorLinespacing = PrefUtils.float(from: dict, for: Keys.Appearance.editorLinespacing),
let editorUsesLigatures = PrefUtils.bool(from: dict, for: Keys.Appearance.editorUsesLigatures)
else {
return nil
}
self.font = PrefUtils.saneFont(editorFontName, fontSize: CGFloat(fEditorFontSize))
self.linespacing = CGFloat(fEditorLinespacing)
self.usesLigatures = editorUsesLigatures
}
func dict() -> [String: Any] {
return [
Keys.Appearance.editorFontName: self.font.fontName,
Keys.Appearance.editorFontSize: Float(self.font.pointSize),
Keys.Appearance.editorLinespacing: Float(self.linespacing),
Keys.Appearance.editorUsesLigatures: self.usesLigatures,
]
}
}
extension MainWindow {
struct State {
struct State: SerializableState {
static let `default` = State(isAllToolsVisible: true, isToolButtonsVisible: true)
@ -112,11 +173,7 @@ extension MainWindow {
var root = FileItem(URL(fileURLWithPath: "/", isDirectory: true))
var lastFileSystemUpdate = Marked(FileItem(URL(fileURLWithPath: "/", isDirectory: true)))
var tools = [
MainWindow.Tools.fileBrowser: WorkspaceToolState(location: .left, dimension: 200, open: true),
MainWindow.Tools.openedFilesList: WorkspaceToolState(location: .left, dimension: 200, open: false),
MainWindow.Tools.preview: WorkspaceToolState(location: .right, dimension: 250, open: false),
]
var tools = WorkspaceToolState.default
var preview = PreviewState.default
var previewTool = PreviewTool.State.default
@ -141,26 +198,129 @@ extension MainWindow {
self.isAllToolsVisible = isAllToolsVisible
self.isToolButtonsVisible = isToolButtonsVisible
}
init?(dict: [String: Any]) {
guard let isAllToolsVisible = PrefUtils.bool(from: dict, for: Keys.MainWindow.allToolsVisible),
let isToolButtonsVisible = PrefUtils.bool(from: dict, for: Keys.MainWindow.toolButtonsVisible),
let isShowHidden = PrefUtils.bool(from: dict, for: Keys.MainWindow.isShowHidden)
else {
return nil
}
self.isAllToolsVisible = isAllToolsVisible
self.isToolButtonsVisible = isToolButtonsVisible
let appearanceDict = dict[Keys.Appearance.key] as? [String: Any] ?? [:]
self.appearance = AppearanceState(dict: appearanceDict) ?? AppearanceState.default
let workspaceToolsDict = dict[Keys.WorkspaceTool.key] as? [String: [String: Any]] ?? [:]
let toolKeys = workspaceToolsDict.keys.flatMap { MainWindow.Tools(rawValue: $0) }
if MainWindow.Tools.all == Set(toolKeys) {
self.tools = Array(toolKeys).toDict { tool in
return WorkspaceToolState(dict: workspaceToolsDict[tool.rawValue]!) ?? WorkspaceToolState.default[tool]!
}
}
let previewToolDict = dict[Keys.PreviewTool.key] as? [String: Any] ?? [:]
self.previewTool = PreviewTool.State(dict: previewToolDict) ?? PreviewTool.State.default
self.fileBrowserShowHidden = isShowHidden
}
func dict() -> [String: Any] {
return [
Keys.MainWindow.allToolsVisible: self.isAllToolsVisible,
Keys.MainWindow.toolButtonsVisible: self.isToolButtonsVisible,
Keys.Appearance.key: self.appearance.dict(),
Keys.WorkspaceTool.key: Array(self.tools.keys.map { $0.rawValue })
.toDict { self.tools[MainWindow.Tools(rawValue: $0)!]!.dict() },
Keys.PreviewTool.key: self.previewTool.dict(),
Keys.MainWindow.isShowHidden: self.fileBrowserShowHidden,
]
}
}
}
struct WorkspaceToolState {
struct WorkspaceToolState: SerializableState {
static let `default` = WorkspaceToolState()
static let `default` = [
MainWindow.Tools.fileBrowser: WorkspaceToolState(location: .left, dimension: 200, open: true),
MainWindow.Tools.openedFilesList: WorkspaceToolState(location: .left, dimension: 200, open: false),
MainWindow.Tools.preview: WorkspaceToolState(location: .right, dimension: 250, open: false),
]
var location = WorkspaceBarLocation.left
var dimension = CGFloat(200)
var open = false
init(location: WorkspaceBarLocation, dimension: CGFloat, open: Bool) {
self.location = location
self.dimension = dimension
self.open = open
}
init?(dict: [String: Any]) {
guard let locationRawValue = dict[Keys.WorkspaceTool.location] as? String,
let isOpen = PrefUtils.bool(from: dict, for: Keys.WorkspaceTool.open),
let fDimension = PrefUtils.float(from: dict, for: Keys.WorkspaceTool.dimension)
else {
return nil
}
guard let location = PrefUtils.location(from: locationRawValue) else {
return nil
}
self.location = location
self.dimension = CGFloat(fDimension)
self.open = isOpen
}
func dict() -> [String: Any] {
return [
Keys.WorkspaceTool.location: PrefUtils.locationAsString(for: self.location),
Keys.WorkspaceTool.open: self.open,
Keys.WorkspaceTool.dimension: Float(self.dimension),
]
}
}
extension PreviewTool {
struct State {
struct State: SerializableState {
static let `default` = State()
var isForwardSearchAutomatically = false
var isReverseSearchAutomatically = false
var isRefreshOnWrite = true
init() {
}
init?(dict: [String: Any]) {
guard let isForward = PrefUtils.bool(from: dict, for: Keys.PreviewTool.forwardSearchAutomatically),
let isReverse = PrefUtils.bool(from: dict, for: Keys.PreviewTool.reverseSearchAutomatically),
let isRefreshOnWrite = PrefUtils.bool(from: dict, for: Keys.PreviewTool.refreshOnWrite)
else {
return nil
}
self.isRefreshOnWrite = isRefreshOnWrite
self.isForwardSearchAutomatically = isForward
self.isReverseSearchAutomatically = isReverse
}
func dict() -> [String: Any] {
return [
Keys.PreviewTool.forwardSearchAutomatically: self.isForwardSearchAutomatically,
Keys.PreviewTool.reverseSearchAutomatically: self.isReverseSearchAutomatically,
Keys.PreviewTool.refreshOnWrite: self.isRefreshOnWrite,
]
}
}
}