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:
@@ -7,6 +7,7 @@
|
||||
#include "Utilities.h"
|
||||
|
||||
#include <Bitmap.h>
|
||||
#include <ControlLook.h>
|
||||
#include <Entry.h>
|
||||
#include <MimeType.h>
|
||||
#include <NodeInfo.h>
|
||||
@@ -22,20 +23,24 @@ AutoIcon::~AutoIcon()
|
||||
BBitmap*
|
||||
AutoIcon::Bitmap()
|
||||
{
|
||||
if (fBitmap == NULL) {
|
||||
fBitmap = new BBitmap(BRect(0, 0, 15, 15), B_RGBA32);
|
||||
if (fBitmap != NULL)
|
||||
return fBitmap;
|
||||
|
||||
if (fSignature) {
|
||||
entry_ref ref;
|
||||
be_roster->FindApp (fSignature, &ref);
|
||||
if (BNodeInfo::GetTrackerIcon(&ref, fBitmap, B_MINI_ICON) != B_OK) {
|
||||
BMimeType genericAppType(B_APP_MIME_TYPE);
|
||||
genericAppType.GetIcon(fBitmap, B_MINI_ICON);
|
||||
}
|
||||
if (fSignature) {
|
||||
fBitmap = new BBitmap(BRect(BPoint(0, 0),
|
||||
be_control_look->ComposeIconSize(B_MINI_ICON)), B_RGBA32);
|
||||
|
||||
entry_ref ref;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,63 +1,61 @@
|
||||
/*
|
||||
* Copyright 2000, Georges-Edouard Berenger. All rights reserved.
|
||||
* Copyright 2022, Haiku, Inc. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#include "IconMenuItem.h"
|
||||
#include <Application.h>
|
||||
#include <NodeInfo.h>
|
||||
|
||||
#include <ControlLook.h>
|
||||
#include <Bitmap.h>
|
||||
#include <Roster.h>
|
||||
|
||||
|
||||
IconMenuItem::IconMenuItem(BBitmap* icon, const char* title,
|
||||
BMessage* msg, bool drawText, bool purge)
|
||||
: BMenuItem(title, msg),
|
||||
:
|
||||
BMenuItem(title, msg),
|
||||
fIcon(icon),
|
||||
fDrawText(drawText),
|
||||
fPurge(purge)
|
||||
{
|
||||
if (!fIcon)
|
||||
DefaultIcon(NULL);
|
||||
}
|
||||
|
||||
|
||||
IconMenuItem::IconMenuItem(BBitmap* icon, BMenu* menu, bool drawText, bool purge)
|
||||
: BMenuItem(menu),
|
||||
:
|
||||
BMenuItem(menu),
|
||||
fIcon(icon),
|
||||
fDrawText(drawText),
|
||||
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()
|
||||
{
|
||||
if (fPurge && fIcon)
|
||||
if (fPurge)
|
||||
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();
|
||||
|
||||
if (fDrawText) {
|
||||
loc = ContentLocation();
|
||||
loc.x += 20;
|
||||
BPoint loc = ContentLocation();
|
||||
loc.x += ceilf(be_control_look->DefaultLabelSpacing() * 3.3f);
|
||||
Menu()->MovePenTo(loc);
|
||||
BMenuItem::DrawContent();
|
||||
}
|
||||
@@ -75,14 +73,13 @@ IconMenuItem::Highlight(bool hilited)
|
||||
void
|
||||
IconMenuItem::DrawIcon()
|
||||
{
|
||||
// TODO: exact code duplication with TeamBarMenuItem::DrawIcon()
|
||||
if (!fIcon)
|
||||
if (fIcon == NULL)
|
||||
return;
|
||||
|
||||
BPoint loc = ContentLocation();
|
||||
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();
|
||||
|
||||
@@ -102,39 +99,15 @@ void
|
||||
IconMenuItem::GetContentSize(float* width, float* 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)
|
||||
*height = limit;
|
||||
if (fDrawText)
|
||||
*width += 20;
|
||||
*width += fIcon->Bounds().Width() + be_control_look->DefaultLabelSpacing();
|
||||
else
|
||||
*width = 16;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
*width = fIcon->Bounds().Width() + 1;
|
||||
}
|
||||
|
||||
@@ -15,24 +15,20 @@ class IconMenuItem : public BMenuItem {
|
||||
public:
|
||||
IconMenuItem(BBitmap*, const char* title,
|
||||
BMessage*, bool drawText = true, bool purge = false);
|
||||
|
||||
IconMenuItem(BBitmap*, BMenu*, bool drawText = true,
|
||||
bool purge = false);
|
||||
|
||||
IconMenuItem(const char* mime, const char* title, BMessage*,
|
||||
bool drawText = true);
|
||||
|
||||
virtual ~IconMenuItem();
|
||||
|
||||
void Reset(BBitmap*, bool purge = false);
|
||||
|
||||
virtual void DrawContent();
|
||||
virtual void Highlight(bool isHighlighted);
|
||||
virtual void GetContentSize(float* width, float* height);
|
||||
|
||||
static int MinHeight();
|
||||
|
||||
private:
|
||||
void DefaultIcon(const char* mime);
|
||||
protected:
|
||||
void DrawIcon();
|
||||
|
||||
private:
|
||||
BBitmap* fIcon;
|
||||
bool fDrawText;
|
||||
bool fPurge;
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
/*
|
||||
* Copyright 2000, Georges-Edouard Berenger. All rights reserved.
|
||||
* Copyright 2022, Haiku, Inc. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "MemoryBarMenuItem.h"
|
||||
|
||||
#include "Colors.h"
|
||||
@@ -11,6 +10,7 @@
|
||||
#include "ProcessController.h"
|
||||
|
||||
#include <Bitmap.h>
|
||||
#include <ControlLook.h>
|
||||
#include <StringForSize.h>
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -18,10 +18,9 @@
|
||||
|
||||
MemoryBarMenuItem::MemoryBarMenuItem(const char *label, team_id team,
|
||||
BBitmap* icon, bool deleteIcon, BMessage* message)
|
||||
: BMenuItem(label, message),
|
||||
fTeamID(team),
|
||||
fIcon(icon),
|
||||
fDeleteIcon(deleteIcon)
|
||||
:
|
||||
IconMenuItem(icon, label, message, true, deleteIcon),
|
||||
fTeamID(team)
|
||||
{
|
||||
Init();
|
||||
}
|
||||
@@ -29,8 +28,6 @@ MemoryBarMenuItem::MemoryBarMenuItem(const char *label, team_id team,
|
||||
|
||||
MemoryBarMenuItem::~MemoryBarMenuItem()
|
||||
{
|
||||
if (fDeleteIcon)
|
||||
delete fIcon;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,44 +48,19 @@ void
|
||||
MemoryBarMenuItem::DrawContent()
|
||||
{
|
||||
DrawIcon();
|
||||
|
||||
if (fWriteMemory < 0)
|
||||
BarUpdate();
|
||||
else
|
||||
DrawBar(true);
|
||||
|
||||
BPoint loc = ContentLocation();
|
||||
loc.x += 20;
|
||||
loc.x += ceilf(be_control_look->DefaultLabelSpacing() * 3.3f);
|
||||
Menu()->MovePenTo(loc);
|
||||
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
|
||||
MemoryBarMenuItem::DrawBar(bool force)
|
||||
{
|
||||
@@ -220,10 +192,9 @@ MemoryBarMenuItem::DrawBar(bool force)
|
||||
void
|
||||
MemoryBarMenuItem::GetContentSize(float* _width, float* _height)
|
||||
{
|
||||
BMenuItem::GetContentSize(_width, _height);
|
||||
if (*_height < 16)
|
||||
*_height = 16;
|
||||
*_width += 30 + kBarWidth + kMargin + gMemoryTextWidth;
|
||||
IconMenuItem::GetContentSize(_width, _height);
|
||||
*_width += ceilf(be_control_look->DefaultLabelSpacing() * 2.0f)
|
||||
+ kBarWidth + kMargin + gMemoryTextWidth;
|
||||
}
|
||||
|
||||
|
||||
@@ -275,10 +246,7 @@ MemoryBarMenuItem::Reset(char* name, team_id team, BBitmap* icon,
|
||||
{
|
||||
SetLabel(name);
|
||||
fTeamID = team;
|
||||
if (fDeleteIcon)
|
||||
delete fIcon;
|
||||
IconMenuItem::Reset(icon, deleteIcon);
|
||||
|
||||
fDeleteIcon = deleteIcon;
|
||||
fIcon = icon;
|
||||
Init();
|
||||
}
|
||||
|
||||
@@ -6,12 +6,10 @@
|
||||
#define _MEMORY_BAR_MENU_ITEM_H_
|
||||
|
||||
|
||||
#include <MenuItem.h>
|
||||
|
||||
class BBitmap;
|
||||
#include "IconMenuItem.h"
|
||||
|
||||
|
||||
class MemoryBarMenuItem : public BMenuItem {
|
||||
class MemoryBarMenuItem : public IconMenuItem {
|
||||
public:
|
||||
MemoryBarMenuItem(const char *label, team_id team,
|
||||
BBitmap* icon, bool deleteIcon, BMessage* message);
|
||||
@@ -20,7 +18,6 @@ class MemoryBarMenuItem : public BMenuItem {
|
||||
virtual void DrawContent();
|
||||
virtual void GetContentSize(float* _width, float* _height);
|
||||
|
||||
void DrawIcon();
|
||||
void DrawBar(bool force);
|
||||
int UpdateSituation(int64 committedMemory);
|
||||
void BarUpdate();
|
||||
@@ -36,10 +33,8 @@ class MemoryBarMenuItem : public BMenuItem {
|
||||
int64 fLastWrite;
|
||||
int64 fLastAll;
|
||||
team_id fTeamID;
|
||||
BBitmap* fIcon;
|
||||
double fGrenze1;
|
||||
double fGrenze2;
|
||||
bool fDeleteIcon;
|
||||
};
|
||||
|
||||
#endif // _MEMORY_BAR_MENU_ITEM_H_
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
/*
|
||||
* Copyright 2000, Georges-Edouard Berenger. All rights reserved.
|
||||
* Copyright 2022, Haiku, Inc. All rights reserved.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "TeamBarMenuItem.h"
|
||||
|
||||
#include "Colors.h"
|
||||
@@ -13,19 +12,16 @@
|
||||
#include "Utilities.h"
|
||||
|
||||
#include <Bitmap.h>
|
||||
|
||||
|
||||
#define B_USAGE_SELF 0
|
||||
#include <ControlLook.h>
|
||||
|
||||
|
||||
TeamBarMenuItem::TeamBarMenuItem(BMenu* menu, BMessage* kill_team, team_id team,
|
||||
BBitmap* icon, bool deleteIcon)
|
||||
:
|
||||
BMenuItem(menu, kill_team),
|
||||
fTeamID(team),
|
||||
fIcon(icon),
|
||||
fDeleteIcon(deleteIcon)
|
||||
IconMenuItem(icon, menu, true, deleteIcon),
|
||||
fTeamID(team)
|
||||
{
|
||||
SetMessage(kill_team);
|
||||
Init();
|
||||
}
|
||||
|
||||
@@ -33,7 +29,7 @@ TeamBarMenuItem::TeamBarMenuItem(BMenu* menu, BMessage* kill_team, team_id team,
|
||||
void
|
||||
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;
|
||||
|
||||
if (fTeamID == B_SYSTEM_TEAM) {
|
||||
@@ -56,15 +52,13 @@ TeamBarMenuItem::Init()
|
||||
|
||||
TeamBarMenuItem::~TeamBarMenuItem()
|
||||
{
|
||||
if (fDeleteIcon)
|
||||
delete fIcon;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
TeamBarMenuItem::DrawContent()
|
||||
{
|
||||
BPoint loc;
|
||||
BPoint loc;
|
||||
|
||||
DrawIcon();
|
||||
if (fKernel < 0)
|
||||
@@ -73,43 +67,18 @@ TeamBarMenuItem::DrawContent()
|
||||
DrawBar(true);
|
||||
|
||||
loc = ContentLocation();
|
||||
loc.x += 20;
|
||||
loc.x += ceilf(be_control_look->DefaultLabelSpacing() * 3.3f);
|
||||
Menu()->MovePenTo(loc);
|
||||
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
|
||||
TeamBarMenuItem::DrawBar(bool force)
|
||||
{
|
||||
bool selected = IsSelected ();
|
||||
const bool selected = IsSelected();
|
||||
BRect frame = Frame();
|
||||
BMenu* menu = Menu ();
|
||||
BMenu* menu = Menu();
|
||||
rgb_color highColor = menu->HighColor();
|
||||
|
||||
BFont font;
|
||||
@@ -206,10 +175,7 @@ TeamBarMenuItem::DrawBar(bool force)
|
||||
void
|
||||
TeamBarMenuItem::GetContentSize(float* width, float* height)
|
||||
{
|
||||
BMenuItem::GetContentSize(width, height);
|
||||
if (height != NULL && *height < 16)
|
||||
*height = 16;
|
||||
|
||||
IconMenuItem::GetContentSize(width, height);
|
||||
if (width != NULL)
|
||||
*width += 40 + kBarWidth;
|
||||
}
|
||||
@@ -219,7 +185,7 @@ void
|
||||
TeamBarMenuItem::BarUpdate()
|
||||
{
|
||||
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 idle = 0;
|
||||
if (fTeamID == B_SYSTEM_TEAM) {
|
||||
@@ -253,15 +219,12 @@ TeamBarMenuItem::BarUpdate()
|
||||
void
|
||||
TeamBarMenuItem::Reset(char* name, team_id team, BBitmap* icon, bool deleteIcon)
|
||||
{
|
||||
IconMenuItem::Reset(icon, deleteIcon);
|
||||
|
||||
SetLabel(name);
|
||||
fTeamID = team;
|
||||
Init();
|
||||
|
||||
if (fDeleteIcon)
|
||||
delete fIcon;
|
||||
|
||||
fDeleteIcon = deleteIcon;
|
||||
fIcon = icon;
|
||||
Message()->ReplaceInt32("team", team);
|
||||
((ThreadBarMenu*)Submenu())->Reset(team);
|
||||
BarUpdate();
|
||||
|
||||
@@ -6,12 +6,10 @@
|
||||
#define _TEAM_BAR_MENU_ITEM_H_
|
||||
|
||||
|
||||
#include <MenuItem.h>
|
||||
|
||||
class BBitmap;
|
||||
#include "IconMenuItem.h"
|
||||
|
||||
|
||||
class TeamBarMenuItem : public BMenuItem {
|
||||
class TeamBarMenuItem : public IconMenuItem {
|
||||
public:
|
||||
TeamBarMenuItem(BMenu* menu, BMessage* kill_team, team_id team,
|
||||
BBitmap* icon, bool deleteIcon);
|
||||
@@ -20,7 +18,6 @@ public:
|
||||
|
||||
virtual void DrawContent();
|
||||
virtual void GetContentSize(float* width, float* height);
|
||||
void DrawIcon();
|
||||
void DrawBar(bool force);
|
||||
void BarUpdate();
|
||||
void Init();
|
||||
@@ -31,12 +28,10 @@ public:
|
||||
|
||||
private:
|
||||
team_id fTeamID;
|
||||
BBitmap* fIcon;
|
||||
team_usage_info fTeamUsageInfo;
|
||||
bigtime_t fLastTime;
|
||||
float fGrenze1;
|
||||
float fGrenze2;
|
||||
bool fDeleteIcon;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <AppMisc.h>
|
||||
#include <Alert.h>
|
||||
#include <Bitmap.h>
|
||||
#include <ControlLook.h>
|
||||
#include <Deskbar.h>
|
||||
#include <FindDirectory.h>
|
||||
#include <NodeInfo.h>
|
||||
@@ -61,12 +62,14 @@ get_team_name_and_icon(info_pack& infoPack, bool icon)
|
||||
B_PATH_NAME_LENGTH - 1);
|
||||
|
||||
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
|
||||
|| BNodeInfo::GetTrackerIcon(&info.ref, infoPack.team_icon,
|
||||
B_MINI_ICON) != B_OK) {
|
||||
(icon_size)-1) != B_OK) {
|
||||
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
|
||||
if (status != B_OK) {
|
||||
delete infoPack.team_icon;
|
||||
|
||||
Reference in New Issue
Block a user