2017-01-22 16:22:05 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
2017-01-17 21:47:59 +03:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
class UiRootReducer {
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
typealias Pair = StateActionPair<AppState, UuidAction<MainWindow.Action>>
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
func reduce(_ pair: Pair) -> Pair {
|
|
|
|
var appState = pair.state
|
|
|
|
let uuid = pair.action.uuid
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
switch pair.action.payload {
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
case .becomeKey:
|
|
|
|
appState.currentMainWindowUuid = uuid
|
2017-05-05 18:42:35 +03:00
|
|
|
appState.mainWindowTemplate = self.mainWindowTemplate(
|
|
|
|
from: appState.mainWindowTemplate, new: appState.mainWindows[uuid] ?? appState.mainWindowTemplate
|
|
|
|
)
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-06-11 14:50:40 +03:00
|
|
|
case let .setToolsState(tools):
|
|
|
|
appState.mainWindowTemplate.orderedTools = tools.map { $0.0 }
|
|
|
|
|
|
|
|
case let .toggleAllTools(value):
|
|
|
|
appState.mainWindowTemplate.isAllToolsVisible = value
|
|
|
|
|
|
|
|
case let .toggleToolButtons(value):
|
|
|
|
appState.mainWindowTemplate.isToolButtonsVisible = value
|
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
case .close:
|
|
|
|
if appState.currentMainWindowUuid == uuid, let mainWindowToClose = appState.mainWindows[uuid] {
|
2017-05-05 18:42:35 +03:00
|
|
|
appState.mainWindowTemplate = self.mainWindowTemplate(from: appState.mainWindowTemplate,
|
|
|
|
new: mainWindowToClose)
|
2017-03-01 18:59:23 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
appState.currentMainWindowUuid = nil
|
|
|
|
}
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
appState.mainWindows.removeValue(forKey: uuid)
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-06-27 07:41:16 +03:00
|
|
|
case let .setTheme(theme):
|
|
|
|
appState.mainWindowTemplate.appearance.theme = Marked(theme)
|
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
default:
|
|
|
|
return pair
|
2017-01-17 21:47:59 +03:00
|
|
|
|
|
|
|
}
|
2017-04-30 10:25:15 +03:00
|
|
|
|
|
|
|
return StateActionPair(state: appState, action: pair.action)
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
2017-05-05 18:42:35 +03:00
|
|
|
|
|
|
|
fileprivate func mainWindowTemplate(from old: MainWindow.State, new: MainWindow.State) -> MainWindow.State {
|
|
|
|
var result = old
|
|
|
|
|
|
|
|
result.isAllToolsVisible = new.isAllToolsVisible
|
|
|
|
result.isToolButtonsVisible = new.isToolButtonsVisible
|
|
|
|
result.tools = new.tools
|
2017-06-11 14:50:40 +03:00
|
|
|
result.orderedTools = new.orderedTools
|
2017-05-05 18:42:35 +03:00
|
|
|
result.previewTool = new.previewTool
|
|
|
|
result.fileBrowserShowHidden = new.fileBrowserShowHidden
|
|
|
|
result.htmlPreview = .default
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|