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.
//
#import "PopupButton.h"
#import <Cocoa/Cocoa.h>
#import "VolumeSlider.h"
@interface VolumeButton : PopupButton {
@interface VolumeButton : NSButton {
IBOutlet VolumeSlider *_popView;
}
@end

View File

@ -7,29 +7,45 @@
//
#import "VolumeButton.h"
#import "VolumeSlider.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
{
double change = [theEvent deltaY];
if ([popover isShown]) {
[_popView scrollWheel:theEvent];
return;
}
[(VolumeSlider *)_popView setDoubleValue:[(VolumeSlider *)_popView doubleValue] + change];
double change = [theEvent deltaY];
[[(VolumeSlider *)_popView target] changeVolume:_popView];
[_popView setDoubleValue:[_popView doubleValue] + change];
[(VolumeSlider *)_popView showToolTipForView:self closeAfter:1.0];
[[_popView target] changeVolume:_popView];
[_popView showToolTipForView:self closeAfter:1.0];
}
- (void)mouseDown:(NSEvent *)theEvent
{
[(VolumeSlider *)_popView hideToolTip];
[_popView hideToolTip];
[popover showRelativeToRect:self.bounds ofView:self preferredEdge:NSRectEdgeMaxY];
[super mouseDown:theEvent];
[(VolumeSlider *)_popView hideToolTip];
[super mouseDown:theEvent];
}

View File

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