Skip to content

Commit

Permalink
feat: auto hide title when fullscreen
Browse files Browse the repository at this point in the history
TODO:
* fix windowFlags
* make autoHideOnFullscreen really work

Change-Id: Id8af30d5c2cd48bfc05778e9f70b08cb4ce9e120
  • Loading branch information
Iceyer committed Jan 24, 2018
1 parent 7dd2e8b commit 3e2e292
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ build*/

src/DtkWidgets
src/dtkwidget_config.h
cmake/DtkWidget/DtkWidgetConfig.cmake
File renamed without changes.
8 changes: 7 additions & 1 deletion examples/dwidget-examples/collections/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ MainWindow::MainWindow(QWidget *parent)
QPushButton *lightBUtton = new QPushButton("Light", this);
QPushButton *enableButtons = new QPushButton("Enable Titlebar ", this);
QPushButton *disableButtons = new QPushButton("Disable Titlebar", this);
QPushButton *fullscreenButtons = new QPushButton("Fullscreen", this);

themeManager->setTheme(lightBUtton, "light");

Expand All @@ -84,10 +85,14 @@ MainWindow::MainWindow(QWidget *parent)
| Qt::WindowMaximizeButtonHint
| Qt::WindowSystemMenuHint);
});
connect(fullscreenButtons, &QPushButton::clicked, [ = ] {
showFullScreen();
});

styleLayout->addWidget(darkButton);
styleLayout->addWidget(lightBUtton);
styleLayout->addWidget(enableButtons);
styleLayout->addWidget(disableButtons);
styleLayout->addWidget(fullscreenButtons);
styleLayout->addStretch();

mainLayout->addLayout(styleLayout);
Expand Down Expand Up @@ -224,6 +229,7 @@ void MainWindow::initTabWidget()
m_mainTab->addTab(simplelistviewTab, "SimpleListView");

m_mainTab->setCurrentIndex(0);

}

MainWindow::~MainWindow()
Expand Down
3 changes: 2 additions & 1 deletion src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ QMAKE_PKGCONFIG_REQUIRES += dtkcore

# CMake configure
INC_DIR = $$replace(includes.path, "/", "\/")
CMD = sed -i -E \'s/DTKWIDGET_INCLUDE_DIR \".*\"\\)$/DTKWIDGET_INCLUDE_DIR \"$${INC_DIR}\"\\)/\' ../cmake/DtkWidget/DtkWidgetConfig.cmake
CMD = sed -E \'s/DTKWIDGET_INCLUDE_DIR \".*\"\\)$/DTKWIDGET_INCLUDE_DIR \"$${INC_DIR}\"\\)/\' ../cmake/DtkWidget/DtkWidgetConfig.cmake.in > ../cmake/DtkWidget/DtkWidgetConfig.cmake
message($$CMD)
system($$CMD)

cmake_config.path = $$LIB_INSTALL_DIR
Expand Down
108 changes: 103 additions & 5 deletions src/widgets/dtitlebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "private/dapplication_p.h"
#include "dthememanager.h"
#include "util/dwindowmanagerhelper.h"
#include "dmainwindow.h"

DWIDGET_BEGIN_NAMESPACE

Expand All @@ -54,6 +55,13 @@ class DTitlebarPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate

private:
void init();
// FIXME: get a batter salution
// hide title will make eventFilter not work, instead set Height to zero
bool isVisableOnFullscreen();
void hideOnFullscreen();
void showOnFullscreen();

void updateFullscreen();
void updateButtonsState(Qt::WindowFlags type);
void updateButtonsFunc();
void _q_toggleWindowState();
Expand All @@ -65,7 +73,6 @@ class DTitlebarPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate
void _q_helpActionTriggered();
void _q_aboutActionTriggered();
void _q_quitActionTriggered();

#endif

QHBoxLayout *mainLayout;
Expand All @@ -75,6 +82,7 @@ class DTitlebarPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate
DWindowMaxButton *maxButton;
DWindowCloseButton *closeButton;
DWindowOptionButton *optionButton;
DImageButton *quitFullButton;

QWidget *customWidget = Q_NULLPTR;
QWidget *coustomAtea;
Expand All @@ -97,6 +105,7 @@ class DTitlebarPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate
bool mousePressed = false;
bool useWindowFlags = false;
bool embedMode = false;
bool autoHideOnFullscreen = false;

Q_DECLARE_PUBLIC(DTitlebar)
};
Expand All @@ -116,6 +125,7 @@ void DTitlebarPrivate::init()
maxButton = new DWindowMaxButton;
closeButton = new DWindowCloseButton;
optionButton = new DWindowOptionButton;
quitFullButton = new DImageButton;
coustomAtea = new QWidget;
buttonArea = new QWidget;
titleArea = new QWidget;
Expand All @@ -127,6 +137,8 @@ void DTitlebarPrivate::init()
minButton->setObjectName("DTitlebarDWindowMinButton");
maxButton->setObjectName("DTitlebarDWindowMaxButton");
closeButton->setObjectName("DTitlebarDWindowCloseButton");
quitFullButton->setObjectName("DTitlebarDWindowQuitFullscreenButton");
quitFullButton->hide();

mainLayout->setContentsMargins(6, 0, 0, 0);
mainLayout->setSpacing(0);
Expand Down Expand Up @@ -158,6 +170,7 @@ void DTitlebarPrivate::init()
buttonAreaLayout->addWidget(minButton);
buttonAreaLayout->addWidget(maxButton);
buttonAreaLayout->addWidget(closeButton);
buttonAreaLayout->addWidget(quitFullButton);
buttonArea->setLayout(buttonAreaLayout);

QHBoxLayout *titleAreaLayout = new QHBoxLayout;
Expand Down Expand Up @@ -192,11 +205,66 @@ void DTitlebarPrivate::init()
coustomAtea->setFixedHeight(q->height());
buttonArea->setFixedHeight(q->height());

q->connect(quitFullButton, &DImageButton::clicked, q, [ = ]() {
bool isFullscreen = parentWindow->windowState().testFlag(Qt::WindowFullScreen);
if (isFullscreen) {
parentWindow->showNormal();
} else {
parentWindow->showFullScreen();
}
});
q->connect(optionButton, &DWindowOptionButton::clicked, q, &DTitlebar::optionClicked);
q->connect(DWindowManagerHelper::instance(), SIGNAL(windowMotifWMHintsChanged(quint32)),
q, SLOT(_q_onTopWindowMotifHintsChanged(quint32)));
}

bool DTitlebarPrivate::isVisableOnFullscreen()
{
D_Q(DTitlebar);
return !q->property("_restore_height").isValid();
}

void DTitlebarPrivate::hideOnFullscreen()
{
D_Q(DTitlebar);
q->setProperty("_restore_height", q->height());
q->setFixedHeight(0);
}

void DTitlebarPrivate::showOnFullscreen()
{
D_Q(DTitlebar);
if (q->property("_restore_height").isValid()) {
q->setFixedHeight(q->property("_restore_height").toInt());
q->setProperty("_restore_height", QVariant());
}
}

void DTitlebarPrivate::updateFullscreen()
{
D_Q(DTitlebar);

if (!autoHideOnFullscreen) {
return;
}

bool isFullscreen = parentWindow->windowState().testFlag(Qt::WindowFullScreen);
auto mainWindow = qobject_cast<DMainWindow *>(parentWindow);
if (!isFullscreen) {
quitFullButton->hide();
mainWindow->setMenuWidget(q);
showOnFullscreen();
} else {
// must set to empty
quitFullButton->show();
mainWindow->menuWidget()->setParent(nullptr);
mainWindow->setMenuWidget(Q_NULLPTR);
q->setParent(mainWindow);
q->show();
hideOnFullscreen();
}
}

void DTitlebarPrivate::updateButtonsState(Qt::WindowFlags type)
{
if (titleLabel) {
Expand Down Expand Up @@ -408,7 +476,7 @@ void DTitlebarPrivate::_q_quitActionTriggered()
* @param parent
*/
DTitlebar::DTitlebar(QWidget *parent) :
QWidget(parent),
QFrame(parent),
DObject(*new DTitlebarPrivate(this))
{
D_THEME_INIT_WIDGET(DTitlebar)
Expand Down Expand Up @@ -538,14 +606,32 @@ bool DTitlebar::eventFilter(QObject *obj, QEvent *event)

if (obj == d->parentWindow) {
switch (event->type()) {
case QEvent::WindowStateChange:
// if (d->maxButton->isEnabled()) {
case QEvent::WindowStateChange: {
d->maxButton->setMaximized(d->parentWindow->windowState() == Qt::WindowMaximized);
// }
d->updateFullscreen();
break;
}
case QEvent::ShowToParent:
d->updateButtonsFunc();
break;
case QEvent::Resize:
if (d->autoHideOnFullscreen) {
setFixedWidth(d->parentWindow->width());
}
break;
case QEvent::HoverMove: {
auto mouseEvent = reinterpret_cast<QMouseEvent *>(event);
bool isFullscreen = d->parentWindow->windowState().testFlag(Qt::WindowFullScreen);
if (isFullscreen && d->autoHideOnFullscreen) {
if (mouseEvent->pos().y() > height() && d->isVisableOnFullscreen()) {
d->hideOnFullscreen();
}
if (mouseEvent->pos().y() < 2) {
d->showOnFullscreen();
}
}
break;
}
default:
break;
}
Expand Down Expand Up @@ -723,6 +809,18 @@ bool DTitlebar::separatorVisible() const
return d->separator->isVisible();
}

bool DTitlebar::autoHideOnFullscreen() const
{
D_DC(DTitlebar);
return d->autoHideOnFullscreen;
}

void DTitlebar::setAutoHideOnFullscreen(bool autohide)
{
D_D(DTitlebar);
d->autoHideOnFullscreen = autohide;
}

/**
* @brief DTitlebar::setVisible overrides QWidget::setVisible(bool visible)
*/
Expand Down
9 changes: 6 additions & 3 deletions src/widgets/dtitlebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#ifndef DTITLEBAR_H
#define DTITLEBAR_H

#include <QWidget>
#include <QFrame>
#include <QMenu>

#include <dobject.h>
Expand All @@ -27,7 +27,7 @@
DWIDGET_BEGIN_NAMESPACE

class DTitlebarPrivate;
class LIBDTKWIDGETSHARED_EXPORT DTitlebar : public QWidget, public DTK_CORE_NAMESPACE::DObject
class LIBDTKWIDGETSHARED_EXPORT DTitlebar : public QFrame, public DTK_CORE_NAMESPACE::DObject
{
Q_OBJECT
public:
Expand All @@ -46,6 +46,9 @@ class LIBDTKWIDGETSHARED_EXPORT DTitlebar : public QWidget, public DTK_CORE_NAME
int buttonAreaWidth() const;
bool separatorVisible() const;

bool autoHideOnFullscreen() const;
void setAutoHideOnFullscreen(bool autohide);

void setVisible(bool visible) Q_DECL_OVERRIDE;
void setEmbedMode(bool embed);

Expand All @@ -70,7 +73,7 @@ public Q_SLOTS:
void setSeparatorVisible(bool visible);
void setTitle(const QString &title);
void setIcon(const QIcon &icon);
void D_DECL_DEPRECATED setIcon(const QPixmap &icon);
D_DECL_DEPRECATED void setIcon(const QPixmap &icon);
D_DECL_DEPRECATED void setWindowState(Qt::WindowState windowState);
/// Maximized/Minumized
void toggleWindowState();
Expand Down
12 changes: 12 additions & 0 deletions src/widgets/themes/dark/DTitlebar.theme
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
Dtk--Widget--DTitlebar {
background: qlineargradient(x1:0 y1:0, x2:0 y2:1,
stop:0 rgba(0,0,0,85%), stop:1 rgba(0,0,0,85%));
}

Dtk--Widget--DWindowCloseButton {
qproperty-normalPic: url(:/images/dark/images/window/close_normal.svg);
qproperty-hoverPic: url(:/images/dark/images/window/close_hover.svg);
qproperty-pressPic: url(:/images/dark/images/window/close_press.svg);
padding-right: 6px;
}

#DTitlebarDWindowQuitFullscreenButton {
qproperty-normalPic: url(:/images/dark/images/window/quit_fullscreen_normal.svg);
qproperty-hoverPic: url(:/images/dark/images/window/quit_fullscreen_hover.svg);
qproperty-pressPic: url(:/images/dark/images/window/quit_fullscreen_press.svg);
padding-right: 6px;
}
16 changes: 16 additions & 0 deletions src/widgets/themes/dark/images/window/quit_fullscreen_hover.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/widgets/themes/dark/images/window/quit_fullscreen_normal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions src/widgets/themes/dark/images/window/quit_fullscreen_press.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/widgets/themes/dui_theme_dark.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,8 @@
<file>dark/images/window/unmaximize_hover.svg</file>
<file>dark/images/window/unmaximize_normal.svg</file>
<file>dark/images/window/unmaximize_press.svg</file>
<file>dark/images/window/quit_fullscreen_hover.svg</file>
<file>dark/images/window/quit_fullscreen_normal.svg</file>
<file>dark/images/window/quit_fullscreen_press.svg</file>
</qresource>
</RCC>
3 changes: 3 additions & 0 deletions src/widgets/themes/dui_theme_light.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,8 @@
<file>light/images/loadfile_hover.svg</file>
<file>light/images/loadfile_normal.svg</file>
<file>light/images/loadfile_press.svg</file>
<file>light/images/window/quit_fullscreen_hover.svg</file>
<file>light/images/window/quit_fullscreen_normal.svg</file>
<file>light/images/window/quit_fullscreen_press.svg</file>
</qresource>
</RCC>
12 changes: 12 additions & 0 deletions src/widgets/themes/light/DTitlebar.theme
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
Dtk--Widget--DTitlebar {
background: qlineargradient(x1:1, y1:0, x2:1, y2:1,
stop:0 #ffffff, stop:1 #f8f8f8);
}

Dtk--Widget--DWindowCloseButton {
qproperty-normalPic: url(:/images/light/images/window/close_normal.svg);
qproperty-hoverPic: url(:/images/light/images/window/close_hover.svg);
qproperty-pressPic: url(:/images/light/images/window/close_press.svg);
padding-right: 6px;
}

#DTitlebarDWindowQuitFullscreenButton {
qproperty-normalPic: url(:/images/light/images/window/quit_fullscreen_normal.svg);
qproperty-hoverPic: url(:/images/light/images/window/quit_fullscreen_hover.svg);
qproperty-pressPic: url(:/images/light/images/window/quit_fullscreen_press.svg);
padding-right: 6px;
}
Loading

0 comments on commit 3e2e292

Please sign in to comment.