diff --git a/launcher/minecraft/WorldList.cpp b/launcher/minecraft/WorldList.cpp index 6a821ba60..9a5cf042c 100644 --- a/launcher/minecraft/WorldList.cpp +++ b/launcher/minecraft/WorldList.cpp @@ -46,6 +46,9 @@ #include #include +#include +#include "minecraft/ShortcutUtils.h" + WorldList::WorldList(const QString& dir, BaseInstance* instance) : QAbstractListModel(), m_instance(instance), m_dir(dir) { FS::ensureFolderPathExists(m_dir.absolutePath()); @@ -454,4 +457,39 @@ void WorldList::loadWorldsAsync() } } +void WorldList::createWorldShortcut(const QModelIndex& index, QWidget* parent) const +{ + if (!m_instance) + return; + + if (DesktopServices::isFlatpak()) + createWorldShortcutInOther(index, parent); + else + createWorldShortcutOnDesktop(index, parent); +} + +void WorldList::createWorldShortcutOnDesktop(const QModelIndex& index, QWidget* parent) const +{ + auto world = static_cast(data(index, ObjectRole).value()); + QString name = QString(tr("%1 - %2")).arg(m_instance->name(), world->name()); + QStringList extraArgs{ "--world", world->name() }; + ShortcutUtils::createInstanceShortcutOnDesktop(m_instance, name, tr("world"), parent, extraArgs); +} + +void WorldList::createWorldShortcutInApplications(const QModelIndex& index, QWidget* parent) const +{ + auto world = static_cast(data(index, ObjectRole).value()); + QString name = QString(tr("%1 - %2")).arg(m_instance->name(), world->name()); + QStringList extraArgs{ "--world", world->name() }; + ShortcutUtils::createInstanceShortcutInApplications(m_instance, name, tr("world"), parent, extraArgs); +} + +void WorldList::createWorldShortcutInOther(const QModelIndex& index, QWidget* parent) const +{ + auto world = static_cast(data(index, ObjectRole).value()); + QString name = QString(tr("%1 - %2")).arg(m_instance->name(), world->name()); + QStringList extraArgs{ "--world", world->name() }; + ShortcutUtils::createInstanceShortcutInOther(m_instance, name, tr("world"), parent, extraArgs); +} + #include "WorldList.moc" diff --git a/launcher/minecraft/WorldList.h b/launcher/minecraft/WorldList.h index 93fecf1f5..4f54e0737 100644 --- a/launcher/minecraft/WorldList.h +++ b/launcher/minecraft/WorldList.h @@ -84,6 +84,11 @@ class WorldList : public QAbstractListModel { const QList& allWorlds() const { return m_worlds; } + void createWorldShortcut(const QModelIndex& index, QWidget* parent = nullptr) const; + void createWorldShortcutOnDesktop(const QModelIndex& index, QWidget* parent = nullptr) const; + void createWorldShortcutInApplications(const QModelIndex& index, QWidget* parent = nullptr) const; + void createWorldShortcutInOther(const QModelIndex& index, QWidget* parent = nullptr) const; + private slots: void directoryChanged(QString path); void loadWorldsAsync(); diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index ed9961975..4f03d14da 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -214,17 +214,17 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi QString desktopDir = FS::getDesktopDir(); QString applicationDir = FS::getApplicationsDir(); - if(!applicationDir.isEmpty()) + if (!applicationDir.isEmpty()) shortcutActions.push_front(ui->actionCreateInstanceShortcutApplications); - if(!desktopDir.isEmpty()) + if (!desktopDir.isEmpty()) shortcutActions.push_front(ui->actionCreateInstanceShortcutDesktop); } - if(shortcutActions.length() > 1) { + if (shortcutActions.length() > 1) { auto shortcutInstanceMenu = new QMenu(this); - for(auto action : shortcutActions) + for (auto action : shortcutActions) shortcutInstanceMenu->addAction(action); ui->actionCreateInstanceShortcut->setMenu(shortcutInstanceMenu); } diff --git a/launcher/ui/pages/instance/WorldListPage.cpp b/launcher/ui/pages/instance/WorldListPage.cpp index 9e1a0fb55..f6a1e0e5f 100644 --- a/launcher/ui/pages/instance/WorldListPage.cpp +++ b/launcher/ui/pages/instance/WorldListPage.cpp @@ -345,6 +345,28 @@ void WorldListPage::worldChanged([[maybe_unused]] const QModelIndex& current, [[ if (!supportsJoin) { ui->toolBar->removeAction(ui->actionJoin); + ui->toolBar->removeAction(ui->actionCreateWorldShortcut); + } else { + QList shortcutActions = { ui->actionCreateWorldShortcutOther }; + if (!DesktopServices::isFlatpak()) { + QString desktopDir = FS::getDesktopDir(); + QString applicationDir = FS::getApplicationsDir(); + + if (!applicationDir.isEmpty()) + shortcutActions.push_front(ui->actionCreateWorldShortcutApplications); + + if (!desktopDir.isEmpty()) + shortcutActions.push_front(ui->actionCreateWorldShortcutDesktop); + } + + if (shortcutActions.length() > 1) { + auto shortcutInstanceMenu = new QMenu(this); + + for (auto action : shortcutActions) + shortcutInstanceMenu->addAction(action); + ui->actionCreateWorldShortcut->setMenu(shortcutInstanceMenu); + } + ui->actionCreateWorldShortcut->setEnabled(enable); } } @@ -420,6 +442,42 @@ void WorldListPage::on_actionRename_triggered() } } +void WorldListPage::on_actionCreateWorldShortcut_triggered() +{ + QModelIndex index = getSelectedWorld(); + if (!index.isValid()) { + return; + } + m_worlds->createWorldShortcut(index, this); +} + +void WorldListPage::on_actionCreateWorldShortcutDesktop_triggered() +{ + QModelIndex index = getSelectedWorld(); + if (!index.isValid()) { + return; + } + m_worlds->createWorldShortcutOnDesktop(index, this); +} + +void WorldListPage::on_actionCreateWorldShortcutApplications_triggered() +{ + QModelIndex index = getSelectedWorld(); + if (!index.isValid()) { + return; + } + m_worlds->createWorldShortcutInApplications(index, this); +} + +void WorldListPage::on_actionCreateWorldShortcutOther_triggered() +{ + QModelIndex index = getSelectedWorld(); + if (!index.isValid()) { + return; + } + m_worlds->createWorldShortcutInOther(index, this); +} + void WorldListPage::on_actionRefresh_triggered() { m_worlds->update(); diff --git a/launcher/ui/pages/instance/WorldListPage.h b/launcher/ui/pages/instance/WorldListPage.h index 84d9cd075..f2c081bc5 100644 --- a/launcher/ui/pages/instance/WorldListPage.h +++ b/launcher/ui/pages/instance/WorldListPage.h @@ -95,6 +95,10 @@ class WorldListPage : public QMainWindow, public BasePage { void on_actionAdd_triggered(); void on_actionCopy_triggered(); void on_actionRename_triggered(); + void on_actionCreateWorldShortcut_triggered(); + void on_actionCreateWorldShortcutDesktop_triggered(); + void on_actionCreateWorldShortcutApplications_triggered(); + void on_actionCreateWorldShortcutOther_triggered(); void on_actionRefresh_triggered(); void on_actionView_Folder_triggered(); void on_actionDatapacks_triggered(); diff --git a/launcher/ui/pages/instance/WorldListPage.ui b/launcher/ui/pages/instance/WorldListPage.ui index 04344b453..f4664d503 100644 --- a/launcher/ui/pages/instance/WorldListPage.ui +++ b/launcher/ui/pages/instance/WorldListPage.ui @@ -85,10 +85,11 @@ + + - @@ -118,6 +119,14 @@ Delete + + + Create Shortcut + + + Creates a shortcut on a selected folder to join the selected world. + + MCEdit @@ -154,6 +163,39 @@ Manage datapacks inside the world. + + + Desktop + + + Creates a shortcut to this world on your desktop + + + QAction::TextHeuristicRole + + + + + Applications + + + Create a shortcut of this world on your start menu + + + QAction::TextHeuristicRole + + + + + Other... + + + Creates a shortcut in a folder selected by you + + + QAction::TextHeuristicRole + +