Merge pull request #69 from nevack/nevack/volume-slider-popover

Show Volume slider in NSPopover instead of CGWindow.
CQTexperiment
Christopher Snowhill 2021-01-07 15:36:03 -08:00 committed by GitHub
commit 0fdabd55dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 16 deletions

View File

@ -6,9 +6,11 @@
// Copyright 2009 __MyCompanyName__. All rights reserved. // Copyright 2009 __MyCompanyName__. All rights reserved.
// //
#import "PopupButton.h" #import <Cocoa/Cocoa.h>
#import "VolumeSlider.h"
@interface VolumeButton : PopupButton { @interface VolumeButton : NSButton {
IBOutlet VolumeSlider *_popView;
} }
@end @end

View File

@ -7,29 +7,45 @@
// //
#import "VolumeButton.h" #import "VolumeButton.h"
#import "VolumeSlider.h"
#import "PlaybackController.h" #import "PlaybackController.h"
@implementation VolumeButton @implementation VolumeButton {
NSPopover *popover;
}
- (void)awakeFromNib
{
popover = [[NSPopover alloc] init];
NSViewController * viewController = [[NSViewController alloc] init];
viewController.view = _popView;
popover.contentViewController = viewController;
popover.behavior = NSPopoverBehaviorTransient;
[popover setContentSize:_popView.bounds.size];
}
- (void)scrollWheel:(NSEvent *)theEvent - (void)scrollWheel:(NSEvent *)theEvent
{ {
if ([popover isShown]) {
[_popView scrollWheel:theEvent];
return;
}
double change = [theEvent deltaY]; double change = [theEvent deltaY];
[(VolumeSlider *)_popView setDoubleValue:[(VolumeSlider *)_popView doubleValue] + change]; [_popView setDoubleValue:[_popView doubleValue] + change];
[[(VolumeSlider *)_popView target] changeVolume:_popView]; [[_popView target] changeVolume:_popView];
[(VolumeSlider *)_popView showToolTipForView:self closeAfter:1.0]; [_popView showToolTipForView:self closeAfter:1.0];
} }
- (void)mouseDown:(NSEvent *)theEvent - (void)mouseDown:(NSEvent *)theEvent
{ {
[(VolumeSlider *)_popView hideToolTip]; [_popView hideToolTip];
[popover showRelativeToRect:self.bounds ofView:self preferredEdge:NSRectEdgeMaxY];
[super mouseDown:theEvent]; [super mouseDown:theEvent];
[(VolumeSlider *)_popView hideToolTip];
} }

View File

@ -12,6 +12,7 @@
@implementation VolumeSlider { @implementation VolumeSlider {
NSTimer *currentTimer; NSTimer *currentTimer;
BOOL wasInsideSnapRange;
} }
- (id)initWithFrame:(NSRect)frame - (id)initWithFrame:(NSRect)frame
@ -27,6 +28,7 @@
} }
- (void) awakeFromNib { - (void) awakeFromNib {
wasInsideSnapRange = NO;
textView = [[NSText alloc] init]; textView = [[NSText alloc] init];
[textView setFrame:NSMakeRect(0, 0, 50, 20)]; [textView setFrame:NSMakeRect(0, 0, 50, 20)];
textView.drawsBackground = NO; textView.drawsBackground = NO;
@ -39,7 +41,7 @@
popover = [[NSPopover alloc] init]; popover = [[NSPopover alloc] init];
popover.contentViewController = viewController; popover.contentViewController = viewController;
// Don't hide the popover automatically. // Don't hide the popover automatically.
popover.behavior = NSPopoverBehaviorApplicationDefined; popover.behavior = NSPopoverBehaviorTransient;
popover.animates = NO; popover.animates = NO;
[popover setContentSize:textView.bounds.size]; [popover setContentSize:textView.bounds.size];
} }
@ -111,9 +113,23 @@
// Snap to 100% if value is close // Snap to 100% if value is close
double snapTarget = logarithmicToLinear(100.0); double snapTarget = logarithmicToLinear(100.0);
double snapProgress = ([self doubleValue] - snapTarget) / (self.maxValue - self.minValue); double snapProgress = ([self doubleValue] - snapTarget) / (self.maxValue - self.minValue);
if (fabs(snapProgress) < 0.005) if (fabs(snapProgress) < 0.005)
{ {
[self setDoubleValue:snapTarget]; [self setDoubleValue:snapTarget];
if (!wasInsideSnapRange)
{
if (@available(macOS 10.11, *))
{
[[NSHapticFeedbackManager defaultPerformer] performFeedbackPattern:NSHapticFeedbackPatternGeneric
performanceTime:NSHapticFeedbackPerformanceTimeDefault];
}
}
wasInsideSnapRange = YES;
}
else
{
wasInsideSnapRange = NO;
} }
[self showToolTip]; [self showToolTip];