mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
* image can be set for slider
This commit is contained in:
parent
3f2dad52ee
commit
37c2e8523c
@ -4,16 +4,17 @@ import AVFoundation
|
|||||||
import CoreAudio
|
import CoreAudio
|
||||||
|
|
||||||
class BrightnessViewController: NSCustomTouchBarItem {
|
class BrightnessViewController: NSCustomTouchBarItem {
|
||||||
private(set) var sliderItem: NSSlider!
|
private(set) var sliderItem: CustomSlider!
|
||||||
|
|
||||||
init(identifier: NSTouchBarItem.Identifier, refreshInterval: Double, image: NSImage? = nil) {
|
init(identifier: NSTouchBarItem.Identifier, refreshInterval: Double, image: NSImage? = nil) {
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
let brightness:Double = Double(getBrightness())
|
|
||||||
sliderItem = NSSlider(value: brightness*100.0, minValue: 0.0, maxValue: 100.0, target: self, action:#selector(BrightnessViewController.sliderValueChanged(_:)))
|
|
||||||
|
|
||||||
if (image != nil) {
|
sliderItem = CustomSlider(knob: image!)
|
||||||
sliderItem.cell = CustomSliderCell(knob: image!)
|
sliderItem.target = self
|
||||||
}
|
sliderItem.action = #selector(BrightnessViewController.sliderValueChanged(_:))
|
||||||
|
sliderItem.minValue = 0.0
|
||||||
|
sliderItem.maxValue = 100.0
|
||||||
|
sliderItem.floatValue = getBrightness()*100
|
||||||
|
|
||||||
self.view = sliderItem
|
self.view = sliderItem
|
||||||
|
|
||||||
|
|||||||
@ -80,11 +80,24 @@ class SupportedTypesHolder {
|
|||||||
let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), refreshInterval: interval ?? 1800.0)
|
let item = ItemType.appleScriptTitledButton(source: Source(filePath: scriptPath), refreshInterval: interval ?? 1800.0)
|
||||||
return (item: item, action: .none, parameters: [:])
|
return (item: item, action: .none, parameters: [:])
|
||||||
},
|
},
|
||||||
|
"volume": { decoder in
|
||||||
|
enum CodingKeys: String, CodingKey { case image }
|
||||||
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
|
if var img = try container.decodeIfPresent(Source.self, forKey: .image) {
|
||||||
|
return (item: .volume(), action: .none, parameters: [.image: .image(source: img)])
|
||||||
|
} else {
|
||||||
|
return (item: .volume(), action: .none, parameters: [:])
|
||||||
|
}
|
||||||
|
},
|
||||||
"brightness": { decoder in
|
"brightness": { decoder in
|
||||||
enum CodingKeys: String, CodingKey { case refreshInterval }
|
enum CodingKeys: String, CodingKey { case refreshInterval; case image }
|
||||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval)
|
||||||
|
if var img = try container.decodeIfPresent(Source.self, forKey: .image) {
|
||||||
|
return (item: .brightness(refreshInterval: interval ?? 0.5), action: .none, parameters: [.image: .image(source: img)])
|
||||||
|
} else {
|
||||||
return (item: .brightness(refreshInterval: interval ?? 0.5), action: .none, parameters: [:])
|
return (item: .brightness(refreshInterval: interval ?? 0.5), action: .none, parameters: [:])
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"battery": { decoder in
|
"battery": { decoder in
|
||||||
enum CodingKeys: String, CodingKey { case refreshInterval }
|
enum CodingKeys: String, CodingKey { case refreshInterval }
|
||||||
|
|||||||
@ -96,7 +96,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupControlStripPresence() {
|
@objc func setupControlStripPresence() {
|
||||||
DFRSystemModalShowsCloseBoxWhenFrontMost(false)
|
DFRSystemModalShowsCloseBoxWhenFrontMost(false)
|
||||||
let item = NSCustomTouchBarItem(identifier: .controlStripItem)
|
let item = NSCustomTouchBarItem(identifier: .controlStripItem)
|
||||||
item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar))
|
item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar))
|
||||||
@ -141,10 +141,18 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
case .timeButton(formatTemplate: let template):
|
case .timeButton(formatTemplate: let template):
|
||||||
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
||||||
case .volume:
|
case .volume:
|
||||||
|
if case .image(let source)? = item.additionalParameters[.image] {
|
||||||
|
barItem = VolumeViewController(identifier: identifier, image: source.image)
|
||||||
|
} else {
|
||||||
barItem = VolumeViewController(identifier: identifier)
|
barItem = VolumeViewController(identifier: identifier)
|
||||||
|
}
|
||||||
case .brightness(refreshInterval: let interval):
|
case .brightness(refreshInterval: let interval):
|
||||||
|
if case .image(let source)? = item.additionalParameters[.image] {
|
||||||
|
barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval, image: source.image)
|
||||||
|
} else {
|
||||||
barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval)
|
barItem = BrightnessViewController(identifier: identifier, refreshInterval: interval)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
||||||
widthBarItem.setWidth(value: value)
|
widthBarItem.setWidth(value: value)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,18 +4,10 @@ import AVFoundation
|
|||||||
import CoreAudio
|
import CoreAudio
|
||||||
|
|
||||||
class VolumeViewController: NSCustomTouchBarItem {
|
class VolumeViewController: NSCustomTouchBarItem {
|
||||||
private(set) var sliderItem: NSSlider!
|
private(set) var sliderItem: CustomSlider!
|
||||||
|
|
||||||
init(identifier: NSTouchBarItem.Identifier, image: NSImage? = nil) {
|
init(identifier: NSTouchBarItem.Identifier, image: NSImage? = nil) {
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
let volume:Double = Double(getInputGain())
|
|
||||||
sliderItem = NSSlider(value: volume*100, minValue: 0.0, maxValue: 100.0, target: self, action:#selector(VolumeViewController.sliderValueChanged(_:)))
|
|
||||||
|
|
||||||
if (image != nil) {
|
|
||||||
sliderItem.cell = CustomSliderCell(knob: image!)
|
|
||||||
}
|
|
||||||
|
|
||||||
self.view = sliderItem
|
|
||||||
|
|
||||||
var forPropertyAddress = AudioObjectPropertyAddress(
|
var forPropertyAddress = AudioObjectPropertyAddress(
|
||||||
mSelector: kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
|
mSelector: kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
|
||||||
@ -25,6 +17,15 @@ class VolumeViewController: NSCustomTouchBarItem {
|
|||||||
addListenerBlock(listenerBlock: audioObjectPropertyListenerBlock,
|
addListenerBlock(listenerBlock: audioObjectPropertyListenerBlock,
|
||||||
onAudioObjectID: defaultDeviceID,
|
onAudioObjectID: defaultDeviceID,
|
||||||
forPropertyAddress: &forPropertyAddress)
|
forPropertyAddress: &forPropertyAddress)
|
||||||
|
|
||||||
|
sliderItem = CustomSlider(knob: image!)
|
||||||
|
sliderItem.target = self
|
||||||
|
sliderItem.action = #selector(VolumeViewController.sliderValueChanged(_:))
|
||||||
|
sliderItem.minValue = 0.0
|
||||||
|
sliderItem.maxValue = 100.0
|
||||||
|
sliderItem.floatValue = getInputGain()*100
|
||||||
|
|
||||||
|
self.view = sliderItem
|
||||||
}
|
}
|
||||||
|
|
||||||
func addListenerBlock( listenerBlock: @escaping AudioObjectPropertyListenerBlock, onAudioObjectID: AudioObjectID, forPropertyAddress: UnsafePointer<AudioObjectPropertyAddress>) {
|
func addListenerBlock( listenerBlock: @escaping AudioObjectPropertyListenerBlock, onAudioObjectID: AudioObjectID, forPropertyAddress: UnsafePointer<AudioObjectPropertyAddress>) {
|
||||||
@ -137,16 +138,13 @@ class CustomSliderCell: NSSliderCell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_currentKnobRect = knobRect;
|
_currentKnobRect = knobRect;
|
||||||
drawBar(inside: _barRect, flipped: true)
|
drawBar(inside: _barRect, flipped: false)
|
||||||
self.controlView?.lockFocus()
|
self.controlView?.lockFocus()
|
||||||
|
|
||||||
knobImage.size.width = knobRect.size.width
|
|
||||||
knobImage.size.height = knobRect.size.height
|
|
||||||
|
|
||||||
let newOriginX:CGFloat = knobRect.origin.x *
|
let newOriginX:CGFloat = knobRect.origin.x *
|
||||||
(_barRect.size.width - (knobImage.size.width - knobRect.size.width)) / _barRect.size.width;
|
(_barRect.size.width - (knobImage.size.width - knobRect.size.width)) / _barRect.size.width;
|
||||||
|
|
||||||
knobImage.draw(at: NSPoint(x: newOriginX, y: knobRect.origin.y), from: NSRect(x: 0, y: 0, width: knobImage.size.width, height: knobImage.size.height), operation: NSCompositingOperation.sourceOver, fraction: 1)
|
knobImage.draw(at: NSPoint(x: newOriginX, y: knobRect.origin.y+3), from: NSRect(x: 0, y: 0, width: knobImage.size.width, height: knobImage.size.height), operation: NSCompositingOperation.sourceOver, fraction: 1)
|
||||||
|
|
||||||
self.controlView?.unlockFocus()
|
self.controlView?.unlockFocus()
|
||||||
|
|
||||||
@ -157,9 +155,10 @@ class CustomSliderCell: NSSliderCell {
|
|||||||
|
|
||||||
var rect = aRect
|
var rect = aRect
|
||||||
rect.size.height = CGFloat(3)
|
rect.size.height = CGFloat(3)
|
||||||
let barRadius = CGFloat(2.5)
|
let barRadius = CGFloat(3)
|
||||||
let value = CGFloat((self.doubleValue - self.minValue) / (self.maxValue - self.minValue))
|
let value = CGFloat((self.doubleValue - self.minValue) / (self.maxValue - self.minValue))
|
||||||
let finalWidth = CGFloat(value * (self.controlView!.frame.size.width - 8))
|
let finalWidth = CGFloat(value * (self.controlView!.frame.size.width - 12))
|
||||||
|
|
||||||
var leftRect = rect
|
var leftRect = rect
|
||||||
leftRect.size.width = finalWidth
|
leftRect.size.width = finalWidth
|
||||||
let bg = NSBezierPath(roundedRect: rect, xRadius: barRadius, yRadius: barRadius)
|
let bg = NSBezierPath(roundedRect: rect, xRadius: barRadius, yRadius: barRadius)
|
||||||
@ -170,3 +169,43 @@ class CustomSliderCell: NSSliderCell {
|
|||||||
active.fill()
|
active.fill()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CustomSlider:NSSlider {
|
||||||
|
|
||||||
|
var currentValue:CGFloat = 0
|
||||||
|
|
||||||
|
override func setNeedsDisplay(_ invalidRect: NSRect) {
|
||||||
|
super.setNeedsDisplay(invalidRect)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func awakeFromNib() {
|
||||||
|
super.awakeFromNib()
|
||||||
|
if ((self.cell?.isKind(of: CustomSliderCell.self)) == false) {
|
||||||
|
let cell:CustomSliderCell = CustomSliderCell()
|
||||||
|
self.cell = cell
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
convenience init(knob:NSImage) {
|
||||||
|
self.init()
|
||||||
|
self.cell = CustomSliderCell(knob: knob)
|
||||||
|
}
|
||||||
|
|
||||||
|
required init?(coder: NSCoder) {
|
||||||
|
super.init(coder: coder)
|
||||||
|
}
|
||||||
|
|
||||||
|
override init(frame frameRect: NSRect) {
|
||||||
|
super.init(frame: frameRect)
|
||||||
|
}
|
||||||
|
|
||||||
|
func knobImage() -> NSImage {
|
||||||
|
let cell = self.cell as! CustomSliderCell
|
||||||
|
return cell.knobImage
|
||||||
|
}
|
||||||
|
|
||||||
|
func setKnobImage(image:NSImage) {
|
||||||
|
let cell = self.cell as! CustomSliderCell
|
||||||
|
cell.knobImage = image
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user