cog/Window/VolumeSlider.m

152 lines
3.9 KiB
Matlab
Raw Normal View History

2009-02-09 05:53:38 +00:00
//
// VolumeSlider.m
// Cog
//
// Created by Vincent Spader on 2/8/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "VolumeSlider.h"
#import "PlaybackController.h"
2009-02-09 05:53:38 +00:00
#import "CogAudio/Helper.h"
2021-01-07 03:48:40 +00:00
@implementation VolumeSlider {
NSTimer *currentTimer;
BOOL wasInsideSnapRange;
2021-01-07 03:48:40 +00:00
}
2009-02-09 05:53:38 +00:00
- (id)initWithFrame:(NSRect)frame
{
2021-01-07 03:48:40 +00:00
self = [super initWithFrame:frame];
return self;
2009-02-09 05:53:38 +00:00
}
- (id)initWithCoder:(NSCoder *)coder
{
2021-01-07 03:48:40 +00:00
self = [super initWithCoder:coder];
return self;
}
- (void) awakeFromNib {
wasInsideSnapRange = NO;
2021-01-07 03:48:40 +00:00
textView = [[NSText alloc] init];
[textView setFrame:NSMakeRect(0, 0, 50, 20)];
textView.drawsBackground = NO;
textView.editable = NO;
textView.alignment = NSTextAlignmentCenter;
NSViewController * viewController = [[NSViewController alloc] init];
viewController.view = textView;
popover = [[NSPopover alloc] init];
popover.contentViewController = viewController;
// Don't hide the popover automatically.
popover.behavior = NSPopoverBehaviorTransient;
2021-01-07 03:48:40 +00:00
popover.animates = NO;
[popover setContentSize:textView.bounds.size];
2009-02-09 05:53:38 +00:00
}
- (void)updateToolTip
{
2021-01-07 03:48:40 +00:00
double value = [self doubleValue];
double volume = linearToLogarithmic(value);
NSString *text = [NSString stringWithFormat:@"%0.lf%%", volume];
[textView setString:text];
2009-02-09 05:53:38 +00:00
}
- (void)showToolTip
{
2021-01-07 03:48:40 +00:00
[self updateToolTip];
double progress = (self.maxValue - [self doubleValue]) / (self.maxValue - self.minValue);
CGFloat width = self.knobThickness - 1;
// Show tooltip to the left of the Slider Knob
CGFloat height = self.knobThickness / 2.f + (self.bounds.size.height - self.knobThickness) * progress - 1;
[popover showRelativeToRect:NSMakeRect(width, height, 2, 2) ofView:self preferredEdge:NSRectEdgeMaxX];
[self.window.parentWindow makeKeyWindow];
2009-02-09 05:53:38 +00:00
}
- (void)showToolTipForDuration:(NSTimeInterval)duration
{
2021-01-07 03:48:40 +00:00
[self showToolTip];
[self hideToolTipAfterDelay:duration];
2009-02-09 05:53:38 +00:00
}
2021-01-07 03:48:40 +00:00
- (void)showToolTipForView:(NSView *)view closeAfter:(NSTimeInterval)duration
{
[self updateToolTip];
[popover showRelativeToRect:view.bounds ofView:view preferredEdge:NSRectEdgeMaxY];
[self hideToolTipAfterDelay:duration];
}
2009-02-09 05:53:38 +00:00
- (void)hideToolTip
{
2021-01-07 03:48:40 +00:00
[popover close];
2009-02-09 05:53:38 +00:00
}
2021-01-07 03:48:40 +00:00
- (void) hideToolTipAfterDelay:(NSTimeInterval)duration
{
if (currentTimer)
{
[currentTimer invalidate];
currentTimer = nil;
}
if (duration > 0.0) {
currentTimer = [NSTimer scheduledTimerWithTimeInterval:duration
target:self
selector:@selector(hideToolTip)
userInfo:nil
repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:currentTimer forMode:NSRunLoopCommonModes];
}
}
2009-02-09 05:53:38 +00:00
- (BOOL)sendAction:(SEL)theAction to:(id)theTarget
{
2021-01-07 03:48:40 +00:00
// Snap to 100% if value is close
double snapTarget = logarithmicToLinear(100.0);
double snapProgress = ([self doubleValue] - snapTarget) / (self.maxValue - self.minValue);
2021-01-07 03:48:40 +00:00
if (fabs(snapProgress) < 0.005)
{
[self setDoubleValue:snapTarget];
if (!wasInsideSnapRange)
{
if (@available(macOS 10.11, *))
{
[[NSHapticFeedbackManager defaultPerformer] performFeedbackPattern:NSHapticFeedbackPatternGeneric
performanceTime:NSHapticFeedbackPerformanceTimeDefault];
}
}
wasInsideSnapRange = YES;
}
else
{
wasInsideSnapRange = NO;
2021-01-07 03:48:40 +00:00
}
[self showToolTip];
return [super sendAction:theAction to:theTarget];
2009-02-09 05:53:38 +00:00
}
- (void)scrollWheel:(NSEvent *)theEvent
{
2021-01-07 03:48:40 +00:00
double change = [theEvent deltaY];
[self setDoubleValue:[self doubleValue] + change];
[[self target] changeVolume:self];
[self showToolTipForDuration:1.0];
}
2009-02-09 05:53:38 +00:00
@end