Skip to content
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
12 changes: 2 additions & 10 deletions src/config/generalconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ GeneralConf::GeneralConf(QWidget* parent)
initScrollArea();

initAutostart();
#if !defined(Q_OS_WIN)
initAutoCloseIdleDaemon();
#endif
initShowTrayIcon();
initShowDesktopNotification();
initShowAbortNotification();
Expand Down Expand Up @@ -112,11 +110,7 @@ void GeneralConf::_updateComponents(bool allowEmptySavePath)
m_squareMagnifier->setChecked(config.squareMagnifier());
m_saveLastRegion->setChecked(config.saveLastRegion());
m_reverseArrow->setChecked(config.reverseArrow());

#if !defined(Q_OS_WIN)
m_autoCloseIdleDaemon->setChecked(config.autoCloseIdleDaemon());
#endif

m_predefinedColorPaletteLarge->setChecked(
config.predefinedColorPaletteLarge());
m_showStartupLaunchMessage->setChecked(config.showStartupLaunchMessage());
Expand All @@ -127,9 +121,9 @@ void GeneralConf::_updateComponents(bool allowEmptySavePath)
if (allowEmptySavePath || !config.savePath().isEmpty()) {
m_savePath->setText(config.savePath());
}
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)

m_showTray->setChecked(!config.disabledTrayIcon());
#endif

#if !defined(Q_OS_MACOS)
m_captureActiveMonitor->setChecked(config.captureActiveMonitor());
#endif
Expand Down Expand Up @@ -335,15 +329,13 @@ void GeneralConf::initShowAbortNotification()

void GeneralConf::initShowTrayIcon()
{
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
m_showTray = new QCheckBox(tr("Show tray icon"), this);
m_showTray->setToolTip(tr("Show icon in the system tray"));
m_scrollAreaLayout->addWidget(m_showTray);

connect(m_showTray, &QCheckBox::clicked, this, [](bool checked) {
ConfigHandler().setDisabledTrayIcon(!checked);
});
#endif
}

void GeneralConf::initHistoryConfirmationToDelete()
Expand Down
81 changes: 73 additions & 8 deletions src/core/flameshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@
#endif

#if defined(Q_OS_MACOS)
#include <QWindow>
#include <objc/message.h>

namespace {

constexpr long NSApplicationActivationPolicyRegular = 0;
constexpr long NSApplicationActivationPolicyAccessory = 1;

void setActivationPolicy(long policy)
{
auto sharedApp = reinterpret_cast<id (*)(id, SEL)>(objc_msgSend);
auto setPolicy = reinterpret_cast<void (*)(id, SEL, long)>(objc_msgSend);
id nsApp = sharedApp(reinterpret_cast<id>(objc_getClass("NSApplication")),
sel_registerName("sharedApplication"));
setPolicy(nsApp, sel_registerName("setActivationPolicy:"), policy);
}

void setActivationPolicyRegular()
{
setActivationPolicy(NSApplicationActivationPolicyRegular);
}

void setActivationPolicyAccessory()
{
setActivationPolicy(NSApplicationActivationPolicyAccessory);
}

constexpr const char* visibleInDockProperty = "_visibleInDock";

} // namespace

#include <CoreGraphics/CoreGraphics.h>
#endif

Expand Down Expand Up @@ -227,8 +258,7 @@ void Flameshot::launcher()
}
m_launcherWindow->show();
#if defined(Q_OS_MACOS)
m_launcherWindow->activateWindow();
m_launcherWindow->raise();
showDockIcon(m_launcherWindow);
#endif
}

Expand All @@ -248,8 +278,7 @@ void Flameshot::config()
position.moveCenter(currentScreen->availableGeometry().center());
m_configWindow->move(position.topLeft());
#if defined(Q_OS_MACOS)
m_configWindow->activateWindow();
m_configWindow->raise();
showDockIcon(m_configWindow);
#endif
}
}
Expand All @@ -259,8 +288,7 @@ void Flameshot::info()
if (m_infoWindow == nullptr) {
m_infoWindow = new InfoWindow();
#if defined(Q_OS_MACOS)
m_infoWindow->activateWindow();
m_infoWindow->raise();
showDockIcon(m_infoWindow);
#endif
}
}
Expand All @@ -286,9 +314,46 @@ void Flameshot::history()
historyWidget->move(position.topLeft());

#if defined(Q_OS_MACOS)
historyWidget->activateWindow();
historyWidget->raise();
showDockIcon(historyWidget);
#endif
}
#endif

#if defined(Q_OS_MACOS)
void Flameshot::onWindowVisibilityChanged(QWindow::Visibility newVisibility)
{
auto* qw = qobject_cast<QWindow*>(sender());
if (!qw) {
return;
}

if (newVisibility == QWindow::Hidden) {
qw->setProperty(visibleInDockProperty, false);
--m_dockIconVisibleCount;
if (m_dockIconVisibleCount == 0) {
setActivationPolicyAccessory();
}
} else {
bool windowTrackedInDock = qw->property(visibleInDockProperty).toBool();
if (!windowTrackedInDock) {
qw->setProperty(visibleInDockProperty, true);
++m_dockIconVisibleCount;
setActivationPolicyRegular();
}
}
}

void Flameshot::showDockIcon(QWidget* w)
{
QWindow* qw = w->windowHandle();
if (!qw) {
return;
}

connect(qw,
&QWindow::visibilityChanged,
this,
&Flameshot::onWindowVisibilityChanged);
}
#endif

Expand Down
11 changes: 11 additions & 0 deletions src/core/flameshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#include <QObject>
#include <QPointer>
#include <QVersionNumber>
#include <QWindow>

class CaptureWidget;
class ConfigWindow;
class InfoWindow;
class CaptureLauncher;
class QWidget;
#ifdef ENABLE_IMGUR
class UploadHistory;
#endif
Expand Down Expand Up @@ -91,6 +93,15 @@ public slots:
QPointer<CaptureLauncher> m_launcherWindow;
QPointer<ConfigWindow> m_configWindow;

#if defined(Q_OS_MACOS)
public:
void showDockIcon(QWidget* window);

private:
void onWindowVisibilityChanged(QWindow::Visibility newVisibility);
int m_dockIconVisibleCount = 0;
#endif

#if (defined(Q_OS_MACOS) || defined(Q_OS_WIN))
QHotkey* m_HotkeyScreenshotCapture;
#endif
Expand Down
14 changes: 3 additions & 11 deletions src/core/flameshotdaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@
* quits.
*
* If the `autoCloseIdleDaemon` option is true, the daemon will close as soon as
* it is not needed to host pinned screenshots and the clipboard. On Windows,
* this option is disabled and the daemon always persists, because the system
* tray is currently the only way to interact with flameshot there.
* it is not needed to host pinned screenshots and the clipboard.
*
* Both the daemon and non-daemon flameshot processes use the same public API,
* which is implemented as static methods. In the daemon process, this class is
Expand Down Expand Up @@ -85,9 +83,7 @@ FlameshotDaemon::FlameshotDaemon()
m_hostingClipboard = false;
quitIfIdle();
});
#ifdef Q_OS_WIN
m_persist = true;
#else

m_persist = !ConfigHandler().autoCloseIdleDaemon();
connect(ConfigHandler::getInstance(),
&ConfigHandler::fileChanged,
Expand All @@ -97,7 +93,6 @@ FlameshotDaemon::FlameshotDaemon()
enableTrayIcon(!config.disabledTrayIcon());
m_persist = !config.autoCloseIdleDaemon();
});
#endif

#if !defined(DISABLE_UPDATE_CHECKER)
if (ConfigHandler().checkForUpdates()) {
Expand Down Expand Up @@ -371,13 +366,10 @@ void FlameshotDaemon::attachTextToClipboard(const QString& text,

void FlameshotDaemon::initTrayIcon()
{
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
if (!ConfigHandler().disabledTrayIcon()) {
enableTrayIcon(true);
}
#elif defined(Q_OS_WIN)
enableTrayIcon(true);

#if defined(Q_OS_WIN)
GlobalShortcutFilter* nativeFilter = new GlobalShortcutFilter(this);
qApp->installNativeEventFilter(nativeFilter);
#endif
Expand Down
2 changes: 0 additions & 2 deletions src/utils/confighandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
OPTION("allowMultipleGuiInstances" ,Bool ( false )),
OPTION("showMagnifier" ,Bool ( false )),
OPTION("squareMagnifier" ,Bool ( false )),
#if !defined(Q_OS_WIN)
OPTION("autoCloseIdleDaemon" ,Bool ( false )),
#endif
OPTION("startupLaunch" ,Bool ( false )),
OPTION("showStartupLaunchMessage" ,Bool ( true )),
OPTION("showQuitPrompt" ,Bool ( false )),
Expand Down
Loading