Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: HaikuArchives/Calendar
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0d1ec0f566d9
Choose a base ref
...
head repository: HaikuArchives/Calendar
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8a2f5f4b08b8
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Nov 26, 2021

  1. Tint calendar highlight color per "busyness"

    Days with events are highlighted with a tinted color based on how many
    events are scheduled for that day, heatmap-style.
    Fixes #106
    JadedCtrl authored and humdinger committed Nov 26, 2021
    Copy the full SHA
    8a2f5f4 View commit details
Showing with 131 additions and 7 deletions.
  1. +118 −6 src/CalendarView.cpp
  2. +13 −1 src/CalendarView.h
124 changes: 118 additions & 6 deletions src/CalendarView.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* Copyright 2017 Jadedctrl
* Copyright 2007-2011, Haiku, Inc. All Rights Reserved.
* Copyright 2020-2021 Jaidyn Levesque, jadedctrl@teknik.io
* All rights reserved. Distributed under the terms of the MIT license.
* Authors:
* Julun <host.haiku@gmx.de>
*/

#include "CalendarView.h"
@@ -50,7 +53,7 @@ CalendarView::_Init ()

void
CalendarView::DrawDay (BView* owner, BRect frame, const char* text,
bool isSelected, bool isEnabled, bool focus, bool highlight)
bool isSelected, bool isEnabled, bool focus, bool isHighlight)
{
int drawnYear = Date().Year();
int drawnMonth = Date().Month();
@@ -61,11 +64,120 @@ CalendarView::DrawDay (BView* owner, BRect frame, const char* text,
if (isEnabled == 0 && drawnDay < 15) { drawnMonth++; }
if (drawnMonth < 1) { drawnMonth += 12; drawnYear--; }
if (drawnMonth > 12) { drawnMonth -= 12; drawnYear++; }

BDate drawnDate = BDate(Date().Year(), drawnMonth, drawnDay);
int currentEventsCount = fDBManager->GetEventsOfDay(drawnDate)->CountItems();

if (currentEventsCount > 0) { highlight = true; }
int eventCount = fDBManager->GetEventsOfDay(drawnDate)->CountItems();
if (isEnabled == false && eventCount != 0)
eventCount = 1;

rgb_color bgColor = ui_color(B_LIST_BACKGROUND_COLOR);
rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);

if (isSelected == true) {
bgColor = ui_color(B_LINK_ACTIVE_COLOR);
textColor = ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR);
} else if (eventCount > 0) {
bgColor = TintColor(bgColor, bgColor, eventCount);
textColor = ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR);
}

if (focus == true && isSelected == false)
textColor = keyboard_navigation_color();

if (isEnabled == false)
textColor = TintColor(textColor, textColor, 2);

// Tint in case of poor background/text contrast
float brightDiff = bgColor.Brightness() - textColor.Brightness();
if (brightDiff < 30 && brightDiff > -30)
textColor = TintColor(textColor, textColor, 2);

isHighlight = (eventCount > 0 && isEnabled == true);
_DrawItem(owner, frame, text, isHighlight, focus, bgColor, textColor);
}


void
CalendarView::_DrawItem(BView* owner, BRect frame, const char* text,
bool isHighlight, bool focus, rgb_color bgColor, rgb_color textColor)
{
rgb_color lColor = LowColor();
rgb_color highColor = HighColor();

SetHighColor(bgColor);
SetLowColor(bgColor);

FillRect(frame.InsetByCopy(1.0, 1.0));

BCalendarView::DrawDay(owner, frame, text, isSelected, isEnabled, focus, highlight);
if (focus) {
rgb_color focusColor = keyboard_navigation_color();
SetHighColor(focusColor);
StrokeRect(frame.InsetByCopy(1.0, 1.0));
}

SetHighColor(textColor);

float offsetH = frame.Width() / 2.0;
float offsetV = frame.Height() / 2.0 + FontHeight(owner) / 2.0 - 2.0;

BFont font(be_plain_font);
if (isHighlight)
font.SetFace(B_BOLD_FACE);
else
font.SetFace(B_REGULAR_FACE);
SetFont(&font);

DrawString(text, BPoint(frame.right - offsetH - StringWidth(text) / 2.0,
frame.top + offsetV));

SetLowColor(lColor);
SetHighColor(highColor);
}


rgb_color
TintColor(rgb_color color, rgb_color base, int severity)
{
bool dark = false;
if (base.Brightness() < 127)
dark = true;

switch (severity) {
case 0:
return color;
case 1:
if (dark == true)
return tint_color(color, B_LIGHTEN_1_TINT + 0.1f);
else
return tint_color(color, B_DARKEN_1_TINT);
case 2:
if (dark == true)
return tint_color(color, B_LIGHTEN_1_TINT);
else
return tint_color(color, B_DARKEN_2_TINT);
case 3: // intentional fallthrough
case 4:
if (dark == true)
return tint_color(color, B_LIGHTEN_2_TINT);
else
return tint_color(color, B_DARKEN_3_TINT);
default:
if (dark == true)
return tint_color(color, B_LIGHTEN_2_TINT - 0.1f);
else
return tint_color(color, B_DARKEN_4_TINT);
}
}


float
FontHeight(const BView* view)
{
if (!view)
return 0.0;
BFont font;
view->GetFont(&font);
font_height fheight;
font.GetHeight(&fheight);
return ceilf(fheight.ascent + fheight.descent + fheight.leading);
}
14 changes: 13 additions & 1 deletion src/CalendarView.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*
* Copyright 2017 Jadedctrl
* Copyright 2007-2011, Haiku, Inc. All Rights Reserved.
* Copyright 2020-2021 Jaidyn Levesque, jadedctrl@teknik.io
* All rights reserved. Distributed under the terms of the MIT license.
* Authors:
* Julun <host.haiku@gmx.de>
*/
#ifndef CALENDAR_VIEW_H
#define CALENDAR_VIEW_H
@@ -29,7 +32,16 @@ class CalendarView: public BCalendarView {
void DrawDay(BView*, BRect, const char*, bool, bool, bool, bool);
private:
void _Init();
void _DrawItem(BView* owner, BRect frame, const char* text,
bool isHighlight, bool focus, rgb_color bgColor,
rgb_color textColor);

QueryDBManager* fDBManager;
};


rgb_color TintColor(rgb_color color, rgb_color base, int severity);
float FontHeight(const BView* view);


#endif