Skip to content

Commit

Permalink
Refactor: remove duplicate code
Browse files Browse the repository at this point in the history
Rework StyledItem so there is no need to reimplement DrawText() in ProjectItem.
  • Loading branch information
jackburton79 committed Apr 23, 2024
1 parent 0d6d4db commit a345ded
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 52 deletions.
59 changes: 22 additions & 37 deletions src/project/ProjectItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,51 +172,36 @@ ProjectItem::DrawItem(BView* owner, BRect bounds, bool complete)
} else {
BPoint textPoint(iconRect.right + be_control_look->DefaultLabelSpacing(),
bounds.top + BaselineOffset());

// TODO: Apply any style change here (i.e. bold, italic)
BString text = Text();
if (fNeedsSave)
text.Append("*");
text.Append(ExtraText());
if (!isProject) {
if (fNeedsSave)
SetExtraText("*");
else
SetExtraText("");
}

DrawText(owner, text, textPoint);
if (isProject) {
// Fill background to the project color
ProjectFolder *projectFolder = static_cast<ProjectFolder*>(GetSourceItem());
const rgb_color oldColor = owner->HighColor();
owner->SetHighColor(projectFolder->Color());

BRect circleRect;
circleRect.top = textPoint.y - BaselineOffset() + 2.5f;
circleRect.left = textPoint.x - 3;
circleRect.bottom = textPoint.y + 6;
circleRect.right = circleRect.left + owner->StringWidth(Text()) + 5;
owner->FillRoundRect(circleRect, 9, 10);
owner->SetHighColor(oldColor);
}
DrawText(owner, Text(), ExtraText(), textPoint);

owner->Sync();
}
}


/* virtual */
void
ProjectItem::DrawText(BView* owner, const char* text, const BPoint& textPoint)
{
BFont font;
owner->GetFont(&font);
font.SetFace(TextFontFace());
owner->SetFont(&font);
owner->SetDrawingMode(B_OP_COPY);

if (GetSourceItem()->Type() == SourceItemType::ProjectFolderItem) {
ProjectFolder *projectFolder = static_cast<ProjectFolder*>(GetSourceItem());
const rgb_color oldColor = owner->HighColor();
owner->SetHighColor(projectFolder->Color());

// TODO: Fix these calculations.
// Better to change DrawText() to also pass a BRect, maybe
BRect circleRect;
circleRect.top = textPoint.y - BaselineOffset() + 2.5f;
circleRect.left = textPoint.x - 3;
circleRect.bottom = textPoint.y + 6;
circleRect.right = circleRect.left + owner->StringWidth(Text()) + 5;
owner->FillRoundRect(circleRect, 9, 10);
owner->SetHighColor(oldColor);
}
owner->MovePenTo(textPoint);
owner->DrawString(text);

owner->Sync();
}


void
ProjectItem::SetNeedsSave(bool needs)
{
Expand Down
1 change: 0 additions & 1 deletion src/project/ProjectItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class ProjectItem : public StyledItem {
virtual ~ProjectItem();

virtual void DrawItem(BView* owner, BRect bounds, bool complete);
virtual void DrawText(BView* owner, const char* text, const BPoint& textPoint);
virtual void Update(BView* owner, const BFont* font);

SourceItem *GetSourceItem() const { return fSourceItem; };
Expand Down
9 changes: 0 additions & 9 deletions src/ui/FunctionsOutlineView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,13 @@ class SymbolListItem: public StyledItem {
SetIconAndTooltip();
}

virtual void DrawItem(BView* owner, BRect bounds, bool complete);
const BMessage& Details() const { return fDetails; }
void SetIconAndTooltip();
private:
BMessage fDetails;
};


/* virtual */
void
SymbolListItem::DrawItem(BView* owner, BRect bounds, bool complete)
{
StyledItem::DrawItem(owner, bounds, complete);
}


void
SymbolListItem::SetIconAndTooltip()
{
Expand Down
9 changes: 5 additions & 4 deletions src/ui/StyledItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ StyledItem::DrawItem(BView* owner, BRect bounds, bool complete)
bounds.top + BaselineOffset());

// TODO: would be nice to draw extra text in different style
BString text(Text());
text << fExtraText;
DrawText(owner, text.String(), textPoint);
DrawText(owner, Text(), ExtraText(), textPoint);

owner->Sync();
}
Expand Down Expand Up @@ -166,7 +164,8 @@ StyledItem::SetIconFollowsTheme(bool follow)

/* virtual */
void
StyledItem::DrawText(BView* owner, const char* text, const BPoint& point)
StyledItem::DrawText(BView* owner, const char* text,
const char* extraText, const BPoint& point)
{
BFont font;
owner->GetFont(&font);
Expand All @@ -176,6 +175,8 @@ StyledItem::DrawText(BView* owner, const char* text, const BPoint& point)
owner->SetDrawingMode(B_OP_COPY);
owner->MovePenTo(point);
owner->DrawString(text);
if (extraText != nullptr)
owner->DrawString(extraText);
}


Expand Down
2 changes: 1 addition & 1 deletion src/ui/StyledItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class StyledItem : public BStringItem {
virtual BRect DrawIcon(BView* owner, const BRect& bounds,
const BBitmap* icon, float& iconSize);
virtual void DrawText(BView* owner, const char* text,
const BPoint& textPoint);
const char* extraText, const BPoint& textPoint);
private:
BString fIconName;
uint16 fFontFace;
Expand Down

0 comments on commit a345ded

Please sign in to comment.