tdesktop/Telegram/SourceFiles/platform/win/window_title_win.cpp

137 lines
3.5 KiB
C++
Raw Normal View History

/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "platform/win/window_title_win.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/shadow.h"
#include "styles/style_window.h"
2019-09-04 07:19:15 +00:00
#include <QtGui/QWindow>
namespace Platform {
2019-10-20 11:32:44 +00:00
TitleWidget::TitleWidget(QWidget *parent)
: Window::TitleWidget(parent)
, _st(st::defaultWindowTitle)
, _minimize(this, _st.minimize)
, _maximizeRestore(this, _st.maximize)
, _close(this, _st.close)
, _shadow(this, st::titleShadow)
, _maximizedState(parent->window()->windowState() & Qt::WindowMaximized) {
_minimize->setClickedCallback([=] {
window()->setWindowState(
window()->windowState() | Qt::WindowMinimized);
_minimize->clearState();
});
_minimize->setPointerCursor(false);
_maximizeRestore->setClickedCallback([=] {
window()->setWindowState(_maximizedState
? Qt::WindowNoState
: Qt::WindowMaximized);
_maximizeRestore->clearState();
});
_maximizeRestore->setPointerCursor(false);
_close->setClickedCallback([=] {
window()->close();
_close->clearState();
});
_close->setPointerCursor(false);
setAttribute(Qt::WA_OpaquePaintEvent);
2019-10-20 11:32:44 +00:00
resize(width(), _st.height);
}
void TitleWidget::init() {
connect(
window()->windowHandle(),
&QWindow::windowStateChanged,
this,
[=](Qt::WindowState state) { windowStateChanged(state); });
_maximizedState = (window()->windowState() & Qt::WindowMaximized);
_activeState = isActiveWindow();
updateButtonsState();
}
void TitleWidget::paintEvent(QPaintEvent *e) {
auto active = isActiveWindow();
if (_activeState != active) {
_activeState = active;
updateButtonsState();
}
2019-10-20 11:32:44 +00:00
Painter(this).fillRect(rect(), active ? _st.bgActive : _st.bg);
}
void TitleWidget::updateControlsPosition() {
auto right = 0;
_close->moveToRight(right, 0); right += _close->width();
_maximizeRestore->moveToRight(right, 0); right += _maximizeRestore->width();
_minimize->moveToRight(right, 0);
}
void TitleWidget::resizeEvent(QResizeEvent *e) {
updateControlsPosition();
_shadow->setGeometry(0, height() - st::lineWidth, width(), st::lineWidth);
}
void TitleWidget::windowStateChanged(Qt::WindowState state) {
if (state == Qt::WindowMinimized) {
return;
}
const auto maximized = (state == Qt::WindowMaximized);
if (_maximizedState != maximized) {
_maximizedState = maximized;
updateButtonsState();
}
}
void TitleWidget::updateButtonsState() {
2019-10-20 11:32:44 +00:00
_minimize->setIconOverride(_activeState
? &_st.minimizeIconActive
: nullptr,
_activeState
? &_st.minimizeIconActiveOver
: nullptr);
if (_maximizedState) {
2019-10-20 11:32:44 +00:00
_maximizeRestore->setIconOverride(
_activeState
? &_st.restoreIconActive : &_st.restoreIcon,
_activeState
? &_st.restoreIconActiveOver
: &_st.restoreIconOver);
} else {
2019-10-20 11:32:44 +00:00
_maximizeRestore->setIconOverride(_activeState
? &_st.maximizeIconActive
: nullptr,
_activeState
? &_st.maximizeIconActiveOver
: nullptr);
}
2019-10-20 11:32:44 +00:00
_close->setIconOverride(_activeState
? &_st.closeIconActive
: nullptr,
_activeState
? &_st.closeIconActiveOver
: nullptr);
}
Window::HitTestResult TitleWidget::hitTest(const QPoint &p) const {
if (false
|| (_minimize->geometry().contains(p))
|| (_maximizeRestore->geometry().contains(p))
|| (_close->geometry().contains(p))
) {
return Window::HitTestResult::SysButton;
} else if (rect().contains(p)) {
return Window::HitTestResult::Caption;
}
return Window::HitTestResult::None;
}
} // namespace Platform