Make AccountsMenu use instance rather than index

This commit is contained in:
Jaidyn Ann 2021-08-06 11:04:00 -05:00
parent 3861fa718d
commit 39e4fc9ac7
2 changed files with 22 additions and 15 deletions

View File

@ -23,7 +23,7 @@
#define B_TRANSLATION_CONTEXT "AccountsMenu" #define B_TRANSLATION_CONTEXT "AccountsMenu"
int32 AccountsMenu::fDefaultSelection = 0; int64 AccountsMenu::fDefaultSelection = -1;
AccountsMenu::AccountsMenu(const char* name, BMessage msg, BMessage* allMsg, AccountsMenu::AccountsMenu(const char* name, BMessage msg, BMessage* allMsg,
@ -49,8 +49,6 @@ AccountsMenu::AccountsMenu(const char* name, BMessage msg, BMessage* allMsg)
} }
AccountsMenu::~AccountsMenu() AccountsMenu::~AccountsMenu()
{ {
delete fAllMessage; delete fAllMessage;
@ -68,9 +66,7 @@ AccountsMenu::ObserveInteger(int32 what, int32 value)
void void
AccountsMenu::SetDefaultSelection(BMenuItem* item) AccountsMenu::SetDefaultSelection(BMenuItem* item)
{ {
fDefaultSelection = IndexOf(item); fDefaultSelection = item->Message()->GetInt64("instance", -1);
if (fAllMessage != NULL)
fDefaultSelection--;
} }
@ -89,6 +85,10 @@ AccountsMenu::_PopulateMenu()
// Add protocol item if not already in menu // Add protocol item if not already in menu
for (int i = 0; i < accounts.CountItems(); i++) { for (int i = 0; i < accounts.CountItems(); i++) {
int64 instance = accounts.ValueAt(i); int64 instance = accounts.ValueAt(i);
// Initialize default selection if necessary
if (fDefaultSelection == -1)
fDefaultSelection = instance;
BString label = accounts.KeyAt(i).String(); BString label = accounts.KeyAt(i).String();
if (label.CountChars() > 15) { if (label.CountChars() > 15) {
label.RemoveChars(16, label.CountChars() - 16); label.RemoveChars(16, label.CountChars() - 16);
@ -106,8 +106,6 @@ AccountsMenu::_PopulateMenu()
AddItem(new AccountMenuItem(label.String(), message, icon)); AddItem(new AccountMenuItem(label.String(), message, icon));
} }
int32 selection = fDefaultSelection;
// If an account has been disabled since last population… get ridda it // If an account has been disabled since last population… get ridda it
if ((fAllMessage != NULL && CountItems() - 1 > accounts.CountItems()) if ((fAllMessage != NULL && CountItems() - 1 > accounts.CountItems())
|| (fAllMessage == NULL && CountItems() > accounts.CountItems())) || (fAllMessage == NULL && CountItems() > accounts.CountItems()))
@ -118,17 +116,25 @@ AccountsMenu::_PopulateMenu()
if (accounts.ValueAt(j) == instance) if (accounts.ValueAt(j) == instance)
found = true; found = true;
if (fAllMessage != NULL && i == 0)
continue;
if (found == false) if (found == false)
RemoveItem(i); RemoveItem(i);
} }
// Deselect all
for (int i = 0; i < CountItems(); i++)
ItemAt(i)->SetMarked(false);
// Apply last/default selection // Apply last/default selection
if (fAllMessage == NULL && selection < CountItems() && selection >= 0) BMenuItem* selection = ItemAt(0);
ItemAt(selection)->SetMarked(true); if (fAllMessage == NULL)
else if (CountItems() > 0) for (int i = 0; i < CountItems(); i++) {
ItemAt(0)->SetMarked(true); BMenuItem* item = ItemAt(i);
else if (item->Message()->GetInt64("instance", -1) == fDefaultSelection)
SetEnabled(false); selection = item;
}
selection->SetMarked(true);
} }

View File

@ -24,6 +24,7 @@ public:
virtual void ObserveInteger(int32 what, int32 value); virtual void ObserveInteger(int32 what, int32 value);
void SetDefaultSelection(BMenuItem* item); void SetDefaultSelection(BMenuItem* item);
int64 GetDefaultSelection() { return fDefaultSelection; }
private: private:
void _PopulateMenu(); void _PopulateMenu();
@ -35,7 +36,7 @@ private:
BMessage fAccountMessage; BMessage fAccountMessage;
BMessage* fAllMessage; BMessage* fAllMessage;
static int32 fDefaultSelection; static int64 fDefaultSelection;
Server* fServer; Server* fServer;
}; };