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

152 lines
4.2 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
}
2016-09-27 19:02:05 +03:00
protocol WorkspaceDelegate: class {
func resizeWillStart(workspace: Workspace)
func resizeDidEnd(workspace: Workspace)
}
class Workspace: NSView, WorkspaceBarDelegate {
2016-09-18 21:49:42 +03:00
2016-09-25 09:53:10 +03:00
struct Config {
let mainViewMinimumSize: CGSize
}
2016-09-25 18:50:33 +03:00
fileprivate(set) var isBarVisible = true {
2016-09-18 21:49:42 +03:00
didSet {
2016-09-25 01:44:10 +03:00
self.relayout()
2016-09-18 21:49:42 +03:00
}
}
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-27 19:02:05 +03:00
let bars: [WorkspaceBarLocation: WorkspaceBar]
2016-09-25 09:53:10 +03:00
let config: Config
2016-09-25 01:44:10 +03:00
2016-09-27 19:02:05 +03:00
weak var delegate: WorkspaceDelegate?
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
2016-09-27 19:02:05 +03:00
self.bars.values.forEach { [unowned self] in $0.delegate = self }
2016-09-18 21:49:42 +03:00
self.relayout()
}
2016-09-25 18:50:33 +03:00
func append(tool: WorkspaceTool, location: WorkspaceBarLocation) {
2016-09-25 01:44:10 +03:00
self.bars[location]?.append(tool: tool)
}
func toggleAllTools() {
self.isBarVisible = !self.isBarVisible
}
func toggleToolButtons() {
self.bars.values.forEach { $0.isButtonVisible = !$0.isButtonVisible }
}
}
2016-09-27 19:02:05 +03:00
// MARK: - WorkspaceBarDelegate
extension Workspace {
func resizeWillStart(workspaceBar: WorkspaceBar) {
self.delegate?.resizeWillStart(workspace: self)
}
func resizeDidEnd(workspaceBar: WorkspaceBar) {
self.delegate?.resizeDidEnd(workspace: self)
}
}
2016-09-25 01:44:10 +03:00
// MARK: - Layout
extension Workspace {
2016-09-25 18:50:33 +03:00
fileprivate func relayout() {
2016-09-18 21:49:42 +03:00
// 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
2016-09-25 18:50:33 +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 18:50:33 +03:00
topBar.autoPinEdge(toSuperviewEdge: .top)
topBar.autoPinEdge(toSuperviewEdge: .right)
topBar.autoPinEdge(toSuperviewEdge: .left)
2016-09-18 21:49:42 +03:00
2016-09-25 18:50:33 +03:00
rightBar.autoPinEdge(.top, to: .bottom, of: topBar)
rightBar.autoPinEdge(toSuperviewEdge: .right)
rightBar.autoPinEdge(.bottom, to: .top, of: bottomBar)
2016-09-18 21:49:42 +03:00
2016-09-25 18:50:33 +03:00
bottomBar.autoPinEdge(toSuperviewEdge: .right)
bottomBar.autoPinEdge(toSuperviewEdge: .bottom)
bottomBar.autoPinEdge(toSuperviewEdge: .left)
2016-09-18 21:49:42 +03:00
2016-09-25 18:50:33 +03:00
leftBar.autoPinEdge(.top, to: .bottom, of: topBar)
leftBar.autoPinEdge(toSuperviewEdge: .left)
leftBar.autoPinEdge(.bottom, to: .top, of: bottomBar)
2016-09-18 21:49:42 +03:00
NSLayoutConstraint.autoSetPriority(NSLayoutPriorityDragThatCannotResizeWindow) {
2016-09-25 18:50:33 +03:00
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)
2016-09-18 21:49:42 +03:00
}
self.bars.values.forEach { $0.relayout() }
2016-09-25 18:50:33 +03:00
mainView.autoPinEdge(.top, to: .bottom, of: topBar)
mainView.autoPinEdge(.right, to: .left, of: rightBar)
mainView.autoPinEdge(.bottom, to: .top, of: bottomBar)
mainView.autoPinEdge(.left, to: .right, of: 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
}
}