[haiku-commits] haiku: hrev51580 - src/apps/deskbar

  • From: pulkomandy@xxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 21 Nov 2017 15:07:09 +0100 (CET)

hrev51580 adds 1 changeset to branch 'master'
old head: b06e48bb2b13b4d9e34da29b5e5f20e70a510b93
new head: 8c82d0edd5f7fc4996c8aebeb8870aa0ca224d12
overview: 
http://cgit.haiku-os.org/haiku/log/?qt=range&q=8c82d0edd5f7+%5Eb06e48bb2b13

----------------------------------------------------------------------------

8c82d0edd5f7: DeskBar: cache time and date formats
  
  Creating BDateTimeFormat and BDateFormat objects is a costly operation
  (it loads locale data from ICU, etc). So, we should do it only once when
  the format changes, instead of doing it each time we format a date
  or time.

                             [ Adrien Destugues <pulkomandy@xxxxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev51580
Commit:      8c82d0edd5f7fc4996c8aebeb8870aa0ca224d12
URL:         http://cgit.haiku-os.org/haiku/commit/?id=8c82d0edd5f7
Author:      Adrien Destugues <pulkomandy@xxxxxxxxxxxxx>
Date:        Tue Nov 21 14:03:17 2017 UTC

----------------------------------------------------------------------------

2 files changed, 29 insertions(+), 13 deletions(-)
src/apps/deskbar/TimeView.cpp | 38 ++++++++++++++++++++++++++------------
src/apps/deskbar/TimeView.h   |  4 +++-

----------------------------------------------------------------------------

diff --git a/src/apps/deskbar/TimeView.cpp b/src/apps/deskbar/TimeView.cpp
index 4187a91..4fb05fa 100644
--- a/src/apps/deskbar/TimeView.cpp
+++ b/src/apps/deskbar/TimeView.cpp
@@ -74,7 +74,9 @@ TTimeView::TTimeView(float maxWidth, float height)
        fShowLevel(0),
        fShowSeconds(false),
        fShowDayOfWeek(false),
-       fShowTimeZone(false)
+       fShowTimeZone(false),
+       fTimeFormat(NULL),
+       fDateFormat(NULL)
 {
        fCurrentTime = fLastTime = time(NULL);
        fSeconds = fMinute = fHour = 0;
@@ -83,24 +85,28 @@ TTimeView::TTimeView(float maxWidth, float height)
        fLastTimeStr[0] = 0;
        fLastDateStr[0] = 0;
        fNeedToUpdate = true;
-       fLocale = *BLocale::Default();
+       UpdateTimeFormat();
 }
 
 
 #ifdef AS_REPLICANT
 TTimeView::TTimeView(BMessage* data)
-       : BView(data)
+       : BView(data),
+       fTimeFormat(NULL),
+       fDateFormat(NULL)
 {
        fCurrentTime = fLastTime = time(NULL);
        data->FindBool("seconds", &fShowSeconds);
 
-       fLocale = *BLocale::Default();
+       UpdateTimeFormat();
 }
 #endif
 
 
 TTimeView::~TTimeView()
 {
+       delete fTimeFormat;
+       delete fDateFormat;
 }
 
 
@@ -322,6 +328,7 @@ void
 TTimeView::SetShowSeconds(bool show)
 {
        fShowSeconds = show;
+       UpdateTimeFormat();
        Update();
 }
 
@@ -337,6 +344,7 @@ void
 TTimeView::SetShowDayOfWeek(bool show)
 {
        fShowDayOfWeek = show;
+       UpdateTimeFormat();
        Update();
 }
 
@@ -352,6 +360,7 @@ void
 TTimeView::SetShowTimeZone(bool show)
 {
        fShowTimeZone = show;
+       UpdateTimeFormat();
        Update();
 }
 
@@ -386,7 +395,7 @@ TTimeView::ShowCalendar(BPoint where)
 
 
 void
-TTimeView::GetCurrentTime()
+TTimeView::UpdateTimeFormat()
 {
        int32 fields = B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE;
        if (fShowSeconds)
@@ -396,10 +405,18 @@ TTimeView::GetCurrentTime()
        if (fShowTimeZone)
                fields |= B_DATE_ELEMENT_TIMEZONE;
 
-       BDateTimeFormat format(&fLocale);
-       format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, 
fields);
+       delete fTimeFormat;
+       fTimeFormat = new BDateTimeFormat(BLocale::Default());
+       fTimeFormat->SetDateTimeFormat(B_SHORT_DATE_FORMAT, 
B_SHORT_TIME_FORMAT, fields);
+
+       delete fDateFormat;
+       fDateFormat = new BDateFormat(BLocale::Default());
+}
 
-       format.Format(fCurrentTimeStr, sizeof(fCurrentTimeStr), fCurrentTime,
+void
+TTimeView::GetCurrentTime()
+{
+       fTimeFormat->Format(fCurrentTimeStr, sizeof(fCurrentTimeStr), 
fCurrentTime,
                B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT);
 }
 
@@ -409,8 +426,7 @@ TTimeView::GetCurrentDate()
 {
        char tmp[sizeof(fCurrentDateStr)];
 
-       BDateFormat format(&fLocale);
-       format.Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
+       fDateFormat->Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
                B_FULL_DATE_FORMAT);
 
        // remove leading 0 from date when month is less than 10 (MM/DD/YY)
@@ -476,8 +492,6 @@ TTimeView::ShowTimeOptions(BPoint point)
 void
 TTimeView::Update()
 {
-       fLocale = *BLocale::Default();
-
        GetCurrentTime();
        GetCurrentDate();
        SetToolTip(fCurrentDateStr);
diff --git a/src/apps/deskbar/TimeView.h b/src/apps/deskbar/TimeView.h
index bb66a42..415c388 100644
--- a/src/apps/deskbar/TimeView.h
+++ b/src/apps/deskbar/TimeView.h
@@ -114,6 +114,7 @@ private:
 
                                void                    GetCurrentTime();
                                void                    GetCurrentDate();
+                               void                    UpdateTimeFormat();
                                void                    
CalculateTextPlacement();
                                void                    ShowTimeOptions(BPoint);
                                void                    Update();
@@ -149,7 +150,8 @@ private:
                                BMessenger              fCalendarWindow;
 
                                // For date and time localization purposes
-                               BLocale                 fLocale;
+                               BDateTimeFormat* fTimeFormat;
+                               BDateFormat*    fDateFormat;
 };
 
 


Other related posts: