FileTypes: Refactor icon handling to use BControlLook::ComposeIconSize().

Fixes #17907.
This commit is contained in:
Augustin Cavalier
2022-09-28 17:41:27 -04:00
parent b25b415fb9
commit d3da4f984e
4 changed files with 45 additions and 30 deletions
+16 -9
View File
@@ -96,6 +96,8 @@ static const char* kAttributeNames[] = {
class TypeIconView : public IconView { class TypeIconView : public IconView {
typedef IconView _inherited;
public: public:
TypeIconView(const char* name); TypeIconView(const char* name);
virtual ~TypeIconView(); virtual ~TypeIconView();
@@ -135,7 +137,7 @@ TypeIconView::TypeIconView(const char* name)
: IconView(name) : IconView(name)
{ {
ShowEmptyFrame(false); ShowEmptyFrame(false);
SetIconSize(48); SetIconSize((icon_size)48);
} }
@@ -176,12 +178,13 @@ TypeIconView::Draw(BRect updateRect)
font_height fontHeight; font_height fontHeight;
GetFontHeight(&fontHeight); GetFontHeight(&fontHeight);
const BRect bitmapRect = _inherited::BitmapRect();
float y = fontHeight.ascent; float y = fontHeight.ascent;
if (IconSource() == kNoIcon) { if (IconSource() == kNoIcon) {
// center text in the middle of the icon // center text in the middle of the icon
y += (IconSize() - fontHeight.ascent - fontHeight.descent) / 2.0f; y += (bitmapRect.Height() - fontHeight.ascent - fontHeight.descent) / 2.0f;
} else } else
y += IconSize() + 3.0f; y += bitmapRect.Height() + 3.0f;
DrawString(text, BPoint(ceilf((Bounds().Width() - StringWidth(text)) / 2.0f), DrawString(text, BPoint(ceilf((Bounds().Width() - StringWidth(text)) / 2.0f),
ceilf(y))); ceilf(y)));
@@ -191,12 +194,14 @@ TypeIconView::Draw(BRect updateRect)
void void
TypeIconView::GetPreferredSize(float* _width, float* _height) TypeIconView::GetPreferredSize(float* _width, float* _height)
{ {
const BRect bitmapRect = _inherited::BitmapRect();
if (_width) { if (_width) {
float a = StringWidth(B_TRANSLATE("(from application)")); float a = StringWidth(B_TRANSLATE("(from application)"));
float b = StringWidth(B_TRANSLATE("(from super type)")); float b = StringWidth(B_TRANSLATE("(from super type)"));
float width = max_c(a, b); float width = max_c(a, b);
if (width < IconSize()) if (width < bitmapRect.Width())
width = IconSize(); width = bitmapRect.Width();
*_width = ceilf(width); *_width = ceilf(width);
} }
@@ -205,7 +210,7 @@ TypeIconView::GetPreferredSize(float* _width, float* _height)
font_height fontHeight; font_height fontHeight;
GetFontHeight(&fontHeight); GetFontHeight(&fontHeight);
*_height = IconSize() + 3.0f + ceilf(fontHeight.ascent *_height = bitmapRect.Height() + 3.0f + ceilf(fontHeight.ascent
+ fontHeight.descent); + fontHeight.descent);
} }
} }
@@ -214,6 +219,8 @@ TypeIconView::GetPreferredSize(float* _width, float* _height)
BRect BRect
TypeIconView::BitmapRect() const TypeIconView::BitmapRect() const
{ {
const BRect bitmapRect = _inherited::BitmapRect();
if (IconSource() == kNoIcon) { if (IconSource() == kNoIcon) {
// this also defines the drop target area // this also defines the drop target area
font_height fontHeight; font_height fontHeight;
@@ -222,14 +229,14 @@ TypeIconView::BitmapRect() const
float width = StringWidth(B_TRANSLATE("no icon")) + 8.0f; float width = StringWidth(B_TRANSLATE("no icon")) + 8.0f;
float height = ceilf(fontHeight.ascent + fontHeight.descent) + 6.0f; float height = ceilf(fontHeight.ascent + fontHeight.descent) + 6.0f;
float x = (Bounds().Width() - width) / 2.0f; float x = (Bounds().Width() - width) / 2.0f;
float y = ceilf((IconSize() - fontHeight.ascent - fontHeight.descent) float y = ceilf((bitmapRect.Height() - fontHeight.ascent - fontHeight.descent)
/ 2.0f) - 3.0f; / 2.0f) - 3.0f;
return BRect(x, y, x + width, y + height); return BRect(x, y, x + width, y + height);
} }
float x = (Bounds().Width() - IconSize()) / 2.0f; float x = (Bounds().Width() - bitmapRect.Width()) / 2.0f;
return BRect(x, 0.0f, x + IconSize() - 1, IconSize() - 1); return BRect(x, 0.0f, x + bitmapRect.Width(), bitmapRect.Height());
} }
+13 -9
View File
@@ -15,6 +15,7 @@
#include <Attributes.h> #include <Attributes.h>
#include <Bitmap.h> #include <Bitmap.h>
#include <Catalog.h> #include <Catalog.h>
#include <ControlLook.h>
#include <IconEditorProtocol.h> #include <IconEditorProtocol.h>
#include <IconUtils.h> #include <IconUtils.h>
#include <Locale.h> #include <Locale.h>
@@ -497,13 +498,14 @@ Icon::AdoptData(uint8* data, size_t size)
/*static*/ BBitmap* /*static*/ BBitmap*
Icon::AllocateBitmap(int32 size, int32 space) Icon::AllocateBitmap(icon_size size, int32 space)
{ {
int32 kSpace = B_RGBA32; int32 kSpace = B_RGBA32;
if (space == -1) if (space == -1)
space = kSpace; space = kSpace;
BBitmap* bitmap = new (nothrow) BBitmap(BRect(0, 0, size - 1, size - 1), BBitmap* bitmap = new (nothrow) BBitmap(BRect(BPoint(0, 0),
be_control_look->ComposeIconSize(size)),
(color_space)space); (color_space)space);
if (bitmap == NULL || bitmap->InitCheck() != B_OK) { if (bitmap == NULL || bitmap->InitCheck() != B_OK) {
delete bitmap; delete bitmap;
@@ -520,7 +522,7 @@ Icon::AllocateBitmap(int32 size, int32 space)
IconView::IconView(const char* name, uint32 flags) IconView::IconView(const char* name, uint32 flags)
: BControl(name, NULL, NULL, B_WILL_DRAW | flags), : BControl(name, NULL, NULL, B_WILL_DRAW | flags),
fModificationMessage(NULL), fModificationMessage(NULL),
fIconSize(B_LARGE_ICON), fIconSize((icon_size)0),
fIcon(NULL), fIcon(NULL),
fHeapIcon(NULL), fHeapIcon(NULL),
fHasRef(false), fHasRef(false),
@@ -531,6 +533,7 @@ IconView::IconView(const char* name, uint32 flags)
fDropTarget(false), fDropTarget(false),
fShowEmptyFrame(true) fShowEmptyFrame(true)
{ {
SetIconSize(B_LARGE_ICON);
} }
@@ -716,7 +719,7 @@ IconView::AcceptsDrag(const BMessage* message)
BRect BRect
IconView::BitmapRect() const IconView::BitmapRect() const
{ {
return BRect(0, 0, fIconSize - 1, fIconSize - 1); return fIconRect;
} }
@@ -761,10 +764,10 @@ void
IconView::GetPreferredSize(float* _width, float* _height) IconView::GetPreferredSize(float* _width, float* _height)
{ {
if (_width) if (_width)
*_width = fIconSize; *_width = fIconRect.Width();
if (_height) if (_height)
*_height = fIconSize; *_height = fIconRect.Height();
} }
@@ -1034,7 +1037,7 @@ IconView::Update()
icon = Icon::AllocateBitmap(fIconSize); icon = Icon::AllocateBitmap(fIconSize);
if (icon != NULL && info.GetTrackerIcon(icon, if (icon != NULL && info.GetTrackerIcon(icon,
(icon_size)fIconSize) != B_OK) { (icon_size)(icon->Bounds().IntegerWidth() + 1)) != B_OK) {
delete icon; delete icon;
return; return;
} }
@@ -1058,16 +1061,17 @@ IconView::Update()
void void
IconView::SetIconSize(int32 size) IconView::SetIconSize(icon_size size)
{ {
if (size < B_MINI_ICON) if (size < B_MINI_ICON)
size = B_MINI_ICON; size = B_MINI_ICON;
if (size > 256) if (size > 256)
size = 256; size = (icon_size)256;
if (size == fIconSize) if (size == fIconSize)
return; return;
fIconSize = size; fIconSize = size;
fIconRect = BRect(BPoint(0, 0), be_control_look->ComposeIconSize(fIconSize));
Update(); Update();
} }
+5 -4
View File
@@ -61,7 +61,7 @@ public:
void AdoptMini(BBitmap* mini); void AdoptMini(BBitmap* mini);
void AdoptData(uint8* data, size_t size); void AdoptData(uint8* data, size_t size);
static BBitmap* AllocateBitmap(int32 size, int32 space = -1); static BBitmap* AllocateBitmap(icon_size size, int32 space = -1);
private: private:
BBitmap* fLarge; BBitmap* fLarge;
@@ -105,7 +105,7 @@ public:
void Unset(); void Unset();
void Update(); void Update();
void SetIconSize(int32 size); void SetIconSize(icon_size size);
void ShowIconHeap(bool show); void ShowIconHeap(bool show);
void ShowEmptyFrame(bool show); void ShowEmptyFrame(bool show);
status_t SetTarget(const BMessenger& target); status_t SetTarget(const BMessenger& target);
@@ -113,7 +113,7 @@ public:
status_t Invoke(BMessage* message = NULL); status_t Invoke(BMessage* message = NULL);
::Icon* Icon(); ::Icon* Icon();
int32 IconSize() const { return fIconSize; } icon_size IconSize() const { return fIconSize; }
icon_source IconSource() const { return fSource; } icon_source IconSource() const { return fSource; }
status_t GetRef(entry_ref& ref) const; status_t GetRef(entry_ref& ref) const;
status_t GetMimeType(BMimeType& type) const; status_t GetMimeType(BMimeType& type) const;
@@ -144,7 +144,8 @@ private:
BMessenger fTarget; BMessenger fTarget;
BMessage* fModificationMessage; BMessage* fModificationMessage;
int32 fIconSize; icon_size fIconSize;
BRect fIconRect;
BBitmap* fIcon; BBitmap* fIcon;
BBitmap* fHeapIcon; BBitmap* fHeapIcon;
+11 -8
View File
@@ -8,6 +8,7 @@
#include "MimeTypeListView.h" #include "MimeTypeListView.h"
#include <Bitmap.h> #include <Bitmap.h>
#include <ControlLook.h>
#include <MessageRunner.h> #include <MessageRunner.h>
#include <strings.h> #include <strings.h>
@@ -90,7 +91,8 @@ MimeTypeItem::DrawItem(BView* owner, BRect frame, bool complete)
owner->FillRect(rect, B_SOLID_LOW); owner->FillRect(rect, B_SOLID_LOW);
} }
BBitmap bitmap(BRect(0, 0, B_MINI_ICON - 1, B_MINI_ICON - 1), B_RGBA32); const BRect iconRect(BPoint(0, 0), be_control_look->ComposeIconSize(B_MINI_ICON));
BBitmap bitmap(iconRect, B_RGBA32);
BMimeType mimeType(fType.String()); BMimeType mimeType(fType.String());
status_t status = icon_for_type(mimeType, bitmap, B_MINI_ICON); status_t status = icon_for_type(mimeType, bitmap, B_MINI_ICON);
if (status < B_OK) { if (status < B_OK) {
@@ -101,8 +103,8 @@ MimeTypeItem::DrawItem(BView* owner, BRect frame, bool complete)
} }
if (status == B_OK) { if (status == B_OK) {
BPoint point(rect.left + 2.0f, BPoint point(rect.left + 2.0f,
rect.top + (rect.Height() - B_MINI_ICON) / 2.0f); rect.top + (rect.Height() - iconRect.Height()) / 2.0f);
owner->SetDrawingMode(B_OP_ALPHA); owner->SetDrawingMode(B_OP_ALPHA);
owner->DrawBitmap(&bitmap, point); owner->DrawBitmap(&bitmap, point);
@@ -110,7 +112,7 @@ MimeTypeItem::DrawItem(BView* owner, BRect frame, bool complete)
owner->SetDrawingMode(B_OP_COPY); owner->SetDrawingMode(B_OP_COPY);
owner->MovePenTo(rect.left + B_MINI_ICON + 8.0f, frame.top + fBaselineOffset); owner->MovePenTo(rect.left + iconRect.Width() + 8.0f, frame.top + fBaselineOffset);
owner->DrawString(Text()); owner->DrawString(Text());
owner->SetLowColor(lowColor); owner->SetLowColor(lowColor);
@@ -128,10 +130,11 @@ MimeTypeItem::Update(BView* owner, const BFont* font)
BStringItem::Update(owner, font); BStringItem::Update(owner, font);
if (fShowIcon) { if (fShowIcon) {
SetWidth(Width() + B_MINI_ICON + 2.0f); const BSize iconSize = be_control_look->ComposeIconSize(B_MINI_ICON);
SetWidth(Width() + iconSize.Width() + 2.0f);
if (Height() < B_MINI_ICON + 4.0f) if (Height() < (iconSize.Height() + 4.0f))
SetHeight(B_MINI_ICON + 4.0f); SetHeight(iconSize.Height() + 4.0f);
font_height fontHeight; font_height fontHeight;
font->GetHeight(&fontHeight); font->GetHeight(&fontHeight);
@@ -544,7 +547,7 @@ MimeTypeListView::MessageReceived(BMessage* message)
/*! /*!
\brief This method makes sure a new MIME type will be selected. \brief This method makes sure a new MIME type will be selected.
If it's not in the list yet, it will be selected as soon as it's If it's not in the list yet, it will be selected as soon as it's
added. added.
*/ */