[vlc-commits] [Git][videolan/vlc][master] 5 commits: macosx: Add function to add/increment a trailing number counter on a string
Felix Paul Kühne (@fkuehne)
gitlab at videolan.org
Sat Feb 8 10:43:00 UTC 2025
Felix Paul Kühne pushed to branch master at VideoLAN / VLC
Commits:
3967e469 by Claudio Cambra at 2025-02-08T09:57:42+00:00
macosx: Add function to add/increment a trailing number counter on a string
Signed-off-by: Claudio Cambra <developer at claudiocambra.com>
- - - - -
7326354f by Claudio Cambra at 2025-02-08T09:57:42+00:00
macosx: Remove messages if they starts match from status notifier view
Signed-off-by: Claudio Cambra <developer at claudiocambra.com>
- - - - -
358b6060 by Claudio Cambra at 2025-02-08T09:57:42+00:00
macosx: Rather than append identical message and duplicating presented messages, just show one message with count
Signed-off-by: Claudio Cambra <developer at claudiocambra.com>
- - - - -
bf60dc50 by Claudio Cambra at 2025-02-08T09:57:42+00:00
macosx: When a notification message is repeated, reset timeout
Signed-off-by: Claudio Cambra <developer at claudiocambra.com>
- - - - -
4d225c66 by Claudio Cambra at 2025-02-08T09:57:42+00:00
macosx: Move incrementTrailingNumber into extension method for NSString
Signed-off-by: Claudio Cambra <developer at claudiocambra.com>
- - - - -
3 changed files:
- modules/gui/macosx/extensions/NSString+Helpers.h
- modules/gui/macosx/extensions/NSString+Helpers.m
- modules/gui/macosx/views/VLCStatusNotifierView.m
Changes:
=====================================
modules/gui/macosx/extensions/NSString+Helpers.h
=====================================
@@ -149,6 +149,14 @@ NSImage *imageFromRes(NSString *name);
*/
- (NSString *)stringWrappedToWidth:(int)width;
+/**
+ Returns a new string with a trailing number in the format of "ORIGINALSTRING (2)"
+
+ If there is no existing trailing number in the string, a starting value of " (2)" will be appended.
+ If there is an existing trailing number in the string, the number value will be incremented.
+ */
+- (NSString *)stringWithIncrementedTrailingNumber;
+
@end
/**
=====================================
modules/gui/macosx/extensions/NSString+Helpers.m
=====================================
@@ -198,6 +198,38 @@ NSString *const kVLCMediaUnknown = @"Unknown";
return [NSString stringWithString:wrapped_string];
}
+- (NSString *)stringWithIncrementedTrailingNumber
+{
+ // Regular expression pattern to match a number in parentheses at the end of the string.
+ // \\( - matches '('
+ // (\\d+) - matches one or more digits
+ // \\) - matches ')'
+ // $ - position at the end of the string
+ NSError *error = nil;
+ NSRegularExpression * const regex =
+ [NSRegularExpression regularExpressionWithPattern:@"\\((\\d+)\\)$" options:0 error:&error];
+ if (error) {
+ NSLog(@"Regex creation error: %@", error);
+ return self;
+ }
+
+ const NSRange fullRange = NSMakeRange(0, self.length);
+ NSTextCheckingResult * const match = [regex firstMatchInString:self options:0 range:fullRange];
+
+ if (match == nil) {
+ return [self stringByAppendingString:@" (2)"];
+ }
+ // Extract the captured digits.
+ const NSRange numberRange = [match rangeAtIndex:1];
+ NSString * const numberString = [self substringWithRange:numberRange];
+ const NSInteger number = numberString.integerValue + 1;
+
+ // Replace the old number with the incremented number.
+ NSMutableString * const result = self.mutableCopy;
+ [result replaceCharactersInRange:numberRange withString:[NSString stringWithFormat:@"%ld", number]];
+ return result.copy;
+}
+
@end
#pragma mark -
=====================================
modules/gui/macosx/views/VLCStatusNotifierView.m
=====================================
@@ -28,6 +28,7 @@
NSString * const VLCStatusNotifierViewActivated = @"VLCStatusNotifierViewActivated";
NSString * const VLCStatusNotifierViewDeactivated = @"VLCStatusNotifierViewDeactivated";
+NSString * const VLCMessageTimeoutTimerUserInfoMessageKey = @"VLCMessageTimeoutTimerUserInfoMessageKey";
@interface VLCStatusNotifierView ()
@@ -35,6 +36,7 @@ NSString * const VLCStatusNotifierViewDeactivated = @"VLCStatusNotifierViewDeact
@property BOOL permanentDiscoveryMessageActive;
@property NSMutableSet<NSString *> *longNotifications;
@property NSMutableArray<NSString *> *messages;
+ at property NSMutableDictionary<NSString *, NSTimer *> *activeTimers;
@property (readonly) NSString *loadingLibraryItemsMessage;
@property (readonly) NSString *libraryItemsLoadedMessage;
@@ -57,6 +59,7 @@ NSString * const VLCStatusNotifierViewDeactivated = @"VLCStatusNotifierViewDeact
_libraryItemsLoadedMessage = _NS("Library items loaded");
_discoveringMediaMessage = _NS("Discovering media");
_discoveryCompletedMessage = _NS("Media discovery completed");
+ _activeTimers = NSMutableDictionary.dictionary;
self.label.stringValue = _NS("Idle");
self.progressIndicator.hidden = YES;
@@ -91,13 +94,26 @@ NSString * const VLCStatusNotifierViewDeactivated = @"VLCStatusNotifierViewDeact
if (self.messages.count == 0) {
[NSNotificationCenter.defaultCenter postNotificationName:VLCStatusNotifierViewActivated object:self];
}
- [self.messages addObject:message];
+ NSString *finalMessage = message;
+ const NSInteger matchingIndex = [self.messages indexOfObjectPassingTest:^BOOL(NSString * const string, NSUInteger, BOOL *){
+ return [string hasPrefix:message];
+ }];
+ if (matchingIndex != NSNotFound) {
+ finalMessage = [[self.messages objectAtIndex:matchingIndex] stringWithIncrementedTrailingNumber];
+ [self.messages removeObjectAtIndex:matchingIndex];
+ }
+ [self.messages addObject:finalMessage];
self.label.stringValue = [self.messages componentsJoinedByString:@"\n"];
}
- (void)removeMessage:(NSString *)message
{
- [self.messages removeObject:message];
+ const NSInteger matchingIndex = [self.messages indexOfObjectPassingTest:^BOOL(NSString * const string, NSUInteger, BOOL *){
+ return [string hasPrefix:message];
+ }];
+ if (matchingIndex != NSNotFound) {
+ [self.messages removeObjectAtIndex:matchingIndex];
+ }
if (self.messages.count == 0) {
[NSNotificationCenter.defaultCenter postNotificationName:VLCStatusNotifierViewDeactivated object:self];
return;
@@ -152,10 +168,31 @@ NSString * const VLCStatusNotifierViewDeactivated = @"VLCStatusNotifierViewDeact
}
}
+- (void)messageTimeout:(NSTimer *)timer
+{
+ NSString * const message =
+ [timer.userInfo objectForKey:VLCMessageTimeoutTimerUserInfoMessageKey];
+ [self removeMessage:message];
+ [self.activeTimers removeObjectForKey:message];
+}
+
- (void)presentTransientMessage:(NSString *)message
{
[self addMessage:message];
- [self performSelector:@selector(removeMessage:) withObject:message afterDelay:2.0];
+
+ NSTimer * const existingTimer = [self.activeTimers objectForKey:message];
+ if (existingTimer != nil) {
+ [existingTimer invalidate];
+ [self.activeTimers removeObjectForKey:message];
+ }
+
+ NSTimer * const newTimer =
+ [NSTimer scheduledTimerWithTimeInterval:2.0
+ target:self
+ selector:@selector(messageTimeout:)
+ userInfo:@{VLCMessageTimeoutTimerUserInfoMessageKey: message}
+ repeats:NO];
+ [self.activeTimers setObject:newTimer forKey:message];
}
@end
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/20be0a1a74688cd6b4f7628f919c8b568f0c1ad1...4d225c66e678656bd0b50f1461a452d4bfc5fe65
--
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/20be0a1a74688cd6b4f7628f919c8b568f0c1ad1...4d225c66e678656bd0b50f1461a452d4bfc5fe65
You're receiving this email because of your account on code.videolan.org.
VideoLAN code repository instance
More information about the vlc-commits
mailing list