1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-22 05:01:50 +03:00
vimr/VimR/Workspace/Workspace.swift

131 lines
3.7 KiB
Swift
Raw Normal View History

2016-09-18 21:49:42 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import PureLayout
enum WorkspaceBarLocation {
case top
case right
case bottom
case left
}
class Workspace: NSView {
2016-09-25 09:53:10 +03:00
struct Config {
let mainViewMinimumSize: CGSize
}
2016-09-18 21:49:42 +03:00
private(set) var isBarVisible = true {
didSet {
2016-09-25 01:44:10 +03:00
self.relayout()
2016-09-18 21:49:42 +03:00
}
}
private let bars: [WorkspaceBarLocation: WorkspaceBar]
2016-09-25 01:44:10 +03:00
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - API
let mainView: NSView
2016-09-25 09:53:10 +03:00
let config: Config
2016-09-25 01:44:10 +03:00
2016-09-25 09:53:10 +03:00
init(mainView: NSView, config: Config = Config(mainViewMinimumSize: CGSize(width: 100, height: 100))) {
self.config = config
2016-09-18 21:49:42 +03:00
self.mainView = mainView
2016-09-25 09:46:40 +03:00
2016-09-18 21:49:42 +03:00
self.bars = [
.top: WorkspaceBar(location: .top),
.right: WorkspaceBar(location: .right),
.bottom: WorkspaceBar(location: .bottom),
.left: WorkspaceBar(location: .left)
]
super.init(frame: CGRect.zero)
self.translatesAutoresizingMaskIntoConstraints = false
self.relayout()
}
2016-09-25 01:44:10 +03:00
func append(tool tool: WorkspaceTool, location: WorkspaceBarLocation) {
self.bars[location]?.append(tool: tool)
}
func toggleAllTools() {
self.isBarVisible = !self.isBarVisible
}
func toggleToolButtons() {
self.bars.values.forEach { $0.isButtonVisible = !$0.isButtonVisible }
}
}
// MARK: - Layout
extension Workspace {
2016-09-18 21:49:42 +03:00
private func relayout() {
// FIXME: I did not investigate why toggleButtons does not work correctly if we store all constraints in an array
// and remove them here by self.removeConstraints(${all constraints). The following seems to work...
self.subviews.forEach { $0.removeAllConstraints() }
self.removeAllSubviews()
let mainView = self.mainView
self.addSubview(mainView)
2016-09-25 09:53:10 +03:00
mainView.autoSetDimension(.Width, toSize: self.config.mainViewMinimumSize.width, relation: .GreaterThanOrEqual)
mainView.autoSetDimension(.Height, toSize: self.config.mainViewMinimumSize.height, relation: .GreaterThanOrEqual)
2016-09-18 21:49:42 +03:00
guard self.isBarVisible else {
mainView.autoPinEdgesToSuperviewEdges()
return
}
let topBar = self.bars[.top]!
let rightBar = self.bars[.right]!
let bottomBar = self.bars[.bottom]!
let leftBar = self.bars[.left]!
self.addSubview(topBar)
self.addSubview(rightBar)
self.addSubview(bottomBar)
self.addSubview(leftBar)
2016-09-25 09:46:40 +03:00
topBar.autoPinEdgeToSuperviewEdge(.Top)
topBar.autoPinEdgeToSuperviewEdge(.Right)
topBar.autoPinEdgeToSuperviewEdge(.Left)
2016-09-18 21:49:42 +03:00
2016-09-25 09:46:40 +03:00
rightBar.autoPinEdge(.Top, toEdge: .Bottom, ofView: topBar)
rightBar.autoPinEdgeToSuperviewEdge(.Right)
rightBar.autoPinEdge(.Bottom, toEdge: .Top, ofView: bottomBar)
2016-09-18 21:49:42 +03:00
2016-09-25 09:46:40 +03:00
bottomBar.autoPinEdgeToSuperviewEdge(.Right)
bottomBar.autoPinEdgeToSuperviewEdge(.Bottom)
bottomBar.autoPinEdgeToSuperviewEdge(.Left)
2016-09-18 21:49:42 +03:00
2016-09-25 09:46:40 +03:00
leftBar.autoPinEdge(.Top, toEdge: .Bottom, ofView: topBar)
leftBar.autoPinEdgeToSuperviewEdge(.Left)
leftBar.autoPinEdge(.Bottom, toEdge: .Top, ofView: bottomBar)
2016-09-18 21:49:42 +03:00
NSLayoutConstraint.autoSetPriority(NSLayoutPriorityDragThatCannotResizeWindow) {
topBar.dimensionConstraint = topBar.autoSetDimension(.Height, toSize: 50)
rightBar.dimensionConstraint = rightBar.autoSetDimension(.Width, toSize: 50)
bottomBar.dimensionConstraint = bottomBar.autoSetDimension(.Height, toSize: 50)
leftBar.dimensionConstraint = leftBar.autoSetDimension(.Width, toSize: 50)
}
self.bars.values.forEach { $0.relayout() }
mainView.autoPinEdge(.Top, toEdge: .Bottom, ofView: topBar)
mainView.autoPinEdge(.Right, toEdge: .Left, ofView: rightBar)
mainView.autoPinEdge(.Bottom, toEdge: .Top, ofView: bottomBar)
mainView.autoPinEdge(.Left, toEdge: .Right, ofView: leftBar)
2016-09-25 09:46:40 +03:00
2016-09-25 01:44:10 +03:00
self.needsDisplay = true
2016-09-18 21:49:42 +03:00
}
}