Fix maximize/restore button state in TitleWidget.

This commit is contained in:
John Preston 2021-03-20 15:16:50 +04:00
parent 0adf0383d8
commit f6a1a44efe
2 changed files with 26 additions and 18 deletions

View file

@ -122,6 +122,22 @@ void TitleControls::init(Fn<void(bool maximized)> maximize) {
updateControlsPosition(); updateControlsPosition();
}, _close->lifetime()); }, _close->lifetime());
subscribeToStateChanges();
_activeState = parent()->isActiveWindow();
updateButtonsState();
}
void TitleControls::subscribeToStateChanges() {
const auto subscribe = [=] {
QObject::connect(
window()->windowHandle(),
&QWindow::windowStateChanged,
[=](Qt::WindowState state) { handleWindowStateChanged(state); });
};
if (window()->windowHandle()) {
subscribe();
} else {
const auto winIdEventFilter = std::make_shared<QObject*>(nullptr); const auto winIdEventFilter = std::make_shared<QObject*>(nullptr);
*winIdEventFilter = base::install_event_filter( *winIdEventFilter = base::install_event_filter(
window(), window(),
@ -129,20 +145,11 @@ void TitleControls::init(Fn<void(bool maximized)> maximize) {
if (!*winIdEventFilter || e->type() != QEvent::WinIdChange) { if (!*winIdEventFilter || e->type() != QEvent::WinIdChange) {
return base::EventFilterResult::Continue; return base::EventFilterResult::Continue;
} }
subscribe();
QObject::connect(
window()->windowHandle(),
&QWindow::windowStateChanged,
[=](Qt::WindowState state) {
handleWindowStateChanged(state);
});
base::take(*winIdEventFilter)->deleteLater(); base::take(*winIdEventFilter)->deleteLater();
return base::EventFilterResult::Continue; return base::EventFilterResult::Continue;
}); });
}
_activeState = parent()->isActiveWindow();
updateButtonsState();
} }
void TitleControls::setResizeEnabled(bool enabled) { void TitleControls::setResizeEnabled(bool enabled) {

View file

@ -54,6 +54,7 @@ private:
[[nodiscard]] Ui::IconButton *controlWidget(Control control) const; [[nodiscard]] Ui::IconButton *controlWidget(Control control) const;
void init(Fn<void(bool maximized)> maximize); void init(Fn<void(bool maximized)> maximize);
void subscribeToStateChanges();
void updateButtonsState(); void updateButtonsState();
void updateControlsPosition(); void updateControlsPosition();
void updateControlsPositionBySide( void updateControlsPositionBySide(