Skip to content

Commit

Permalink
Fix the coloring for different system themes
Browse files Browse the repository at this point in the history
* Use B_LIST_SELECTED_ITEM_TEXT_COLOR when drawing text of a
  selected item.

* When calling GetMutedTint() to draw items of a closed account,
  it's not OK to check always against B_PANEL_BACKGROUND_COLOR
  to determine if to lighten or darken. We need to compare to the
  background colour the text will be drawn on or - vice-versa - the
  text colour that will be drawn on the background colour.

  Therefore, we call GetMutedTint() with the colour we want it have
  compared to.

* Added TODO to use Haiku's IsDark() once Haiku Beta 5 is out.

* Replace SetHighUIColor() with SetHighColor(), because the former
  can only deal with the system color_which constants, not rgb_color.

* Fixed "No memo" not being drawn tinted in closed accounts.
  • Loading branch information
humdingerb committed May 30, 2024
1 parent 349b6d8 commit 03db804
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
9 changes: 7 additions & 2 deletions src/AccountListItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ AccountListItem::DrawItem(BView* owner, BRect frame, bool complete)

// Draw account title
owner->SetFont(be_bold_font);
owner->SetHighUIColor(
B_LIST_ITEM_TEXT_COLOR, fAccount->IsClosed() ? GetMutedTint(CB_MUTED_TEXT) : B_NO_TINT);
if (IsSelected()) {
owner->SetHighUIColor(B_LIST_SELECTED_ITEM_TEXT_COLOR, fAccount->IsClosed()
? GetMutedTint(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR), CB_MUTED_TEXT) : B_NO_TINT);
} else {
owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, fAccount->IsClosed()
? GetMutedTint(ui_color(B_LIST_BACKGROUND_COLOR), CB_MUTED_TEXT) : B_NO_TINT);
}

BFont font;
owner->DrawString(fAccount->Name(), BPoint(frame.left + 5, frame.top + (font.Size())));
Expand Down
10 changes: 5 additions & 5 deletions src/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ LoadPreferences(const char* path)


bool
IsDark()
IsDark(rgb_color color)
{
rgb_color color = ui_color(B_PANEL_BACKGROUND_COLOR);
// From http://alienryderflex.com/hsp.html
// Useful in particular to decide if the color is "light" or "dark"
// by checking if the perceptual brightness is > 127.
// TODO: Once Haiku Beta5 is out, we can use its IsDark()

int32 brightness =
(uint8)roundf(sqrtf(0.299f * color.red * color.red + 0.587f * color.green * color.green +
Expand All @@ -79,16 +79,16 @@ IsDark()


const float
GetMutedTint(const CapitalBeMuted& type)
GetMutedTint(const rgb_color color, const CapitalBeMuted& type)
{
switch (type) {
case CB_MUTED_TEXT:
{
return IsDark() ? B_DARKEN_2_TINT : B_LIGHTEN_1_TINT;
return IsDark(color) ? B_DARKEN_2_TINT : B_LIGHTEN_1_TINT;
}
case CB_MUTED_BG:
{
return IsDark() ? B_LIGHTEN_2_TINT : B_DARKEN_1_TINT;
return IsDark(color) ? B_LIGHTEN_2_TINT : B_DARKEN_1_TINT;
}
default:
return B_NO_TINT;
Expand Down
2 changes: 1 addition & 1 deletion src/Preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ typedef enum {
} CapitalBeMuted;

rgb_color GetColor(const CapitalBeColor& color);
const float GetMutedTint(const CapitalBeMuted& type);
const float GetMutedTint(const rgb_color color, const CapitalBeMuted& type);

#endif
3 changes: 2 additions & 1 deletion src/ScheduledTransItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ ScheduledTransItem::DrawItem(BView* owner, BRect frame, bool complete)
owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR);
owner->DrawString(fMemo.String(), BPoint(xpos + 5, ypos - 3));
} else {
owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, GetMutedTint(CB_MUTED_TEXT));
owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR,
GetMutedTint(ui_color(B_LIST_ITEM_TEXT_COLOR), CB_MUTED_TEXT));
owner->DrawString(B_TRANSLATE("No memo"), BPoint(xpos + 5, ypos - 3));
}
owner->ConstrainClippingRegion(NULL);
Expand Down
50 changes: 32 additions & 18 deletions src/TransactionItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ TransactionItem::DrawItem(BView* owner, BRect frame, bool complete)
BString string;
Locale locale = fAccount->GetLocale();

float textTint = B_NO_TINT;
if (fAccount->IsClosed()) {
// Gray out all transactions from closed accounts
// Todo: disable editing of transactions from closed accounts
textTint = GetMutedTint(CB_MUTED_TEXT);
}

BRect r(frame);
r.right--;

Expand All @@ -59,7 +52,8 @@ TransactionItem::DrawItem(BView* owner, BRect frame, bool complete)
fStatus == TRANS_RECONCILED ? B_TOOL_TIP_BACKGROUND_COLOR : B_CONTROL_HIGHLIGHT_COLOR);
owner->StrokeRect(frame);
} else if (fStatus == TRANS_RECONCILED) {
owner->SetHighUIColor(B_MENU_BACKGROUND_COLOR, GetMutedTint(CB_MUTED_BG));
owner->SetHighUIColor(B_MENU_BACKGROUND_COLOR,
GetMutedTint(ui_color(B_MENU_BACKGROUND_COLOR), CB_MUTED_BG));
owner->FillRect(frame);
owner->SetHighUIColor(B_CONTROL_TEXT_COLOR);
owner->StrokeLine(r.LeftBottom(), r.RightBottom());
Expand All @@ -68,7 +62,22 @@ TransactionItem::DrawItem(BView* owner, BRect frame, bool complete)
owner->StrokeLine(r.LeftBottom(), r.RightBottom());
}

owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, textTint);
float textTint = B_NO_TINT;
rgb_color textColor;
if (IsSelected()) {
textColor = ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR);
if (fAccount->IsClosed()) {
// Gray out all transactions from closed accounts
// Todo: disable editing of transactions from closed accounts
textTint = GetMutedTint(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR), CB_MUTED_TEXT);
}
} else {
textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
if (fAccount->IsClosed())
textTint = GetMutedTint(ui_color(B_LIST_BACKGROUND_COLOR), CB_MUTED_TEXT);
}

owner->SetHighColor(tint_color(textColor, textTint));

BRect cliprect;
BRegion clip(cliprect);
Expand All @@ -94,7 +103,7 @@ TransactionItem::DrawItem(BView* owner, BRect frame, bool complete)
owner->StrokeLine(BPoint(xpos, ypos - TRowHeight()), BPoint(xpos, ypos));

// Type
owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, textTint);
owner->SetHighColor(tint_color(textColor, textTint));
owner->DrawString(fType.String(), BPoint(xpos + 5, ypos - 6));

// Line between Type and Payee
Expand All @@ -107,14 +116,13 @@ TransactionItem::DrawItem(BView* owner, BRect frame, bool complete)
// of the rectangle just yet
BRect payee_rect(xpos, ypos, xpos, ypos - TRowHeight());


// Balance
xpos = r.right - TAmountWidth();
cliprect.right = r.right;
cliprect.left = xpos;
clip = cliprect;

owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, textTint);
owner->SetHighColor(tint_color(textColor, textTint));
Fixed balance = fAccount->BalanceAtTransaction(fDate, fPayee.String());
if (balance.AsFixed() < 0)
owner->SetHighUIColor(B_FAILURE_COLOR, textTint);
Expand All @@ -130,7 +138,7 @@ TransactionItem::DrawItem(BView* owner, BRect frame, bool complete)
cliprect.right = cliprect.left;
cliprect.left = xpos;
clip = cliprect;
owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, textTint);
owner->SetHighColor(tint_color(textColor, textTint));
fAccount->GetLocale().CurrencyToString(fAmount.AbsoluteValue(), string);

owner->ConstrainClippingRegion(&clip);
Expand All @@ -147,7 +155,7 @@ TransactionItem::DrawItem(BView* owner, BRect frame, bool complete)
payee_rect.bottom = ypos;
xpos = payee_rect.left;

owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, textTint);
owner->SetHighColor(tint_color(textColor, textTint));
clip = payee_rect;
owner->ConstrainClippingRegion(&clip);
owner->DrawString(fPayee.String(), BPoint(xpos + 5, ypos - 6));
Expand All @@ -157,7 +165,7 @@ TransactionItem::DrawItem(BView* owner, BRect frame, bool complete)
owner->StrokeLine(BPoint(r.left, ypos), BPoint(r.right, ypos));

// Category
owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, textTint);
owner->SetHighColor(tint_color(textColor, textTint));
ypos += TRowHeight();
xpos = TLeftPadding();
cliprect.left = TLeftPadding();
Expand All @@ -180,13 +188,19 @@ TransactionItem::DrawItem(BView* owner, BRect frame, bool complete)
// Memo
clip = cliprect;
owner->ConstrainClippingRegion(&clip);

if (fMemo.CountChars() > 0) {
owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, textTint);
owner->SetHighColor(tint_color(textColor, textTint));
owner->DrawString(fMemo.String(), BPoint(xpos + 5, ypos - 6));
} else {
owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR, GetMutedTint(CB_MUTED_TEXT));
} else { // Always mute "No memo"
if (IsSelected())
textTint = GetMutedTint(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR), CB_MUTED_TEXT);
else
textTint = GetMutedTint(ui_color(B_LIST_BACKGROUND_COLOR), CB_MUTED_TEXT);
owner->SetHighColor(tint_color(textColor, textTint));
owner->DrawString(B_TRANSLATE("No memo"), BPoint(xpos + 5, ypos - 6));
}

owner->ConstrainClippingRegion(NULL);
}

Expand Down

0 comments on commit 03db804

Please sign in to comment.