Merge pull request #1073 from leo78913/accounts-menus-fixes

This commit is contained in:
Tayou 2023-06-06 20:34:41 +02:00 committed by GitHub
commit 3b9d822d72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 61 deletions

View File

@ -200,7 +200,6 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
helpMenuButton->setPopupMode(QToolButton::InstantPopup);
auto accountMenuButton = dynamic_cast<QToolButton*>(ui->mainToolBar->widgetForAction(ui->actionAccountsButton));
ui->actionAccountsButton->setMenu(ui->accountsMenu);
accountMenuButton->setPopupMode(QToolButton::InstantPopup);
}
@ -417,15 +416,6 @@ void MainWindow::keyReleaseEvent(QKeyEvent *event)
void MainWindow::retranslateUi()
{
auto accounts = APPLICATION->accounts();
MinecraftAccountPtr defaultAccount = accounts->defaultAccount();
if(defaultAccount) {
auto profileLabel = profileInUseFilter(defaultAccount->profileName(), defaultAccount->isInUse());
ui->actionAccountsButton->setText(profileLabel);
}
else {
ui->actionAccountsButton->setText(tr("Accounts"));
}
if (m_selectedInstance) {
m_statusLeft->setText(m_selectedInstance->getStatusbarDescription());
@ -435,6 +425,12 @@ void MainWindow::retranslateUi()
ui->retranslateUi(this);
MinecraftAccountPtr defaultAccount = APPLICATION->accounts()->defaultAccount();
if(defaultAccount) {
auto profileLabel = profileInUseFilter(defaultAccount->profileName(), defaultAccount->isInUse());
ui->actionAccountsButton->setText(profileLabel);
}
changeIconButton->setToolTip(ui->actionChangeInstIcon->toolTip());
renameButton->setToolTip(ui->actionRenameInstance->toolTip());
@ -676,6 +672,15 @@ void MainWindow::repopulateAccountsMenu()
{
ui->accountsMenu->clear();
// NOTE: this is done so the accounts button text is not set to the accounts menu title
QMenu *accountsButtonMenu = ui->actionAccountsButton->menu();
if (accountsButtonMenu) {
accountsButtonMenu->clear();
} else {
accountsButtonMenu = new QMenu(this);
ui->actionAccountsButton->setMenu(accountsButtonMenu);
}
auto accounts = APPLICATION->accounts();
MinecraftAccountPtr defaultAccount = accounts->defaultAccount();
@ -690,6 +695,8 @@ void MainWindow::repopulateAccountsMenu()
}
}
QActionGroup* accountsGroup = new QActionGroup(this);
if (accounts->count() <= 0)
{
ui->actionNoAccountsAdded->setEnabled(false);
@ -705,6 +712,7 @@ void MainWindow::repopulateAccountsMenu()
QAction *action = new QAction(profileLabel, this);
action->setData(i);
action->setCheckable(true);
action->setActionGroup(accountsGroup);
if (defaultAccount == account)
{
action->setChecked(true);
@ -733,6 +741,7 @@ void MainWindow::repopulateAccountsMenu()
ui->actionNoDefaultAccount->setData(-1);
ui->actionNoDefaultAccount->setChecked(!defaultAccount);
ui->actionNoDefaultAccount->setActionGroup(accountsGroup);
ui->accountsMenu->addAction(ui->actionNoDefaultAccount);
@ -740,6 +749,8 @@ void MainWindow::repopulateAccountsMenu()
ui->accountsMenu->addSeparator();
ui->accountsMenu->addAction(ui->actionManageAccounts);
accountsButtonMenu->addActions(ui->accountsMenu->actions());
}
void MainWindow::updatesAllowedChanged(bool allowed)

View File

@ -60,15 +60,13 @@ InstanceSettingsPage::InstanceSettingsPage(BaseInstance *inst, QWidget *parent)
m_settings = inst->settings();
ui->setupUi(this);
accountMenu = new QMenu(this);
// Use undocumented property... https://stackoverflow.com/questions/7121718/create-a-scrollbar-in-a-submenu-qt
accountMenu->setStyleSheet("QMenu { menu-scrollable: 1; }");
ui->instanceAccountSelector->setMenu(accountMenu);
connect(ui->openGlobalJavaSettingsButton, &QCommandLinkButton::clicked, this, &InstanceSettingsPage::globalSettingsButtonClicked);
connect(APPLICATION, &Application::globalSettingsAboutToOpen, this, &InstanceSettingsPage::applySettings);
connect(APPLICATION, &Application::globalSettingsClosed, this, &InstanceSettingsPage::loadSettings);
connect(ui->instanceAccountSelector, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &InstanceSettingsPage::changeInstanceAccount);
loadSettings();
updateThresholds();
}
@ -454,36 +452,17 @@ void InstanceSettingsPage::on_javaTestBtn_clicked()
void InstanceSettingsPage::updateAccountsMenu()
{
accountMenu->clear();
ui->instanceAccountSelector->clear();
auto accounts = APPLICATION->accounts();
int accountIndex = accounts->findAccountByProfileId(m_settings->get("InstanceAccountId").toString());
MinecraftAccountPtr defaultAccount = accounts->defaultAccount();
if (accountIndex != -1 && accounts->at(accountIndex)) {
defaultAccount = accounts->at(accountIndex);
}
if (defaultAccount) {
ui->instanceAccountSelector->setText(defaultAccount->profileName());
ui->instanceAccountSelector->setIcon(getFaceForAccount(defaultAccount));
} else {
ui->instanceAccountSelector->setText(tr("No default account"));
ui->instanceAccountSelector->setIcon(APPLICATION->getThemedIcon("noaccount"));
}
for (int i = 0; i < accounts->count(); i++) {
MinecraftAccountPtr account = accounts->at(i);
QAction* action = new QAction(account->profileName(), this);
action->setData(i);
action->setCheckable(true);
if (accountIndex == i) {
action->setChecked(true);
}
action->setIcon(getFaceForAccount(account));
accountMenu->addAction(action);
connect(action, SIGNAL(triggered(bool)), this, SLOT(changeInstanceAccount()));
ui->instanceAccountSelector->addItem(getFaceForAccount(account), account->profileName(), i);
if (i == accountIndex)
ui->instanceAccountSelector->setCurrentIndex(i);
}
}
QIcon InstanceSettingsPage::getFaceForAccount(MinecraftAccountPtr account)
@ -495,20 +474,13 @@ QIcon InstanceSettingsPage::getFaceForAccount(MinecraftAccountPtr account)
return APPLICATION->getThemedIcon("noaccount");
}
void InstanceSettingsPage::changeInstanceAccount()
void InstanceSettingsPage::changeInstanceAccount(int index)
{
QAction* sAction = (QAction*)sender();
Q_ASSERT(sAction->data().type() == QVariant::Type::Int);
QVariant data = sAction->data();
int index = data.toInt();
auto accounts = APPLICATION->accounts();
if (index != -1 && accounts->at(index) && ui->instanceAccountGroupBox->isChecked()) {
auto account = accounts->at(index);
m_settings->set("InstanceAccountId", account->profileId());
ui->instanceAccountSelector->setText(account->profileName());
ui->instanceAccountSelector->setIcon(getFaceForAccount(account));
}
}
void InstanceSettingsPage::on_maxMemSpinBox_valueChanged(int i)

View File

@ -95,12 +95,11 @@ private slots:
void updateAccountsMenu();
QIcon getFaceForAccount(MinecraftAccountPtr account);
void changeInstanceAccount();
void changeInstanceAccount(int index);
private:
Ui::InstanceSettingsPage *ui;
BaseInstance *m_instance;
SettingsObjectPtr m_settings;
unique_qobject_ptr<JavaCommon::TestCheck> checker;
QMenu *accountMenu = nullptr;
};

View File

@ -636,14 +636,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QToolButton" name="instanceAccountSelector">
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
</widget>
<widget class="QComboBox" name="instanceAccountSelector"/>
</item>
</layout>
</item>