From c473e987f48286376112cc4bf1665484870d323a Mon Sep 17 00:00:00 2001 From: Xavier Michelon Date: Tue, 22 Nov 2022 09:50:20 +0100 Subject: [PATCH] GODT-2134: fix dock icon on macOS when launched with '--no-window'. --- dist/bridgeMacOS.svg | 22 +++++++++++++++++++ .../bridge-gui/bridge-gui/CommandLine.cpp | 10 ++++++++- .../bridge-gui/bridge-gui/CommandLine.h | 3 ++- .../bridge-gui/bridge-gui/Resources.qrc | 1 + .../frontend/bridge-gui/bridge-gui/main.cpp | 16 ++++++++++++-- 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 dist/bridgeMacOS.svg diff --git a/dist/bridgeMacOS.svg b/dist/bridgeMacOS.svg new file mode 100644 index 00000000..30857a9a --- /dev/null +++ b/dist/bridgeMacOS.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/internal/frontend/bridge-gui/bridge-gui/CommandLine.cpp b/internal/frontend/bridge-gui/bridge-gui/CommandLine.cpp index 0e05ad3a..b2d254e5 100644 --- a/internal/frontend/bridge-gui/bridge-gui/CommandLine.cpp +++ b/internal/frontend/bridge-gui/bridge-gui/CommandLine.cpp @@ -28,6 +28,7 @@ namespace QString const launcherFlag = "--launcher"; ///< launcher flag parameter used for bridge. +QString const noWindowFlag = "--no-window"; ///< The no-window command-line flag. //**************************************************************************************************************************************************** @@ -89,9 +90,12 @@ Log::Level parseLogLevel(int argc, char *argv[]) /// \param[out] launcher launcher used in argument, forced to self application if not specify. /// \param[out] outAttach The value for the 'attach' command-line parameter. /// \param[out] outLogLevel The parsed log level. If not found, the default log level is returned. +/// \param[out] outNoWindow True if the '--no-window' flag was found on the command-line. //**************************************************************************************************************************************************** -void parseCommandLineArguments(int argc, char *argv[], QStringList& args, QString& launcher, bool &outAttach, Log::Level& outLogLevel) { +void parseCommandLineArguments(int argc, char *argv[], QStringList& args, QString& launcher, bool &outAttach, Log::Level& outLogLevel, + bool &outNoWindow) { bool flagFound = false; + outNoWindow = false; launcher = QString::fromLocal8Bit(argv[0]); // for unknown reasons, on Windows QCoreApplication::arguments() frequently returns an empty list, which is incorrect, so we rebuild the argument // list from the original argc and argv values. @@ -99,6 +103,10 @@ void parseCommandLineArguments(int argc, char *argv[], QStringList& args, QStrin QString const &arg = QString::fromLocal8Bit(argv[i]); // we can't use QCommandLineParser here since it will fail on unknown options. // Arguments may contain some bridge flags. + + if (arg == noWindowFlag) + outNoWindow = true; + if (arg == launcherFlag) { args.append(arg); diff --git a/internal/frontend/bridge-gui/bridge-gui/CommandLine.h b/internal/frontend/bridge-gui/bridge-gui/CommandLine.h index e32f8bfd..103b358b 100644 --- a/internal/frontend/bridge-gui/bridge-gui/CommandLine.h +++ b/internal/frontend/bridge-gui/bridge-gui/CommandLine.h @@ -23,7 +23,8 @@ #include -void parseCommandLineArguments(int argc, char *argv[], QStringList& args, QString& launcher, bool &outAttach, bridgepp::Log::Level& outLogLevel); ///< Parse the command-line arguments +void parseCommandLineArguments(int argc, char *argv[], QStringList& args, QString& launcher, bool &outAttach, bridgepp::Log::Level& outLogLevel, + bool &outNoWindow); ///< Parse the command-line arguments #endif //BRIDGE_GUI_COMMAND_LINE_H diff --git a/internal/frontend/bridge-gui/bridge-gui/Resources.qrc b/internal/frontend/bridge-gui/bridge-gui/Resources.qrc index ae39db15..21a67fde 100644 --- a/internal/frontend/bridge-gui/bridge-gui/Resources.qrc +++ b/internal/frontend/bridge-gui/bridge-gui/Resources.qrc @@ -67,6 +67,7 @@ qml/icons/systray-mono-warn.png qml/icons/systray.svg ../../../../dist/bridge.svg + ../../../../dist/bridgeMacOS.svg qml/KeychainSettings.qml qml/LocalCacheSettings.qml qml/MainWindow.qml diff --git a/internal/frontend/bridge-gui/bridge-gui/main.cpp b/internal/frontend/bridge-gui/bridge-gui/main.cpp index 20213891..93df1ab7 100644 --- a/internal/frontend/bridge-gui/bridge-gui/main.cpp +++ b/internal/frontend/bridge-gui/bridge-gui/main.cpp @@ -72,7 +72,16 @@ void initQtApplication() QGuiApplication::setOrganizationName(PROJECT_VENDOR); QGuiApplication::setOrganizationDomain("proton.ch"); QGuiApplication::setQuitOnLastWindowClosed(false); +#ifdef Q_OS_MACOS + // on macOS, the app icon as it appears in the dock and file system is defined by in the app bundle plist, not here. + // We still use this copy (lock icon in white rectangle), so that devs that use the bridge-gui exe directly get a decent looking icon in the dock. + // Qt does not support the native .icns format, so we use a PNG file. + QGuiApplication::setWindowIcon(QIcon(":bridgeMacOS.svg")); +#else + // On non macOS platform, this icon (without the white rectangle background, is used in the OS decoration elements (title bar, task bar, etc...) + // It's not use as the executable icon. QGuiApplication::setWindowIcon(QIcon(":bridge.svg")); +#endif // #ifdef Q_OS_MACOS } @@ -113,7 +122,6 @@ Log &initLog() QQmlComponent *createRootQmlComponent(QQmlApplicationEngine &engine) { QString const qrcQmlDir = "qrc:/qml"; - qmlRegisterSingletonInstance("Proton", 1, 0, "Backend", &app().backend()); qmlRegisterType("Proton", 1, 0, "UserList"); qmlRegisterType("Proton", 1, 0, "User"); @@ -302,8 +310,12 @@ int main(int argc, char *argv[]) QStringList args; QString launcher; bool attach = false; + bool noWindow; Log::Level logLevel = Log::defaultLevel; - parseCommandLineArguments(argc, argv, args, launcher, attach, logLevel); + parseCommandLineArguments(argc, argv, args, launcher, attach, logLevel, noWindow); +#ifdef Q_OS_MACOS + setDockIconVisibleState(!noWindow); +#endif // In attached mode, we do not intercept stderr and stdout of bridge, as we did not launch it ourselves, so we output the log to the console. // When not in attached mode, log entries are forwarded to bridge, which output it on stdout/stderr. bridge-gui's process monitor intercept