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

292 lines
11 KiB
Swift
Raw Normal View History

/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import PureLayout
2016-07-21 20:28:58 +03:00
import RxSwift
2016-09-11 15:28:56 +03:00
struct AppearancePrefData: Equatable {
2016-07-21 20:28:58 +03:00
let editorFont: NSFont
2016-10-27 01:26:04 +03:00
let editorLinespacing: CGFloat
2016-07-30 23:06:42 +03:00
let editorUsesLigatures: Bool
2016-07-21 20:28:58 +03:00
}
2016-08-14 22:46:03 +03:00
func == (left: AppearancePrefData, right: AppearancePrefData) -> Bool {
return left.editorUsesLigatures == right.editorUsesLigatures
&& left.editorFont.isEqual(to: right.editorFont)
2016-10-27 01:26:04 +03:00
&& left.editorLinespacing == right.editorLinespacing
2016-08-14 22:46:03 +03:00
}
class AppearancePrefPane: PrefPane, NSComboBoxDelegate, NSControlTextEditingDelegate {
2016-09-24 17:35:27 +03:00
override var displayName: String {
return "Appearance"
}
2016-08-25 10:27:59 +03:00
override var pinToContainer: Bool {
return true
}
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
fileprivate var data: AppearancePrefData {
didSet {
self.updateViews(newData: self.data)
}
}
2016-08-14 22:46:03 +03:00
2016-09-25 18:50:33 +03:00
fileprivate let fontManager = NSFontManager.shared()
2016-08-14 22:46:03 +03:00
2016-09-25 18:50:33 +03:00
fileprivate let sizes = [9, 10, 11, 12, 13, 14, 16, 18, 24, 36, 48, 64]
fileprivate let sizeCombo = NSComboBox(forAutoLayout: ())
fileprivate let fontPopup = NSPopUpButton(frame: CGRect.zero, pullsDown: false)
2016-10-27 00:37:51 +03:00
fileprivate let linespacingField = NSTextField(forAutoLayout: ())
2016-09-25 18:50:33 +03:00
fileprivate let ligatureCheckbox = NSButton(forAutoLayout: ())
fileprivate let previewArea = NSTextView(frame: CGRect.zero)
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
fileprivate let exampleText =
"abcdefghijklmnopqrstuvwxyz\n" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" +
"0123456789\n" +
"(){}[] +-*/= .,;:!?#&$%@|^\n" +
"<- -> => >> << >>= =<< .. \n" +
":: -< >- -<< >>- ++ /= =="
2016-07-27 00:40:20 +03:00
init(source: Observable<Any>, initialData: AppearancePrefData) {
2016-08-14 22:46:03 +03:00
self.data = initialData
super.init(source: source)
2016-09-11 15:28:56 +03:00
self.updateViews(newData: initialData)
2016-07-21 20:28:58 +03:00
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2016-09-25 18:50:33 +03:00
fileprivate func set(data: AppearancePrefData) {
2016-09-11 15:28:56 +03:00
self.data = data
self.publish(event: data)
}
2016-09-25 18:50:33 +03:00
override func subscription(source: Observable<Any>) -> Disposable {
return source
2016-07-27 00:40:20 +03:00
.filter { $0 is PrefData }
.map { ($0 as! PrefData).appearance }
2016-08-14 22:46:03 +03:00
.filter { [unowned self] data in data != self.data }
2016-09-25 19:10:07 +03:00
.subscribe(onNext: { [unowned self] data in
2016-09-11 15:28:56 +03:00
self.data = data
2016-09-25 19:10:07 +03:00
})
2016-07-27 00:40:20 +03:00
}
override func addViews() {
let paneTitle = self.paneTitleTextField(title: "Appearance")
let fontTitle = self.titleTextField(title: "Default Font:")
2016-07-21 20:28:58 +03:00
let fontPopup = self.fontPopup
fontPopup.translatesAutoresizingMaskIntoConstraints = false
fontPopup.target = self
2016-07-21 20:28:58 +03:00
fontPopup.action = #selector(AppearancePrefPane.fontPopupAction)
2016-09-25 18:50:33 +03:00
fontPopup.addItems(withTitles: self.fontManager.availableFontNames(with: .fixedPitchFontMask)!)
let sizeCombo = self.sizeCombo
2016-09-25 18:50:33 +03:00
sizeCombo.delegate = self
sizeCombo.target = self
sizeCombo.action = #selector(AppearancePrefPane.sizeComboBoxDidEnter(_:))
2016-07-21 20:28:58 +03:00
self.sizes.forEach { string in
2016-09-25 18:50:33 +03:00
sizeCombo.addItem(withObjectValue: string)
2016-07-21 20:28:58 +03:00
}
2016-10-27 00:37:51 +03:00
let linespacingTitle = self.titleTextField(title: "Line Spacing:")
let linespacingField = self.linespacingField
2016-07-30 23:06:42 +03:00
let ligatureCheckbox = self.ligatureCheckbox
self.configureCheckbox(button: ligatureCheckbox,
title: "Use Ligatures",
action: #selector(AppearancePrefPane.usesLigaturesAction(_:)))
2016-07-21 20:28:58 +03:00
let previewArea = self.previewArea
2016-09-25 18:50:33 +03:00
previewArea.isEditable = true
previewArea.maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
previewArea.isVerticallyResizable = true
previewArea.isHorizontallyResizable = true
previewArea.textContainer?.heightTracksTextView = false
previewArea.textContainer?.widthTracksTextView = false
2016-09-25 18:50:33 +03:00
previewArea.autoresizingMask = [ .viewWidthSizable, .viewHeightSizable]
previewArea.textContainer?.containerSize = CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
previewArea.layoutManager?.replaceTextStorage(NSTextStorage(string: self.exampleText))
2016-09-25 18:50:33 +03:00
previewArea.isRichText = false
previewArea.turnOffLigatures(self)
let previewScrollView = NSScrollView(forAutoLayout: ())
previewScrollView.hasVerticalScroller = true
previewScrollView.hasHorizontalScroller = true
previewScrollView.autohidesScrollers = true
2016-09-25 18:50:33 +03:00
previewScrollView.borderType = .bezelBorder
previewScrollView.documentView = previewArea
self.addSubview(paneTitle)
self.addSubview(fontTitle)
self.addSubview(fontPopup)
self.addSubview(sizeCombo)
2016-10-27 00:37:51 +03:00
self.addSubview(linespacingTitle)
self.addSubview(linespacingField)
self.addSubview(ligatureCheckbox)
self.addSubview(previewScrollView)
2016-09-25 18:50:33 +03:00
paneTitle.autoPinEdge(toSuperviewEdge: .top, withInset: 18)
paneTitle.autoPinEdge(toSuperviewEdge: .left, withInset: 18)
2016-10-27 00:37:51 +03:00
fontTitle.autoPinEdge(toSuperviewEdge: .left, withInset: 18, relation: .greaterThanOrEqual)
2016-09-25 18:50:33 +03:00
fontTitle.autoAlignAxis(.baseline, toSameAxisOf: fontPopup)
2016-09-25 18:50:33 +03:00
fontPopup.autoPinEdge(.top, to: .bottom, of: paneTitle, withOffset: 18)
fontPopup.autoPinEdge(.left, to: .right, of: fontTitle, withOffset: 5)
fontPopup.autoSetDimension(.width, toSize: 240)
2016-09-25 18:50:33 +03:00
sizeCombo.autoSetDimension(.width, toSize: 60)
2016-07-21 20:28:58 +03:00
// If we use .Baseline the combo box is placed one pixel off...
2016-09-25 18:50:33 +03:00
sizeCombo.autoAlignAxis(.horizontal, toSameAxisOf: fontPopup)
sizeCombo.autoPinEdge(.left, to: .right, of: fontPopup, withOffset: 5)
2016-08-14 15:29:50 +03:00
2016-10-27 00:37:51 +03:00
linespacingTitle.autoPinEdge(toSuperviewEdge: .left, withInset: 18, relation: .greaterThanOrEqual)
linespacingTitle.autoPinEdge(.right, to: .right, of: fontTitle)
linespacingTitle.autoAlignAxis(.baseline, toSameAxisOf: linespacingField)
linespacingField.autoPinEdge(.top, to: .bottom, of: sizeCombo, withOffset: 18)
linespacingField.autoPinEdge(.left, to: .right, of: linespacingTitle, withOffset: 5)
linespacingField.autoSetDimension(.width, toSize: 60)
2016-10-27 01:26:04 +03:00
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSControlTextDidEndEditing,
object: linespacingField,
queue: nil)
{ [unowned self] _ in
self.linespacingAction()
}
2016-10-27 00:37:51 +03:00
ligatureCheckbox.autoPinEdge(.top, to: .bottom, of: linespacingField, withOffset: 18)
2016-09-25 18:50:33 +03:00
ligatureCheckbox.autoPinEdge(.left, to: .right, of: fontTitle, withOffset: 5)
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
previewScrollView.autoSetDimension(.height, toSize: 200, relation: .greaterThanOrEqual)
previewScrollView.autoPinEdge(.top, to: .bottom, of: ligatureCheckbox, withOffset: 18)
previewScrollView.autoPinEdge(toSuperviewEdge: .right, withInset: 18)
previewScrollView.autoPinEdge(toSuperviewEdge: .bottom, withInset: 18)
previewScrollView.autoPinEdge(toSuperviewEdge: .left, withInset: 18)
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
self.fontPopup.selectItem(withTitle: self.data.editorFont.fontName)
2016-08-14 22:46:03 +03:00
self.sizeCombo.stringValue = String(Int(self.data.editorFont.pointSize))
self.ligatureCheckbox.state = self.data.editorUsesLigatures ? NSOnState : NSOffState
self.previewArea.font = self.data.editorFont
if self.data.editorUsesLigatures {
self.previewArea.useAllLigatures(self)
} else {
self.previewArea.turnOffLigatures(self)
}
}
2016-08-14 22:46:03 +03:00
2016-09-25 18:50:33 +03:00
fileprivate func updateViews(newData: AppearancePrefData) {
2016-08-14 22:46:03 +03:00
let newFont = newData.editorFont
2016-09-11 15:28:56 +03:00
2016-09-25 18:50:33 +03:00
self.fontPopup.selectItem(withTitle: newFont.fontName)
2016-09-11 15:28:56 +03:00
self.sizeCombo.stringValue = String(Int(newFont.pointSize))
2016-10-29 19:45:14 +03:00
self.linespacingField.stringValue = String(format: "%.2f", newData.editorLinespacing)
2016-09-11 15:28:56 +03:00
self.ligatureCheckbox.boolState = newData.editorUsesLigatures
self.previewArea.font = newData.editorFont
if newData.editorUsesLigatures {
self.previewArea.useAllLigatures(self)
} else {
self.previewArea.turnOffLigatures(self)
2016-08-14 22:46:03 +03:00
}
}
2016-10-27 09:21:29 +03:00
override func windowWillClose() {
self.linespacingAction()
}
}
2016-07-21 20:28:58 +03:00
// MARK: - Actions
extension AppearancePrefPane {
2016-07-30 23:06:42 +03:00
2016-09-25 18:50:33 +03:00
func usesLigaturesAction(_ sender: NSButton) {
self.set(data: AppearancePrefData(editorFont: self.data.editorFont,
2016-10-27 01:26:04 +03:00
editorLinespacing: self.data.editorLinespacing,
editorUsesLigatures: sender.boolState))
2016-07-30 23:06:42 +03:00
}
2016-07-21 20:28:58 +03:00
2016-09-25 18:50:33 +03:00
func fontPopupAction(_ sender: NSPopUpButton) {
2016-08-14 22:46:03 +03:00
guard let selectedItem = self.fontPopup.selectedItem else {
return
2016-07-21 20:28:58 +03:00
}
2016-09-25 18:50:33 +03:00
guard selectedItem.title != self.data.editorFont.fontName else {
2016-08-14 22:46:03 +03:00
return
}
guard let newFont = NSFont(name: selectedItem.title, size: self.data.editorFont.pointSize) else {
return
}
self.set(data: AppearancePrefData(editorFont: newFont,
2016-10-27 01:26:04 +03:00
editorLinespacing: self.data.editorLinespacing,
editorUsesLigatures: self.data.editorUsesLigatures))
2016-07-21 20:28:58 +03:00
}
2016-09-25 18:50:33 +03:00
func comboBoxSelectionDidChange(_ notification: Notification) {
guard (notification.object as! NSComboBox) === self.sizeCombo else {
return
}
2016-08-14 22:46:03 +03:00
let newFontSize = self.cappedFontSize(Int(self.sizes[self.sizeCombo.indexOfSelectedItem]))
2016-09-25 18:50:33 +03:00
let newFont = self.fontManager.convert(self.data.editorFont, toSize: newFontSize)
self.set(data: AppearancePrefData(editorFont: newFont,
2016-10-27 01:26:04 +03:00
editorLinespacing: self.data.editorLinespacing,
editorUsesLigatures: self.data.editorUsesLigatures))
2016-07-21 20:28:58 +03:00
}
2016-09-25 18:50:33 +03:00
func sizeComboBoxDidEnter(_ sender: AnyObject!) {
2016-08-14 22:46:03 +03:00
let newFontSize = self.cappedFontSize(self.sizeCombo.integerValue)
2016-09-25 18:50:33 +03:00
let newFont = self.fontManager.convert(self.data.editorFont, toSize: newFontSize)
self.set(data: AppearancePrefData(editorFont: newFont,
2016-10-27 01:26:04 +03:00
editorLinespacing: self.data.editorLinespacing,
editorUsesLigatures: self.data.editorUsesLigatures))
2016-07-21 20:28:58 +03:00
}
2016-10-27 01:26:04 +03:00
func linespacingAction() {
2016-10-27 09:21:29 +03:00
let newLinespacing = self.cappedLinespacing(self.linespacingField.floatValue)
self.set(data: AppearancePrefData(editorFont: self.data.editorFont,
editorLinespacing: newLinespacing,
editorUsesLigatures: self.data.editorUsesLigatures))
}
fileprivate func cappedLinespacing(_ linespacing: Float) -> CGFloat {
let cgfLinespacing = CGFloat(linespacing)
guard cgfLinespacing >= PrefStore.minEditorLinespacing else {
return PrefStore.defaultEditorLinespacing
}
guard cgfLinespacing <= PrefStore.maxEditorLinespacing else {
return PrefStore.maxEditorLinespacing
}
return cgfLinespacing
2016-10-27 01:26:04 +03:00
}
2016-09-25 18:50:33 +03:00
fileprivate func cappedFontSize(_ size: Int) -> CGFloat {
2016-10-27 09:21:29 +03:00
let cgfSize = CGFloat(size)
guard cgfSize >= PrefStore.minEditorFontSize else {
return PrefStore.defaultEditorFontSize
2016-07-21 20:28:58 +03:00
}
2016-10-27 09:21:29 +03:00
guard cgfSize <= PrefStore.maxEditorFontSize else {
return PrefStore.maxEditorFontSize
2016-07-21 20:28:58 +03:00
}
2016-10-27 09:21:29 +03:00
return cgfSize
}
}