1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-01 01:32:04 +03:00
vimr/VimR/States.swift

212 lines
4.0 KiB
Swift
Raw Normal View History

2017-01-22 16:22:05 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
import RxSwift
typealias ActionEmitter = Emitter<Any>
class Emitter<T> {
let observable: Observable<T>
init() {
self.observable = self.subject.asObservable().observeOn(scheduler)
}
func emit(_ action: T) {
self.subject.onNext(action)
}
deinit {
self.subject.onCompleted()
}
fileprivate let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated)
fileprivate let subject = PublishSubject<T>()
}
class StateActionPair<S, A> {
2017-01-22 16:22:05 +03:00
let modified: Bool
let state: S
let action: A
2017-01-22 16:22:05 +03:00
init(state: S, action: A, modified: Bool = true) {
self.modified = modified
self.state = state
self.action = action
}
}
class UuidAction<A>: CustomStringConvertible {
let uuid: String
let payload: A
var description: String {
return "UuidAction(uuid: \(uuid), payload: \(String(reflecting: payload)))"
}
init(uuid: String, action: A) {
self.uuid = uuid
self.payload = action
}
}
class UuidState<S>: CustomStringConvertible {
let uuid: String
let payload: S
var description: String {
2017-02-03 00:39:05 +03:00
return "UuidState(uuid: \(uuid), payload: \(String(reflecting: payload)))"
}
init(uuid: String, state: S) {
self.uuid = uuid
self.payload = state
}
}
class Marked<T> {
let mark = Token()
let payload: T
init(_ payload: T) {
self.payload = payload
}
}
2017-01-22 16:22:05 +03:00
protocol Morpher {
2017-01-22 16:22:05 +03:00
associatedtype In
associatedtype Out
2017-01-22 16:22:05 +03:00
func transform(_ source: Observable<In>) -> Observable<Out>
}
2017-01-22 16:22:05 +03:00
protocol Transformer: Morpher {
2017-01-22 16:22:05 +03:00
associatedtype Element
2017-01-22 16:22:05 +03:00
typealias In = Element
typealias Out = Element
2017-01-22 16:22:05 +03:00
func transform(_ source: Observable<Element>) -> Observable<Element>
}
2017-02-05 17:17:25 +03:00
protocol Service {
associatedtype Element
func apply(_: Element)
}
2017-01-22 16:22:05 +03:00
struct AppState {
2017-02-06 20:57:50 +03:00
static let `default` = AppState(baseServerUrl: URL(string: "http://localhost:\(NetUtils.openPort())")!,
mainWindow: MainWindow.State.default)
2017-01-22 16:22:05 +03:00
var currentMainWindow: MainWindow.State
var mainWindows: [String: MainWindow.State] = [:]
2017-01-22 16:22:05 +03:00
let baseServerUrl: URL
2017-02-06 20:57:50 +03:00
init(baseServerUrl: URL, mainWindow: MainWindow.State) {
self.baseServerUrl = baseServerUrl
2017-01-22 16:22:05 +03:00
self.currentMainWindow = mainWindow
}
}
2017-02-06 00:39:55 +03:00
struct PreviewState {
2017-02-03 00:39:05 +03:00
2017-02-07 00:54:22 +03:00
static let `default` = PreviewState()
2017-02-06 20:36:57 +03:00
enum Status {
case none
case notSaved
case error
case markdown
}
var status = Status.none
2017-02-06 00:39:55 +03:00
var buffer: URL?
var html: URL?
var server: URL?
2017-02-07 00:54:22 +03:00
var updateDate = Date.distantPast
var editorPosition = Marked(Position(row: 1, column: 1))
var previewPosition = Marked(Position(row: 1, column: 1))
2017-02-07 00:54:22 +03:00
init(status: Status = .none,
buffer: URL? = nil,
html: URL? = nil,
server: URL? = nil,
updateDate: Date = Date.distantPast)
2017-02-07 00:54:22 +03:00
{
self.status = status
self.buffer = buffer
self.html = html
self.server = server
self.updateDate = updateDate
}
2017-02-03 00:39:05 +03:00
}
extension MainWindow {
2017-01-22 16:22:05 +03:00
struct State {
static let `default` = State(isAllToolsVisible: true, isToolButtonsVisible: true)
var isAllToolsVisible = true
var isToolButtonsVisible = true
2017-01-22 16:22:05 +03:00
////// transient
2017-02-06 00:39:55 +03:00
var preview = PreviewState.default
2017-02-07 00:54:22 +03:00
var previewTool = PreviewTool.State.default
var isClosed = false
2017-02-03 00:39:05 +03:00
2017-01-22 16:22:05 +03:00
// neovim
var uuid = UUID().uuidString
2017-01-22 16:22:05 +03:00
var currentBuffer: NeoVimBuffer?
var buffers = [NeoVimBuffer]()
var cwd = FileUtils.userHomeUrl
var isDirty = false
var font = NSFont.userFixedPitchFont(ofSize: 13)!
var linespacing: CGFloat = 1
var isUseLigatures = false
var isUseInteractiveZsh = false
// transient^2
var urlsToOpen = [URL: OpenMode]()
init(isAllToolsVisible: Bool, isToolButtonsVisible: Bool) {
self.isAllToolsVisible = isAllToolsVisible
self.isToolButtonsVisible = isToolButtonsVisible
}
}
}
2017-02-07 00:54:22 +03:00
extension PreviewTool {
struct State {
static let `default` = State()
var isForwardSearchAutomatically = false
var isReverseSearchAutomatically = false
var isRefreshOnWrite = true
}
}