From 12f9220e347177e41afd9b36c79740d03464aee8 Mon Sep 17 00:00:00 2001 From: Serg Date: Sat, 14 Apr 2018 01:58:07 +0700 Subject: [PATCH] implement scrolling view at the center --- MTMR.xcodeproj/project.pbxproj | 4 ++++ MTMR/ScrollViewItem.swift | 19 ++++++++++++++++++ MTMR/TouchBarController.swift | 35 ++++++++++++++++++++-------------- README.md | 2 +- 4 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 MTMR/ScrollViewItem.swift diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index 304bc2d..fac7ea6 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -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 = ""; }; 36BDC22F207CDA8600FCFEBE /* TECHNICAL_DEBT.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TECHNICAL_DEBT.md; sourceTree = ""; }; 36C2ECD2207B3B1D003CDA33 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeTouchBarItem.swift; sourceTree = ""; }; @@ -160,6 +162,7 @@ 36C2ECDF207CB1B0003CDA33 /* defaultPreset.json */, 6027D1B72080E52A004FFDC7 /* BrightnessViewController.swift */, 6027D1B82080E52A004FFDC7 /* VolumeViewController.swift */, + 368EDDE620812A1D00E10953 /* ScrollViewItem.swift */, ); path = MTMR; sourceTree = ""; @@ -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; }; diff --git a/MTMR/ScrollViewItem.swift b/MTMR/ScrollViewItem.swift new file mode 100644 index 0000000..2ac11de --- /dev/null +++ b/MTMR/ScrollViewItem.swift @@ -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") + } + +} diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 7a37084..c2cb914 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -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,9 +47,10 @@ 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() SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in @@ -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 } } diff --git a/README.md b/README.md index 19952fc..01d38b3 100644 --- a/README.md +++ b/README.md @@ -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/)