MainWindow: Simplify hiding to tray logic

This commit is contained in:
Jonas Kvinge 2024-09-27 19:44:02 +02:00
parent 1c833a28dc
commit a239374c4b
2 changed files with 24 additions and 44 deletions

View File

@ -354,11 +354,9 @@ MainWindow::MainWindow(Application *app, SharedPtr<SystemTrayIcon> tray_icon, OS
initialized_(false),
was_maximized_(true),
was_minimized_(false),
hidden_(false),
exit_(false),
exit_count_(0),
delete_files_(false),
ignore_close_(false) {
delete_files_(false) {
qLog(Debug) << "Starting";
@ -990,17 +988,9 @@ MainWindow::MainWindow(Application *app, SharedPtr<SystemTrayIcon> tray_icon, OS
was_minimized_ = settings_.value("minimized", false).toBool();
if (was_minimized_) setWindowState(windowState() | Qt::WindowMinimized);
if (!tray_icon_->IsSystemTrayAvailable() || !tray_icon_->isVisible()) {
hidden_ = false;
settings_.setValue("hidden", false);
if (!tray_icon_->IsSystemTrayAvailable() || !tray_icon_->isVisible() || !settings_.value("hidden", false).toBool()) {
show();
}
else {
hidden_ = settings_.value("hidden", false).toBool();
if (!hidden_) {
show();
}
}
break;
}
}
@ -1307,8 +1297,7 @@ void MainWindow::Exit() {
QObject::connect(&*app_->player()->engine(), &EngineBase::Finished, this, &MainWindow::DoExit);
if (app_->player()->GetState() == EngineBase::State::Playing) {
app_->player()->Stop();
ignore_close_ = true;
close();
hide();
if (tray_icon_->IsSystemTrayAvailable()) {
tray_icon_->setVisible(false);
}
@ -1539,7 +1528,7 @@ void MainWindow::SaveGeometry() {
settings_.setValue("maximized", isMaximized());
settings_.setValue("minimized", isMinimized());
settings_.setValue("hidden", hidden_);
settings_.setValue("hidden", isHidden());
settings_.setValue("geometry", saveGeometry());
settings_.setValue("splitter_state", ui_->splitter->saveState());
@ -1593,7 +1582,7 @@ void MainWindow::VolumeWheelEvent(const int delta) {
void MainWindow::ToggleShowHide() {
if (hidden_) {
if (isHidden()) {
SetHiddenInTray(false);
}
else if (isActiveWindow()) {
@ -1617,7 +1606,7 @@ void MainWindow::ToggleShowHide() {
}
void MainWindow::ToggleHide() {
if (!hidden_) SetHiddenInTray(true);
if (isVisible()) SetHiddenInTray(true);
}
void MainWindow::StopAfterCurrent() {
@ -1627,27 +1616,26 @@ void MainWindow::StopAfterCurrent() {
void MainWindow::showEvent(QShowEvent *e) {
hidden_ = false;
QMainWindow::showEvent(e);
}
void MainWindow::hideEvent(QHideEvent *e) {
// Some window managers don't remember maximized state between
// calls to hide() and show(), so we have to remember it ourself.
was_maximized_ = isMaximized();
was_minimized_ = isMinimized();
QMainWindow::hideEvent(e);
}
void MainWindow::closeEvent(QCloseEvent *e) {
if (ignore_close_) {
ignore_close_ = false;
QMainWindow::closeEvent(e);
return;
}
if (!exit_) {
if (!hidden_ && tray_icon_->IsSystemTrayAvailable() && tray_icon_->isVisible() && keep_running_) {
SetHiddenInTray(true);
}
else {
Exit();
}
if (!exit_ && (!tray_icon_->IsSystemTrayAvailable() || !tray_icon_->isVisible() || !keep_running_)) {
Exit();
}
QMainWindow::closeEvent(e);
@ -1656,17 +1644,10 @@ void MainWindow::closeEvent(QCloseEvent *e) {
void MainWindow::SetHiddenInTray(const bool hidden) {
hidden_ = hidden;
settings_.setValue("hidden", hidden_);
// Some window managers don't remember maximized state between calls to hide() and show(), so we have to remember it ourself.
if (hidden) {
was_maximized_ = isMaximized();
was_minimized_ = isMinimized();
ignore_close_ = true;
if (hidden && isVisible()) {
close();
}
else {
else if (!hidden && isHidden()) {
if (was_minimized_) {
showMinimized();
}
@ -2392,7 +2373,6 @@ void MainWindow::CommandlineOptionsReceived(const QByteArray &string_options) {
raise();
show();
activateWindow();
hidden_ = false;
return;
}
@ -2958,7 +2938,7 @@ void MainWindow::Raise() {
show();
activateWindow();
hidden_ = false;
}
#ifdef Q_OS_WIN

View File

@ -117,6 +117,7 @@ class MainWindow : public QMainWindow, public PlatformInterface {
protected:
void showEvent(QShowEvent *e) override;
void hideEvent(QHideEvent *e) override;
void closeEvent(QCloseEvent *e) override;
void keyPressEvent(QKeyEvent *e) override;
#ifdef Q_OS_WIN
@ -396,7 +397,6 @@ class MainWindow : public QMainWindow, public PlatformInterface {
bool initialized_;
bool was_maximized_;
bool was_minimized_;
bool hidden_;
Song song_;
Song song_playing_;