Tracker: Optimisation of AddonMenu menu construction

1. Build the list of mimetypes of files in selection only once and
reuse it for all further tests.

2. Fix a regression introduced in hrev44384 where the MimeType()
wouldn't get recognized when just changed by tracker (by that same
right click).  It would be on subsequent clicks.

3. Rename the static map variable to better fit our coding style
and be more understandable.
This commit is contained in:
Philippe Saint-Pierre
2012-07-23 14:47:24 -04:00
parent b6a70ecba9
commit 5cdd07a814
3 changed files with 41 additions and 35 deletions
+34 -30
View File
@@ -130,6 +130,7 @@ class DraggableContainerIcon : public BView {
struct AddOneAddonParams { struct AddOneAddonParams {
BObjectList<BMenuItem> *primaryList; BObjectList<BMenuItem> *primaryList;
BObjectList<BMenuItem> *secondaryList; BObjectList<BMenuItem> *secondaryList;
BObjectList<BString> *mimeTypes;
}; };
struct StaggerOneParams { struct StaggerOneParams {
@@ -2838,34 +2839,12 @@ BContainerWindow::EachAddon(BPath &path, bool (*eachAddon)(const Model *,
BDirectory dir; BDirectory dir;
BEntry entry; BEntry entry;
BObjectList<BString> *mimeTypes = ((AddOneAddonParams *)params)->mimeTypes;
if (dir.SetTo(path.Path()) != B_OK) if (dir.SetTo(path.Path()) != B_OK)
return false; return false;
// build a list of the MIME types of the selected items
BObjectList<BString> mimeTypes(10, true);
int32 count = PoseView()->SelectionList()->CountItems();
if (!count) {
// just add the type of the current directory
AddMimeTypeString(mimeTypes, TargetModel());
} else {
for (int32 index = 0; index < count; index++) {
BPose *pose = PoseView()->SelectionList()->ItemAt(index);
AddMimeTypeString(mimeTypes, pose->TargetModel());
// If it's a symlink, resolves it and add the Target's MimeType
if (pose->TargetModel()->IsSymLink()) {
Model* resolved = new Model(
pose->TargetModel()->EntryRef(), true, true);
if (resolved->InitCheck() == B_OK) {
AddMimeTypeString(mimeTypes, resolved);
}
delete resolved;
}
}
}
dir.Rewind(); dir.Rewind();
while (dir.GetNextEntry(&entry) == B_OK) { while (dir.GetNextEntry(&entry) == B_OK) {
Model *model = new Model(&entry); Model *model = new Model(&entry);
@@ -2887,7 +2866,7 @@ BContainerWindow::EachAddon(BPath &path, bool (*eachAddon)(const Model *,
bool primary = false; bool primary = false;
if (mimeTypes.CountItems()) { if (mimeTypes->CountItems()) {
BFile file(&entry, B_READ_ONLY); BFile file(&entry, B_READ_ONLY);
if (file.InitCheck() == B_OK) { if (file.InitCheck() == B_OK) {
BAppFileInfo info(&file); BAppFileInfo info(&file);
@@ -2905,8 +2884,8 @@ BContainerWindow::EachAddon(BPath &path, bool (*eachAddon)(const Model *,
// check all supported types if it has some set // check all supported types if it has some set
if (!secondary) { if (!secondary) {
for (int32 i = mimeTypes.CountItems(); !primary && i-- > 0;) { for (int32 i = mimeTypes->CountItems(); !primary && i-- > 0;) {
BString *type = mimeTypes.ItemAt(i); BString *type = mimeTypes->ItemAt(i);
if (info.IsSupportedType(type->String())) { if (info.IsSupportedType(type->String())) {
BMimeType mimeType(type->String()); BMimeType mimeType(type->String());
if (info.Supports(&mimeType)) if (info.Supports(&mimeType))
@@ -2970,8 +2949,6 @@ BContainerWindow::BuildAddOnMenu(BMenu *menu)
break; break;
delete item; delete item;
} }
_UpdateSelectionMIMEInfo();
BObjectList<BMenuItem> primaryList; BObjectList<BMenuItem> primaryList;
BObjectList<BMenuItem> secondaryList; BObjectList<BMenuItem> secondaryList;
@@ -2980,12 +2957,39 @@ BContainerWindow::BuildAddOnMenu(BMenu *menu)
params.primaryList = &primaryList; params.primaryList = &primaryList;
params.secondaryList = &secondaryList; params.secondaryList = &secondaryList;
// build a list of the MIME types of the selected items
BObjectList<BString> mimeTypes(10, true);
int32 count = PoseView()->SelectionList()->CountItems();
if (!count) {
// just add the type of the current directory
AddMimeTypeString(mimeTypes, TargetModel());
} else {
_UpdateSelectionMIMEInfo();
for (int32 index = 0; index < count; index++) {
BPose *pose = PoseView()->SelectionList()->ItemAt(index);
AddMimeTypeString(mimeTypes, pose->TargetModel());
// If it's a symlink, resolves it and add the Target's MimeType
if (pose->TargetModel()->IsSymLink()) {
Model* resolved = new Model(
pose->TargetModel()->EntryRef(), true, true);
if (resolved->InitCheck() == B_OK) {
AddMimeTypeString(mimeTypes, resolved);
}
delete resolved;
}
}
}
params.mimeTypes = &mimeTypes;
EachAddon(AddOneAddon, &params); EachAddon(AddOneAddon, &params);
primaryList.SortItems(CompareLabels); primaryList.SortItems(CompareLabels);
secondaryList.SortItems(CompareLabels); secondaryList.SortItems(CompareLabels);
int32 count = primaryList.CountItems(); count = primaryList.CountItems();
for (int32 index = 0; index < count; index++) for (int32 index = 0; index < count; index++)
menu->AddItem(primaryList.ItemAt(index)); menu->AddItem(primaryList.ItemAt(index));
+2
View File
@@ -883,6 +883,8 @@ Model::AttrChanged(const char *attrName)
if (!attrName if (!attrName
|| strcmp(attrName, kAttrMIMEType) == 0 || strcmp(attrName, kAttrMIMEType) == 0
|| strcmp(attrName, kAttrPreferredApp) == 0) { || strcmp(attrName, kAttrPreferredApp) == 0) {
ModelNodeLazyOpener opener(this);
opener.OpenNode();
char mimeString[B_MIME_TYPE_LENGTH]; char mimeString[B_MIME_TYPE_LENGTH];
BNodeInfo info(fNode); BNodeInfo info(fNode);
if (info.GetType(mimeString) != B_OK) if (info.GetType(mimeString) != B_OK)
+5 -5
View File
@@ -155,7 +155,7 @@ struct attr_column_relation {
}; };
static struct attr_column_relation attributes[] = { static struct attr_column_relation sAttrColumnMap[] = {
{ AttrHashString(kAttrStatModified, B_TIME_TYPE), { AttrHashString(kAttrStatModified, B_TIME_TYPE),
B_STAT_MODIFICATION_TIME }, B_STAT_MODIFICATION_TIME },
{ AttrHashString(kAttrStatSize, B_OFF_T_TYPE), { AttrHashString(kAttrStatSize, B_OFF_T_TYPE),
@@ -5466,11 +5466,11 @@ BPoseView::AttributeChanged(const BMessage *message)
if (message->FindInt32("fields", &fields) != B_OK) if (message->FindInt32("fields", &fields) != B_OK)
return true; return true;
for (int32 i = sizeof(attributes) / sizeof(attr_column_relation); for (int i = sizeof(sAttrColumnMap) / sizeof(attr_column_relation);
i--;) { i--;) {
if (attributes[i].attrHash == PrimarySort() if (sAttrColumnMap[i].attrHash == PrimarySort()
|| attributes[i].attrHash == SecondarySort()) { || sAttrColumnMap[i].attrHash == SecondarySort()) {
if ((fields & attributes[i].fieldMask) != 0) { if ((fields & sAttrColumnMap[i].fieldMask) != 0) {
_CheckPoseSortOrder(fPoseList, pose, poseListIndex); _CheckPoseSortOrder(fPoseList, pose, poseListIndex);
if (fFiltering && visible) if (fFiltering && visible)
_CheckPoseSortOrder(fFilteredPoseList, pose, index); _CheckPoseSortOrder(fFilteredPoseList, pose, index);