GODT-2010: add Cocoa app delegate handler for second application instance.

This commit is contained in:
Xavier Michelon
2023-01-05 17:06:21 +01:00
parent 8790d3cfcf
commit 16aaa1b050
8 changed files with 110 additions and 5 deletions

View File

@ -105,9 +105,9 @@ if (NOT TARGET bridgepp)
endif()
if (APPLE)
set(DOCK_ICON_SRC_FILE DockIcon/DockIcon.mm)
set(DOCK_ICON_SRC_FILE MacOS/DockIcon.mm)
else()
set(DOCK_ICON_SRC_FILE DockIcon/DockIcon.cpp)
set(DOCK_ICON_SRC_FILE MacOS/DockIcon.cpp)
endif()
if(UNIX)
@ -125,9 +125,15 @@ add_executable(bridge-gui
QMLBackend.cpp QMLBackend.h
UserList.cpp UserList.h
SentryUtils.cpp SentryUtils.h
${DOCK_ICON_SRC_FILE} DockIcon/DockIcon.h
${DOCK_ICON_SRC_FILE} MacOS/DockIcon.h
)
if (APPLE)
target_sources(bridge-gui PRIVATE MacOS/SecondInstance.mm MacOS/SecondInstance.h)
endif(APPLE)
if (WIN32) # on Windows, we add a (non-Qt) resource file that contains the application icon and version information.
string(TIMESTAMP BRIDGE_BUILD_YEAR "%Y")
set(REGEX_NUMBER "[0123456789]") # CMake matches does not support \d.

View File

@ -0,0 +1,28 @@
// Copyright (c) 2023 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
#ifndef BRIDGE_GUI_APP_DELEGATE_H
#define BRIDGE_GUI_APP_DELEGATE_H
#ifdef Q_OS_MACOS
void registerSecondInstanceHandler();
#endif // Q_OS_MACOS
#endif //BRIDGE_GUI_APP_DELEGATE_H

View File

@ -0,0 +1,63 @@
// Copyright (c) 2023 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
#include "SecondInstance.h"
#include "QMLBackend.h"
#include <bridgepp/Exception/Exception.h>
#import <Cocoa/Cocoa.h>
#import <objc/runtime.h>
using namespace bridgepp;
#ifdef Q_OS_MACOS
//****************************************************************************************************************************************************
/// \brief handle notification of attempt to re-open the application.
//****************************************************************************************************************************************************
void applicationShouldHandleReopen(id, SEL) {
app().backend().showMainWindow();
}
//****************************************************************************************************************************************************
/// \brief Register our handler for the NSApplicationDelegate applicationShouldHandleReopen:hasVisibleWindows: method that is called when the user
/// tries to open a second instance of an application.
///
/// Objective-C(++) lets you add or replace selector within a class at runtime. we use it to implement our handler for the
/// applicationShouldHandleReopen:hasVisibleWindows: selector of the Cocoa application delegate.
//****************************************************************************************************************************************************
void registerSecondInstanceHandler() {
try {
Class cls = [[[NSApplication sharedApplication] delegate] class];
if (!cls) {
throw Exception("Could not retrieve Cocoa NSApplicationDelegate instance");
}
if (!class_replaceMethod(cls, @selector(applicationShouldHandleReopen:hasVisibleWindows:), (IMP) applicationShouldHandleReopen, "v@:")) {
throw Exception("Could not register second application instance handler");
}
} catch (Exception const &e) {
app().log().error(e.qwhat());
}
}
#endif // Q_OS_MACOS

View File

@ -20,7 +20,7 @@
#define BRIDGE_GUI_QML_BACKEND_H
#include "DockIcon/DockIcon.h"
#include "MacOS/DockIcon.h"
#include "Version.h"
#include "UserList.h"
#include <bridgepp/GRPC/GRPCClient.h>

View File

@ -30,9 +30,16 @@
#include <project_sentry_config.h>
using namespace bridgepp;
#ifdef Q_OS_MACOS
#include "MacOS/SecondInstance.h"
#endif
using namespace bridgepp;
namespace {
/// \brief The file extension for the bridge executable file.
@ -320,6 +327,7 @@ int main(int argc, char *argv[]) {
CommandLineOptions const cliOptions = parseCommandLine(argc, argv);
#ifdef Q_OS_MACOS
registerSecondInstanceHandler();
setDockIconVisibleState(!cliOptions.noWindow);
#endif