Ticket #13592: 0001-Highlight-current-system-date-in-BCalendarView.patch

File 0001-Highlight-current-system-date-in-BCalendarView.patch, 13.1 KB (added by akshay, 7 years ago)
  • headers/private/shared/CalendarView.h

    From 7a24e5420e494dd609888b225f1ed7a8c9c852f5 Mon Sep 17 00:00:00 2001
    From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
    Date: Mon, 10 Jul 2017 05:48:36 +0530
    Subject: [PATCH] Highlight current system date in BCalendarView.
    
    * Issue: In BCalendarView presently, there is no notion of a current date
    and the current date is not highlighted. So in the deskbar tray calendar
    which uses BCalendarView, we cannot know the current date once we change
    the selected day.
    * Fix: Make BCalendarView accept pulse messages, check for system date
    with every pulse message and update the current date accordingly.
    Highlight the current date by rendering its day number text in a
    different color.
    ---
     headers/private/shared/CalendarView.h |  23 +++--
     src/kits/shared/CalendarView.cpp      | 159 +++++++++++++++++++++++++++++++---
     2 files changed, 167 insertions(+), 15 deletions(-)
    
    diff --git a/headers/private/shared/CalendarView.h b/headers/private/shared/CalendarView.h
    index d48c991..c644dfb 100644
    a b public:  
    2828                                BCalendarView(BRect frame, const char* name,
    2929                                    uint32 resizeMask = B_FOLLOW_LEFT_TOP,
    3030                                    uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
    31                                         | B_NAVIGABLE);
     31                                        | B_NAVIGABLE | B_PULSE_NEEDED);
    3232
    3333                                BCalendarView(const char* name,
    3434                                    uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS
    35                                         | B_NAVIGABLE);
     35                                        | B_NAVIGABLE | B_PULSE_NEEDED);
    3636
    3737    virtual                     ~BCalendarView();
    3838
    public:  
    4949
    5050    virtual void                DrawDay(BView* owner, BRect frame,
    5151                                    const char* text, bool isSelected = false,
    52                                     bool isEnabled = true, bool focus = false);
     52                                    bool isEnabled = true, bool focus = false,
     53                                    bool highlight = false);
    5354    virtual void                DrawDayName(BView* owner, BRect frame,
    5455                                    const char* text);
    5556    virtual void                DrawWeekNumber(BView* owner, BRect frame,
    public:  
    7071
    7172    virtual void                KeyDown(const char* bytes, int32 numBytes);
    7273
     74    virtual void                Pulse();
     75
    7376    virtual void                ResizeToPreferred();
    7477    virtual void                GetPreferredSize(float* width, float* height);
    7578
    private:  
    138141            void                _InitObject();
    139142
    140143            void                _SetToDay();
     144            void                _SetToCurrentDay();
    141145            void                _GetYearMonthForSelection(
    142146                                    const Selection& selection, int32* year,
    143147                                    int32* month) const;
    private:  
    154158            void                _DrawDay(int32 curRow, int32 curColumn,
    155159                                    int32 row, int32 column, int32 counter,
    156160                                    BRect frame, const char* text,
    157                                     bool focus = false);
     161                                    bool focus = false, bool highlight = false);
    158162            void                _DrawItem(BView* owner, BRect frame,
    159163                                    const char* text, bool isSelected = false,
    160                                     bool isEnabled = true, bool focus = false);
     164                                    bool isEnabled = true, bool focus = false,
     165                                    bool highlight = false);
    161166
    162167            void                _UpdateSelection();
     168            void                _UpdateCurrentDay();
     169            void                _UpdateCurrentDate();
     170
    163171            BRect               _FirstCalendarItemFrame() const;
    164172            BRect               _SetNewSelectedDay(const BPoint& where);
    165173
    private:  
    169177            BMessage*           fSelectionMessage;
    170178
    171179            BDate               fDate;
     180            BDate               fCurrentDate;
    172181
    173182            Selection           fFocusedDay;
    174183            Selection           fNewFocusedDay;
    private:  
    178187            Selection           fNewSelectedDay;
    179188            bool                fSelectionChanged;
    180189
     190            Selection           fCurrentDay;
     191            Selection           fNewCurrentDay;
     192            bool                fCurrentDayChanged;
     193
    181194            int32               fStartOfWeek;
    182195            bool                fDayNameHeaderVisible;
    183196            bool                fWeekNumberHeaderVisible;
  • src/kits/shared/CalendarView.cpp

    diff --git a/src/kits/shared/CalendarView.cpp b/src/kits/shared/CalendarView.cpp
    index 58c7fe2..a60aa6b 100644
    a b BCalendarView::BCalendarView(BRect frame, const char* name, uint32 resizeMask,  
    4242    BInvoker(),
    4343    fSelectionMessage(NULL),
    4444    fDate(),
     45    fCurrentDate(BDate::CurrentDate(B_LOCAL_TIME)),
    4546    fFocusChanged(false),
    4647    fSelectionChanged(false),
     48    fCurrentDayChanged(false),
    4749    fStartOfWeek((int32)B_WEEKDAY_MONDAY),
    4850    fDayNameHeaderVisible(true),
    4951    fWeekNumberHeaderVisible(true)
    BCalendarView::BCalendarView(const char* name, uint32 flags)  
    5860    BInvoker(),
    5961    fSelectionMessage(NULL),
    6062    fDate(),
     63    fCurrentDate(BDate::CurrentDate(B_LOCAL_TIME)),
    6164    fFocusChanged(false),
    6265    fSelectionChanged(false),
     66    fCurrentDayChanged(false),
    6367    fStartOfWeek((int32)B_WEEKDAY_MONDAY),
    6468    fDayNameHeaderVisible(true),
    6569    fWeekNumberHeaderVisible(true)
    BCalendarView::BCalendarView(BMessage* archive)  
    8084    BInvoker(),
    8185    fSelectionMessage(NULL),
    8286    fDate(archive),
     87    fCurrentDate(BDate::CurrentDate(B_LOCAL_TIME)),
    8388    fFocusChanged(false),
    8489    fSelectionChanged(false),
     90    fCurrentDayChanged(false),
    8591    fStartOfWeek((int32)B_WEEKDAY_MONDAY),
    8692    fDayNameHeaderVisible(true),
    8793    fWeekNumberHeaderVisible(true)
    BCalendarView::Draw(BRect updateRect)  
    185191            return;
    186192        }
    187193
     194        if (fCurrentDayChanged) {
     195            _UpdateCurrentDay();
     196            UnlockLooper();
     197            return;
     198        }
     199
    188200        _DrawDays();
    189201        _DrawDayHeader();
    190202        _DrawWeekHeader();
    BCalendarView::Draw(BRect updateRect)  
    200212
    201213void
    202214BCalendarView::DrawDay(BView* owner, BRect frame, const char* text,
    203     bool isSelected, bool isEnabled, bool focus)
     215    bool isSelected, bool isEnabled, bool focus, bool highlight)
    204216{
    205     _DrawItem(owner, frame, text, isSelected, isEnabled, focus);
     217    _DrawItem(owner, frame, text, isSelected, isEnabled, focus, highlight);
    206218}
    207219
    208220
    BCalendarView::KeyDown(const char* bytes, int32 numBytes)  
    464476
    465477
    466478void
     479BCalendarView::Pulse()
     480{
     481    _UpdateCurrentDate();
     482}
     483
     484
     485void
    467486BCalendarView::ResizeToPreferred()
    468487{
    469488    float width;
    BCalendarView::_SetToDay()  
    742761
    743762
    744763void
     764BCalendarView::_SetToCurrentDay()
     765{
     766    BDate date(fDate.Year(), fDate.Month(), 1);
     767    if (!date.IsValid())
     768        return;
     769
     770    const int32 firstDayOffset = (7 + date.DayOfWeek() - fStartOfWeek) % 7;
     771
     772    int32 day = 1 - firstDayOffset;
     773    for (int32 row = 0; row < 6; ++row) {
     774        for (int32 column = 0; column < 7; ++column) {
     775            if (day == fCurrentDate.Day()) {
     776                fNewCurrentDay.SetTo(row, column);
     777                return;
     778            }
     779            day++;
     780        }
     781    }
     782
     783    fNewCurrentDay.SetTo(0, 0);
     784}
     785
     786
     787void
    745788BCalendarView::_GetYearMonthForSelection(const Selection& selection,
    746789    int32* year, int32* month) const
    747790{
    BCalendarView::_SetupDayNumbers()  
    813856    fFocusedDay.SetTo(0, 0);
    814857    fSelectedDay.SetTo(0, 0);
    815858    fNewFocusedDay.SetTo(0, 0);
     859    fCurrentDay.SetTo(-1, -1);
    816860
    817861    const int32 daysInMonth = startOfMonth.DaysInMonth();
    818862    const int32 firstDayOffset
    BCalendarView::_SetupDayNumbers()  
    837881                fSelectedDay.SetTo(row, column);
    838882                fNewFocusedDay.SetTo(row, column);
    839883            }
     884            if (day == fCurrentDate.Day()
     885                && fDate.Month() == fCurrentDate.Month())
     886                fCurrentDay.SetTo(row, column);
    840887            counter++;
    841888            fDayNumbers[row][column].Truncate(0);
    842889            fDayNumbers[row][column] << day;
    BCalendarView::_SetupWeekNumbers()  
    862909
    863910void
    864911BCalendarView::_DrawDay(int32 currRow, int32 currColumn, int32 row,
    865     int32 column, int32 counter, BRect frame, const char* text, bool focus)
     912    int32 column, int32 counter, BRect frame, const char* text,
     913    bool focus, bool highlight)
    866914{
    867915    BDate startOfMonth(fDate.Year(), fDate.Month(), 1);
    868916    const int32 firstDayOffset
    BCalendarView::_DrawDay(int32 currRow, int32 currColumn, int32 row,  
    883931            enabled = false;    // days of month before or after
    884932    }
    885933
    886     DrawDay(this, frame, text, selected, enabled, focus);
     934    DrawDay(this, frame, text, selected, enabled, focus, highlight);
    887935}
    888936
    889937
    BCalendarView::_DrawDays()  
    899947    const int32 focusRow = fFocusedDay.row;
    900948    const int32 focusColumn = fFocusedDay.column;
    901949
     950    const int32 highlightRow = fCurrentDay.row;
     951    const int32 highlightColumn = fCurrentDay.column;
     952
    902953    int32 counter = 0;
    903954    for (int32 row = 0; row < 6; ++row) {
    904955        BRect tmp = frame;
    BCalendarView::_DrawDays()  
    906957            counter++;
    907958            const char* day = fDayNumbers[row][column].String();
    908959            bool focus = isFocus && focusRow == row && focusColumn == column;
     960            bool highlight = highlightRow == row && highlightColumn == column;
    909961            _DrawDay(currRow, currColumn, row, column, counter, tmp, day,
    910                 focus);
     962                focus, highlight);
    911963
    912964            tmp.OffsetBy(tmp.Width(), 0.0);
    913965        }
    BCalendarView::_DrawFocusRect()  
    927979    const int32 focusRow = fFocusedDay.row;
    928980    const int32 focusColumn = fFocusedDay.column;
    929981
     982    const int32 highlightRow = fCurrentDay.row;
     983    const int32 highlightColumn = fCurrentDay.column;
     984
    930985    int32 counter = 0;
    931986    for (int32 row = 0; row < 6; ++row) {
    932987        BRect tmp = frame;
    BCalendarView::_DrawFocusRect()  
    936991                fFocusedDay.SetTo(row, column);
    937992
    938993                bool focus = IsFocus() && true;
     994                bool highlight = highlightRow == row && highlightColumn == column;
    939995                const char* day = fDayNumbers[row][column].String();
    940996                _DrawDay(currRow, currColumn, row, column, counter, tmp, day,
    941                     focus);
     997                    focus, highlight);
    942998            } else if (focusRow == row && focusColumn == column) {
    943999                const char* day = fDayNumbers[row][column].String();
     1000                bool highlight = highlightRow == row && highlightColumn == column;
    9441001                _DrawDay(currRow, currColumn, row, column, counter, tmp, day,
    945                     false);
     1002                    false, highlight);
    9461003            }
    9471004            tmp.OffsetBy(tmp.Width(), 0.0);
    9481005        }
    BCalendarView::_DrawWeekHeader()  
    10101067
    10111068void
    10121069BCalendarView::_DrawItem(BView* owner, BRect frame, const char* text,
    1013     bool isSelected, bool isEnabled, bool focus)
     1070    bool isSelected, bool isEnabled, bool focus, bool isHighlight)
    10141071{
    10151072    rgb_color lColor = LowColor();
    10161073    rgb_color highColor = HighColor();
    BCalendarView::_DrawItem(BView* owner, BRect frame, const char* text,  
    10271084    } else
    10281085        SetHighColor(ui_color(B_LIST_BACKGROUND_COLOR));
    10291086
     1087    if (isHighlight) {
     1088        textColor = keyboard_navigation_color();
     1089    }
     1090
    10301091    SetLowColor(HighColor());
    10311092
    10321093    FillRect(frame.InsetByCopy(1.0, 1.0));
    BCalendarView::_UpdateSelection()  
    10621123    const int32 focusRow = fFocusedDay.row;
    10631124    const int32 focusColumn = fFocusedDay.column;
    10641125
     1126    const int32 highlightRow = fCurrentDay.row;
     1127    const int32 highlightColumn = fCurrentDay.column;
     1128
    10651129    int32 counter = 0;
    10661130    for (int32 row = 0; row < 6; ++row) {
    10671131        BRect tmp = frame;
    BCalendarView::_UpdateSelection()  
    10741138                const char* day = fDayNumbers[row][column].String();
    10751139                bool focus = IsFocus() && focusRow == row
    10761140                    && focusColumn == column;
    1077                 _DrawDay(row, column, row, column, counter, tmp, day, focus);
     1141                bool highlight = highlightRow == row && highlightColumn == column;
     1142                _DrawDay(row, column, row, column, counter, tmp, day, focus, highlight);
    10781143            } else if (currRow == row && currColumn == column) {
    10791144                const char* day = fDayNumbers[row][column].String();
    10801145                bool focus = IsFocus() && focusRow == row
    10811146                    && focusColumn == column;
    1082                 _DrawDay(currRow, currColumn, -1, -1, counter, tmp, day, focus);
     1147                bool highlight = highlightRow == row && highlightColumn == column;
     1148                _DrawDay(currRow, currColumn, -1, -1, counter, tmp, day, focus, highlight);
    10831149            }
    10841150            tmp.OffsetBy(tmp.Width(), 0.0);
    10851151        }
    BCalendarView::_UpdateSelection()  
    10881154}
    10891155
    10901156
     1157void
     1158BCalendarView::_UpdateCurrentDay()
     1159{
     1160    BRect frame = _FirstCalendarItemFrame();
     1161
     1162    const int32 selectRow = fSelectedDay.row;
     1163    const int32 selectColumn = fSelectedDay.column;
     1164
     1165    const int32 focusRow = fFocusedDay.row;
     1166    const int32 focusColumn = fFocusedDay.column;
     1167
     1168    const int32 currRow = fCurrentDay.row;
     1169    const int32 currColumn = fCurrentDay.column;
     1170
     1171    int32 counter = 0;
     1172    for (int32 row = 0; row < 6; ++row) {
     1173        BRect tmp = frame;
     1174        for (int32 column = 0; column < 7; ++column) {
     1175            counter++;
     1176            if (fNewCurrentDay.row == row
     1177                && fNewCurrentDay.column == column) {
     1178                fCurrentDay.SetTo(row, column);
     1179
     1180                const char* day = fDayNumbers[row][column].String();
     1181                bool focus = IsFocus() && focusRow == row
     1182                    && focusColumn == column;
     1183                bool isSelected = selectRow == row && selectColumn == column;
     1184                if (isSelected)
     1185                    _DrawDay(row, column, row, column, counter, tmp, day, focus, true);
     1186                else
     1187                    _DrawDay(row, column, -1, -1, counter, tmp, day, focus, true);
     1188
     1189            } else if (currRow == row && currColumn == column) {
     1190                const char* day = fDayNumbers[row][column].String();
     1191                bool focus = IsFocus() && focusRow == row
     1192                    && focusColumn == column;
     1193                bool isSelected = selectRow == row && selectColumn == column;
     1194                if(isSelected)
     1195                    _DrawDay(currRow, currColumn, row, column, counter, tmp, day, focus, false);
     1196                else
     1197                    _DrawDay(currRow, currColumn, -1, -1, counter, tmp, day, focus, false);
     1198            }
     1199            tmp.OffsetBy(tmp.Width(), 0.0);
     1200        }
     1201        frame.OffsetBy(0.0, frame.Height());
     1202    }
     1203}
     1204
     1205
     1206void
     1207BCalendarView::_UpdateCurrentDate()
     1208{
     1209    BDate date = BDate::CurrentDate(B_LOCAL_TIME);
     1210
     1211    if (!date.IsValid())
     1212        return;
     1213    if (date == fCurrentDate)
     1214        return;
     1215
     1216    fCurrentDate = date;
     1217
     1218    if (fDate.Year() == date.Year() && fDate.Month() == date.Month()) {
     1219        _SetToCurrentDay();
     1220        fCurrentDayChanged = true;
     1221        Draw(_RectOfDay(fCurrentDay));
     1222        Draw(_RectOfDay(fNewCurrentDay));
     1223        fCurrentDayChanged = false;
     1224    }
     1225
     1226    return;
     1227}
     1228
     1229
    10911230BRect
    10921231BCalendarView::_FirstCalendarItemFrame() const
    10931232{