1
0

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

@ -0,0 +1,30 @@
// 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 Q_OS_MACOS
void setDockIconVisibleState(bool visible) { Q_UNUSED(visible) }
bool getDockIconVisibleState() { return true; }
#endif

View File

@ -0,0 +1,27 @@
// 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_DOCK_ICON_H
#define BRIDGE_GUI_DOCK_ICON_H
void setDockIconVisibleState(bool visible); ///< Set the DOCK icon visibility state
bool getDockIconVisibleState(); ///< Get the Dock icon visibility state
#endif // #ifndef BRIDGE_GUI_DOCK_ICON_H

View File

@ -0,0 +1,54 @@
// Copyright (c) 2022 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/>.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wavailability"
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wnullability-completeness"
#pragma clang diagnostic ignored "-Wdeprecated-anon-enum-enum-conversion"
#include <Cocoa/Cocoa.h>
#pragma clang diagnostic pop
#include "DockIcon.h"
#ifdef Q_OS_MACOS
void setDockIconVisibleState(bool visible) {
if (visible) {
[NSApp setActivationPolicy: NSApplicationActivationPolicyRegular];
return;
} else {
[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];
return;
}
}
bool getDockIconVisibleState() {
switch ([NSApp activationPolicy]) {
case NSApplicationActivationPolicyAccessory:
case NSApplicationActivationPolicyProhibited:
return false;
case NSApplicationActivationPolicyRegular:
return true;
}
}
#endif // #ifdef Q_OS_MACOS

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