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

A bit better Slider UI

This commit is contained in:
Toxblh 2018-04-16 00:53:03 +01:00
parent 9d343a31d3
commit d15764d41d

View File

@ -27,27 +27,31 @@ class CustomSliderCell: NSSliderCell {
} }
override func drawKnob(_ knobRect: NSRect) { override func drawKnob(_ knobRect: NSRect) {
if (knobImage == nil) { if (knobImage == nil) {
super.drawKnob(knobRect) super.drawKnob(knobRect)
return; return;
} }
_currentKnobRect = knobRect; _currentKnobRect = knobRect;
drawBar(inside: _barRect, flipped: false) drawBar(inside: _barRect, flipped: true)
let newOriginX:CGFloat = knobRect.origin.x * let x = (knobRect.origin.x * (_barRect.size.width - (knobImage.size.width - knobRect.size.width)) / _barRect.size.width)+1;
(_barRect.size.width - (knobImage.size.width - knobRect.size.width)) / _barRect.size.width; let y = knobRect.origin.y+3
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) knobImage.draw(
at: NSPoint(x: x, y: y),
from: NSZeroRect,
operation: NSCompositingOperation.sourceOver,
fraction: 1
)
} }
override func drawBar(inside aRect: NSRect, flipped: Bool) { override func drawBar(inside aRect: NSRect, flipped: Bool) {
_barRect = aRect _barRect = aRect
var rect = aRect var rect = aRect
rect.size.height = CGFloat(3) rect.size.height = CGFloat(4)
let barRadius = CGFloat(3) let barRadius = CGFloat(2)
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 - 12)) let finalWidth = CGFloat(value * (self.controlView!.frame.size.width - 12))
@ -56,6 +60,7 @@ class CustomSliderCell: NSSliderCell {
let bg = NSBezierPath(roundedRect: rect, xRadius: barRadius, yRadius: barRadius) let bg = NSBezierPath(roundedRect: rect, xRadius: barRadius, yRadius: barRadius)
NSColor.lightGray.setFill() NSColor.lightGray.setFill()
bg.fill() bg.fill()
let active = NSBezierPath(roundedRect: leftRect, xRadius: barRadius, yRadius: barRadius) let active = NSBezierPath(roundedRect: leftRect, xRadius: barRadius, yRadius: barRadius)
NSColor.darkGray.setFill() NSColor.darkGray.setFill()
active.fill() active.fill()
@ -78,9 +83,9 @@ class CustomSlider:NSSlider {
} }
} }
convenience init(knob:NSImage?) { convenience init(knob:NSImage) {
self.init() self.init()
self.cell = CustomSliderCell(knob: knob!) self.cell = CustomSliderCell(knob: knob)
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {