From d2fc2bbc6d1301ca9c89811f03a6334eb30f139d Mon Sep 17 00:00:00 2001 From: Elias Steurer Date: Tue, 27 Dec 2022 22:07:44 +0100 Subject: [PATCH] Hide app icon from dock on macOS --- ScreenPlay/inc/public/ScreenPlay/app.h | 1 + ScreenPlay/main.qml | 6 +- ScreenPlay/qml/Navigation/ExitPopup.qml | 2 + ScreenPlay/src/app.cpp | 10 +++ ScreenPlayUtil/CMakeLists.txt | 3 +- .../inc/public/ScreenPlayUtil/macutils.h | 49 +++++++++++++++ ScreenPlayUtil/qml/TrayIcon.qml | 1 + ScreenPlayUtil/src/macutils.mm | 62 +++++++++++++++++++ 8 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 ScreenPlayUtil/inc/public/ScreenPlayUtil/macutils.h create mode 100644 ScreenPlayUtil/src/macutils.mm diff --git a/ScreenPlay/inc/public/ScreenPlay/app.h b/ScreenPlay/inc/public/ScreenPlay/app.h index 677e9b64..a1c97719 100644 --- a/ScreenPlay/inc/public/ScreenPlay/app.h +++ b/ScreenPlay/inc/public/ScreenPlay/app.h @@ -117,6 +117,7 @@ signals: public slots: QString version() const; + void showDockIcon(const bool show); void exit(); QPointF cursorPos() { return QCursor::pos(); } diff --git a/ScreenPlay/main.qml b/ScreenPlay/main.qml index 2dfb19b7..cabd6268 100644 --- a/ScreenPlay/main.qml +++ b/ScreenPlay/main.qml @@ -84,6 +84,7 @@ ApplicationWindow { if(alwaysMinimize === "true"){ root.hide() + App.showDockIcon(false); return } @@ -105,8 +106,10 @@ ApplicationWindow { stackView.push("qrc:/qml/ScreenPlayApp/qml/Installed/Installed.qml", { "sidebar": sidebar }) - if (!App.settings.silentStart) + if (!App.settings.silentStart){ + App.showDockIcon(true) root.show() + } } Item { @@ -152,6 +155,7 @@ ApplicationWindow { Connections { function onRequestRaise() { + App.showDockIcon(true) root.show() } diff --git a/ScreenPlay/qml/Navigation/ExitPopup.qml b/ScreenPlay/qml/Navigation/ExitPopup.qml index 0818f7e4..4e5b9312 100644 --- a/ScreenPlay/qml/Navigation/ExitPopup.qml +++ b/ScreenPlay/qml/Navigation/ExitPopup.qml @@ -76,6 +76,7 @@ Util.Popup { text: qsTr("Minimize ScreenPlay") onClicked: { applicationWindow.hide() + App.showDockIcon(false) root.close() } } @@ -85,6 +86,7 @@ Util.Popup { onClicked: { settings.setValue("alwaysMinimize", true) settings.sync() + App.showDockIcon(false) applicationWindow.hide() root.close() } diff --git a/ScreenPlay/src/app.cpp b/ScreenPlay/src/app.cpp index 659fea93..c0533845 100644 --- a/ScreenPlay/src/app.cpp +++ b/ScreenPlay/src/app.cpp @@ -1,4 +1,7 @@ #include "ScreenPlay/app.h" +#if defined(Q_OS_OSX) +#include "ScreenPlayUtil/macutils.h" +#endif #include "steam/steam_qt_enums_generated.h" #include @@ -332,4 +335,11 @@ bool App::setupKDE() } } +void App::showDockIcon(const bool show) +{ +#if defined(Q_OS_OSX) + MacUtils::instance()->showDockIcon(show); +#endif +} + } diff --git a/ScreenPlayUtil/CMakeLists.txt b/ScreenPlayUtil/CMakeLists.txt index 890f0528..f3abbb47 100644 --- a/ScreenPlayUtil/CMakeLists.txt +++ b/ScreenPlayUtil/CMakeLists.txt @@ -40,7 +40,7 @@ set(QML qml/TrayIcon.qml) set(SOURCES # cmake-format: sort - inc/public/ScreenPlayUtil/httpfileserver.cpp src/contenttypes.cpp src/util.cpp) + inc/public/ScreenPlayUtil/httpfileserver.cpp src/contenttypes.cpp src/util.cpp src/macutils.mm) set(HEADER # cmake-format: sort @@ -54,6 +54,7 @@ set(HEADER inc/public/ScreenPlayUtil/PropertyHelpers.h inc/public/ScreenPlayUtil/PtrPropertyHelpers.h inc/public/ScreenPlayUtil/SingletonHelper.h + inc/public/ScreenPlayUtil/macutils.h inc/public/ScreenPlayUtil/util.h) set(RESOURCES # cmake-format: sort diff --git a/ScreenPlayUtil/inc/public/ScreenPlayUtil/macutils.h b/ScreenPlayUtil/inc/public/ScreenPlayUtil/macutils.h new file mode 100644 index 00000000..11d20e85 --- /dev/null +++ b/ScreenPlayUtil/inc/public/ScreenPlayUtil/macutils.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2020 Elias Steurer (Kelteseth) +** Contact: https://screen-play.app +** +** This file is part of ScreenPlay. ScreenPlay is licensed under a dual license in +** order to ensure its sustainability. When you contribute to ScreenPlay +** you accept that your work will be available under the two following licenses: +** +** $SCREENPLAY_BEGIN_LICENSE$ +** +** #### Affero General Public License Usage (AGPLv3) +** Alternatively, this file may be used under the terms of the GNU Affero +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file "ScreenPlay License.md" included in the +** packaging of this App. Please review the following information to +** ensure the GNU Affero Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/agpl-3.0.en.html. +** +** #### Commercial License +** This code is owned by Elias Steurer. By changing/adding to the code you agree to the +** terms written in: +** * Legal/corporate_contributor_license_agreement.md - For corporate contributors +** * Legal/individual_contributor_license_agreement.md - For individual contributors +** +** #### Additional Limitations to the AGPLv3 and Commercial Lincese +** This License does not grant any rights in the trademarks, +** service marks, or logos. +** +** +** $SCREENPLAY_END_LICENSE +** +****************************************************************************/ + +#pragma once + +#include + +class MacUtils : public QObject { + Q_OBJECT + +private: + static MacUtils* macUtils; + MacUtils(); + +public: + static MacUtils* instance(); + static void showDockIcon(const bool show); +}; diff --git a/ScreenPlayUtil/qml/TrayIcon.qml b/ScreenPlayUtil/qml/TrayIcon.qml index 88133a7e..17f693a7 100644 --- a/ScreenPlayUtil/qml/TrayIcon.qml +++ b/ScreenPlayUtil/qml/TrayIcon.qml @@ -31,6 +31,7 @@ SystemTrayIcon { MenuItem { text: qsTr("Open ScreenPlay") onTriggered: { + App.showDockIcon(true); window.show() } } diff --git a/ScreenPlayUtil/src/macutils.mm b/ScreenPlayUtil/src/macutils.mm new file mode 100644 index 00000000..f31efbaf --- /dev/null +++ b/ScreenPlayUtil/src/macutils.mm @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2020 Elias Steurer (Kelteseth) +** Contact: https://screen-play.app +** +** This file is part of ScreenPlay. ScreenPlay is licensed under a dual license in +** order to ensure its sustainability. When you contribute to ScreenPlay +** you accept that your work will be available under the two following licenses: +** +** $SCREENPLAY_BEGIN_LICENSE$ +** +** #### Affero General Public License Usage (AGPLv3) +** Alternatively, this file may be used under the terms of the GNU Affero +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file "ScreenPlay License.md" included in the +** packaging of this App. Please review the following information to +** ensure the GNU Affero Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/agpl-3.0.en.html. +** +** #### Commercial License +** This code is owned by Elias Steurer. By changing/adding to the code you agree to the +** terms written in: +** * Legal/corporate_contributor_license_agreement.md - For corporate contributors +** * Legal/individual_contributor_license_agreement.md - For individual contributors +** +** #### Additional Limitations to the AGPLv3 and Commercial Lincese +** This License does not grant any rights in the trademarks, +** service marks, or logos. +** +** +** $SCREENPLAY_END_LICENSE$ +** +****************************************************************************/ + +#include "ScreenPlayUtil/macutils.h" +#import +#import +#import + +MacUtils* MacUtils::macUtils = nullptr; + +MacUtils::MacUtils() +{ +} + +MacUtils* MacUtils::instance() +{ + if (!macUtils) + macUtils = new MacUtils; + return macUtils; +} + +void MacUtils::showDockIcon(const bool show) +{ + ProcessSerialNumber psn = { 0, kCurrentProcess }; + + if (show) { + TransformProcessType(&psn, kProcessTransformToForegroundApplication); + } else { + TransformProcessType(&psn, kProcessTransformToUIElementApplication); + } +}