Extract TitleControls from TitleWidget.
This commit is contained in:
parent
6735ee93dc
commit
b534567e11
3 changed files with 146 additions and 74 deletions
|
|
@ -538,7 +538,7 @@ notificationBg: windowBg; // custom notification window background
|
|||
// calls
|
||||
callBg: #26282cf2; // old phone call popup background
|
||||
callBgOpaque: #1b1f23 | callBg; // phone call popup background
|
||||
callBgButton: #1b1f2356 | callBg; // phone call window control buttons bg
|
||||
callBgButton: #1b1f237f | callBg; // phone call window control buttons bg
|
||||
callNameFg: #ffffff; // phone call popup name text
|
||||
callFingerprintBg: #00000066; // phone call popup emoji fingerprint background
|
||||
callStatusFg: #aaabac; // phone call popup status text
|
||||
|
|
|
|||
|
|
@ -19,39 +19,59 @@
|
|||
namespace Ui {
|
||||
namespace Platform {
|
||||
|
||||
TitleWidget::TitleWidget(not_null<RpWidget*> parent)
|
||||
: RpWidget(parent)
|
||||
, _st(&st::defaultWindowTitle)
|
||||
, _minimize(this, _st->minimize)
|
||||
, _maximizeRestore(this, _st->maximize)
|
||||
, _close(this, _st->close)
|
||||
, _shadow(this, st::titleShadow)
|
||||
, _maximizedState(parent->windowState() & Qt::WindowMaximized)
|
||||
TitleControls::TitleControls(
|
||||
not_null<RpWidget*> parent,
|
||||
Fn<void(bool maximized)> maximize)
|
||||
: _st(&st::defaultWindowTitle)
|
||||
, _minimize(parent, _st->minimize)
|
||||
, _maximizeRestore(parent, _st->maximize)
|
||||
, _close(parent, _st->close)
|
||||
, _maximizedState(parent->windowState()
|
||||
& (Qt::WindowMaximized | Qt::WindowFullScreen))
|
||||
, _activeState(parent->isActiveWindow()) {
|
||||
init();
|
||||
}
|
||||
init(std::move(maximize));
|
||||
|
||||
void TitleWidget::setText(const QString &text) {
|
||||
window()->setWindowTitle(text);
|
||||
}
|
||||
|
||||
void TitleWidget::setStyle(const style::WindowTitle &st) {
|
||||
_st = &st;
|
||||
setGeometry(0, 0, window()->width(), _st->height);
|
||||
_close->paintRequest(
|
||||
) | rpl::start_with_next([=] {
|
||||
const auto active = window()->isActiveWindow();
|
||||
if (_activeState != active) {
|
||||
_activeState = active;
|
||||
updateButtonsState();
|
||||
update();
|
||||
}
|
||||
}, _close->lifetime());
|
||||
}
|
||||
|
||||
not_null<RpWidget*> TitleWidget::window() const {
|
||||
return static_cast<RpWidget*>(parentWidget());
|
||||
void TitleControls::setStyle(const style::WindowTitle &st) {
|
||||
_st = &st;
|
||||
updateButtonsState();
|
||||
}
|
||||
|
||||
void TitleWidget::setResizeEnabled(bool enabled) {
|
||||
_resizeEnabled = enabled;
|
||||
updateControlsVisibility();
|
||||
not_null<const style::WindowTitle*> TitleControls::st() const {
|
||||
return _st;
|
||||
}
|
||||
|
||||
void TitleWidget::init() {
|
||||
QRect TitleControls::geometry() const {
|
||||
auto result = QRect();
|
||||
const auto add = [&](auto &&control) {
|
||||
if (!control->isHidden()) {
|
||||
result = result.united(control->geometry());
|
||||
}
|
||||
};
|
||||
add(_minimize);
|
||||
add(_maximizeRestore);
|
||||
add(_close);
|
||||
return result;
|
||||
}
|
||||
|
||||
not_null<RpWidget*> TitleControls::parent() const {
|
||||
return static_cast<RpWidget*>(_close->parentWidget());
|
||||
}
|
||||
|
||||
not_null<QWidget*> TitleControls::window() const {
|
||||
return _close->window();
|
||||
}
|
||||
|
||||
void TitleControls::init(Fn<void(bool maximized)> maximize) {
|
||||
_minimize->setClickedCallback([=] {
|
||||
window()->setWindowState(
|
||||
window()->windowState() | Qt::WindowMinimized);
|
||||
|
|
@ -59,9 +79,13 @@ void TitleWidget::init() {
|
|||
});
|
||||
_minimize->setPointerCursor(false);
|
||||
_maximizeRestore->setClickedCallback([=] {
|
||||
if (maximize) {
|
||||
maximize(!_maximizedState);
|
||||
} else {
|
||||
window()->setWindowState(_maximizedState
|
||||
? Qt::WindowNoState
|
||||
: Qt::WindowMaximized);
|
||||
}
|
||||
_maximizeRestore->clearState();
|
||||
});
|
||||
_maximizeRestore->setPointerCursor(false);
|
||||
|
|
@ -71,32 +95,32 @@ void TitleWidget::init() {
|
|||
});
|
||||
_close->setPointerCursor(false);
|
||||
|
||||
setAttribute(Qt::WA_OpaquePaintEvent);
|
||||
|
||||
window()->widthValue(
|
||||
parent()->widthValue(
|
||||
) | rpl::start_with_next([=](int width) {
|
||||
setGeometry(0, 0, width, _st->height);
|
||||
}, lifetime());
|
||||
updateControlsPosition();
|
||||
}, _close->lifetime());
|
||||
|
||||
window()->createWinId();
|
||||
connect(
|
||||
QObject::connect(
|
||||
window()->windowHandle(),
|
||||
&QWindow::windowStateChanged,
|
||||
[=](Qt::WindowState state) { handleWindowStateChanged(state); });
|
||||
_activeState = isActiveWindow();
|
||||
_activeState = parent()->isActiveWindow();
|
||||
updateButtonsState();
|
||||
}
|
||||
|
||||
void TitleWidget::paintEvent(QPaintEvent *e) {
|
||||
const auto active = isActiveWindow();
|
||||
if (_activeState != active) {
|
||||
_activeState = active;
|
||||
updateButtonsState();
|
||||
}
|
||||
QPainter(this).fillRect(e->rect(), active ? _st->bgActive : _st->bg);
|
||||
void TitleControls::setResizeEnabled(bool enabled) {
|
||||
_resizeEnabled = enabled;
|
||||
updateControlsVisibility();
|
||||
}
|
||||
|
||||
void TitleWidget::updateControlsPosition() {
|
||||
void TitleControls::raise() {
|
||||
_minimize->raise();
|
||||
_maximizeRestore->raise();
|
||||
_close->raise();
|
||||
}
|
||||
|
||||
void TitleControls::updateControlsPosition() {
|
||||
auto right = 0;
|
||||
_close->moveToRight(right, 0); right += _close->width();
|
||||
_maximizeRestore->moveToRight(right, 0);
|
||||
|
|
@ -106,28 +130,25 @@ void TitleWidget::updateControlsPosition() {
|
|||
_minimize->moveToRight(right, 0);
|
||||
}
|
||||
|
||||
void TitleWidget::resizeEvent(QResizeEvent *e) {
|
||||
updateControlsPosition();
|
||||
_shadow->setGeometry(0, height() - st::lineWidth, width(), st::lineWidth);
|
||||
}
|
||||
|
||||
void TitleWidget::updateControlsVisibility() {
|
||||
void TitleControls::updateControlsVisibility() {
|
||||
_maximizeRestore->setVisible(_resizeEnabled);
|
||||
updateControlsPosition();
|
||||
update();
|
||||
}
|
||||
|
||||
void TitleWidget::handleWindowStateChanged(Qt::WindowState state) {
|
||||
if (state == Qt::WindowMinimized) return;
|
||||
void TitleControls::handleWindowStateChanged(Qt::WindowState state) {
|
||||
if (state == Qt::WindowMinimized) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto maximized = (state == Qt::WindowMaximized);
|
||||
auto maximized = (state == Qt::WindowMaximized)
|
||||
|| (state == Qt::WindowFullScreen);
|
||||
if (_maximizedState != maximized) {
|
||||
_maximizedState = maximized;
|
||||
updateButtonsState();
|
||||
}
|
||||
}
|
||||
|
||||
void TitleWidget::updateButtonsState() {
|
||||
void TitleControls::updateButtonsState() {
|
||||
const auto minimize = _activeState
|
||||
? &_st->minimizeIconActive
|
||||
: &_st->minimize.icon;
|
||||
|
|
@ -161,12 +182,45 @@ void TitleWidget::updateButtonsState() {
|
|||
_close->setIconOverride(close, closeOver);
|
||||
}
|
||||
|
||||
TitleWidget::TitleWidget(not_null<RpWidget*> parent)
|
||||
: RpWidget(parent)
|
||||
, _controls(this)
|
||||
, _shadow(this, st::titleShadow) {
|
||||
setAttribute(Qt::WA_OpaquePaintEvent);
|
||||
|
||||
parent->widthValue(
|
||||
) | rpl::start_with_next([=](int width) {
|
||||
setGeometry(0, 0, width, _controls.st()->height);
|
||||
}, lifetime());
|
||||
}
|
||||
|
||||
void TitleWidget::setText(const QString &text) {
|
||||
window()->setWindowTitle(text);
|
||||
}
|
||||
|
||||
void TitleWidget::setStyle(const style::WindowTitle &st) {
|
||||
_controls.setStyle(st);
|
||||
setGeometry(0, 0, window()->width(), _controls.st()->height);
|
||||
update();
|
||||
}
|
||||
|
||||
void TitleWidget::setResizeEnabled(bool enabled) {
|
||||
_controls.setResizeEnabled(enabled);
|
||||
}
|
||||
|
||||
void TitleWidget::paintEvent(QPaintEvent *e) {
|
||||
const auto active = window()->isActiveWindow();
|
||||
QPainter(this).fillRect(
|
||||
e->rect(),
|
||||
active ? _controls.st()->bgActive : _controls.st()->bg);
|
||||
}
|
||||
|
||||
void TitleWidget::resizeEvent(QResizeEvent *e) {
|
||||
_shadow->setGeometry(0, height() - st::lineWidth, width(), st::lineWidth);
|
||||
}
|
||||
|
||||
HitTestResult TitleWidget::hitTest(QPoint point) const {
|
||||
if (false
|
||||
|| (_minimize->geometry().contains(point))
|
||||
|| (_maximizeRestore->geometry().contains(point))
|
||||
|| (_close->geometry().contains(point))
|
||||
) {
|
||||
if (_controls.geometry().contains(point)) {
|
||||
return HitTestResult::SysButton;
|
||||
} else if (rect().contains(point)) {
|
||||
return HitTestResult::Caption;
|
||||
|
|
|
|||
|
|
@ -38,6 +38,40 @@ enum class HitTestResult {
|
|||
TopLeft,
|
||||
};
|
||||
|
||||
class TitleControls final {
|
||||
public:
|
||||
TitleControls(
|
||||
not_null<RpWidget*> parent,
|
||||
Fn<void(bool maximized)> maximize = nullptr);
|
||||
|
||||
void setStyle(const style::WindowTitle &st);
|
||||
[[nodiscard]] not_null<const style::WindowTitle*> st() const;
|
||||
[[nodiscard]] QRect geometry() const;
|
||||
void setResizeEnabled(bool enabled);
|
||||
void raise();
|
||||
|
||||
private:
|
||||
[[nodiscard]] not_null<RpWidget*> parent() const;
|
||||
[[nodiscard]] not_null<QWidget*> window() const;
|
||||
|
||||
void init(Fn<void(bool maximized)> maximize);
|
||||
void updateControlsVisibility();
|
||||
void updateButtonsState();
|
||||
void updateControlsPosition();
|
||||
void handleWindowStateChanged(Qt::WindowState state = Qt::WindowNoState);
|
||||
|
||||
not_null<const style::WindowTitle*> _st;
|
||||
|
||||
object_ptr<Ui::IconButton> _minimize;
|
||||
object_ptr<Ui::IconButton> _maximizeRestore;
|
||||
object_ptr<Ui::IconButton> _close;
|
||||
|
||||
bool _maximizedState = false;
|
||||
bool _activeState = false;
|
||||
bool _resizeEnabled = true;
|
||||
|
||||
};
|
||||
|
||||
class TitleWidget : public RpWidget {
|
||||
public:
|
||||
explicit TitleWidget(not_null<RpWidget*> parent);
|
||||
|
|
@ -52,25 +86,9 @@ protected:
|
|||
void resizeEvent(QResizeEvent *e) override;
|
||||
|
||||
private:
|
||||
not_null<RpWidget*> window() const;
|
||||
|
||||
void init();
|
||||
void handleWindowStateChanged(Qt::WindowState state = Qt::WindowNoState);
|
||||
void updateControlsVisibility();
|
||||
void updateButtonsState();
|
||||
void updateControlsPosition();
|
||||
|
||||
not_null<const style::WindowTitle*> _st;
|
||||
|
||||
object_ptr<Ui::IconButton> _minimize;
|
||||
object_ptr<Ui::IconButton> _maximizeRestore;
|
||||
object_ptr<Ui::IconButton> _close;
|
||||
TitleControls _controls;
|
||||
object_ptr<Ui::PlainShadow> _shadow;
|
||||
|
||||
bool _maximizedState = false;
|
||||
bool _activeState = false;
|
||||
bool _resizeEnabled = true;
|
||||
|
||||
};
|
||||
|
||||
} // namespace Platform
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue