Skip to content

Commit

Permalink
Show/Hide Menubar and Dock Icon
Browse files Browse the repository at this point in the history
Signed-off-by: Elsie Hupp <9206310+elsiehupp@users.noreply.github.com>
  • Loading branch information
elsiehupp committed May 8, 2021
1 parent e0dd06b commit a48cc3f
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ endif()
IF( APPLE )
list(APPEND client_SRCS cocoainitializer_mac.mm)
list(APPEND client_SRCS socketapisocket_mac.mm)
list(APPEND client_SRCS foregroundbackground_interface.h)
list(APPEND client_SRCS foregroundbackground_mac.mm)
list(APPEND client_SRCS foregroundbackground_cocoa.mm)
list(APPEND client_SRCS systray.mm)

if(SPARKLE_FOUND AND BUILD_UPDATER)
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>

/**
* @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 @@
/*
* 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>
#include <QEvent>

namespace OCC {
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];
}
9 changes: 9 additions & 0 deletions src/gui/owncloudgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#include "owncloudgui.h"
#include "theme.h"
#include "folderman.h"

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

#include "progressdispatcher.h"
#include "owncloudsetupwizard.h"
#include "sharedialog.h"
Expand Down Expand Up @@ -526,6 +531,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
17 changes: 16 additions & 1 deletion src/gui/owncloudsetupwizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include "filesystem.h"
#include "owncloudgui.h"

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

#include "creds/credentialsfactory.h"
#include "creds/abstractcredentials.h"
#include "creds/dummycredentials.h"
Expand All @@ -45,6 +49,10 @@ OwncloudSetupWizard::OwncloudSetupWizard(QObject *parent)
, _ocWizard(new OwncloudWizard)
, _remoteFolder()
{
#ifdef Q_OS_MAC
auto *fgbg = new ForegroundBackground();
this->installEventFilter(fgbg);
#endif
connect(_ocWizard, &OwncloudWizard::determineAuthType,
this, &OwncloudSetupWizard::slotCheckServer);
connect(_ocWizard, &OwncloudWizard::connectToOCUrl,
Expand Down Expand Up @@ -77,6 +85,10 @@ void OwncloudSetupWizard::runWizard(QObject *obj, const char *amember, QWidget *
wiz = new OwncloudSetupWizard(parent);
connect(wiz, SIGNAL(ownCloudWizardDone(int)), obj, amember);
FolderMan::instance()->setSyncEnabled(false);
// #ifdef Q_OS_MAC
// auto *fgbg = new ForegroundBackground();
// wiz->installEventFilter(fgbg);
// #endif
wiz->startWizard();
}

Expand All @@ -85,7 +97,10 @@ bool OwncloudSetupWizard::bringWizardToFrontIfVisible()
if (wiz.isNull()) {
return false;
}

// #ifdef Q_OS_MAC
// auto *fgbg = new ForegroundBackground();
// wiz->_ocWizard->installEventFilter(fgbg);
// #endif
ownCloudGui::raiseDialog(wiz->_ocWizard);
return true;
}
Expand Down

0 comments on commit a48cc3f

Please sign in to comment.