Implement console window tray icon, console window hiding, better scrolling
This commit is contained in:
parent
ddb961fad5
commit
fcb8612c10
@ -19,12 +19,14 @@
|
|||||||
|
|
||||||
#include <QScrollBar>
|
#include <QScrollBar>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
#include <QSystemTrayIcon>
|
||||||
|
|
||||||
#include <gui/Platform.h>
|
#include <gui/Platform.h>
|
||||||
#include <gui/dialogs/CustomMessageBox.h>
|
#include <gui/dialogs/CustomMessageBox.h>
|
||||||
#include <gui/dialogs/ProgressDialog.h>
|
#include <gui/dialogs/ProgressDialog.h>
|
||||||
|
|
||||||
#include "logic/net/PasteUpload.h"
|
#include "logic/net/PasteUpload.h"
|
||||||
|
#include "logic/icons/IconList.h"
|
||||||
|
|
||||||
ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent)
|
ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent)
|
||||||
: QMainWindow(parent), ui(new Ui::ConsoleWindow), proc(mcproc)
|
: QMainWindow(parent), ui(new Ui::ConsoleWindow), proc(mcproc)
|
||||||
@ -35,14 +37,28 @@ ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent)
|
|||||||
SLOT(write(QString, MessageLevel::Enum)));
|
SLOT(write(QString, MessageLevel::Enum)));
|
||||||
connect(mcproc, SIGNAL(ended(BaseInstance *, int, QProcess::ExitStatus)), this,
|
connect(mcproc, SIGNAL(ended(BaseInstance *, int, QProcess::ExitStatus)), this,
|
||||||
SLOT(onEnded(BaseInstance *, int, QProcess::ExitStatus)));
|
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)));
|
SLOT(onEnded(BaseInstance *, int, QProcess::ExitStatus)));
|
||||||
connect(mcproc, SIGNAL(launch_failed(BaseInstance*)), this,
|
connect(mcproc, SIGNAL(launch_failed(BaseInstance *)), this,
|
||||||
SLOT(onLaunchFailed(BaseInstance*)));
|
SLOT(onLaunchFailed(BaseInstance *)));
|
||||||
|
|
||||||
restoreState(QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowState").toByteArray()));
|
restoreState(
|
||||||
restoreGeometry(QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowGeometry").toByteArray()));
|
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())
|
if (mcproc->instance()->settings().get("ShowConsole").toBool())
|
||||||
{
|
{
|
||||||
show();
|
show();
|
||||||
@ -55,13 +71,26 @@ ConsoleWindow::~ConsoleWindow()
|
|||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ConsoleWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
|
||||||
|
{
|
||||||
|
switch (reason)
|
||||||
|
{
|
||||||
|
case QSystemTrayIcon::Trigger:
|
||||||
|
{
|
||||||
|
toggleConsole();
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ConsoleWindow::writeColor(QString text, const char *color)
|
void ConsoleWindow::writeColor(QString text, const char *color)
|
||||||
{
|
{
|
||||||
// append a paragraph
|
// append a paragraph
|
||||||
QString newtext;
|
QString newtext;
|
||||||
newtext += "<span style=\"";
|
newtext += "<span style=\"";
|
||||||
{
|
{
|
||||||
if(color)
|
if (color)
|
||||||
newtext += QString("color:") + color + ";";
|
newtext += QString("color:") + color + ";";
|
||||||
newtext += "font-family: monospace;";
|
newtext += "font-family: monospace;";
|
||||||
}
|
}
|
||||||
@ -76,7 +105,9 @@ void ConsoleWindow::write(QString data, MessageLevel::Enum mode)
|
|||||||
QScrollBar *bar = ui->text->verticalScrollBar();
|
QScrollBar *bar = ui->text->verticalScrollBar();
|
||||||
int max_bar = bar->maximum();
|
int max_bar = bar->maximum();
|
||||||
int val_bar = bar->value();
|
int val_bar = bar->value();
|
||||||
if(m_scroll_active)
|
if(isVisible())
|
||||||
|
{
|
||||||
|
if (m_scroll_active)
|
||||||
{
|
{
|
||||||
m_scroll_active = (max_bar - val_bar) <= 1;
|
m_scroll_active = (max_bar - val_bar) <= 1;
|
||||||
}
|
}
|
||||||
@ -84,7 +115,7 @@ void ConsoleWindow::write(QString data, MessageLevel::Enum mode)
|
|||||||
{
|
{
|
||||||
m_scroll_active = val_bar == max_bar;
|
m_scroll_active = val_bar == max_bar;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (data.endsWith('\n'))
|
if (data.endsWith('\n'))
|
||||||
data = data.left(data.length() - 1);
|
data = data.left(data.length() - 1);
|
||||||
QStringList paragraphs = data.split('\n');
|
QStringList paragraphs = data.split('\n');
|
||||||
@ -113,11 +144,14 @@ void ConsoleWindow::write(QString data, MessageLevel::Enum mode)
|
|||||||
else
|
else
|
||||||
while (iter.hasNext())
|
while (iter.hasNext())
|
||||||
writeColor(iter.next());
|
writeColor(iter.next());
|
||||||
if(m_scroll_active)
|
if(isVisible())
|
||||||
|
{
|
||||||
|
if (m_scroll_active)
|
||||||
{
|
{
|
||||||
bar->setValue(bar->maximum());
|
bar->setValue(bar->maximum());
|
||||||
}
|
}
|
||||||
m_last_scroll_value = bar->value();
|
m_last_scroll_value = bar->value();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConsoleWindow::clear()
|
void ConsoleWindow::clear()
|
||||||
@ -133,22 +167,45 @@ void ConsoleWindow::on_closeButton_clicked()
|
|||||||
void ConsoleWindow::setMayClose(bool mayclose)
|
void ConsoleWindow::setMayClose(bool mayclose)
|
||||||
{
|
{
|
||||||
m_mayclose = 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
|
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)
|
void ConsoleWindow::closeEvent(QCloseEvent *event)
|
||||||
{
|
{
|
||||||
if (!m_mayclose)
|
if (!m_mayclose)
|
||||||
event->ignore();
|
{
|
||||||
|
toggleConsole();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MMC->settings()->set("ConsoleWindowState", saveState().toBase64());
|
MMC->settings()->set("ConsoleWindowState", saveState().toBase64());
|
||||||
MMC->settings()->set("ConsoleWindowGeometry", saveGeometry().toBase64());
|
MMC->settings()->set("ConsoleWindowGeometry", saveGeometry().toBase64());
|
||||||
|
|
||||||
emit isClosing();
|
emit isClosing();
|
||||||
|
m_trayIcon->hide();
|
||||||
QMainWindow::closeEvent(event);
|
QMainWindow::closeEvent(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,19 +226,26 @@ void ConsoleWindow::on_btnKillMinecraft_clicked()
|
|||||||
|
|
||||||
void ConsoleWindow::onEnded(BaseInstance *instance, int code, QProcess::ExitStatus status)
|
void ConsoleWindow::onEnded(BaseInstance *instance, int code, QProcess::ExitStatus status)
|
||||||
{
|
{
|
||||||
|
bool peacefulExit = code == 0 && status != QProcess::CrashExit;
|
||||||
ui->btnKillMinecraft->setEnabled(false);
|
ui->btnKillMinecraft->setEnabled(false);
|
||||||
|
|
||||||
setMayClose(true);
|
setMayClose(true);
|
||||||
|
|
||||||
if (instance->settings().get("AutoCloseConsole").toBool())
|
if (instance->settings().get("AutoCloseConsole").toBool())
|
||||||
{
|
{
|
||||||
if (code == 0 && status != QProcess::CrashExit)
|
if (peacefulExit)
|
||||||
{
|
{
|
||||||
this->close();
|
this->close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!isVisible())
|
/*
|
||||||
|
if(!peacefulExit)
|
||||||
|
{
|
||||||
|
m_trayIcon->showMessage(tr("Oh no!"), tr("Minecraft crashed!"), QSystemTrayIcon::Critical);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
if (!isVisible())
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,7 +255,7 @@ void ConsoleWindow::onLaunchFailed(BaseInstance *instance)
|
|||||||
|
|
||||||
setMayClose(true);
|
setMayClose(true);
|
||||||
|
|
||||||
if(!isVisible())
|
if (!isVisible())
|
||||||
show();
|
show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,10 +263,11 @@ void ConsoleWindow::on_btnPaste_clicked()
|
|||||||
{
|
{
|
||||||
auto text = ui->text->toPlainText();
|
auto text = ui->text->toPlainText();
|
||||||
ProgressDialog dialog(this);
|
ProgressDialog dialog(this);
|
||||||
PasteUpload* paste=new PasteUpload(this, text);
|
PasteUpload *paste = new PasteUpload(this, text);
|
||||||
dialog.exec(paste);
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
#include <QSystemTrayIcon>
|
||||||
#include "logic/MinecraftProcess.h"
|
#include "logic/MinecraftProcess.h"
|
||||||
|
|
||||||
namespace Ui
|
namespace Ui
|
||||||
@ -77,7 +78,8 @@ slots:
|
|||||||
// failures)
|
// failures)
|
||||||
|
|
||||||
void on_btnPaste_clicked();
|
void on_btnPaste_clicked();
|
||||||
|
void iconActivated(QSystemTrayIcon::ActivationReason);
|
||||||
|
void toggleConsole();
|
||||||
protected:
|
protected:
|
||||||
void closeEvent(QCloseEvent *);
|
void closeEvent(QCloseEvent *);
|
||||||
|
|
||||||
@ -87,4 +89,6 @@ private:
|
|||||||
bool m_mayclose = true;
|
bool m_mayclose = true;
|
||||||
int m_last_scroll_value = 0;
|
int m_last_scroll_value = 0;
|
||||||
bool m_scroll_active = true;
|
bool m_scroll_active = true;
|
||||||
|
QSystemTrayIcon *m_trayIcon = nullptr;
|
||||||
|
int m_saved_offset = 0;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user