Deskbar: Label truncation code refactor & cleanup

* Increase kVPad to 2.0f in ExpandoMenuBar matching the
  constant in WindowMenuItem
* Also replace a hardcoded instance of 4.0f with kVPad * 2
* Remove TTeamMenuItem::DrawContentLabel(), move code to DrawContent()
* Cleanup label truncation code in TTeamMenuItem, still needs work.
* Rename WindowMenuItem::FullTitle() to Name() matching TeamMenuItem
* Move the code to set all label related variables in SetLabel() in
  TeamMenuItem
* Change WindowMenuItem::SetRequireUpdate() to take a bool parameter
* Make WindowMenuItem::Initialize() private and rename it to _Init()
* Rename WindowMenuItem::ExpandedItem() to SetExpanded()
* Truncate WindowMenuItem label like we are doing TeamMenuItem
* Rename all WindowMenuItem::Title* methods and variables to Label*
This commit is contained in:
John Scipione 2015-02-12 21:01:18 -05:00
parent deaae5fc20
commit 5e625ead65
6 changed files with 158 additions and 185 deletions

View File

@ -368,10 +368,10 @@ TExpandoMenuBar::MouseMoved(BPoint where, uint32 code, const BMessage* message)
&& fBarView->ExpandoState() && item->IsExpanded()) {
// expando mode window menu item
fLastMousedOverItem = menuItem;
if (strcmp(windowMenuItem->Label(),
windowMenuItem->FullTitle()) != 0) {
if (strcasecmp(windowMenuItem->Label(),
windowMenuItem->Name()) > 0) {
// label is truncated, set tooltip
SetToolTip(windowMenuItem->FullTitle());
SetToolTip(windowMenuItem->Name());
} else
SetToolTip((const char*)NULL);
@ -946,7 +946,7 @@ TExpandoMenuBar::monitor_team_windows(void* arg)
for (int32 i = 0; i < totalItems; i++) {
if (!teamMenu->SubmenuAt(i)) {
item = static_cast<TWindowMenuItem*>(teamMenu->ItemAt(i));
item->SetRequireUpdate();
item->SetRequireUpdate(true);
}
}
@ -986,7 +986,7 @@ TExpandoMenuBar::monitor_team_windows(void* arg)
((1 << current_workspace())
& wInfo->workspaces) != 0);
if (strcmp(wInfo->name, item->Label()) != 0)
if (strcasecmp(item->Label(), wInfo->name) > 0)
item->SetLabel(wInfo->name);
if (item->ChangedState())
@ -997,7 +997,7 @@ TExpandoMenuBar::monitor_team_windows(void* arg)
wInfo->server_token, wInfo->is_mini,
((1 << current_workspace())
& wInfo->workspaces) != 0, false);
item->ExpandedItem(true);
item->SetExpanded(true);
teamMenu->AddItem(item,
TWindowMenuItem::InsertIndexFor(
teamMenu, i + 1, item));

View File

@ -60,7 +60,7 @@ All rights reserved.
const float kHPad = 8.0f;
const float kVPad = 1.0f;
const float kVPad = 2.0f;
const float kLabelOffset = 8.0f;
const float kSwitchWidth = 12.0f;
@ -91,6 +91,19 @@ TTeamMenuItem::~TTeamMenuItem()
}
void
TTeamMenuItem::SetLabel(const char* label) {
BFont font(be_plain_font);
fLabelWidth = ceilf(font.StringWidth(label));
font_height fontHeight;
font.GetHeight(&fontHeight);
fLabelAscent = ceilf(fontHeight.ascent);
fLabelDescent = ceilf(fontHeight.descent + fontHeight.leading);
BMenuItem::SetLabel(label);
}
status_t
TTeamMenuItem::Invoke(BMessage* message)
{
@ -157,13 +170,13 @@ TTeamMenuItem::GetContentSize(float* width, float* height)
*height = fOverrideHeight;
else {
if (fBarView->Vertical()) {
*height = iconBounds.Height() + kVPad * 4;
*height = iconBounds.Height() + kVPad * 2;
if (!static_cast<TBarApp*>(be_app)->Settings()->hideLabels
&& iconBounds.Width() > 32) {
*height += fLabelAscent + fLabelDescent;
}
} else {
*height = iconBounds.Height() + kVPad * 4;
*height = iconBounds.Height() + kVPad * 2;
}
}
*height += 2;
@ -243,7 +256,7 @@ TTeamMenuItem::DrawContent()
menu->DrawBitmapAsync(fIcon, dstRect);
drawLoc.x = ((frame.Width() - LabelWidth()) / 2);
drawLoc.y = frame.top + iconBounds.Height() + 4.0f;
drawLoc.y = frame.top + iconBounds.Height() + kVPad * 2;
} else {
float offsetx = contLoc.x + kHPad;
float offsety = contLoc.y +
@ -268,7 +281,49 @@ TTeamMenuItem::DrawContent()
// override the drawing of the content when the item is disabled
// the wrong lowcolor is used when the item is disabled since the
// text color does not change
DrawContentLabel();
menu->MovePenBy(0, fLabelAscent);
float cachedWidth = menu->StringWidth(Label());
if (Submenu() != NULL && fBarView->Vertical())
cachedWidth += 18;
const char* label = Label();
char* truncLabel = NULL;
float maxWidth = fBarView->Vertical()
&& static_cast<TBarApp*>(be_app)->Settings()->superExpando
? menu->MaxContentWidth() - kSwitchWidth
: menu->MaxContentWidth() - kVPad * 2;
if (maxWidth > 0) {
float offset = menu->PenLocation().x - Frame().left;
if (cachedWidth + offset > maxWidth) {
truncLabel = (char*)malloc(strlen(label) + 4);
if (truncLabel == NULL)
return;
TruncateLabel(maxWidth - offset, truncLabel);
label = truncLabel;
}
}
if (label == NULL)
label = Label();
bool canHandle = !fBarView->Dragging()
|| fBarView->AppCanHandleTypes(Signature());
if (_IsSelected() && IsEnabled() && canHandle)
menu->SetLowColor(tint_color(menu->LowColor(),
B_HIGHLIGHT_BACKGROUND_TINT));
else
menu->SetLowColor(menu->LowColor());
if (IsSelected())
menu->SetHighColor(ui_color(B_MENU_SELECTED_ITEM_TEXT_COLOR));
else
menu->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
menu->DrawString(label);
free(truncLabel);
}
if (fBarView->Vertical()
@ -279,61 +334,6 @@ TTeamMenuItem::DrawContent()
}
void
TTeamMenuItem::DrawContentLabel()
{
BMenu* menu = Menu();
menu->MovePenBy(0, fLabelAscent);
float cachedWidth = menu->StringWidth(Label());
if (Submenu() != NULL && fBarView->Vertical())
cachedWidth += 18;
const char* label = Label();
char* truncLabel = NULL;
float max = 0;
if (fBarView->Vertical()
&& static_cast<TBarApp*>(be_app)->Settings()->superExpando) {
max = menu->MaxContentWidth() - kSwitchWidth;
} else
max = menu->MaxContentWidth() - 4.0f;
if (max > 0) {
BPoint penloc = menu->PenLocation();
BRect frame = Frame();
float offset = penloc.x - frame.left;
if (cachedWidth + offset > max) {
truncLabel = (char*)malloc(strlen(label) + 4);
if (truncLabel == NULL)
return;
TruncateLabel(max-offset, truncLabel);
label = truncLabel;
}
}
if (label == NULL)
label = Label();
bool canHandle = !fBarView->Dragging()
|| fBarView->AppCanHandleTypes(Signature());
if (_IsSelected() && IsEnabled() && canHandle)
menu->SetLowColor(tint_color(menu->LowColor(),
B_HIGHLIGHT_BACKGROUND_TINT));
else
menu->SetLowColor(menu->LowColor());
if (IsSelected())
menu->SetHighColor(ui_color(B_MENU_SELECTED_ITEM_TEXT_COLOR));
else
menu->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
menu->DrawString(label);
free(truncLabel);
}
void
TTeamMenuItem::DrawExpanderArrow()
{
@ -378,7 +378,7 @@ TTeamMenuItem::ToggleExpandState(bool resizeWindow)
windowItem = static_cast<TWindowMenuItem*>
(sub->RemoveItem((int32)0));
parent->AddItem(windowItem, myindex + childIndex);
windowItem->ExpandedItem(true);
windowItem->SetExpanded(true);
}
sub->SetExpanded(true, myindex + childIndex);
@ -399,7 +399,7 @@ TTeamMenuItem::ToggleExpandState(bool resizeWindow)
windowItem
= static_cast<TWindowMenuItem*>(parent->RemoveItem(childIndex));
sub->AddItem(windowItem, 0);
windowItem->ExpandedItem(false);
windowItem->SetExpanded(false);
}
sub->SetExpanded(false, 0);
@ -464,12 +464,6 @@ TTeamMenuItem::_InitData(BList* team, BBitmap* icon, char* name, char* sig,
fOverrideHeight = height;
fBarView = static_cast<TBarApp*>(be_app)->BarView();
BFont font(be_plain_font);
fLabelWidth = ceilf(font.StringWidth(fName));
font_height fontHeight;
font.GetHeight(&fontHeight);
fLabelAscent = ceilf(fontHeight.ascent);
fLabelDescent = ceilf(fontHeight.descent + fontHeight.leading);
fOverriddenSelected = false;

View File

@ -58,7 +58,9 @@ public:
float height = -1.0f);
virtual ~TTeamMenuItem();
status_t Invoke(BMessage* msg = NULL);
virtual void SetLabel(const char* label);
status_t Invoke(BMessage* message = NULL);
void SetOverrideWidth(float width)
{ fOverrideWidth = width; };
@ -88,7 +90,6 @@ protected:
void GetContentSize(float* width, float* height);
void Draw();
void DrawContent();
void DrawContentLabel();
void DrawExpanderArrow();
private:

View File

@ -141,8 +141,9 @@ TWindowMenu::AttachedToWindow()
TWindowMenuItem* item
= static_cast<TWindowMenuItem*>(ItemAt(addIndex));
if (item != NULL
&& strcasecmp(item->FullTitle(), wInfo->name) > 0)
&& strcasecmp(item->Name(), wInfo->name) > 0) {
break;
}
}
if (!fExpanded) {

View File

@ -55,51 +55,28 @@ All rights reserved.
const float kHPad = 10.0f;
const float kVPad = 2.0f;
const float kLabelOffset = 8.0f;
const BRect kIconRect(1.0f, 1.0f, 13.0f, 14.0f);
TWindowMenuItem::TWindowMenuItem(const char* title, int32 id, bool mini,
bool currentWorkspace, bool dragging)
TWindowMenuItem::TWindowMenuItem(const char* label, int32 id, bool mini,
bool currentWorkspace, bool dragging)
:
BMenuItem(title, NULL),
BMenuItem(label, NULL),
fID(id),
fMini(mini),
fCurrentWorkSpace(currentWorkspace),
fDragging(dragging),
fExpanded(false),
fRequireUpdate(false),
fModified(false),
fFullTitle("")
fModified(false)
{
Initialize(title);
_Init(label);
}
void
TWindowMenuItem::Initialize(const char* title)
{
if (fMini) {
fBitmap = fCurrentWorkSpace
? AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowHiddenIcon)
: AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowHiddenSwitchIcon);
} else {
fBitmap = fCurrentWorkSpace
? AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowShownIcon)
: AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowShownSwitchIcon);
}
BFont font(be_plain_font);
fTitleWidth = ceilf(font.StringWidth(title));
fFullTitle = title;
font_height fontHeight;
font.GetHeight(&fontHeight);
fTitleAscent = ceilf(fontHeight.ascent);
fTitleDescent = ceilf(fontHeight.descent + fontHeight.leading);
}
void
TWindowMenuItem::SetTo(const char* title, int32 id, bool mini,
TWindowMenuItem::SetTo(const char* label, int32 id, bool mini,
bool currentWorkspace, bool dragging)
{
fModified = fCurrentWorkSpace != currentWorkspace || fMini != mini;
@ -110,7 +87,7 @@ TWindowMenuItem::SetTo(const char* title, int32 id, bool mini,
fDragging = dragging;
fRequireUpdate = false;
Initialize(title);
_Init(label);
}
@ -121,32 +98,6 @@ TWindowMenuItem::ChangedState()
}
void
TWindowMenuItem::SetLabel(const char* string)
{
fFullTitle = string;
BString truncatedTitle = fFullTitle;
if (fExpanded && Menu()) {
BPoint contLoc = ContentLocation() + BPoint(kHPad, kVPad);
contLoc.x += kIconRect.Width() + kLabelOffset;
be_plain_font->TruncateString(&truncatedTitle, B_TRUNCATE_MIDDLE,
Frame().Width() - contLoc.x - 3.0f);
}
if (strcmp(Label(), truncatedTitle.String()) != 0)
BMenuItem::SetLabel(truncatedTitle.String());
}
const char*
TWindowMenuItem::FullTitle() const
{
return fFullTitle.String();
}
/*static*/ int32
TWindowMenuItem::InsertIndexFor(BMenu* menu, int32 startIndex,
TWindowMenuItem* newItem)
@ -154,9 +105,10 @@ TWindowMenuItem::InsertIndexFor(BMenu* menu, int32 startIndex,
for (int32 index = startIndex;; index++) {
TWindowMenuItem* item
= dynamic_cast<TWindowMenuItem*>(menu->ItemAt(index));
if (item == NULL || NaturalCompare(item->FullTitle(),
newItem->FullTitle()) > 0)
if (item == NULL
|| NaturalCompare(item->Label(), newItem->Label()) > 0) {
return index;
}
}
}
@ -173,7 +125,7 @@ TWindowMenuItem::GetContentSize(float* width, float* height)
{
if (width != NULL) {
if (!fExpanded) {
*width = kHPad + fTitleWidth + kHPad;
*width = kHPad + fLabelWidth + kHPad;
if (fID >= 0)
*width += fBitmap->Bounds().Width() + kLabelOffset;
} else
@ -187,7 +139,7 @@ TWindowMenuItem::GetContentSize(float* width, float* height)
if (height != NULL) {
*height = (fID >= 0) ? fBitmap->Bounds().Height() : 0.0f;
float labelHeight = fTitleAscent + fTitleDescent;
float labelHeight = fLabelAscent + fLabelDescent;
*height = (labelHeight > *height) ? labelHeight : *height;
*height += kVPad * 2;
}
@ -228,13 +180,18 @@ TWindowMenuItem::Draw()
if (IsEnabled() && IsSelected() && !menu->IsRedrawAfterSticky()) {
// fill
menu->SetHighColor(tint_color(menuColor,
B_HIGHLIGHT_BACKGROUND_TINT));
rgb_color backColor = tint_color(menuColor,
B_HIGHLIGHT_BACKGROUND_TINT);
menu->SetLowColor(backColor);
menu->SetHighColor(backColor);
menu->FillRect(frame);
} else
} else {
menu->SetLowColor(menuColor);
menu->SetHighColor(menuColor);
}
DrawContent();
menu->PopState();
}
@ -243,18 +200,12 @@ void
TWindowMenuItem::DrawContent()
{
BMenu* menu = Menu();
menu->PushState();
BRect frame(Frame());
BPoint contLoc = ContentLocation() + BPoint(kHPad, kVPad);
//if (fExpanded)
// contLoc.x += kHPad;
if (fID >= 0) {
menu->SetDrawingMode(B_OP_OVER);
float width = fBitmap->Bounds().Width();
if (width > 16)
contLoc.x -= 8;
@ -266,22 +217,40 @@ TWindowMenuItem::DrawContent()
contLoc.x += kIconRect.Width() + kLabelOffset;
}
contLoc.y += fLabelAscent;
menu->SetDrawingMode(B_OP_COPY);
contLoc.y = frame.top
+ ((frame.Height() - fTitleAscent - fTitleDescent) / 2) + 1.0f;
menu->MovePenTo(contLoc);
float cachedWidth = menu->StringWidth(Label());
const char* label = Label();
char* truncLabel = NULL;
float maxWidth = menu->MaxContentWidth() - kVPad * 2;
if (maxWidth > 0) {
float offset = menu->PenLocation().x - Frame().left;
if (cachedWidth + offset > maxWidth) {
truncLabel = (char*)malloc(strlen(label) + 4);
if (truncLabel == NULL)
return;
TruncateLabel(maxWidth - offset, truncLabel);
label = truncLabel;
}
}
if (label == NULL)
label = Label();
SetLabel(label);
if (IsSelected())
menu->SetHighColor(ui_color(B_MENU_SELECTED_ITEM_TEXT_COLOR));
else
menu->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
BMenuItem::DrawContent();
menu->DrawString(label);
menu->PopState();
free(truncLabel);
}
@ -314,25 +283,29 @@ TWindowMenuItem::Invoke(BMessage* /*message*/)
}
// #pragma mark - private methods
void
TWindowMenuItem::ExpandedItem(bool status)
TWindowMenuItem::_Init(const char* label)
{
if (fExpanded != status) {
fExpanded = status;
SetLabel(fFullTitle.String());
if (fMini) {
fBitmap = fCurrentWorkSpace
? AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowHiddenIcon)
: AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowHiddenSwitchIcon);
} else {
fBitmap = fCurrentWorkSpace
? AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowShownIcon)
: AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowShownSwitchIcon);
}
}
void
TWindowMenuItem::SetRequireUpdate()
{
fRequireUpdate = true;
}
bool
TWindowMenuItem::RequiresUpdate()
{
return fRequireUpdate;
fName = label;
BFont font(be_plain_font);
fLabelWidth = ceilf(font.StringWidth(label));
font_height fontHeight;
font.GetHeight(&fontHeight);
fLabelAscent = ceilf(fontHeight.ascent);
fLabelDescent = ceilf(fontHeight.descent + fontHeight.leading);
SetLabel(label);
}

View File

@ -47,45 +47,49 @@ class BBitmap;
// sub of TeamMenuItem all DB positions
class TWindowMenuItem : public BMenuItem {
public:
TWindowMenuItem(const char* title, int32 id,
TWindowMenuItem(const char* label, int32 id,
bool mini, bool currentWorkSpace,
bool dragging = false);
void ExpandedItem(bool state);
void SetTo(const char* title, int32 id, bool mini,
void SetTo(const char* label, int32 id, bool mini,
bool currentWorkSpace,
bool dragging = false);
int32 ID();
void SetRequireUpdate();
bool RequiresUpdate();
bool ChangedState();
virtual void SetLabel(const char* string);
const char* FullTitle() const;
int32 ID();
const char* Name() const { return fName; };
bool Expanded() { return fExpanded; };
void SetExpanded(bool expand) { fExpanded = expand; };
bool RequiresUpdate() { return fRequireUpdate; };
void SetRequireUpdate(bool update) { fRequireUpdate = update; };
bool ChangedState();
static int32 InsertIndexFor(BMenu* menu, int32 startIndex,
TWindowMenuItem* item);
protected:
void Initialize(const char* title);
virtual void GetContentSize(float* width, float* height);
virtual void DrawContent();
virtual status_t Invoke(BMessage* message = NULL);
virtual void Draw();
private:
void _Init(const char* label);
int32 fID;
bool fMini;
bool fCurrentWorkSpace;
const BBitmap* fBitmap;
float fTitleWidth;
float fTitleAscent;
float fTitleDescent;
float fLabelWidth;
float fLabelAscent;
float fLabelDescent;
bool fDragging;
bool fExpanded;
bool fRequireUpdate;
bool fModified;
BString fFullTitle;
const char* fName;
};