Skip to content

Commit e013bb1

Browse files
committed
Added option to use the native window frame
This change disables window manager frame by default again for Linux and instead adds an option to use the native frame for all platforms.
1 parent ccf71e5 commit e013bb1

File tree

2 files changed

+84
-59
lines changed

2 files changed

+84
-59
lines changed

src/mainwindow.cpp

Lines changed: 82 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ MainWindow::MainWindow(QWidget *parent) :
3434
m_autoSaveTimer(new QTimer(this)),
3535
m_settingsDatabase(Q_NULLPTR),
3636
m_clearButton(Q_NULLPTR),
37-
#ifndef Q_OS_LINUX
3837
m_greenMaximizeButton(Q_NULLPTR),
3938
m_redCloseButton(Q_NULLPTR),
4039
m_yellowMinimizeButton(Q_NULLPTR),
4140
m_trafficLightLayout(Q_NULLPTR),
42-
#endif
4341
m_newNoteButton(Q_NULLPTR),
4442
m_trashButton(Q_NULLPTR),
4543
m_dotsButton(Q_NULLPTR),
@@ -68,7 +66,9 @@ MainWindow::MainWindow(QWidget *parent) :
6866
m_isListViewScrollBarHidden(true),
6967
m_isContentModified(false),
7068
m_isOperationRunning(false),
71-
m_dontShowUpdateWindow(false)
69+
m_dontShowUpdateWindow(false),
70+
m_alwaysStayOnTop(false),
71+
m_useNativeWindowFrame(false)
7272
{
7373
ui->setupUi(this);
7474
setupMainWindow();
@@ -159,25 +159,26 @@ void MainWindow::setMainWindowVisibility(bool state)
159159
*/
160160
void MainWindow::paintEvent(QPaintEvent* event)
161161
{
162-
#ifndef Q_OS_LINUX
163-
QPainter painter(this);
164-
painter.save();
162+
if (!m_useNativeWindowFrame) {
163+
QPainter painter(this);
164+
painter.save();
165165

166-
painter.setRenderHint(QPainter::Antialiasing);
167-
painter.setPen(Qt::NoPen);
166+
painter.setRenderHint(QPainter::Antialiasing);
167+
painter.setPen(Qt::NoPen);
168168

169-
dropShadow(painter, ShadowType::Linear, ShadowSide::Left );
170-
dropShadow(painter, ShadowType::Linear, ShadowSide::Top );
171-
dropShadow(painter, ShadowType::Linear, ShadowSide::Right );
172-
dropShadow(painter, ShadowType::Linear, ShadowSide::Bottom);
169+
dropShadow(painter, ShadowType::Linear, ShadowSide::Left );
170+
dropShadow(painter, ShadowType::Linear, ShadowSide::Top );
171+
dropShadow(painter, ShadowType::Linear, ShadowSide::Right );
172+
dropShadow(painter, ShadowType::Linear, ShadowSide::Bottom);
173173

174-
dropShadow(painter, ShadowType::Radial, ShadowSide::TopLeft );
175-
dropShadow(painter, ShadowType::Radial, ShadowSide::TopRight );
176-
dropShadow(painter, ShadowType::Radial, ShadowSide::BottomRight);
177-
dropShadow(painter, ShadowType::Radial, ShadowSide::BottomLeft );
174+
dropShadow(painter, ShadowType::Radial, ShadowSide::TopLeft );
175+
dropShadow(painter, ShadowType::Radial, ShadowSide::TopRight );
176+
dropShadow(painter, ShadowType::Radial, ShadowSide::BottomRight);
177+
dropShadow(painter, ShadowType::Radial, ShadowSide::BottomLeft );
178+
179+
painter.restore();
180+
}
178181

179-
painter.restore();
180-
#endif
181182
QMainWindow::paintEvent(event);
182183
}
183184

@@ -219,23 +220,21 @@ MainWindow::~MainWindow()
219220
*/
220221
void MainWindow::setupMainWindow()
221222
{
222-
#ifdef _WIN32
223-
this->setWindowFlags(Qt::CustomizeWindowHint);
224-
#elif defined(__APPLE__)
223+
#if defined(Q_OS_LINUX) || defined(__APPLE__)
225224
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
226225
this->setAttribute(Qt::WA_TranslucentBackground);
227-
#elif !defined(Q_OS_LINUX)
226+
#elif _WIN32
227+
this->setWindowFlags(Qt::CustomizeWindowHint);
228+
#elif
228229
#error "We don't support that version yet..."
229230
#endif
230231

231-
#ifndef Q_OS_LINUX
232232
m_greenMaximizeButton = new QPushButton(this);
233233
m_redCloseButton = new QPushButton(this);
234234
m_yellowMinimizeButton = new QPushButton(this);
235235
m_trafficLightLayout.addWidget(m_redCloseButton);
236236
m_trafficLightLayout.addWidget(m_yellowMinimizeButton);
237237
m_trafficLightLayout.addWidget(m_greenMaximizeButton);
238-
#endif
239238

240239
#ifdef _WIN32
241240
m_trafficLightLayout.setSpacing(0);
@@ -416,11 +415,9 @@ void MainWindow::setupTitleBarButtons()
416415
" padding: 0px; "
417416
"}");
418417

419-
#ifndef Q_OS_LINUX
420418
m_redCloseButton->setStyleSheet(ss);
421419
m_yellowMinimizeButton->setStyleSheet(ss);
422420
m_greenMaximizeButton->setStyleSheet(ss);
423-
#endif
424421

425422
#ifdef _WIN32
426423
m_redCloseButton->setIcon(QIcon(QStringLiteral(":images/windows_close_regular.png")));
@@ -432,11 +429,9 @@ void MainWindow::setupTitleBarButtons()
432429
m_greenMaximizeButton->setIconSize(QSize(28, 16));
433430
#endif
434431

435-
#ifndef Q_OS_LINUX
436432
m_redCloseButton->installEventFilter(this);
437433
m_yellowMinimizeButton->installEventFilter(this);
438434
m_greenMaximizeButton->installEventFilter(this);
439-
#endif
440435
}
441436

442437
/*!
@@ -449,7 +444,6 @@ void MainWindow::setupSignalsSlots()
449444
// actions
450445
// connect(rightToLeftActionion, &QAction::triggered, this, );
451446
//connect(checkForUpdatesAction, &QAction::triggered, this, );
452-
#ifndef Q_OS_LINUX
453447
// green button
454448
connect(m_greenMaximizeButton, &QPushButton::pressed, this, &MainWindow::onGreenMaximizeButtonPressed);
455449
connect(m_greenMaximizeButton, &QPushButton::clicked, this, &MainWindow::onGreenMaximizeButtonClicked);
@@ -459,7 +453,6 @@ void MainWindow::setupSignalsSlots()
459453
// yellow button
460454
connect(m_yellowMinimizeButton, &QPushButton::pressed, this, &MainWindow::onYellowMinimizeButtonPressed);
461455
connect(m_yellowMinimizeButton, &QPushButton::clicked, this, &MainWindow::onYellowMinimizeButtonClicked);
462-
#endif
463456
// new note button
464457
connect(m_newNoteButton, &QPushButton::pressed, this, &MainWindow::onNewNoteButtonPressed);
465458
connect(m_newNoteButton, &QPushButton::clicked, this, &MainWindow::onNewNoteButtonClicked);
@@ -728,6 +721,8 @@ void MainWindow::setupModelView()
728721
*/
729722
void MainWindow::restoreStates()
730723
{
724+
setUseNativeWindowFrame(m_settingsDatabase->value(QStringLiteral("useNativeWindowFrame"), false).toBool());
725+
731726
if(m_settingsDatabase->value(QStringLiteral("windowGeometry"), "NULL") != "NULL")
732727
this->restoreGeometry(m_settingsDatabase->value(QStringLiteral("windowGeometry")).toByteArray());
733728

@@ -931,11 +926,9 @@ void MainWindow::createNewNoteIfEmpty()
931926
*/
932927
void MainWindow::setButtonsAndFieldsEnabled(bool doEnable)
933928
{
934-
#ifndef Q_OS_LINUX
935929
m_greenMaximizeButton->setEnabled(doEnable);
936930
m_redCloseButton->setEnabled(doEnable);
937931
m_yellowMinimizeButton->setEnabled(doEnable);
938-
#endif
939932
m_newNoteButton->setEnabled(doEnable);
940933
m_trashButton->setEnabled(doEnable);
941934
m_searchEdit->setEnabled(doEnable);
@@ -1106,6 +1099,13 @@ void MainWindow::onDotsButtonClicked()
11061099
connect(stayOnTopAction, &QAction::triggered, this, &MainWindow::stayOnTop);
11071100
#endif
11081101

1102+
// Use native frame action
1103+
QAction* useNativeFrameAction = viewMenu->addAction(tr("Use native window frame"));
1104+
useNativeFrameAction->setToolTip(tr("Use the window frame provided by the window manager"));
1105+
useNativeFrameAction->setCheckable(true);
1106+
useNativeFrameAction->setChecked(m_useNativeWindowFrame);
1107+
connect(useNativeFrameAction, &QAction::triggered, this, &MainWindow::setUseNativeWindowFrame);
1108+
11091109
mainMenu.exec(m_dotsButton->mapToGlobal(QPoint(0, m_dotsButton->height())));
11101110
}
11111111

@@ -1748,7 +1748,6 @@ void MainWindow::expandNoteList()
17481748
*/
17491749
void MainWindow::onGreenMaximizeButtonPressed()
17501750
{
1751-
#ifndef Q_OS_LINUX
17521751
#ifdef _WIN32
17531752
m_greenMaximizeButton->setIcon(QIcon(":images/windows_minimize_pressed.png"));
17541753
#else
@@ -1758,7 +1757,6 @@ void MainWindow::onGreenMaximizeButtonPressed()
17581757
m_greenMaximizeButton->setIcon(QIcon(QStringLiteral(":images/greenPressed.png")));
17591758
}
17601759
#endif
1761-
#endif
17621760
}
17631761

17641762
/*!
@@ -1767,7 +1765,6 @@ void MainWindow::onGreenMaximizeButtonPressed()
17671765
*/
17681766
void MainWindow::onYellowMinimizeButtonPressed()
17691767
{
1770-
#ifndef Q_OS_LINUX
17711768
#ifdef _WIN32
17721769
if(this->windowState() == Qt::WindowFullScreen){
17731770
m_yellowMinimizeButton->setIcon(QIcon(QStringLiteral(":images/windows_de-maximize_pressed.png")));
@@ -1777,7 +1774,6 @@ void MainWindow::onYellowMinimizeButtonPressed()
17771774
#else
17781775
m_yellowMinimizeButton->setIcon(QIcon(QStringLiteral(":images/yellowPressed.png")));
17791776
#endif
1780-
#endif
17811777
}
17821778

17831779
/*!
@@ -1786,13 +1782,11 @@ void MainWindow::onYellowMinimizeButtonPressed()
17861782
*/
17871783
void MainWindow::onRedCloseButtonPressed()
17881784
{
1789-
#ifndef Q_OS_LINUX
17901785
#ifdef _WIN32
17911786
m_redCloseButton->setIcon(QIcon(QStringLiteral(":images/windows_close_pressed.png")));
17921787
#else
17931788
m_redCloseButton->setIcon(QIcon(QStringLiteral(":images/redPressed.png")));
17941789
#endif
1795-
#endif
17961790
}
17971791

17981792
/*!
@@ -1801,7 +1795,6 @@ void MainWindow::onRedCloseButtonPressed()
18011795
*/
18021796
void MainWindow::onGreenMaximizeButtonClicked()
18031797
{
1804-
#ifndef Q_OS_LINUX
18051798
#ifdef _WIN32
18061799
m_greenMaximizeButton->setIcon(QIcon(QStringLiteral(":images/windows_minimize_regular.png")));
18071800

@@ -1812,7 +1805,6 @@ void MainWindow::onGreenMaximizeButtonClicked()
18121805

18131806
fullscreenWindow();
18141807
#endif
1815-
#endif
18161808
}
18171809

18181810
/*!
@@ -1821,7 +1813,6 @@ void MainWindow::onGreenMaximizeButtonClicked()
18211813
*/
18221814
void MainWindow::onYellowMinimizeButtonClicked()
18231815
{
1824-
#ifndef Q_OS_LINUX
18251816
#ifdef _WIN32
18261817
m_yellowMinimizeButton->setIcon(QIcon(QStringLiteral(":images/windows_de-maximize_regular.png")));
18271818

@@ -1832,7 +1823,6 @@ void MainWindow::onYellowMinimizeButtonClicked()
18321823
minimizeWindow();
18331824
m_restoreAction->setText(tr("&Show Notes"));
18341825
#endif
1835-
#endif
18361826
}
18371827

18381828
/*!
@@ -1842,15 +1832,13 @@ void MainWindow::onYellowMinimizeButtonClicked()
18421832
*/
18431833
void MainWindow::onRedCloseButtonClicked()
18441834
{
1845-
#ifndef Q_OS_LINUX
18461835
#ifdef _WIN32
18471836
m_redCloseButton->setIcon(QIcon(QStringLiteral(":images/windows_close_regular.png")));
18481837
#else
18491838
m_redCloseButton->setIcon(QIcon(QStringLiteral(":images/red.png")));
18501839
#endif
18511840

18521841
setMainWindowVisibility(false);
1853-
#endif
18541842
}
18551843

18561844
/*!
@@ -2551,7 +2539,7 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event)
25512539
if(object == m_greenMaximizeButton){
25522540
m_greenMaximizeButton->setIcon(QIcon(QStringLiteral(":images/windows_minimize_hovered.png")));
25532541
}
2554-
#elif !defined(Q_OS_LINUX)
2542+
#else
25552543
// When hovering one of the traffic light buttons (red, yellow, green),
25562544
// set new icons to show their function
25572545
if(object == m_redCloseButton
@@ -2593,7 +2581,6 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event)
25932581
case QEvent::Leave:{
25942582
if(qApp->applicationState() == Qt::ApplicationActive){
25952583
// When not hovering, change back the icons of the traffic lights to their default icon
2596-
#ifndef Q_OS_LINUX
25972584
if(object == m_redCloseButton
25982585
|| object == m_yellowMinimizeButton
25992586
|| object == m_greenMaximizeButton){
@@ -2613,7 +2600,6 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event)
26132600
m_greenMaximizeButton->setIcon(QIcon(QStringLiteral(":images/green.png")));
26142601
#endif
26152602
}
2616-
#endif
26172603

26182604
if(object == m_newNoteButton){
26192605
this->unsetCursor();
@@ -2638,7 +2624,7 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event)
26382624
m_canStretchWindow = false;
26392625
QApplication::restoreOverrideCursor();
26402626

2641-
#if !defined(_WIN32) && !defined(Q_OS_LINUX)
2627+
#ifndef _WIN32
26422628
m_redCloseButton->setIcon(QIcon(QStringLiteral(":images/unfocusedButton")));
26432629
m_yellowMinimizeButton->setIcon(QIcon(QStringLiteral(":images/unfocusedButton")));
26442630
m_greenMaximizeButton->setIcon(QIcon(QStringLiteral(":images/unfocusedButton")));
@@ -2658,7 +2644,7 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event)
26582644
}else{
26592645
m_yellowMinimizeButton->setIcon(QIcon (QStringLiteral(":images/windows_maximize_regular.png")));
26602646
}
2661-
#elif !defined(Q_OS_LINUX)
2647+
#else
26622648
m_redCloseButton->setIcon(QIcon(QStringLiteral(":images/red.png")));
26632649
m_yellowMinimizeButton->setIcon(QIcon(QStringLiteral(":images/yellow.png")));
26642650
m_greenMaximizeButton->setIcon(QIcon(QStringLiteral(":images/green.png")));
@@ -2799,7 +2785,6 @@ bool MainWindow::eventFilter(QObject *object, QEvent *event)
27992785
*/
28002786
void MainWindow::stayOnTop(bool checked)
28012787
{
2802-
#ifndef Q_OS_LINUX
28032788
Qt::WindowFlags flags = windowFlags();
28042789

28052790
if (checked)
@@ -2809,11 +2794,52 @@ void MainWindow::stayOnTop(bool checked)
28092794

28102795
m_alwaysStayOnTop = checked;
28112796

2812-
this->setWindowFlags(flags);
2797+
setWindowFlags(flags);
28132798
setMainWindowVisibility(true);
2814-
#else
2815-
Q_UNUSED(checked)
2799+
}
2800+
2801+
void MainWindow::setUseNativeWindowFrame(bool useNativeWindowFrame)
2802+
{
2803+
if (m_useNativeWindowFrame == useNativeWindowFrame)
2804+
return;
2805+
2806+
m_useNativeWindowFrame = useNativeWindowFrame;
2807+
m_settingsDatabase->setValue(QStringLiteral("useNativeWindowFrame"), useNativeWindowFrame);
2808+
2809+
m_greenMaximizeButton->setVisible(!useNativeWindowFrame);
2810+
m_redCloseButton->setVisible(!useNativeWindowFrame);
2811+
m_yellowMinimizeButton->setVisible(!useNativeWindowFrame);
2812+
2813+
auto flags = windowFlags();
2814+
2815+
#if defined(Q_OS_LINUX) || defined(__APPLE__)
2816+
if (useNativeWindowFrame)
2817+
flags &= ~Qt::FramelessWindowHint;
2818+
else
2819+
flags |= Qt::FramelessWindowHint;
2820+
2821+
setAttribute(Qt::WA_TranslucentBackground, !useNativeWindowFrame);
2822+
#elif _WIN32
2823+
if (useNativeWindowFrame)
2824+
flags &= ~Qt::CustomizeWindowHint;
2825+
else
2826+
flags |= Qt::CustomizeWindowHint;
2827+
#elif
2828+
#error "We don't support that version yet..."
2829+
#endif
2830+
2831+
setWindowFlags(flags);
2832+
2833+
#ifndef _WIN32
2834+
if (useNativeWindowFrame || isMaximized()) {
2835+
ui->centralWidget->layout()->setContentsMargins(QMargins());
2836+
} else {
2837+
QMargins margins(m_layoutMargin,m_layoutMargin,m_layoutMargin,m_layoutMargin);
2838+
ui->centralWidget->layout()->setContentsMargins(margins);
2839+
}
28162840
#endif
2841+
2842+
setMainWindowVisibility(true);
28172843
}
28182844

28192845
/*!
@@ -2846,12 +2872,11 @@ void MainWindow::onSearchEditReturnPressed()
28462872
*/
28472873
void MainWindow::setMargins(QMargins margins)
28482874
{
2849-
#ifndef Q_OS_LINUX
2875+
if (m_useNativeWindowFrame)
2876+
return;
2877+
28502878
ui->centralWidget->layout()->setContentsMargins(margins);
28512879
m_trafficLightLayout.setGeometry(QRect(4+margins.left(),4+margins.top(),56,16));
2852-
#else
2853-
Q_UNUSED(margins)
2854-
#endif
28552880
}
28562881

28572882
/*!

0 commit comments

Comments
 (0)