Add create shortcut button for servers

Signed-off-by: Yihe Li <winmikedows@hotmail.com>
This commit is contained in:
Yihe Li 2025-05-11 19:16:37 +08:00
parent 37213ecc34
commit dbdc9bea7a
No known key found for this signature in database
4 changed files with 123 additions and 2 deletions

View File

@ -170,7 +170,6 @@ void createInstanceShortcut(BaseInstance* instance,
iconFile.remove();
#endif
QMessageBox::critical(parent, QObject::tr("Create Shortcut"), QObject::tr("Failed to create %1 shortcut!").arg(targetString));
return;
}
}

View File

@ -40,8 +40,10 @@
#include "ui/dialogs/CustomMessageBox.h"
#include "ui_ServersPage.h"
#include <DesktopServices.h>
#include <FileSystem.h>
#include <io/stream_reader.h>
#include <minecraft/ShortcutUtils.h>
#include <minecraft/MinecraftInstance.h>
#include <tag_compound.h>
#include <tag_list.h>
@ -102,6 +104,38 @@ struct Server {
}
}
void createServerShortcut(BaseInstance* instance, QWidget* parent = nullptr) const
{
if (!instance)
return;
if (DesktopServices::isFlatpak())
createServerShortcutInOther(instance, parent);
else
createServerShortcutOnDesktop(instance, parent);
}
void createServerShortcutOnDesktop(BaseInstance* instance, QWidget* parent = nullptr) const
{
QString name = QString(QObject::tr("%1 - Server %2")).arg(instance->name(), m_name);
QStringList extraArgs{ "--server", m_address };
ShortcutUtils::createInstanceShortcutOnDesktop(instance, name, QObject::tr("server"), parent, extraArgs);
}
void createServerShortcutInApplications(BaseInstance* instance, QWidget* parent = nullptr) const
{
QString name = QString(QObject::tr("%1 - Server %2")).arg(instance->name(), m_name);
QStringList extraArgs{ "--server", m_address };
ShortcutUtils::createInstanceShortcutInApplications(instance, name, QObject::tr("server"), parent, extraArgs);
}
void createServerShortcutInOther(BaseInstance* instance, QWidget* parent = nullptr) const
{
QString name = QString(QObject::tr("%1 - Server %2")).arg(instance->name(), m_name);
QStringList extraArgs{ "--server", m_address };
ShortcutUtils::createInstanceShortcutInOther(instance, name, QObject::tr("server"), parent, extraArgs);
}
// Data - persistent and user changeable
QString m_name;
QString m_address;
@ -696,6 +730,27 @@ void ServersPage::updateState()
}
ui->actionAdd->setDisabled(m_locked);
QList<QAction*> shortcutActions = { ui->actionCreateServerShortcutOther };
if (!DesktopServices::isFlatpak()) {
QString desktopDir = FS::getDesktopDir();
QString applicationDir = FS::getApplicationsDir();
if (!applicationDir.isEmpty())
shortcutActions.push_front(ui->actionCreateServerShortcutApplications);
if (!desktopDir.isEmpty())
shortcutActions.push_front(ui->actionCreateServerShortcutDesktop);
}
if (shortcutActions.length() > 1) {
auto shortcutInstanceMenu = new QMenu(this);
for (auto action : shortcutActions)
shortcutInstanceMenu->addAction(action);
ui->actionCreateServerShortcut->setMenu(shortcutInstanceMenu);
}
ui->actionCreateServerShortcut->setEnabled(serverEditEnabled);
}
void ServersPage::openedImpl()
@ -767,6 +822,26 @@ void ServersPage::on_actionJoin_triggered()
APPLICATION->launch(m_inst, true, false, std::make_shared<MinecraftTarget>(MinecraftTarget::parse(address, false)));
}
void ServersPage::on_actionCreateServerShortcut_triggered()
{
m_model->at(currentServer)->createServerShortcut(m_inst.get(), this);
}
void ServersPage::on_actionCreateServerShortcutDesktop_triggered()
{
m_model->at(currentServer)->createServerShortcutOnDesktop(m_inst.get(), this);
}
void ServersPage::on_actionCreateServerShortcutApplications_triggered()
{
m_model->at(currentServer)->createServerShortcutInApplications(m_inst.get(), this);
}
void ServersPage::on_actionCreateServerShortcutOther_triggered()
{
m_model->at(currentServer)->createServerShortcutInOther(m_inst.get(), this);
}
void ServersPage::on_actionRefresh_triggered()
{
m_model->queryServersStatus();

View File

@ -85,6 +85,10 @@ class ServersPage : public QMainWindow, public BasePage {
void on_actionMove_Up_triggered();
void on_actionMove_Down_triggered();
void on_actionJoin_triggered();
void on_actionCreateServerShortcut_triggered();
void on_actionCreateServerShortcutDesktop_triggered();
void on_actionCreateServerShortcutApplications_triggered();
void on_actionCreateServerShortcutOther_triggered();
void on_actionRefresh_triggered();
void runningStateChanged(bool running);

View File

@ -145,10 +145,12 @@
<bool>false</bool>
</attribute>
<addaction name="actionAdd"/>
<addaction name="separator"/>
<addaction name="actionJoin"/>
<addaction name="actionRemove"/>
<addaction name="actionMove_Up"/>
<addaction name="actionMove_Down"/>
<addaction name="actionJoin"/>
<addaction name="actionCreateServerShortcut"/>
<addaction name="separator"/>
<addaction name="actionRefresh"/>
</widget>
@ -177,6 +179,47 @@
<string>Join</string>
</property>
</action>
<action name="actionCreateServerShortcut">
<property name="text">
<string>Create Shortcut</string>
</property>
<property name="toolTip">
<string>Creates a shortcut on a selected folder to join the selected server.</string>
</property>
</action>
<action name="actionCreateServerShortcutDesktop">
<property name="text">
<string>Desktop</string>
</property>
<property name="toolTip">
<string>Creates a shortcut to this server on your desktop</string>
</property>
<property name="menuRole">
<enum>QAction::TextHeuristicRole</enum>
</property>
</action>
<action name="actionCreateServerShortcutApplications">
<property name="text">
<string>Applications</string>
</property>
<property name="toolTip">
<string>Create a shortcut of this server on your start menu</string>
</property>
<property name="menuRole">
<enum>QAction::TextHeuristicRole</enum>
</property>
</action>
<action name="actionCreateServerShortcutOther">
<property name="text">
<string>Other...</string>
</property>
<property name="toolTip">
<string>Creates a shortcut in a folder selected by you</string>
</property>
<property name="menuRole">
<enum>QAction::TextHeuristicRole</enum>
</property>
</action>
<action name="actionRefresh">
<property name="text">
<string>Refresh</string>