From a345ded249eb9220daa6cb730f4606fe11333397 Mon Sep 17 00:00:00 2001 From: Jackburton79 Date: Tue, 23 Apr 2024 23:35:55 +0200 Subject: [PATCH] Refactor: remove duplicate code Rework StyledItem so there is no need to reimplement DrawText() in ProjectItem. --- src/project/ProjectItem.cpp | 59 ++++++++++++--------------------- src/project/ProjectItem.h | 1 - src/ui/FunctionsOutlineView.cpp | 9 ----- src/ui/StyledItem.cpp | 9 ++--- src/ui/StyledItem.h | 2 +- 5 files changed, 28 insertions(+), 52 deletions(-) diff --git a/src/project/ProjectItem.cpp b/src/project/ProjectItem.cpp index f74a1244..9dcb6c71 100644 --- a/src/project/ProjectItem.cpp +++ b/src/project/ProjectItem.cpp @@ -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(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(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) { diff --git a/src/project/ProjectItem.h b/src/project/ProjectItem.h index 3be77e11..5c86b97c 100644 --- a/src/project/ProjectItem.h +++ b/src/project/ProjectItem.h @@ -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; }; diff --git a/src/ui/FunctionsOutlineView.cpp b/src/ui/FunctionsOutlineView.cpp index d0745558..13252f4d 100755 --- a/src/ui/FunctionsOutlineView.cpp +++ b/src/ui/FunctionsOutlineView.cpp @@ -50,7 +50,6 @@ class SymbolListItem: public StyledItem { SetIconAndTooltip(); } - virtual void DrawItem(BView* owner, BRect bounds, bool complete); const BMessage& Details() const { return fDetails; } void SetIconAndTooltip(); private: @@ -58,14 +57,6 @@ class SymbolListItem: public StyledItem { }; -/* virtual */ -void -SymbolListItem::DrawItem(BView* owner, BRect bounds, bool complete) -{ - StyledItem::DrawItem(owner, bounds, complete); -} - - void SymbolListItem::SetIconAndTooltip() { diff --git a/src/ui/StyledItem.cpp b/src/ui/StyledItem.cpp index 79b92794..98b7cb12 100644 --- a/src/ui/StyledItem.cpp +++ b/src/ui/StyledItem.cpp @@ -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(); } @@ -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); @@ -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); } diff --git a/src/ui/StyledItem.h b/src/ui/StyledItem.h index 59200b83..ef8e2517 100644 --- a/src/ui/StyledItem.h +++ b/src/ui/StyledItem.h @@ -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;