Allow selecting a default account to use with an instance

Signed-off-by: Aaron <10217842+byteduck@users.noreply.github.com>
This commit is contained in:
Aaron 2022-12-27 15:00:15 -07:00
parent 4d334b645d
commit 7e2d78bab5
5 changed files with 150 additions and 6 deletions

View File

@ -112,7 +112,19 @@ void LaunchController::decideAccount()
}
}
m_accountToUse = accounts->defaultAccount();
// Select the account to use. If the instance has a specific account set, that will be used. Otherwise, the default account will be used
auto instanceAccountId = m_instance->settings()->get("InstanceAccountId").toString();
auto instanceAccountIndex = accounts->findAccountByProfileId(instanceAccountId);
if (instanceAccountIndex == -1)
{
m_accountToUse = accounts->defaultAccount();
}
else
{
m_accountToUse = accounts->at(instanceAccountIndex);
}
if (!m_accountToUse)
{
// If no default account is set, ask the user which one to use.

View File

@ -192,6 +192,10 @@ void MinecraftInstance::loadSpecificSettings()
m_settings->registerSetting("JoinServerOnLaunch", false);
m_settings->registerSetting("JoinServerOnLaunchAddress", "");
// Use account for instance, this does not have a global override
m_settings->registerSetting("UseAccountForInstance", false);
m_settings->registerSetting("InstanceAccountId", "");
qDebug() << "Instance-type specific settings were loaded!";
setSpecificSettingsLoaded(true);

View File

@ -48,18 +48,23 @@
#include "JavaCommon.h"
#include "Application.h"
#include "minecraft/auth/AccountList.h"
#include "java/JavaInstallList.h"
#include "java/JavaUtils.h"
#include "FileSystem.h"
InstanceSettingsPage::InstanceSettingsPage(BaseInstance *inst, QWidget *parent)
: QWidget(parent), ui(new Ui::InstanceSettingsPage), m_instance(inst)
{
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);
@ -75,6 +80,7 @@ bool InstanceSettingsPage::shouldDisplay() const
InstanceSettingsPage::~InstanceSettingsPage()
{
delete ui;
delete accountMenu;
}
void InstanceSettingsPage::globalSettingsButtonClicked(bool)
@ -275,6 +281,14 @@ void InstanceSettingsPage::applySettings()
m_settings->reset("JoinServerOnLaunchAddress");
}
// Use an account for this instance
bool useAccountForInstance = ui->instanceAccountGroupBox->isChecked();
m_settings->set("UseAccountForInstance", useAccountForInstance);
if (!useAccountForInstance)
{
m_settings->reset("InstanceAccountId");
}
// FIXME: This should probably be called by a signal instead
m_instance->updateRuntimeContext();
}
@ -372,6 +386,9 @@ void InstanceSettingsPage::loadSettings()
ui->serverJoinGroupBox->setChecked(m_settings->get("JoinServerOnLaunch").toBool());
ui->serverJoinAddress->setText(m_settings->get("JoinServerOnLaunchAddress").toString());
ui->instanceAccountGroupBox->setChecked(m_settings->get("UseAccountForInstance").toBool());
updateAccountsMenu();
}
void InstanceSettingsPage::on_javaDetectBtn_clicked()
@ -437,6 +454,70 @@ void InstanceSettingsPage::on_javaTestBtn_clicked()
checker->run();
}
void InstanceSettingsPage::updateAccountsMenu()
{
accountMenu->clear();
auto accounts = APPLICATION->accounts();
int accountIndex = accounts->findAccountByProfileId(m_settings->get("InstanceAccountId").toString());
if (accountIndex != -1)
{
auto account = accounts->at(accountIndex);
ui->instanceAccountSelector->setText(account->profileName());
ui->instanceAccountSelector->setIcon(account->getFace());
} 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);
}
auto face = account->getFace();
if(!face.isNull()) {
action->setIcon(face);
}
else {
action->setIcon(APPLICATION->getThemedIcon("noaccount"));
}
accountMenu->addAction(action);
connect(action, SIGNAL(triggered(bool)), SLOT(changeInstanceAccount()));
}
}
void InstanceSettingsPage::changeInstanceAccount()
{
QAction *sAction = (QAction *)sender();
// Profile's associated Mojang username
if (sAction->data().type() != QVariant::Type::Int)
return;
QVariant data = sAction->data();
bool valid = false;
int index = data.toInt(&valid);
if(!valid) {
index = -1;
}
auto accounts = APPLICATION->accounts();
auto account = accounts->at(index);
m_settings->set("InstanceAccountId", account->profileId());
ui->instanceAccountSelector->setText(account->profileName());
ui->instanceAccountSelector->setIcon(account->getFace());
}
void InstanceSettingsPage::on_maxMemSpinBox_valueChanged(int i)
{
updateThresholds();

View File

@ -37,12 +37,13 @@
#include <QWidget>
#include "java/JavaChecker.h"
#include "BaseInstance.h"
#include <QObjectPtr.h>
#include "ui/pages/BasePage.h"
#include "JavaCommon.h"
#include <QMenu>
#include "Application.h"
#include "BaseInstance.h"
#include "JavaCommon.h"
#include "java/JavaChecker.h"
#include "ui/pages/BasePage.h"
class JavaChecker;
namespace Ui
@ -92,9 +93,13 @@ private slots:
void globalSettingsButtonClicked(bool checked);
void updateAccountsMenu();
void changeInstanceAccount();
private:
Ui::InstanceSettingsPage *ui;
BaseInstance *m_instance;
SettingsObjectPtr m_settings;
unique_qobject_ptr<JavaCommon::TestCheck> checker;
QMenu *accountMenu = nullptr;
};

View File

@ -608,6 +608,48 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="instanceAccountGroupBox">
<property name="title">
<string>Set a default account to use with this instance</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_11">
<item>
<layout class="QGridLayout" name="instanceAccountLayout">
<item row="0" column="0">
<widget class="QLabel" name="instanceAccountNameLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Account:</string>
</property>
</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>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacerMiscellaneous">
<property name="orientation">