Skip to content

Commit

Permalink
Dynamically show and hide menubar and dock icon
Browse files Browse the repository at this point in the history
Signed-off-by: Elsie Hupp <[email protected]>
  • Loading branch information
elsiehupp committed Mar 6, 2021
1 parent 8f81d24 commit da9b1cc
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmake/modules/MacOSXBundleInfo.plist.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<dict>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>LSUIElement</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ endif()

IF( APPLE )
list(APPEND client_SRCS cocoainitializer_mac.mm)
list(APPEND client_SRCS cocoaprocesstype.mm)
list(APPEND client_SRCS cocoaprocesstypeinterface.mm)
list(APPEND client_SRCS socketapisocket_mac.mm)
list(APPEND client_SRCS systray.mm)

Expand Down
40 changes: 40 additions & 0 deletions src/gui/cocoaprocesstype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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
32 changes: 32 additions & 0 deletions src/gui/cocoaprocesstype.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 "cocoaprocesstype.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
40 changes: 40 additions & 0 deletions src/gui/cocoaprocesstypeinterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

/*
* 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 CocoaProcessTypeInterface allows CocoaProcessType to be called C++
* @ingroup gui
*/

namespace OCC {
namespace Mac {

#ifndef COCOAPROCESSTYPEINTERFACE_H
#define COCOAPROCESSTYPEINTERFACE_H

/**
* @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 CocoaProcessTypeToForeground();

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

#endif

} // namespace Mac
} // namespace OCC
29 changes: 29 additions & 0 deletions src/gui/cocoaprocesstypeinterface.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 "cocoaprocesstypeinterface.h"
#include "cocoaprocesstype.h"

namespace OCC {
namespace Mac {

void CocoaProcessTypeToForeground()
{
[CocoaProcessType ToForeground];
}
void CocoaProcessTypeToBackground()
{
[CocoaProcessType ToBackground];
}

} // namespace Mac
} // namespace OCC
14 changes: 14 additions & 0 deletions src/gui/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "cocoaprocesstypeinterface.h"

#include "folderman.h"
#include "theme.h"
Expand Down Expand Up @@ -86,6 +87,10 @@ SettingsDialog::SettingsDialog(ownCloudGui *gui, QWidget *parent)
, _ui(new Ui::SettingsDialog)
, _gui(gui)
{
if (OCC::Utility::isMac()) {
Mac::CocoaProcessTypeToForeground();
}

ConfigFile cfg;

_ui->setupUi(this);
Expand Down Expand Up @@ -170,6 +175,9 @@ SettingsDialog::SettingsDialog(ownCloudGui *gui, QWidget *parent)

SettingsDialog::~SettingsDialog()
{
if (OCC::Utility::isMac()) {
Mac::CocoaProcessTypeToBackground();
}
delete _ui;
}

Expand All @@ -184,13 +192,19 @@ void SettingsDialog::reject()
ConfigFile cfg;
cfg.saveGeometry(this);
QDialog::reject();
if (OCC::Utility::isMac()) {
Mac::CocoaProcessTypeToBackground();
}
}

void SettingsDialog::accept()
{
ConfigFile cfg;
cfg.saveGeometry(this);
QDialog::accept();
if (OCC::Utility::isMac()) {
Mac::CocoaProcessTypeToBackground();
}
}

void SettingsDialog::changeEvent(QEvent *e)
Expand Down

0 comments on commit da9b1cc

Please sign in to comment.