Skip to content

Commit 9803b38

Browse files
Added feature on x11 and windows to capture active monitor (flameshot-org#4594)
1 parent 51a5518 commit 9803b38

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

src/config/generalconf.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ GeneralConf::GeneralConf(QWidget* parent)
5656
initAntialiasingPinZoom();
5757
initUndoLimit();
5858
initInsecurePixelate();
59+
#if !defined(Q_OS_MACOS)
60+
initCaptureActiveMonitor();
61+
#endif
5962
#if defined(Q_OS_LINUX)
6063
initUseX11LegacyScreenshot();
6164
#endif
@@ -127,6 +130,9 @@ void GeneralConf::_updateComponents(bool allowEmptySavePath)
127130
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
128131
m_showTray->setChecked(!config.disabledTrayIcon());
129132
#endif
133+
#if !defined(Q_OS_MACOS)
134+
m_captureActiveMonitor->setChecked(config.captureActiveMonitor());
135+
#endif
130136
#if defined(Q_OS_LINUX)
131137
m_useX11LegacyScreenshot->setChecked(config.useX11LegacyScreenshot());
132138
#endif
@@ -916,6 +922,29 @@ void GeneralConf::setInsecurePixelate(bool checked)
916922
ConfigHandler().setInsecurePixelate(checked);
917923
}
918924

925+
#if !defined(Q_OS_MACOS)
926+
void GeneralConf::initCaptureActiveMonitor()
927+
{
928+
m_captureActiveMonitor = new QCheckBox(
929+
tr("Capture active monitor (skip monitor selection)"), this);
930+
m_captureActiveMonitor->setToolTip(
931+
tr("Automatically capture the monitor where the cursor is located "
932+
"instead of showing the monitor selection dialog. "
933+
"This feature is not supported on Wayland."));
934+
m_scrollAreaLayout->addWidget(m_captureActiveMonitor);
935+
936+
connect(m_captureActiveMonitor,
937+
&QCheckBox::clicked,
938+
this,
939+
&GeneralConf::captureActiveMonitorChanged);
940+
}
941+
942+
void GeneralConf::captureActiveMonitorChanged(bool checked)
943+
{
944+
ConfigHandler().setCaptureActiveMonitor(checked);
945+
}
946+
#endif
947+
919948
#if defined(Q_OS_LINUX)
920949
void GeneralConf::initUseX11LegacyScreenshot()
921950
{

src/config/generalconf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ private slots:
6161
void setJpegQuality(int v);
6262
void setReverseArrow(bool checked);
6363
void setInsecurePixelate(bool checked);
64+
#if !defined(Q_OS_MACOS)
65+
void captureActiveMonitorChanged(bool checked);
66+
#endif
6467
#if defined(Q_OS_LINUX)
6568
void useX11LegacyScreenshotChanged(bool checked);
6669
#endif
@@ -102,6 +105,9 @@ private slots:
102105
void initJpegQuality();
103106
void initReverseArrow();
104107
void initInsecurePixelate();
108+
#if !defined(Q_OS_MACOS)
109+
void initCaptureActiveMonitor();
110+
#endif
105111
#if defined(Q_OS_LINUX)
106112
void initUseX11LegacyScreenshot();
107113
#endif
@@ -153,6 +159,9 @@ private slots:
153159
QSpinBox* m_jpegQuality;
154160
QCheckBox* m_reverseArrow;
155161
QCheckBox* m_insecurePixelate;
162+
#if !defined(Q_OS_MACOS)
163+
QCheckBox* m_captureActiveMonitor;
164+
#endif
156165
#if defined(Q_OS_LINUX)
157166
QCheckBox* m_useX11LegacyScreenshot;
158167
#endif

src/utils/confighandler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
140140
// Not visible on settings dialog
141141
OPTION("ignorePrntScrForcesSnipping" ,Bool ( false )),
142142
#endif
143+
#if !defined(Q_OS_MACOS)
144+
// Auto-select the monitor under the cursor instead of showing
145+
// the monitor selection UI. Not supported on Wayland.
146+
OPTION("captureActiveMonitor" ,Bool ( false )),
147+
#endif
143148
#if defined(Q_OS_LINUX)
144149
// Bypass freedesktop portal and use Qt's native X11
145150
// screenshot method. Intended for WMs without xdg-desktop-portal.

src/utils/confighandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ class ConfigHandler : public QObject
145145
setIgnorePrntScrForcesSnipping,
146146
bool)
147147
#endif
148+
#if !defined(Q_OS_MACOS)
149+
CONFIG_GETTER_SETTER(captureActiveMonitor, setCaptureActiveMonitor, bool)
150+
#endif
148151
#if defined(Q_OS_LINUX)
149152
CONFIG_GETTER_SETTER(useX11LegacyScreenshot,
150153
setUseX11LegacyScreenshot,

src/utils/screengrabber.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ QPixmap ScreenGrabber::selectMonitorAndCrop(const QPixmap& fullScreenshot,
152152
return cropToMonitor(fullScreenshot, 0);
153153
}
154154

155+
// Capture Active Monitor: auto-select monitor under cursor
156+
if (ConfigHandler().captureActiveMonitor()) {
157+
if (m_info.waylandDetected()) {
158+
AbstractLogger::error()
159+
<< tr("Capture Active Monitor is not supported on Wayland due to "
160+
"Wayland security model.");
161+
ok = false;
162+
return QPixmap();
163+
}
164+
165+
QGuiAppCurrentScreen screenFinder;
166+
QScreen* cursorScreen = screenFinder.currentScreen();
167+
int monitorIndex = screens.indexOf(cursorScreen);
168+
if (monitorIndex >= 0) {
169+
m_selectedMonitor = monitorIndex;
170+
return cropToMonitor(fullScreenshot, monitorIndex);
171+
}
172+
// Fall through to manual selection if screen lookup fails
173+
}
174+
155175
if (m_monitorSelectionActive) {
156176
AbstractLogger::error()
157177
<< tr("Screenshot already in progress, please wait for the current "

0 commit comments

Comments
 (0)