Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show/Hide Menubar and Dock Icon on macOS #3014

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ IF( APPLE )
macOS/progressobserver.m)
endif()

list(APPEND client_SRCS foregroundbackground_interface.h)
list(APPEND client_SRCS foregroundbackground_mac.mm)
list(APPEND client_SRCS foregroundbackground_cocoa.mm)

if(SPARKLE_FOUND AND BUILD_UPDATER)
# Define this, we need to check in updater.cpp
add_definitions(-DHAVE_SPARKLE)
Expand Down
42 changes: 42 additions & 0 deletions src/gui/foregroundbackground_cocoa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) by Elsie Hupp <gpl at elsiehupp dot com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*/

#import <AppKit/NSApplication.h>

Check failure on line 15 in src/gui/foregroundbackground_cocoa.h

View workflow job for this annotation

GitHub Actions / build

src/gui/foregroundbackground_cocoa.h:15:9 [clang-diagnostic-error]

'AppKit/NSApplication.h' file not found

/**
* @brief CocoaProcessType provides methods for moving the application between
* the background and foreground.
* @ingroup gui
*/

#ifndef COCOAPROCESSTYPE_H
#define COCOAPROCESSTYPE_H

@interface CocoaProcessType : NSApplication

/**
* @brief CocoaProcessTypeToForeground() enables the macOS menubar and dock icon, which are necessary for a maximized window to be able to exit full screen.
* @ingroup gui
*/
+ (void)ToForeground;

/**
* @brief CocoaProcessTypeToBackground() disables the macOS menubar and dock icon, so that the application will only be present as a menubar icon.
* @ingroup gui
*/
+ (void)ToBackground;

@end

#endif
34 changes: 34 additions & 0 deletions src/gui/foregroundbackground_cocoa.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) by Elsie Hupp <gpl at elsiehupp dot com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*/

#include "foregroundbackground_cocoa.h"
#include "common/utility.h"

@implementation CocoaProcessType

+ (void)ToForeground
{
NSApplicationLoad();
ProcessSerialNumber processSerialNumber = { 0, kCurrentProcess };
TransformProcessType(&processSerialNumber, kProcessTransformToForegroundApplication);
}

+ (void)ToBackground
{
NSApplicationLoad();
ProcessSerialNumber processSerialNumber = { 0, kCurrentProcess };
TransformProcessType(&processSerialNumber, kProcessTransformToUIElementApplication);
}

@end
63 changes: 63 additions & 0 deletions src/gui/foregroundbackground_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*

Check notice on line 1 in src/gui/foregroundbackground_interface.h

View workflow job for this annotation

GitHub Actions / build

Run clang-format on src/gui/foregroundbackground_interface.h

File src/gui/foregroundbackground_interface.h does not conform to Custom style guidelines. (lines 15, 16, 17, 23, 26, 27, 38, 40, 43, 44, 45, 50, 51, 52, 56, 57, 58)
* Copyright (C) by Elsie Hupp <gpl at elsiehupp dot com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*/

/**
* @brief ForegroundBackgroundInterface allows ForegroundBackground to be implemented differently per platform
* @ingroup gui
*/

#ifndef FOREGROUNDBACKGROUND_INTERFACE_H
#define FOREGROUNDBACKGROUND_INTERFACE_H

#include <QObject>

Check failure on line 23 in src/gui/foregroundbackground_interface.h

View workflow job for this annotation

GitHub Actions / build

src/gui/foregroundbackground_interface.h:23:10 [clang-diagnostic-error]

'QObject' file not found
#include <QEvent>

namespace OCC {

Check warning on line 26 in src/gui/foregroundbackground_interface.h

View workflow job for this annotation

GitHub Actions / build

src/gui/foregroundbackground_interface.h:26:11 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'OCC' is non-const and globally accessible, consider making it const
namespace Ui {

class ForegroundBackground;

}
}

class ForegroundBackground : public QObject
{
Q_OBJECT

public:

ForegroundBackground() = default;
~ForegroundBackground() = default;

/**
* @brief EventFilter catches events that should trigger ForegroundBackground
* @ingroup gui
*/
bool eventFilter(QObject *obj, QEvent *event) override;

private:
/**
* @brief ToForeground() enables the macOS menubar and dock icon, which are necessary for a maximized window to be able to exit full screen.
* @ingroup gui
*/
void ToForeground();

/**
* @brief ToBackground() disables the macOS menubar and dock icon, so that the application will only be present as a menubar icon.
* @ingroup gui
*/
void ToBackground();
};

#endif
39 changes: 39 additions & 0 deletions src/gui/foregroundbackground_mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) by Elsie Hupp <gpl at elsiehupp dot com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*/

#include "foregroundbackground_interface.h"
#include "foregroundbackground_cocoa.h"

bool ForegroundBackground::eventFilter(QObject * /*obj*/, QEvent *event)
{
if (event->type() == QEvent::Show) {
ToForeground();
return true;
}
if (event->type() == QEvent::Close) {
ToBackground();
return true;
}
return false;
}

void ForegroundBackground::ToForeground()
{
[CocoaProcessType ToForeground];
}

void ForegroundBackground::ToBackground()
{
[CocoaProcessType ToBackground];
}
8 changes: 8 additions & 0 deletions src/gui/owncloudgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
#include <QQuickItem>
#include <QQmlContext>

#ifdef Q_OS_MAC
#include "foregroundbackground_interface.h"
#endif

#ifdef BUILD_FILE_PROVIDER_MODULE
#include "macOS/fileprovider.h"
#include "macOS/fileproviderdomainmanager.h"
Expand Down Expand Up @@ -598,6 +602,10 @@ void ownCloudGui::slotShowSettings()
if (_settingsDialog.isNull()) {
_settingsDialog = new SettingsDialog(this);
_settingsDialog->setAttribute(Qt::WA_DeleteOnClose, true);
#ifdef Q_OS_MAC
auto *fgbg = new ForegroundBackground();
_settingsDialog->installEventFilter(fgbg);
#endif
_settingsDialog->show();
}
raiseDialog(_settingsDialog.data());
Expand Down
8 changes: 8 additions & 0 deletions src/gui/wizard/owncloudwizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
* for more details.
*/

#include "account.h"

Check failure on line 16 in src/gui/wizard/owncloudwizard.cpp

View workflow job for this annotation

GitHub Actions / build

src/gui/wizard/owncloudwizard.cpp:16:10 [clang-diagnostic-error]

'account.h' file not found
#include "config.h"
#include "configfile.h"
#include "theme.h"
#include "owncloudgui.h"

#ifdef Q_OS_MAC
#include "foregroundbackground_interface.h"
#endif

#include "wizard/owncloudwizard.h"
#include "wizard/welcomepage.h"
#include "wizard/owncloudsetuppage.h"
Expand Down Expand Up @@ -56,6 +60,10 @@
, _webViewPage(nullptr)
#endif // WITH_WEBENGINE
{
#ifdef Q_OS_MAC
auto *fgbg = new ForegroundBackground();
this->installEventFilter(fgbg);
#endif
setObjectName("owncloudWizard");

setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
Expand Down
Loading