1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 17:38:38 +00:00

implement scrolling view at the center

This commit is contained in:
Serg 2018-04-14 01:58:07 +07:00
parent ba753ff331
commit 12f9220e34
4 changed files with 45 additions and 15 deletions

View File

@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
368EDDE720812A1D00E10953 /* ScrollViewItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 368EDDE620812A1D00E10953 /* ScrollViewItem.swift */; };
36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */; };
36C2ECD9207B74B4003CDA33 /* AppleScriptTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */; };
36C2ECDB207C3FE7003CDA33 /* ItemsParsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECDA207C3FE7003CDA33 /* ItemsParsing.swift */; };
@ -49,6 +50,7 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
368EDDE620812A1D00E10953 /* ScrollViewItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollViewItem.swift; sourceTree = "<group>"; };
36BDC22F207CDA8600FCFEBE /* TECHNICAL_DEBT.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TECHNICAL_DEBT.md; sourceTree = "<group>"; };
36C2ECD2207B3B1D003CDA33 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeTouchBarItem.swift; sourceTree = "<group>"; };
@ -160,6 +162,7 @@
36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */,
6027D1B72080E52A004FFDC7 /* BrightnessViewController.swift */,
6027D1B82080E52A004FFDC7 /* VolumeViewController.swift */,
368EDDE620812A1D00E10953 /* ScrollViewItem.swift */,
);
path = MTMR;
sourceTree = "<group>";
@ -329,6 +332,7 @@
B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */,
36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */,
6027D1B92080E52A004FFDC7 /* BrightnessViewController.swift in Sources */,
368EDDE720812A1D00E10953 /* ScrollViewItem.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

19
MTMR/ScrollViewItem.swift Normal file
View File

@ -0,0 +1,19 @@
import Foundation
class ScrollViewItem: NSCustomTouchBarItem {
init(identifier: NSTouchBarItem.Identifier, items: [NSTouchBarItem]) {
super.init(identifier: identifier)
let views = items.flatMap { $0.view }
let stackView = NSStackView(views: views)
stackView.orientation = .horizontal
let scrollView = NSScrollView(frame: CGRect(origin: .zero, size: stackView.fittingSize))
scrollView.documentView = stackView
self.view = scrollView
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

View File

@ -36,6 +36,7 @@ extension ItemType {
extension NSTouchBarItem.Identifier {
static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip")
static let centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea")
}
class TouchBarController: NSObject, NSTouchBarDelegate {
@ -46,8 +47,9 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
var itemDefinitions: [NSTouchBarItem.Identifier: BarItemDefinition] = [:]
var items: [NSTouchBarItem.Identifier: NSTouchBarItem] = [:]
var orderedIdentifiers: [NSTouchBarItem.Identifier] = []
var leftIdentifiers: [NSTouchBarItem.Identifier] = []
var centerItems: [NSTouchBarItem] = []
var rightIdentifiers: [NSTouchBarItem.Identifier] = []
private override init() {
super.init()
@ -57,12 +59,12 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
loadItemDefinitions()
createItems()
centerItems = self.orderedIdentifiers.flatMap { identifier -> NSTouchBarItem? in
return itemDefinitions[identifier]?.centerAligned == true ? items[identifier] : nil
centerItems = self.itemDefinitions.flatMap { (identifier, definition) -> NSTouchBarItem? in
return definition.align == .center ? items[identifier] : nil
}
touchBar.delegate = self
touchBar.defaultItemIdentifiers = self.orderedIdentifiers
touchBar.defaultItemIdentifiers = self.leftIdentifiers + [.centerScrollArea] + self.rightIdentifiers
self.presentTouchBar()
}
@ -83,7 +85,12 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
? NSTouchBarItem.Identifier.flexibleSpace
: NSTouchBarItem.Identifier(identifierString)
itemDefinitions[identifier] = item
orderedIdentifiers.append(identifier)
if item.align == .left {
leftIdentifiers.append(identifier)
}
if item.align == .right {
rightIdentifiers.append(identifier)
}
}
}
@ -114,9 +121,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
}
func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
if identifier == .centerScrollArea {
return ScrollViewItem(identifier: identifier, items: centerItems)
}
guard let item = self.items[identifier],
let definition = self.itemDefinitions[identifier],
!definition.centerAligned else {
definition.align != .center else {
return nil
}
return item
@ -139,10 +150,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
barItem = VolumeViewController(identifier: identifier)
case .brightness:
barItem = BrightnessViewController(identifier: identifier)
// case .scrollArea:
// for item in centerItems {
// //todo:add item.view to scrollview
// }
}
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
widthBarItem.setWidth(value: value)
@ -202,10 +209,10 @@ extension NSCustomTouchBarItem: CanSetWidth {
}
extension BarItemDefinition {
var centerAligned: Bool {
var align: Align {
if case .align(let result)? = self.additionalParameters[.align] {
return result == .center
return result
}
return true
return .center
}
}

View File

@ -25,7 +25,7 @@ My the idea is to create the program like a platform for plugins for customizati
- [x] Status menu: "preferences", "quit"
- [x] JSON or another approch for save preset, maybe in `~/Library/Application Support/MTMR/`
- [x] Custom buttons size, actions by click
- [ ] Layout: [always left, NSSliderView for center, always right]
- [x] Layout: [always left, NSSliderView for center, always right]
- [ ] Overwrite default values from item types (e.g. title for brightness)
- [ ] System for autoupdate (maybe https://sparkle-project.org/)