Skip to content

Commit

Permalink
MainWindow: Simplify hiding to tray logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaski committed Sep 27, 2024
1 parent 1c833a2 commit a239374
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 44 deletions.
66 changes: 23 additions & 43 deletions src/core/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -1593,7 +1582,7 @@ void MainWindow::VolumeWheelEvent(const int delta) {

void MainWindow::ToggleShowHide() {

if (hidden_) {
if (isHidden()) {
SetHiddenInTray(false);
}
else if (isActiveWindow()) {
Expand All @@ -1617,7 +1606,7 @@ void MainWindow::ToggleShowHide() {
}

void MainWindow::ToggleHide() {
if (!hidden_) SetHiddenInTray(true);
if (isVisible()) SetHiddenInTray(true);
}

void MainWindow::StopAfterCurrent() {
Expand All @@ -1627,27 +1616,26 @@ void MainWindow::StopAfterCurrent() {

void MainWindow::showEvent(QShowEvent *e) {

hidden_ = false;

QMainWindow::showEvent(e);

}

void MainWindow::closeEvent(QCloseEvent *e) {
void MainWindow::hideEvent(QHideEvent *e) {

if (ignore_close_) {
ignore_close_ = false;
QMainWindow::closeEvent(e);
return;
}
// Some window managers don't remember maximized state between
// calls to hide() and show(), so we have to remember it ourself.

if (!exit_) {
if (!hidden_ && tray_icon_->IsSystemTrayAvailable() && tray_icon_->isVisible() && keep_running_) {
SetHiddenInTray(true);
}
else {
Exit();
}
was_maximized_ = isMaximized();
was_minimized_ = isMinimized();

QMainWindow::hideEvent(e);

}

void MainWindow::closeEvent(QCloseEvent *e) {

if (!exit_ && (!tray_icon_->IsSystemTrayAvailable() || !tray_icon_->isVisible() || !keep_running_)) {
Exit();
}

QMainWindow::closeEvent(e);
Expand All @@ -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();
}
Expand Down Expand Up @@ -2392,7 +2373,6 @@ void MainWindow::CommandlineOptionsReceived(const QByteArray &string_options) {
raise();
show();
activateWindow();
hidden_ = false;
return;
}

Expand Down Expand Up @@ -2958,7 +2938,7 @@ void MainWindow::Raise() {

show();
activateWindow();
hidden_ = false;

}

#ifdef Q_OS_WIN
Expand Down
2 changes: 1 addition & 1 deletion src/core/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_;
Expand Down

0 comments on commit a239374

Please sign in to comment.