Skip to content

Commit

Permalink
Feature: Add fractional track length tooltips
Browse files Browse the repository at this point in the history
Add fractional track length tooltips, for extra verbose info goodness.

Signed-off-by: Christopher Snowhill <[email protected]>
  • Loading branch information
kode54 committed Feb 27, 2025
1 parent d5ca037 commit b05f428
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Base.lproj/InfoInspector.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22154" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="23504" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22154"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="23504"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down Expand Up @@ -167,6 +167,7 @@
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<binding destination="-2" name="toolTip" keyPath="valueToDisplay.lengthInfo" id="26q-iJ-ecn"/>
<binding destination="-2" name="value" keyPath="valueToDisplay.lengthText" id="ji7-tL-8rb"/>
</connections>
</textField>
Expand Down
5 changes: 5 additions & 0 deletions Formatters/SecondsFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@
}

@end

@interface SecondsFractionFormatter : NSFormatter {
}

@end
80 changes: 80 additions & 0 deletions Formatters/SecondsFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,83 @@ - (NSAttributedString *)attributedStringForObjectValue:(id)object withDefaultAtt
}

@end

@implementation SecondsFractionFormatter

- (NSString *)stringForObjectValue:(id)object {
NSString *result = nil;
double value;
unsigned days = 0;
unsigned hours = 0;
unsigned minutes = 0;
float seconds = 0.0;

if(nil == object || NO == [object isKindOfClass:[NSNumber class]] || isnan([object doubleValue])) {
return @"";
}

value = [object doubleValue];

seconds = fmod(value, 60.0);
minutes = (unsigned)floor(value / 60.0);

while(60 <= minutes) {
minutes -= 60;
++hours;
}

while(24 <= hours) {
hours -= 24;
++days;
}

if(0 < days) {
result = [NSString stringWithFormat:@"%u:%.2u:%.2u:%06.3f", days, hours, minutes, seconds];
} else if(0 < hours) {
result = [NSString stringWithFormat:@"%u:%.2u:%06.3f", hours, minutes, seconds];
} else if(0 < minutes) {
result = [NSString stringWithFormat:@"%u:%06.3f", minutes, seconds];
} else {
result = [NSString stringWithFormat:@"0:%06.3f", seconds];
}

return result;
}

- (BOOL)getObjectValue:(id *)object forString:(NSString *)string errorDescription:(NSString **)error {
NSScanner *scanner = nil;
BOOL result = NO;
double value = 0.0;
double seconds = 0.0;

scanner = [NSScanner scannerWithString:string];

while(NO == [scanner isAtEnd]) {
// Grab a value
if([scanner scanDouble:&value]) {
seconds *= 60.0;
seconds += value;
result = YES;
}

// Grab the separator, if present
[scanner scanString:@":" intoString:NULL];
}

if(result && NULL != object) {
*object = @(seconds);
} else if(NULL != error) {
*error = @"Couldn't convert value to seconds";
}

return result;
}

- (NSAttributedString *)attributedStringForObjectValue:(id)object withDefaultAttributes:(NSDictionary *)attributes {
NSAttributedString *result = nil;

result = [[NSAttributedString alloc] initWithString:[self stringForObjectValue:object] attributes:attributes];
return result;
}

@end
6 changes: 6 additions & 0 deletions Playlist/PlaylistController.m
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ - (NSView *_Nullable)tableView:(NSTableView *)tableView viewForTableColumn:(NSTa
NSImage *cellImage = nil;
NSString *cellText = @"";
NSString *cellIdentifier = @"";
NSString *cellToolTip = nil;
NSTextAlignment cellTextAlignment = NSTextAlignmentLeft;

PlaylistEntry *pe = [[self arrangedObjects] objectAtIndex:row];
Expand Down Expand Up @@ -486,6 +487,7 @@ - (NSView *_Nullable)tableView:(NSTableView *)tableView viewForTableColumn:(NSTa
case 6:
cellText = pe.lengthText;
cellTextAlignment = NSTextAlignmentRight;
cellToolTip = pe.lengthInfo;
break;

case 7:
Expand Down Expand Up @@ -583,6 +585,10 @@ - (NSView *_Nullable)tableView:(NSTableView *)tableView viewForTableColumn:(NSTa
else
cellView.textField.toolTip = [pe statusMessage];

if(cellToolTip) {
cellView.textField.toolTip = cellToolTip;
}

NSRect cellFrameRect = cellView.textField.frame;
cellFrameRect.origin.y = 1;
cellFrameRect.size.height = frameRect.size.height;
Expand Down
2 changes: 2 additions & 0 deletions Playlist/PlaylistEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
+ (NSSet *_Nonnull)keyPathsForValuesAffectingAlbumArt;
+ (NSSet *_Nonnull)keyPathsForValuesAffectingTrackText;
+ (NSSet *_Nonnull)keyPathsForValuesAffectingLengthText;
+ (NSSet *_Nonnull)keyPathsForValuesAffectingLengthInfo;
+ (NSSet *_Nonnull)keyPathsForValuesAffectingYearText;
+ (NSSet *_Nonnull)keyPathsForValuesAffectingCuesheetPresent;
+ (NSSet *_Nonnull)keyPathsForValuesAffectingGainCorrection;
Expand All @@ -42,6 +43,7 @@
@property(nonatomic, readonly) NSString *_Nonnull positionText;

@property(nonatomic, readonly) NSString *_Nonnull lengthText;
@property(nonatomic, readonly) NSString *_Nonnull lengthInfo;

@property(nonatomic, readonly) NSString *_Nonnull yearText;

Expand Down
11 changes: 11 additions & 0 deletions Playlist/PlaylistEntry.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ + (NSSet *)keyPathsForValuesAffectingLengthText {
return [NSSet setWithObject:@"length"];
}

+ (NSSet *)keyPathsForValuesAffectingLengthInfo {
return [NSSet setWithObject:@"length"];
}

+ (NSSet *)keyPathsForValuesAffectingAlbumArt {
return [NSSet setWithObjects:@"albumArtInternal", @"artId", nil];
}
Expand Down Expand Up @@ -316,6 +320,13 @@ - (NSString *)lengthText {
return time;
}

@dynamic lengthInfo;
- (NSString *)lengthInfo {
SecondsFractionFormatter * secondsFormatter = [[SecondsFractionFormatter alloc] init];
NSString *time = [secondsFormatter stringForObjectValue:self.length];
return time;
}

@dynamic albumArt;
- (NSImage *)albumArt {
if(!self.albumArtInternal || ![self.albumArtInternal length]) return nil;
Expand Down

0 comments on commit b05f428

Please sign in to comment.