Deskbar: Truncate menu item labels refactor

* Create a new TTruncatableMenuItem class to store the truncated string
  and share the label truncation code between TTeamMenuItem and
  TWindowMenuItem. In the future more shared code may be added to
  this class, if so, the class name may change.
* The TTeamMenuItem and TWindowMenuItem classes inherit from
  TTruncatableMenuItem, which inherits from BMenuItem.
* Make sure to truncate the label before drawing it in all cases.
* Some other related refactoring.

Fixes #9507
This commit is contained in:
John Scipione 2015-02-19 18:24:17 -05:00
parent 25295e6899
commit c9363f78d6
10 changed files with 250 additions and 132 deletions

View File

@ -993,7 +993,7 @@ TExpandoMenuBar::monitor_team_windows(void* arg)
if (strcasecmp(item->Label(), wInfo->name) > 0)
item->SetLabel(wInfo->name);
if (item->ChangedState())
if (item->Modified())
itemModified = true;
} else if (teamItem->IsExpanded()) {
// Add the item

View File

@ -35,6 +35,7 @@ Application Deskbar :
TeamMenu.cpp
TeamMenuItem.cpp
TimeView.cpp
TruncatableMenuItem.cpp
WindowMenu.cpp
WindowMenuItem.cpp
ResourceSet.cpp

View File

@ -42,6 +42,7 @@ All rights reserved.
#include <Debug.h>
#include <Roster.h>
#include <WindowInfo.h>
#include "WindowMenuItem.h"
#include "tracker_private.h"

View File

@ -49,6 +49,7 @@ All rights reserved.
#include <Roster.h>
#include <Screen.h>
#include <String.h>
#include <WindowInfo.h>
#include "BarApp.h"
#include "ResourceSet.h"

View File

@ -65,18 +65,18 @@ const float kLabelOffset = 8.0f;
const float kSwitchWidth = 12.0f;
TTeamMenuItem::TTeamMenuItem(BList* team, BBitmap* icon, char* name, char* sig,
float width, float height)
TTeamMenuItem::TTeamMenuItem(BList* team, BBitmap* icon, char* name,
char* signature, float width, float height)
:
BMenuItem(new TWindowMenu(team, sig))
TTruncatableMenuItem(new TWindowMenu(team, signature))
{
_InitData(team, icon, name, sig, width, height);
_InitData(team, icon, name, signature, width, height);
}
TTeamMenuItem::TTeamMenuItem(float width, float height)
:
BMenuItem("", NULL)
TTruncatableMenuItem("", NULL)
{
_InitData(NULL, NULL, strdup(""), strdup(""), width, height);
}
@ -87,20 +87,7 @@ TTeamMenuItem::~TTeamMenuItem()
delete fTeam;
delete fIcon;
free(fName);
free(fSig);
}
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);
free(fSignature);
}
@ -243,35 +230,35 @@ TTeamMenuItem::DrawContent()
BRect iconBounds(fIcon->Bounds());
BRect dstRect(iconBounds);
float extra = fBarView->Vertical() ? 0.0f : -1.0f;
BPoint contLoc = ContentLocation();
BPoint drawLoc = contLoc + BPoint(kHPad, kVPad);
BPoint contentLocation = ContentLocation();
BPoint drawLocation = contentLocation + BPoint(kHPad, kVPad);
if (static_cast<TBarApp*>(be_app)->Settings()->hideLabels
|| (fBarView->Vertical() && iconBounds.Width() > 32)) {
float offsetx = contLoc.x
float offsetx = contentLocation.x
+ ((frame.Width() - iconBounds.Width()) / 2) + extra;
float offsety = contLoc.y + 3.0f + extra;
float offsety = contentLocation.y + 3.0f + extra;
dstRect.OffsetTo(BPoint(offsetx, offsety));
menu->DrawBitmapAsync(fIcon, dstRect);
drawLoc.x = ((frame.Width() - LabelWidth()) / 2);
drawLoc.y = frame.top + iconBounds.Height() + kVPad * 2;
drawLocation.x = ((frame.Width() - LabelWidth()) / 2);
drawLocation.y = frame.top + iconBounds.Height() + kVPad * 2;
} else {
float offsetx = contLoc.x + kHPad;
float offsety = contLoc.y +
float offsetx = contentLocation.x + kHPad;
float offsety = contentLocation.y +
((frame.Height() - iconBounds.Height()) / 2) + extra;
dstRect.OffsetTo(BPoint(offsetx, offsety));
menu->DrawBitmapAsync(fIcon, dstRect);
float labelHeight = fLabelAscent + fLabelDescent;
drawLoc.x += iconBounds.Width() + kLabelOffset;
drawLoc.y = frame.top + ((frame.Height() - labelHeight) / 2)
drawLocation.x += iconBounds.Width() + kLabelOffset;
drawLocation.y = frame.top + ((frame.Height() - labelHeight) / 2)
+ extra;
}
menu->MovePenTo(drawLoc);
menu->MovePenTo(drawLocation);
}
menu->SetDrawingMode(B_OP_OVER);
@ -282,34 +269,6 @@ TTeamMenuItem::DrawContent()
// text color does not change
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) {
TruncateLabel(maxWidth - offset, truncLabel);
label = truncLabel;
}
}
}
if (label == NULL)
label = Label();
BMenuItem::SetLabel(label);
free(truncLabel);
bool canHandle = !fBarView->Dragging()
|| fBarView->AppCanHandleTypes(Signature());
if (_IsSelected() && IsEnabled() && canHandle)
@ -323,8 +282,12 @@ TTeamMenuItem::DrawContent()
else
menu->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
if (!static_cast<TBarApp*>(be_app)->Settings()->hideLabels)
menu->DrawString(label);
if (!static_cast<TBarApp*>(be_app)->Settings()->hideLabels) {
float labelWidth = menu->StringWidth(Label());
BPoint penLocation = menu->PenLocation();
float offset = penLocation.x - Frame().left;
menu->DrawString(Label(labelWidth + offset));
}
if (fBarView->Vertical()
&& static_cast<TBarApp*>(be_app)->Settings()->superExpando
@ -339,7 +302,7 @@ TTeamMenuItem::DrawExpanderArrow()
{
BMenu* menu = Menu();
BRect frame(Frame());
BRect rect(0, 0, kSwitchWidth, 10);
BRect rect(0.0f, 0.0f, kSwitchWidth, kHPad + 2.0f);
rect.OffsetTo(BPoint(frame.right - rect.Width(),
ContentLocation().y + ((frame.Height() - rect.Height()) / 2)));
@ -447,19 +410,27 @@ TTeamMenuItem::ExpanderBounds() const
void
TTeamMenuItem::_InitData(BList* team, BBitmap* icon, char* name, char* sig,
TTeamMenuItem::_InitData(BList* team, BBitmap* icon, char* name, char* signature,
float width, float height)
{
fTeam = team;
fIcon = icon;
fName = name;
fSig = sig;
fSignature = signature;
if (fName == NULL) {
char temp[32];
snprintf(temp, sizeof(temp), "team %ld", (addr_t)team->ItemAt(0));
fName = strdup(temp);
}
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);
SetLabel(fName);
fOverrideWidth = width;
fOverrideHeight = height;

View File

@ -41,25 +41,23 @@ All rights reserved.
// item for ExpandoMenuBar in vertical or horizontal expanded mode
#include <MenuItem.h>
#include "WindowMenuItem.h"
#include "BarMenuBar.h"
#include "TruncatableMenuItem.h"
class BBitmap;
class TBarView;
class TWindowMenuItem;
class TTeamMenuItem : public BMenuItem {
class TTeamMenuItem : public TTruncatableMenuItem {
public:
TTeamMenuItem(BList* team, BBitmap* icon,
char* name, char* sig,
char* name, char*
nature,
float width = -1.0f, float height = -1.0f);
TTeamMenuItem(float width = -1.0f,
float height = -1.0f);
virtual ~TTeamMenuItem();
virtual void SetLabel(const char* label);
status_t Invoke(BMessage* message = NULL);
void SetOverrideWidth(float width)
@ -83,7 +81,7 @@ public:
float LabelWidth() const { return fLabelWidth; };
BList* Teams() const { return fTeam; };
const char* Signature() const { return fSig; };
const char* Signature() const { return fSignature; };
const char* Name() const { return fName; };
protected:
@ -95,7 +93,7 @@ protected:
private:
friend class TExpandoMenuBar;
void _InitData(BList* team, BBitmap* icon,
char* name, char* sig,
char* name, char* signature,
float width = -1.0f, float height = -1.0f);
bool _IsSelected() const;
@ -104,7 +102,7 @@ private:
BList* fTeam;
BBitmap* fIcon;
char* fName;
char* fSig;
char* fSignature;
float fOverrideWidth;
float fOverrideHeight;

View File

@ -0,0 +1,121 @@
/*
Open Tracker License
Terms and Conditions
Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice applies to all licensees
and shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Be Incorporated shall not be
used in advertising or otherwise to promote the sale, use or other dealings in
this Software without prior written authorization from Be Incorporated.
Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered
trademarks of Be Incorporated in the United States and other countries. Other
brand product names are registered trademarks or trademarks of their respective
holders.
All rights reserved.
*/
#include "TruncatableMenuItem.h"
#include <stdlib.h>
#include <String.h>
#include "BarApp.h"
#include "BarView.h"
#include "ExpandoMenuBar.h"
const float kVPad = 2.0f;
const float kSwitchWidth = 12.0f;
// #pragma mark - TTruncatableMenuItem
TTruncatableMenuItem::TTruncatableMenuItem(const char* label, BMessage* message,
char shortcut, uint32 modifiers)
:
BMenuItem(label, message, shortcut, modifiers),
fTruncatedString(new BString())
{
}
TTruncatableMenuItem::TTruncatableMenuItem(BMenu* menu, BMessage* message)
:
BMenuItem(menu, message),
fTruncatedString(new BString())
{
}
TTruncatableMenuItem::TTruncatableMenuItem(BMessage* data)
:
BMenuItem(data),
fTruncatedString(new BString())
{
}
TTruncatableMenuItem::~TTruncatableMenuItem()
{
delete fTruncatedString;
}
const char*
TTruncatableMenuItem::Label()
{
return BMenuItem::Label();
}
const char*
TTruncatableMenuItem::Label(float width)
{
BMenu* menu = Menu();
TExpandoMenuBar* expandoMenuBar = dynamic_cast<TExpandoMenuBar*>(menu);
float maxWidth = menu->MaxContentWidth() - kVPad * 2;
if (expandoMenuBar != NULL
&& static_cast<TBarApp*>(be_app)->BarView()->Vertical()
&& static_cast<TBarApp*>(be_app)->Settings()->superExpando) {
maxWidth -= kSwitchWidth;
}
const char* label = Label();
if (width > 0 && maxWidth > 0 && width > maxWidth) {
char* truncatedLabel = (char*)malloc(strlen(label) + 4);
if (truncatedLabel != NULL) {
float labelWidth = menu->StringWidth(label);
float offset = width - labelWidth;
TruncateLabel(maxWidth - offset, truncatedLabel);
fTruncatedString->SetTo(truncatedLabel);
free(truncatedLabel);
return fTruncatedString->String();
}
}
return label;
}

View File

@ -0,0 +1,60 @@
/*
Open Tracker License
Terms and Conditions
Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice applies to all licensees
and shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Be Incorporated shall not be
used in advertising or otherwise to promote the sale, use or other dealings in
this Software without prior written authorization from Be Incorporated.
Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered
trademarks of Be Incorporated in the United States and other countries. Other
brand product names are registered trademarks or trademarks of their respective
holders.
All rights reserved.
*/
#ifndef TRUNCATE_MENU_ITEM_H
#define TRUNCATE_MENU_ITEM_H
#include <MenuItem.h>
class BString;
class TTruncatableMenuItem : public BMenuItem {
public:
TTruncatableMenuItem(const char* label, BMessage* message,
char shortcut = 0, uint32 modifiers = 0);
TTruncatableMenuItem(BMenu* menu, BMessage* message = NULL);
TTruncatableMenuItem(BMessage* data);
virtual ~TTruncatableMenuItem();
virtual const char* Label();
virtual const char* Label(float width);
private:
BString* fTruncatedString;
};
#endif // TRUNCATE_MENU_ITEM_H

View File

@ -36,8 +36,6 @@ All rights reserved.
#include "WindowMenuItem.h"
#include <stdio.h>
#include <Bitmap.h>
#include <Debug.h>
#include <NaturalCompare.h>
@ -62,7 +60,7 @@ const BRect kIconRect(1.0f, 1.0f, 13.0f, 14.0f);
TWindowMenuItem::TWindowMenuItem(const char* label, int32 id, bool mini,
bool currentWorkspace, bool dragging)
:
BMenuItem(label, NULL),
TTruncatableMenuItem(label, NULL),
fID(id),
fMini(mini),
fCurrentWorkSpace(currentWorkspace),
@ -91,13 +89,6 @@ TWindowMenuItem::SetTo(const char* label, int32 id, bool mini,
}
bool
TWindowMenuItem::ChangedState()
{
return fModified;
}
/*static*/ int32
TWindowMenuItem::InsertIndexFor(BMenu* menu, int32 startIndex,
TWindowMenuItem* newItem)
@ -113,13 +104,6 @@ TWindowMenuItem::InsertIndexFor(BMenu* menu, int32 startIndex,
}
int32
TWindowMenuItem::ID()
{
return fID;
}
void
TWindowMenuItem::GetContentSize(float* width, float* height)
{
@ -200,57 +184,38 @@ void
TWindowMenuItem::DrawContent()
{
BMenu* menu = Menu();
BPoint contLoc = ContentLocation() + BPoint(kHPad, kVPad);
BPoint contentLocation = ContentLocation() + BPoint(kHPad, kVPad);
if (fID >= 0) {
menu->SetDrawingMode(B_OP_OVER);
float width = fBitmap->Bounds().Width();
if (width > 16)
contLoc.x -= 8;
contentLocation.x -= 8;
menu->MovePenTo(contLoc);
menu->MovePenTo(contentLocation);
menu->DrawBitmapAsync(fBitmap);
if (width > 16)
contLoc.x += 8;
contentLocation.x += 8;
contLoc.x += kIconRect.Width() + kLabelOffset;
contentLocation.x += kIconRect.Width() + kLabelOffset;
}
contLoc.y += fLabelAscent;
contentLocation.y += fLabelAscent;
menu->SetDrawingMode(B_OP_COPY);
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);
menu->MovePenTo(contentLocation);
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);
float labelWidth = menu->StringWidth(Label());
BPoint penLocation = menu->PenLocation();
float offset = penLocation.x - Frame().left;
free(truncLabel);
menu->DrawString(Label(labelWidth + offset));
}

View File

@ -36,16 +36,16 @@ All rights reserved.
#define WINDOWMENUITEM_H
#include <MenuItem.h>
#include "TruncatableMenuItem.h"
#include <String.h>
#include <WindowInfo.h>
class BBitmap;
// Individual windows of an application item for WindowMenu,
// sub of TeamMenuItem all DB positions
class TWindowMenuItem : public BMenuItem {
class TWindowMenuItem : public TTruncatableMenuItem {
public:
TWindowMenuItem(const char* label, int32 id,
bool mini, bool currentWorkSpace,
@ -55,16 +55,16 @@ public:
bool currentWorkSpace,
bool dragging = false);
int32 ID();
const char* Name() const { return fName; };
bool Expanded() { return fExpanded; };
bool Expanded() const { return fExpanded; };
void SetExpanded(bool expand) { fExpanded = expand; };
bool RequiresUpdate() { return fRequireUpdate; };
void SetRequireUpdate(bool update) { fRequireUpdate = update; };
int32 ID() const { return fID; };
bool Modified() const { return fModified; };
const char* Name() const { return fName; };
bool ChangedState();
bool RequiresUpdate() { return fRequireUpdate; };
void SetRequireUpdate(bool update)
{ fRequireUpdate = update; };
static int32 InsertIndexFor(BMenu* menu, int32 startIndex,
TWindowMenuItem* item);