0ffe3e5ce8
CreateAccountMenu() is used to populate a BMenu with items corresponding to the map of accounts provided to it― but when an account is disabled or enabled, it can't update automatically. A dedicated class, fAccountsMenu, now replaces it― it'll automatically repopulate the list whenever the active accounts update.
66 lines
1.3 KiB
C++
66 lines
1.3 KiB
C++
/*
|
|
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
|
* All rights reserved. Distributed under the terms of the MIT license.
|
|
*/
|
|
|
|
#include "AccountsMenu.h"
|
|
#include "MainWindow.h"
|
|
#include "Server.h"
|
|
#include "TheApp.h"
|
|
|
|
#include <MenuItem.h>
|
|
|
|
|
|
AccountsMenu::AccountsMenu(const char* name, BMessage msg, BMessage* allMsg)
|
|
:
|
|
BMenu(name),
|
|
fAccountMessage(msg),
|
|
fAllMessage(allMsg)
|
|
{
|
|
_PopulateMenu();
|
|
|
|
SetRadioMode(true);
|
|
SetLabelFromMarked(true);
|
|
|
|
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
|
server->RegisterObserver(this);
|
|
}
|
|
|
|
|
|
AccountsMenu::~AccountsMenu()
|
|
{
|
|
delete fAllMessage;
|
|
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
|
server->UnregisterObserver(this);
|
|
}
|
|
|
|
|
|
void
|
|
AccountsMenu::ObserveInteger(int32 what, int32 value)
|
|
{
|
|
_PopulateMenu();
|
|
}
|
|
|
|
|
|
void
|
|
AccountsMenu::_PopulateMenu()
|
|
{
|
|
if (CountItems() > 0)
|
|
RemoveItems(0, CountItems(), true);
|
|
|
|
if (fAllMessage != NULL)
|
|
AddItem(new BMenuItem("All", new BMessage(*fAllMessage)));
|
|
|
|
Server* server = ((TheApp*)be_app)->GetMainWindow()->GetServer();
|
|
AccountInstances accounts = server->GetActiveAccounts();
|
|
|
|
for (int i = 0; i < accounts.CountItems(); i++)
|
|
AddItem(new BMenuItem(accounts.KeyAt(i).String(),
|
|
new BMessage(fAccountMessage)));
|
|
|
|
if (CountItems() > 0)
|
|
ItemAt(0)->SetMarked(true);
|
|
else
|
|
SetEnabled(false);
|
|
}
|