diff --git a/gui/ConsoleWindow.cpp b/gui/ConsoleWindow.cpp index e7ac6008..84a141ce 100644 --- a/gui/ConsoleWindow.cpp +++ b/gui/ConsoleWindow.cpp @@ -19,12 +19,14 @@ #include #include +#include #include #include #include #include "logic/net/PasteUpload.h" +#include "logic/icons/IconList.h" ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent) : QMainWindow(parent), ui(new Ui::ConsoleWindow), proc(mcproc) @@ -35,14 +37,28 @@ ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent) SLOT(write(QString, MessageLevel::Enum))); connect(mcproc, SIGNAL(ended(BaseInstance *, int, QProcess::ExitStatus)), this, SLOT(onEnded(BaseInstance *, int, QProcess::ExitStatus))); - connect(mcproc, SIGNAL(prelaunch_failed(BaseInstance*,int,QProcess::ExitStatus)), this, + connect(mcproc, SIGNAL(prelaunch_failed(BaseInstance *, int, QProcess::ExitStatus)), this, SLOT(onEnded(BaseInstance *, int, QProcess::ExitStatus))); - connect(mcproc, SIGNAL(launch_failed(BaseInstance*)), this, - SLOT(onLaunchFailed(BaseInstance*))); + connect(mcproc, SIGNAL(launch_failed(BaseInstance *)), this, + SLOT(onLaunchFailed(BaseInstance *))); - restoreState(QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowState").toByteArray())); - restoreGeometry(QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowGeometry").toByteArray())); + restoreState( + QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowState").toByteArray())); + restoreGeometry( + QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowGeometry").toByteArray())); + QString iconKey = proc->instance()->iconKey(); + QString name = proc->instance()->name(); + auto icon = MMC->icons()->getIcon(iconKey); + setWindowIcon(icon); + m_trayIcon = new QSystemTrayIcon(icon, this); + QString consoleTitle = tr("Console window for ") + name; + m_trayIcon->setToolTip(consoleTitle); + setWindowTitle(consoleTitle); + + connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), + SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); + m_trayIcon->show(); if (mcproc->instance()->settings().get("ShowConsole").toBool()) { show(); @@ -55,13 +71,26 @@ ConsoleWindow::~ConsoleWindow() delete ui; } +void ConsoleWindow::iconActivated(QSystemTrayIcon::ActivationReason reason) +{ + switch (reason) + { + case QSystemTrayIcon::Trigger: + { + toggleConsole(); + } + default: + return; + } +} + void ConsoleWindow::writeColor(QString text, const char *color) { // append a paragraph QString newtext; newtext += "text->verticalScrollBar(); int max_bar = bar->maximum(); int val_bar = bar->value(); - if(m_scroll_active) + if(isVisible()) { - m_scroll_active = (max_bar - val_bar) <= 1; + if (m_scroll_active) + { + m_scroll_active = (max_bar - val_bar) <= 1; + } + else + { + m_scroll_active = val_bar == max_bar; + } } - else - { - m_scroll_active = val_bar == max_bar; - } - if (data.endsWith('\n')) data = data.left(data.length() - 1); QStringList paragraphs = data.split('\n'); @@ -113,11 +144,14 @@ void ConsoleWindow::write(QString data, MessageLevel::Enum mode) else while (iter.hasNext()) writeColor(iter.next()); - if(m_scroll_active) + if(isVisible()) { - bar->setValue(bar->maximum()); + if (m_scroll_active) + { + bar->setValue(bar->maximum()); + } + m_last_scroll_value = bar->value(); } - m_last_scroll_value = bar->value(); } void ConsoleWindow::clear() @@ -133,22 +167,45 @@ void ConsoleWindow::on_closeButton_clicked() void ConsoleWindow::setMayClose(bool mayclose) { m_mayclose = mayclose; - if (mayclose) - ui->closeButton->setEnabled(true); +} + +void ConsoleWindow::toggleConsole() +{ + QScrollBar *bar = ui->text->verticalScrollBar(); + if (isVisible()) + { + int max_bar = bar->maximum(); + int val_bar = m_last_scroll_value = bar->value(); + m_scroll_active = (max_bar - val_bar) <= 1; + hide(); + } else - ui->closeButton->setEnabled(false); + { + show(); + if (m_scroll_active) + { + bar->setValue(bar->maximum()); + } + else + { + bar->setValue(m_last_scroll_value); + } + } } void ConsoleWindow::closeEvent(QCloseEvent *event) { if (!m_mayclose) - event->ignore(); + { + toggleConsole(); + } else { MMC->settings()->set("ConsoleWindowState", saveState().toBase64()); MMC->settings()->set("ConsoleWindowGeometry", saveGeometry().toBase64()); emit isClosing(); + m_trayIcon->hide(); QMainWindow::closeEvent(event); } } @@ -169,19 +226,26 @@ void ConsoleWindow::on_btnKillMinecraft_clicked() void ConsoleWindow::onEnded(BaseInstance *instance, int code, QProcess::ExitStatus status) { + bool peacefulExit = code == 0 && status != QProcess::CrashExit; ui->btnKillMinecraft->setEnabled(false); setMayClose(true); if (instance->settings().get("AutoCloseConsole").toBool()) { - if (code == 0 && status != QProcess::CrashExit) + if (peacefulExit) { this->close(); return; } } - if(!isVisible()) + /* + if(!peacefulExit) + { + m_trayIcon->showMessage(tr("Oh no!"), tr("Minecraft crashed!"), QSystemTrayIcon::Critical); + } + */ + if (!isVisible()) show(); } @@ -191,7 +255,7 @@ void ConsoleWindow::onLaunchFailed(BaseInstance *instance) setMayClose(true); - if(!isVisible()) + if (!isVisible()) show(); } @@ -199,10 +263,11 @@ void ConsoleWindow::on_btnPaste_clicked() { auto text = ui->text->toPlainText(); ProgressDialog dialog(this); - PasteUpload* paste=new PasteUpload(this, text); + PasteUpload *paste = new PasteUpload(this, text); dialog.exec(paste); - if(!paste->successful()) + if (!paste->successful()) { - CustomMessageBox::selectable(this, "Upload failed", paste->failReason(), QMessageBox::Critical)->exec(); + CustomMessageBox::selectable(this, "Upload failed", paste->failReason(), + QMessageBox::Critical)->exec(); } } diff --git a/gui/ConsoleWindow.h b/gui/ConsoleWindow.h index 731c616c..9291320e 100644 --- a/gui/ConsoleWindow.h +++ b/gui/ConsoleWindow.h @@ -16,6 +16,7 @@ #pragma once #include +#include #include "logic/MinecraftProcess.h" namespace Ui @@ -77,7 +78,8 @@ slots: // failures) void on_btnPaste_clicked(); - + void iconActivated(QSystemTrayIcon::ActivationReason); + void toggleConsole(); protected: void closeEvent(QCloseEvent *); @@ -87,4 +89,6 @@ private: bool m_mayclose = true; int m_last_scroll_value = 0; bool m_scroll_active = true; + QSystemTrayIcon *m_trayIcon = nullptr; + int m_saved_offset = 0; };