From e1268d0d65f32a0a17aa0ab0a7bcf234b2ed036c Mon Sep 17 00:00:00 2001 From: ad Date: Thu, 26 Apr 2018 22:36:12 +0300 Subject: [PATCH] * NSButtonCell --- MTMR/CustomButtonTouchBarItem.swift | 25 ++++++++++++++++++++++++- MTMR/CustomSlider.swift | 16 ++++++++-------- MTMR/Widgets/InputSourceBarItem.swift | 3 ++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 671d20f..b068ef1 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -22,8 +22,11 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat super.init(identifier: identifier) button = NSButton(title: title, target: self, action: nil) + + button.cell = CustomButtonCell(backgroundColor: bezelColor!) + button.cell?.title = title + button.bezelColor = bezelColor - button.title = title self.view = button longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) @@ -82,3 +85,23 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat } } +class CustomButtonCell: NSButtonCell { + init(backgroundColor: NSColor) { + super.init(textCell: "") + if backgroundColor != .clear { + self.isBordered = true + self.bezelStyle = .rounded + self.backgroundColor = backgroundColor + } else { + self.isBordered = false + } + + self.font = NSFont.systemFont(ofSize: 15, weight: .regular ) + + } + + required init(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} + diff --git a/MTMR/CustomSlider.swift b/MTMR/CustomSlider.swift index 81aea36..621b6cb 100644 --- a/MTMR/CustomSlider.swift +++ b/MTMR/CustomSlider.swift @@ -49,19 +49,19 @@ class CustomSliderCell: NSSliderCell { override func drawBar(inside aRect: NSRect, flipped: Bool) { _barRect = aRect - var rect = aRect - rect.size.height = CGFloat(4) let barRadius = CGFloat(2) - let value = CGFloat((self.doubleValue - self.minValue) / (self.maxValue - self.minValue)) - let finalWidth = CGFloat(value * (self.controlView!.frame.size.width - 12)) + + var bgRect = aRect + bgRect.size.height = CGFloat(4) - var leftRect = rect - leftRect.size.width = finalWidth - let bg = NSBezierPath(roundedRect: rect, xRadius: barRadius, yRadius: barRadius) + let bg = NSBezierPath(roundedRect: bgRect, xRadius: barRadius, yRadius: barRadius) NSColor.lightGray.setFill() bg.fill() - let active = NSBezierPath(roundedRect: leftRect, xRadius: barRadius, yRadius: barRadius) + var activeRect = bgRect + + activeRect.size.width = CGFloat((Double(bgRect.size.width) / (self.maxValue - self.minValue)) * self.doubleValue) + let active = NSBezierPath(roundedRect: activeRect, xRadius: barRadius, yRadius: barRadius) NSColor.darkGray.setFill() active.fill() } diff --git a/MTMR/Widgets/InputSourceBarItem.swift b/MTMR/Widgets/InputSourceBarItem.swift index d131111..e71e41b 100644 --- a/MTMR/Widgets/InputSourceBarItem.swift +++ b/MTMR/Widgets/InputSourceBarItem.swift @@ -42,7 +42,8 @@ class InputSourceBarItem: CustomButtonTouchBarItem { } if (iconImage != nil) { - self.button.image = iconImage + self.button.imageScaling = .scaleProportionallyUpOrDown + self.button.cell?.image = iconImage } else { self.button.title = currentSource.name }