1
1
mirror of https://github.com/qvacua/vimr.git synced 2025-01-08 06:58:50 +03:00
vimr/VimR/Workspace/WorkspaceTool.swift

139 lines
3.0 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
2016-09-18 21:49:42 +03:00
protocol WorkspaceToolDelegate: class {
2016-09-25 18:50:33 +03:00
func toggle(_ tool: WorkspaceTool)
2016-09-18 21:49:42 +03:00
}
class WorkspaceTool: NSView {
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
static func ==(left: WorkspaceTool, right: WorkspaceTool) -> Bool {
return left.uuid == right.uuid
}
2016-09-18 21:49:42 +03:00
2016-09-25 01:44:10 +03:00
// MARK: - API
override var hashValue: Int {
2016-11-17 02:16:03 +03:00
return self.uuid.hashValue
}
2016-11-20 12:57:27 +03:00
/**
This UUID is only memory-persistent. It's generated when the tool is instantiated.
*/
let uuid = UUID().uuidString
2016-09-18 21:49:42 +03:00
let title: String
let view: NSView
let button: WorkspaceToolButton
var location = WorkspaceBarLocation.left {
didSet {
self.button.location = self.location
}
}
var isSelected = false {
didSet {
if self.isSelected {
self.button.highlight()
} else {
self.button.dehighlight()
}
}
}
weak var delegate: WorkspaceToolDelegate?
2016-11-18 00:13:31 +03:00
weak var bar: WorkspaceBar?
2016-09-18 21:49:42 +03:00
2016-12-04 18:36:57 +03:00
var workspace: Workspace? {
return self.bar?.workspace
}
var innerToolbar: InnerToolBar?
let minimumDimension: CGFloat
var dimension: CGFloat
2016-09-18 21:49:42 +03:00
struct Config {
let title: String
let view: NSView
let minimumDimension: CGFloat
let isWithInnerToolbar: Bool
let customToolbar: NSView?
let customMenuItems: [NSMenuItem]
init(title: String,
view: NSView,
minimumDimension: CGFloat = 50,
withInnerToolbar: Bool = true,
customToolbar: NSView? = nil,
customMenuItems: [NSMenuItem] = [])
{
self.title = title
self.view = view
self.minimumDimension = minimumDimension
self.isWithInnerToolbar = withInnerToolbar
self.customToolbar = customToolbar
self.customMenuItems = customMenuItems
}
}
init(_ config: Config) {
self.title = config.title
self.view = config.view
self.minimumDimension = config.minimumDimension
self.dimension = minimumDimension
2016-09-18 21:49:42 +03:00
self.button = WorkspaceToolButton(title: title)
2016-11-20 12:57:27 +03:00
super.init(frame: .zero)
self.configureForAutoLayout()
2016-09-18 21:49:42 +03:00
self.button.tool = self
if config.isWithInnerToolbar {
self.innerToolbar = InnerToolBar(customToolbar: config.customToolbar, customMenuItems: config.customMenuItems)
self.innerToolbar?.tool = self
}
self.addViews()
2016-09-18 21:49:42 +03:00
}
func toggle() {
2016-09-25 18:50:33 +03:00
self.delegate?.toggle(self)
2016-09-18 21:49:42 +03:00
self.isSelected = !self.isSelected
}
fileprivate func addViews() {
let view = self.view
self.addSubview(view)
if let innerToolbar = self.innerToolbar {
self.addSubview(innerToolbar)
innerToolbar.autoPinEdge(toSuperviewEdge: .top)
innerToolbar.autoPinEdge(toSuperviewEdge: .right)
innerToolbar.autoPinEdge(toSuperviewEdge: .left)
view.autoPinEdge(.top, to: .bottom, of: innerToolbar)
view.autoPinEdge(toSuperviewEdge: .right)
view.autoPinEdge(toSuperviewEdge: .bottom)
view.autoPinEdge(toSuperviewEdge: .left)
return
}
view.autoPinEdgesToSuperviewEdges()
}
2016-09-18 21:49:42 +03:00
}