ProcessController: Utilize ComposeIconSize and refactor icon menu items.

* All MenuItem variants which draw icons now derive from IconMenuItem
   and use its functions to draw and otherwise manage their icons. This
   resolves a number of TODOs and reduces code duplication.

 * Use BControlLook::ComposeIconSize() to compose icon sizes throughout.

 * Remove unused methods from IconMenuItem.
This commit is contained in:
Augustin Cavalier
2022-08-31 17:07:49 -04:00
parent 72e4928f8a
commit 33cf9d22ff
8 changed files with 88 additions and 190 deletions
+16 -11
View File
@@ -7,6 +7,7 @@
#include "Utilities.h" #include "Utilities.h"
#include <Bitmap.h> #include <Bitmap.h>
#include <ControlLook.h>
#include <Entry.h> #include <Entry.h>
#include <MimeType.h> #include <MimeType.h>
#include <NodeInfo.h> #include <NodeInfo.h>
@@ -22,20 +23,24 @@ AutoIcon::~AutoIcon()
BBitmap* BBitmap*
AutoIcon::Bitmap() AutoIcon::Bitmap()
{ {
if (fBitmap == NULL) { if (fBitmap != NULL)
fBitmap = new BBitmap(BRect(0, 0, 15, 15), B_RGBA32); return fBitmap;
if (fSignature) { if (fSignature) {
entry_ref ref; fBitmap = new BBitmap(BRect(BPoint(0, 0),
be_roster->FindApp (fSignature, &ref); be_control_look->ComposeIconSize(B_MINI_ICON)), B_RGBA32);
if (BNodeInfo::GetTrackerIcon(&ref, fBitmap, B_MINI_ICON) != B_OK) {
BMimeType genericAppType(B_APP_MIME_TYPE); entry_ref ref;
genericAppType.GetIcon(fBitmap, B_MINI_ICON); be_roster->FindApp (fSignature, &ref);
} if (BNodeInfo::GetTrackerIcon(&ref, fBitmap, (icon_size)-1) != B_OK) {
BMimeType genericAppType(B_APP_MIME_TYPE);
genericAppType.GetIcon(fBitmap, (icon_size)(fBitmap->Bounds().IntegerWidth() + 1));
} }
} else if (fbits) {
fBitmap = new BBitmap(BRect(BPoint(0, 0),
BSize(B_MINI_ICON - 1, B_MINI_ICON - 1)), B_RGBA32);
if (fbits) fBitmap->SetBits(fbits, 256, 0, B_CMAP8);
fBitmap->SetBits(fbits, 256, 0, B_CMAP8);
} }
return fBitmap; return fBitmap;
} }
+32 -59
View File
@@ -1,63 +1,61 @@
/* /*
* Copyright 2000, Georges-Edouard Berenger. All rights reserved. * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
* Copyright 2022, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include "IconMenuItem.h" #include "IconMenuItem.h"
#include <Application.h>
#include <NodeInfo.h> #include <ControlLook.h>
#include <Bitmap.h> #include <Bitmap.h>
#include <Roster.h>
IconMenuItem::IconMenuItem(BBitmap* icon, const char* title, IconMenuItem::IconMenuItem(BBitmap* icon, const char* title,
BMessage* msg, bool drawText, bool purge) BMessage* msg, bool drawText, bool purge)
: BMenuItem(title, msg), :
BMenuItem(title, msg),
fIcon(icon), fIcon(icon),
fDrawText(drawText), fDrawText(drawText),
fPurge(purge) fPurge(purge)
{ {
if (!fIcon)
DefaultIcon(NULL);
} }
IconMenuItem::IconMenuItem(BBitmap* icon, BMenu* menu, bool drawText, bool purge) IconMenuItem::IconMenuItem(BBitmap* icon, BMenu* menu, bool drawText, bool purge)
: BMenuItem(menu), :
BMenuItem(menu),
fIcon(icon), fIcon(icon),
fDrawText(drawText), fDrawText(drawText),
fPurge(purge) fPurge(purge)
{ {
if (!fIcon)
DefaultIcon(NULL);
}
IconMenuItem::IconMenuItem(const char* mime, const char* title, BMessage* msg, bool drawText)
: BMenuItem(title, msg),
fIcon(NULL),
fDrawText(drawText)
{
DefaultIcon(mime);
} }
IconMenuItem::~IconMenuItem() IconMenuItem::~IconMenuItem()
{ {
if (fPurge && fIcon) if (fPurge)
delete fIcon; delete fIcon;
} }
void IconMenuItem::DrawContent() void
IconMenuItem::Reset(BBitmap* icon, bool purge)
{ {
BPoint loc; if (fPurge)
delete fIcon;
fPurge = purge;
fIcon = icon;
}
void
IconMenuItem::DrawContent()
{
DrawIcon(); DrawIcon();
if (fDrawText) { if (fDrawText) {
loc = ContentLocation(); BPoint loc = ContentLocation();
loc.x += 20; loc.x += ceilf(be_control_look->DefaultLabelSpacing() * 3.3f);
Menu()->MovePenTo(loc); Menu()->MovePenTo(loc);
BMenuItem::DrawContent(); BMenuItem::DrawContent();
} }
@@ -75,14 +73,13 @@ IconMenuItem::Highlight(bool hilited)
void void
IconMenuItem::DrawIcon() IconMenuItem::DrawIcon()
{ {
// TODO: exact code duplication with TeamBarMenuItem::DrawIcon() if (fIcon == NULL)
if (!fIcon)
return; return;
BPoint loc = ContentLocation(); BPoint loc = ContentLocation();
BRect frame = Frame(); BRect frame = Frame();
loc.y = frame.top + (frame.bottom - frame.top - 15) / 2; loc.y = frame.top + (frame.bottom - frame.top - fIcon->Bounds().Height()) / 2;
BMenu* menu = Menu(); BMenu* menu = Menu();
@@ -102,39 +99,15 @@ void
IconMenuItem::GetContentSize(float* width, float* height) IconMenuItem::GetContentSize(float* width, float* height)
{ {
BMenuItem::GetContentSize(width, height); BMenuItem::GetContentSize(width, height);
int limit = IconMenuItem::MinHeight(); if (fIcon == NULL)
return;
const float limit = ceilf(fIcon->Bounds().Height() +
(be_control_look->DefaultLabelSpacing() / 3.0f));
if (*height < limit) if (*height < limit)
*height = limit; *height = limit;
if (fDrawText) if (fDrawText)
*width += 20; *width += fIcon->Bounds().Width() + be_control_look->DefaultLabelSpacing();
else else
*width = 16; *width = fIcon->Bounds().Width() + 1;
}
void
IconMenuItem::DefaultIcon(const char* mime)
{
BRect rect(0, 0, 15, 15);
fIcon = new BBitmap(rect, B_COLOR_8_BIT);
if (mime) {
BMimeType mimeType(mime);
if (mimeType.GetIcon(fIcon, B_MINI_ICON) != B_OK)
fDrawText = true;
} else {
app_info info;
be_app->GetAppInfo(&info);
if (BNodeInfo::GetTrackerIcon(&info.ref, fIcon, B_MINI_ICON) != B_OK)
fDrawText = true;
}
fPurge = true;
}
int IconMenuItem::MinHeight()
{
static int minheight = -1;
if (minheight < 0)
minheight = 17;
return minheight;
} }
+5 -9
View File
@@ -15,24 +15,20 @@ class IconMenuItem : public BMenuItem {
public: public:
IconMenuItem(BBitmap*, const char* title, IconMenuItem(BBitmap*, const char* title,
BMessage*, bool drawText = true, bool purge = false); BMessage*, bool drawText = true, bool purge = false);
IconMenuItem(BBitmap*, BMenu*, bool drawText = true, IconMenuItem(BBitmap*, BMenu*, bool drawText = true,
bool purge = false); bool purge = false);
IconMenuItem(const char* mime, const char* title, BMessage*,
bool drawText = true);
virtual ~IconMenuItem(); virtual ~IconMenuItem();
void Reset(BBitmap*, bool purge = false);
virtual void DrawContent(); virtual void DrawContent();
virtual void Highlight(bool isHighlighted); virtual void Highlight(bool isHighlighted);
virtual void GetContentSize(float* width, float* height); virtual void GetContentSize(float* width, float* height);
static int MinHeight(); protected:
private:
void DefaultIcon(const char* mime);
void DrawIcon(); void DrawIcon();
private:
BBitmap* fIcon; BBitmap* fIcon;
bool fDrawText; bool fDrawText;
bool fPurge; bool fPurge;
@@ -1,9 +1,8 @@
/* /*
* Copyright 2000, Georges-Edouard Berenger. All rights reserved. * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
* Copyright 2022, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include "MemoryBarMenuItem.h" #include "MemoryBarMenuItem.h"
#include "Colors.h" #include "Colors.h"
@@ -11,6 +10,7 @@
#include "ProcessController.h" #include "ProcessController.h"
#include <Bitmap.h> #include <Bitmap.h>
#include <ControlLook.h>
#include <StringForSize.h> #include <StringForSize.h>
#include <stdio.h> #include <stdio.h>
@@ -18,10 +18,9 @@
MemoryBarMenuItem::MemoryBarMenuItem(const char *label, team_id team, MemoryBarMenuItem::MemoryBarMenuItem(const char *label, team_id team,
BBitmap* icon, bool deleteIcon, BMessage* message) BBitmap* icon, bool deleteIcon, BMessage* message)
: BMenuItem(label, message), :
fTeamID(team), IconMenuItem(icon, label, message, true, deleteIcon),
fIcon(icon), fTeamID(team)
fDeleteIcon(deleteIcon)
{ {
Init(); Init();
} }
@@ -29,8 +28,6 @@ MemoryBarMenuItem::MemoryBarMenuItem(const char *label, team_id team,
MemoryBarMenuItem::~MemoryBarMenuItem() MemoryBarMenuItem::~MemoryBarMenuItem()
{ {
if (fDeleteIcon)
delete fIcon;
} }
@@ -51,44 +48,19 @@ void
MemoryBarMenuItem::DrawContent() MemoryBarMenuItem::DrawContent()
{ {
DrawIcon(); DrawIcon();
if (fWriteMemory < 0) if (fWriteMemory < 0)
BarUpdate(); BarUpdate();
else else
DrawBar(true); DrawBar(true);
BPoint loc = ContentLocation(); BPoint loc = ContentLocation();
loc.x += 20; loc.x += ceilf(be_control_look->DefaultLabelSpacing() * 3.3f);
Menu()->MovePenTo(loc); Menu()->MovePenTo(loc);
BMenuItem::DrawContent(); BMenuItem::DrawContent();
} }
void
MemoryBarMenuItem::DrawIcon()
{
// TODO: exact code duplication with TeamBarMenuItem::DrawIcon()
if (!fIcon)
return;
BPoint loc = ContentLocation();
BRect frame = Frame();
loc.y = frame.top + (frame.bottom - frame.top - 15) / 2;
BMenu* menu = Menu();
if (fIcon->ColorSpace() == B_RGBA32) {
menu->SetDrawingMode(B_OP_ALPHA);
menu->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
} else
menu->SetDrawingMode(B_OP_OVER);
menu->DrawBitmap(fIcon, loc);
menu->SetDrawingMode(B_OP_COPY);
}
void void
MemoryBarMenuItem::DrawBar(bool force) MemoryBarMenuItem::DrawBar(bool force)
{ {
@@ -220,10 +192,9 @@ MemoryBarMenuItem::DrawBar(bool force)
void void
MemoryBarMenuItem::GetContentSize(float* _width, float* _height) MemoryBarMenuItem::GetContentSize(float* _width, float* _height)
{ {
BMenuItem::GetContentSize(_width, _height); IconMenuItem::GetContentSize(_width, _height);
if (*_height < 16) *_width += ceilf(be_control_look->DefaultLabelSpacing() * 2.0f)
*_height = 16; + kBarWidth + kMargin + gMemoryTextWidth;
*_width += 30 + kBarWidth + kMargin + gMemoryTextWidth;
} }
@@ -275,10 +246,7 @@ MemoryBarMenuItem::Reset(char* name, team_id team, BBitmap* icon,
{ {
SetLabel(name); SetLabel(name);
fTeamID = team; fTeamID = team;
if (fDeleteIcon) IconMenuItem::Reset(icon, deleteIcon);
delete fIcon;
fDeleteIcon = deleteIcon;
fIcon = icon;
Init(); Init();
} }
@@ -6,12 +6,10 @@
#define _MEMORY_BAR_MENU_ITEM_H_ #define _MEMORY_BAR_MENU_ITEM_H_
#include <MenuItem.h> #include "IconMenuItem.h"
class BBitmap;
class MemoryBarMenuItem : public BMenuItem { class MemoryBarMenuItem : public IconMenuItem {
public: public:
MemoryBarMenuItem(const char *label, team_id team, MemoryBarMenuItem(const char *label, team_id team,
BBitmap* icon, bool deleteIcon, BMessage* message); BBitmap* icon, bool deleteIcon, BMessage* message);
@@ -20,7 +18,6 @@ class MemoryBarMenuItem : public BMenuItem {
virtual void DrawContent(); virtual void DrawContent();
virtual void GetContentSize(float* _width, float* _height); virtual void GetContentSize(float* _width, float* _height);
void DrawIcon();
void DrawBar(bool force); void DrawBar(bool force);
int UpdateSituation(int64 committedMemory); int UpdateSituation(int64 committedMemory);
void BarUpdate(); void BarUpdate();
@@ -36,10 +33,8 @@ class MemoryBarMenuItem : public BMenuItem {
int64 fLastWrite; int64 fLastWrite;
int64 fLastAll; int64 fLastAll;
team_id fTeamID; team_id fTeamID;
BBitmap* fIcon;
double fGrenze1; double fGrenze1;
double fGrenze2; double fGrenze2;
bool fDeleteIcon;
}; };
#endif // _MEMORY_BAR_MENU_ITEM_H_ #endif // _MEMORY_BAR_MENU_ITEM_H_
+14 -51
View File
@@ -1,9 +1,8 @@
/* /*
* Copyright 2000, Georges-Edouard Berenger. All rights reserved. * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
* Copyright 2022, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include "TeamBarMenuItem.h" #include "TeamBarMenuItem.h"
#include "Colors.h" #include "Colors.h"
@@ -13,19 +12,16 @@
#include "Utilities.h" #include "Utilities.h"
#include <Bitmap.h> #include <Bitmap.h>
#include <ControlLook.h>
#define B_USAGE_SELF 0
TeamBarMenuItem::TeamBarMenuItem(BMenu* menu, BMessage* kill_team, team_id team, TeamBarMenuItem::TeamBarMenuItem(BMenu* menu, BMessage* kill_team, team_id team,
BBitmap* icon, bool deleteIcon) BBitmap* icon, bool deleteIcon)
: :
BMenuItem(menu, kill_team), IconMenuItem(icon, menu, true, deleteIcon),
fTeamID(team), fTeamID(team)
fIcon(icon),
fDeleteIcon(deleteIcon)
{ {
SetMessage(kill_team);
Init(); Init();
} }
@@ -33,7 +29,7 @@ TeamBarMenuItem::TeamBarMenuItem(BMenu* menu, BMessage* kill_team, team_id team,
void void
TeamBarMenuItem::Init() TeamBarMenuItem::Init()
{ {
if (get_team_usage_info(fTeamID, B_USAGE_SELF, &fTeamUsageInfo) != B_OK) if (get_team_usage_info(fTeamID, B_TEAM_USAGE_SELF, &fTeamUsageInfo) != B_OK)
fTeamUsageInfo.kernel_time = fTeamUsageInfo.user_time = 0; fTeamUsageInfo.kernel_time = fTeamUsageInfo.user_time = 0;
if (fTeamID == B_SYSTEM_TEAM) { if (fTeamID == B_SYSTEM_TEAM) {
@@ -56,15 +52,13 @@ TeamBarMenuItem::Init()
TeamBarMenuItem::~TeamBarMenuItem() TeamBarMenuItem::~TeamBarMenuItem()
{ {
if (fDeleteIcon)
delete fIcon;
} }
void void
TeamBarMenuItem::DrawContent() TeamBarMenuItem::DrawContent()
{ {
BPoint loc; BPoint loc;
DrawIcon(); DrawIcon();
if (fKernel < 0) if (fKernel < 0)
@@ -73,43 +67,18 @@ TeamBarMenuItem::DrawContent()
DrawBar(true); DrawBar(true);
loc = ContentLocation(); loc = ContentLocation();
loc.x += 20; loc.x += ceilf(be_control_look->DefaultLabelSpacing() * 3.3f);
Menu()->MovePenTo(loc); Menu()->MovePenTo(loc);
BMenuItem::DrawContent(); BMenuItem::DrawContent();
} }
void
TeamBarMenuItem::DrawIcon()
{
if (fIcon == NULL)
return;
BPoint loc = ContentLocation();
BRect frame = Frame();
loc.y = frame.top + (frame.bottom - frame.top - 15) / 2;
BMenu* menu = Menu();
if (fIcon->ColorSpace() == B_RGBA32) {
menu->SetDrawingMode(B_OP_ALPHA);
menu->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
} else
menu->SetDrawingMode(B_OP_OVER);
menu->DrawBitmap(fIcon, loc);
menu->SetDrawingMode(B_OP_COPY);
}
void void
TeamBarMenuItem::DrawBar(bool force) TeamBarMenuItem::DrawBar(bool force)
{ {
bool selected = IsSelected (); const bool selected = IsSelected();
BRect frame = Frame(); BRect frame = Frame();
BMenu* menu = Menu (); BMenu* menu = Menu();
rgb_color highColor = menu->HighColor(); rgb_color highColor = menu->HighColor();
BFont font; BFont font;
@@ -206,10 +175,7 @@ TeamBarMenuItem::DrawBar(bool force)
void void
TeamBarMenuItem::GetContentSize(float* width, float* height) TeamBarMenuItem::GetContentSize(float* width, float* height)
{ {
BMenuItem::GetContentSize(width, height); IconMenuItem::GetContentSize(width, height);
if (height != NULL && *height < 16)
*height = 16;
if (width != NULL) if (width != NULL)
*width += 40 + kBarWidth; *width += 40 + kBarWidth;
} }
@@ -219,7 +185,7 @@ void
TeamBarMenuItem::BarUpdate() TeamBarMenuItem::BarUpdate()
{ {
team_usage_info usage; team_usage_info usage;
if (get_team_usage_info(fTeamID, B_USAGE_SELF, &usage) == B_OK) { if (get_team_usage_info(fTeamID, B_TEAM_USAGE_SELF, &usage) == B_OK) {
bigtime_t now = system_time(); bigtime_t now = system_time();
bigtime_t idle = 0; bigtime_t idle = 0;
if (fTeamID == B_SYSTEM_TEAM) { if (fTeamID == B_SYSTEM_TEAM) {
@@ -253,15 +219,12 @@ TeamBarMenuItem::BarUpdate()
void void
TeamBarMenuItem::Reset(char* name, team_id team, BBitmap* icon, bool deleteIcon) TeamBarMenuItem::Reset(char* name, team_id team, BBitmap* icon, bool deleteIcon)
{ {
IconMenuItem::Reset(icon, deleteIcon);
SetLabel(name); SetLabel(name);
fTeamID = team; fTeamID = team;
Init(); Init();
if (fDeleteIcon)
delete fIcon;
fDeleteIcon = deleteIcon;
fIcon = icon;
Message()->ReplaceInt32("team", team); Message()->ReplaceInt32("team", team);
((ThreadBarMenu*)Submenu())->Reset(team); ((ThreadBarMenu*)Submenu())->Reset(team);
BarUpdate(); BarUpdate();
+2 -7
View File
@@ -6,12 +6,10 @@
#define _TEAM_BAR_MENU_ITEM_H_ #define _TEAM_BAR_MENU_ITEM_H_
#include <MenuItem.h> #include "IconMenuItem.h"
class BBitmap;
class TeamBarMenuItem : public BMenuItem { class TeamBarMenuItem : public IconMenuItem {
public: public:
TeamBarMenuItem(BMenu* menu, BMessage* kill_team, team_id team, TeamBarMenuItem(BMenu* menu, BMessage* kill_team, team_id team,
BBitmap* icon, bool deleteIcon); BBitmap* icon, bool deleteIcon);
@@ -20,7 +18,6 @@ public:
virtual void DrawContent(); virtual void DrawContent();
virtual void GetContentSize(float* width, float* height); virtual void GetContentSize(float* width, float* height);
void DrawIcon();
void DrawBar(bool force); void DrawBar(bool force);
void BarUpdate(); void BarUpdate();
void Init(); void Init();
@@ -31,12 +28,10 @@ public:
private: private:
team_id fTeamID; team_id fTeamID;
BBitmap* fIcon;
team_usage_info fTeamUsageInfo; team_usage_info fTeamUsageInfo;
bigtime_t fLastTime; bigtime_t fLastTime;
float fGrenze1; float fGrenze1;
float fGrenze2; float fGrenze2;
bool fDeleteIcon;
}; };
+6 -3
View File
@@ -12,6 +12,7 @@
#include <AppMisc.h> #include <AppMisc.h>
#include <Alert.h> #include <Alert.h>
#include <Bitmap.h> #include <Bitmap.h>
#include <ControlLook.h>
#include <Deskbar.h> #include <Deskbar.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include <NodeInfo.h> #include <NodeInfo.h>
@@ -61,12 +62,14 @@ get_team_name_and_icon(info_pack& infoPack, bool icon)
B_PATH_NAME_LENGTH - 1); B_PATH_NAME_LENGTH - 1);
if (icon) { if (icon) {
infoPack.team_icon = new BBitmap(BRect(0, 0, 15, 15), B_RGBA32); infoPack.team_icon = new BBitmap(BRect(BPoint(0, 0),
be_control_look->ComposeIconSize(B_MINI_ICON)), B_RGBA32);
if (!tryTrackerIcon if (!tryTrackerIcon
|| BNodeInfo::GetTrackerIcon(&info.ref, infoPack.team_icon, || BNodeInfo::GetTrackerIcon(&info.ref, infoPack.team_icon,
B_MINI_ICON) != B_OK) { (icon_size)-1) != B_OK) {
BMimeType genericAppType(B_APP_MIME_TYPE); BMimeType genericAppType(B_APP_MIME_TYPE);
status = genericAppType.GetIcon(infoPack.team_icon, B_MINI_ICON); status = genericAppType.GetIcon(infoPack.team_icon,
(icon_size)(infoPack.team_icon->Bounds().IntegerWidth() + 1));
// failed to get icon // failed to get icon
if (status != B_OK) { if (status != B_OK) {
delete infoPack.team_icon; delete infoPack.team_icon;