[vlc-commits] macosx: time fields: Cache alternative time representation

David Fuhrmann git at videolan.org
Tue Apr 6 09:53:02 UTC 2021


vlc | branch: master | David Fuhrmann <dfuhrmann at videolan.org> | Tue Apr  6 11:46:12 2021 +0200| [497fc4216d776fc81d241578eaf9abd17dbeedcb] | committer: David Fuhrmann

macosx: time fields: Cache alternative time representation

This allows to toggle between remaining and elapsed
time at any time, also while the media is paused.

Close #25433

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=497fc4216d776fc81d241578eaf9abd17dbeedcb
---

 modules/gui/macosx/views/VLCTimeField.h            |  6 +--
 modules/gui/macosx/views/VLCTimeField.m            | 60 +++++++++++++++++-----
 .../windows/mainwindow/VLCControlsBarCommon.m      | 11 ++--
 .../macosx/windows/video/VLCFSPanelController.m    |  7 ++-
 4 files changed, 62 insertions(+), 22 deletions(-)

diff --git a/modules/gui/macosx/views/VLCTimeField.h b/modules/gui/macosx/views/VLCTimeField.h
index 7b4b37464b..6236197ab7 100644
--- a/modules/gui/macosx/views/VLCTimeField.h
+++ b/modules/gui/macosx/views/VLCTimeField.h
@@ -2,6 +2,7 @@
  * VLCTimeField.h: NSTextField subclass for playback time fields
  *****************************************************************************
  * Copyright (C) 2003-2017 VLC authors and VideoLAN
+ * $Id$
  *
  * Authors: Jon Lech Johansen <jon-vl at nanocrew.net>
  *          Felix Paul Kühne <fkuehne at videolan dot org>
@@ -34,8 +35,7 @@ extern NSString *VLCTimeFieldDisplayTimeAsRemaining;
 
 @interface VLCTimeField : NSTextField
 
- at property (readonly) BOOL timeRemaining;
-
-- (void)setRemainingIdentifier:(NSString *)o_string;
+- (void)setRemainingIdentifier:(NSString *)identifier;
+- (void)setTime:(NSString *)time withRemainingTime:(NSString *)remainingTime;
 
 @end
diff --git a/modules/gui/macosx/views/VLCTimeField.m b/modules/gui/macosx/views/VLCTimeField.m
index 578cb9b6f7..fccd62ddcc 100644
--- a/modules/gui/macosx/views/VLCTimeField.m
+++ b/modules/gui/macosx/views/VLCTimeField.m
@@ -2,6 +2,7 @@
  * VLCTimeField.m: NSTextField subclass for playback time fields
  *****************************************************************************
  * Copyright (C) 2003-2017 VLC authors and VideoLAN
+ * $Id$
  *
  * Authors: Jon Lech Johansen <jon-vl at nanocrew.net>
  *          Felix Paul Kühne <fkuehne at videolan dot org>
@@ -31,8 +32,11 @@ NSString *VLCTimeFieldDisplayTimeAsRemaining = @"DisplayTimeAsTimeRemaining";
 
 @interface VLCTimeField ()
 {
-    NSString *o_remaining_identifier;
-    BOOL b_time_remaining;
+    NSString *_identifier;
+    BOOL _isTimeRemaining;
+
+    NSString *_cachedTime;
+    NSString *_remainingTime;
 }
 @end
 
@@ -48,10 +52,10 @@ NSString *VLCTimeFieldDisplayTimeAsRemaining = @"DisplayTimeAsTimeRemaining";
 }
 
 
-- (void)setRemainingIdentifier:(NSString *)o_string
+- (void)setRemainingIdentifier:(NSString *)identifier
 {
-    o_remaining_identifier = o_string;
-    b_time_remaining = [[NSUserDefaults standardUserDefaults] boolForKey:o_remaining_identifier];
+    _identifier = identifier;
+    _isTimeRemaining = [[NSUserDefaults standardUserDefaults] boolForKey:_identifier];
 }
 
 - (void)mouseDown: (NSEvent *)ourEvent
@@ -60,24 +64,54 @@ NSString *VLCTimeFieldDisplayTimeAsRemaining = @"DisplayTimeAsTimeRemaining";
         [[[VLCMain sharedInstance] mainMenu] goToSpecificTime: nil];
     else
     {
-        if (o_remaining_identifier) {
-            b_time_remaining = [[NSUserDefaults standardUserDefaults] boolForKey:o_remaining_identifier];
-            b_time_remaining = !b_time_remaining;
-            [[NSUserDefaults standardUserDefaults] setObject:(b_time_remaining ? @"YES" : @"NO") forKey:o_remaining_identifier];
+        if (_identifier) {
+            _isTimeRemaining = [[NSUserDefaults standardUserDefaults] boolForKey:_identifier];
+            _isTimeRemaining = !_isTimeRemaining;
+            [[NSUserDefaults standardUserDefaults] setObject:(_isTimeRemaining ? @"YES" : @"NO") forKey:_identifier];
         } else {
-            b_time_remaining = !b_time_remaining;
+            _isTimeRemaining = !_isTimeRemaining;
         }
+
+        [self updateTimeValue];
     }
 
     [[self nextResponder] mouseDown:ourEvent];
 }
 
+- (void)setTime:(NSString *)time withRemainingTime:(NSString *)remainingTime
+{
+    _cachedTime = time;
+    _remainingTime = remainingTime;
+
+    [self updateTimeValue];
+}
+
+- (void)updateTimeValue
+{
+    if (!_cachedTime || !_remainingTime)
+        return;
+
+    if ([self timeRemaining]) {
+        [super setStringValue:_remainingTime];
+    } else {
+        [super setStringValue:_cachedTime];
+    }
+}
+
+- (void)setStringValue:(NSString *)stringValue
+{
+    [super setStringValue:stringValue];
+
+    _cachedTime = nil;
+    _remainingTime = nil;
+}
+
 - (BOOL)timeRemaining
 {
-    if (o_remaining_identifier)
-        return [[NSUserDefaults standardUserDefaults] boolForKey:o_remaining_identifier];
+    if (_identifier)
+        return [[NSUserDefaults standardUserDefaults] boolForKey:_identifier];
     else
-        return b_time_remaining;
+        return _isTimeRemaining;
 }
 
 @end
diff --git a/modules/gui/macosx/windows/mainwindow/VLCControlsBarCommon.m b/modules/gui/macosx/windows/mainwindow/VLCControlsBarCommon.m
index bd2c1fa2cc..997ba860d2 100644
--- a/modules/gui/macosx/windows/mainwindow/VLCControlsBarCommon.m
+++ b/modules/gui/macosx/windows/mainwindow/VLCControlsBarCommon.m
@@ -295,10 +295,13 @@
         [self.timeSlider setEnabled:_playerController.seekable];
     }
 
-    NSString *time = [NSString stringWithDuration:duration
-                                      currentTime:_playerController.time
-                                         negative:self.timeField.timeRemaining];
-    [self.timeField setStringValue:time];
+    NSString *timeString = [NSString stringWithDuration:duration
+                                            currentTime:_playerController.time
+                                               negative:NO];
+    NSString *remainingTime = [NSString stringWithDuration:duration
+                                               currentTime:_playerController.time
+                                                  negative:YES];
+    [self.timeField setTime:timeString withRemainingTime:remainingTime];
     [self.timeField setNeedsDisplay:YES];
 }
 
diff --git a/modules/gui/macosx/windows/video/VLCFSPanelController.m b/modules/gui/macosx/windows/video/VLCFSPanelController.m
index 96bf8c8f3e..3e32c679fe 100644
--- a/modules/gui/macosx/windows/video/VLCFSPanelController.m
+++ b/modules/gui/macosx/windows/video/VLCFSPanelController.m
@@ -335,8 +335,11 @@ static NSString *kAssociatedFullscreenRect = @"VLCFullscreenAssociatedWindowRect
     /* Update total duration (right field) */
     NSString *timeString = [NSString stringWithDuration:duration
                                             currentTime:time
-                                               negative:_remainingOrTotalTime.timeRemaining];
-    [_remainingOrTotalTime setStringValue:timeString];
+                                               negative:NO];
+    NSString *remainingTime = [NSString stringWithDuration:duration
+                                            currentTime:time
+                                               negative:YES];
+    [_remainingOrTotalTime setTime:timeString withRemainingTime:remainingTime];
     [_remainingOrTotalTime setNeedsDisplay:YES];
     [_remainingOrTotalTime setHidden:duration <= 0];
 



More information about the vlc-commits mailing list