mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Occasionally save window/split/tab layout
The window layout is only saved if something has been changed. When something relevant to the window layout is changed, a save is queued to run after 10 seconds. If within those 10 seconds, another thing is changed, that timer is reset and will run after 10 seconds again. Events that cause the save to be queued up: - Tab created - Tab removed - Tab moved - Tab name changed - Split created - Split removed - Split moved - Split channel changed - Split resized - Window moved - Window resized What currently does not trigger the save to be queued up: - Active tab changed
This commit is contained in:
parent
7879fef3a0
commit
a947bf74c8
8 changed files with 64 additions and 0 deletions
|
@ -23,6 +23,8 @@
|
|||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#define SETTINGS_FILENAME "/window-layout.json"
|
||||
|
||||
namespace chatterino {
|
||||
|
@ -83,6 +85,14 @@ WindowManager::WindowManager()
|
|||
this->wordFlagsListener_.cb = [this](auto) {
|
||||
this->updateWordTypeMask(); //
|
||||
};
|
||||
|
||||
this->saveTimer = new QTimer;
|
||||
|
||||
this->saveTimer->setSingleShot(true);
|
||||
|
||||
QObject::connect(this->saveTimer, &QTimer::timeout, [] {
|
||||
getApp()->windows->save(); //
|
||||
});
|
||||
}
|
||||
|
||||
MessageElementFlags WindowManager::getWordFlags()
|
||||
|
@ -357,6 +367,8 @@ void WindowManager::initialize(Settings &settings, Paths &paths)
|
|||
|
||||
void WindowManager::save()
|
||||
{
|
||||
log("[WindowManager] Saving");
|
||||
|
||||
assertInGuiThread();
|
||||
auto app = getApp();
|
||||
|
||||
|
@ -453,6 +465,13 @@ void WindowManager::sendAlert()
|
|||
QApplication::alert(this->getMainWindow().window(), flashDuration);
|
||||
}
|
||||
|
||||
void WindowManager::queueSave()
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
this->saveTimer->start(10s);
|
||||
}
|
||||
|
||||
void WindowManager::encodeNodeRecusively(SplitNode *node, QJsonObject &obj)
|
||||
{
|
||||
switch (node->getType()) {
|
||||
|
|
|
@ -77,6 +77,11 @@ public:
|
|||
// or not
|
||||
void sendAlert();
|
||||
|
||||
// Queue up a save in the next 10 seconds
|
||||
// If a save was already queued up, we reset the to happen in 10 seconds
|
||||
// again
|
||||
void queueSave();
|
||||
|
||||
private:
|
||||
void encodeNodeRecusively(SplitContainer::Node *node, QJsonObject &obj);
|
||||
|
||||
|
@ -91,6 +96,8 @@ private:
|
|||
|
||||
MessageElementFlags wordFlags_{};
|
||||
pajlada::Settings::SettingListener wordFlagsListener_;
|
||||
|
||||
QTimer *saveTimer;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
|
|
|
@ -420,11 +420,22 @@ void BaseWindow::moveTo(QWidget *parent, QPoint point, bool offset)
|
|||
|
||||
void BaseWindow::resizeEvent(QResizeEvent *)
|
||||
{
|
||||
// Queue up save because: Window resized
|
||||
getApp()->windows->queueSave();
|
||||
|
||||
this->moveIntoDesktopRect(this);
|
||||
|
||||
this->calcButtonsSizes();
|
||||
}
|
||||
|
||||
void BaseWindow::moveEvent(QMoveEvent *event)
|
||||
{
|
||||
// Queue up save because: Window position changed
|
||||
getApp()->windows->queueSave();
|
||||
|
||||
BaseWidget::moveEvent(event);
|
||||
}
|
||||
|
||||
void BaseWindow::closeEvent(QCloseEvent *)
|
||||
{
|
||||
this->closing.invoke();
|
||||
|
|
|
@ -64,6 +64,7 @@ protected:
|
|||
virtual void changeEvent(QEvent *) override;
|
||||
virtual void leaveEvent(QEvent *) override;
|
||||
virtual void resizeEvent(QResizeEvent *) override;
|
||||
virtual void moveEvent(QMoveEvent *) override;
|
||||
virtual void closeEvent(QCloseEvent *) override;
|
||||
|
||||
virtual void themeChangedEvent() override;
|
||||
|
|
|
@ -45,6 +45,9 @@ Notebook::Notebook(QWidget *parent)
|
|||
|
||||
NotebookTab *Notebook::addPage(QWidget *page, QString title, bool select)
|
||||
{
|
||||
// Queue up save because: Tab added
|
||||
getApp()->windows->queueSave();
|
||||
|
||||
auto *tab = new NotebookTab(this);
|
||||
tab->page = page;
|
||||
|
||||
|
@ -72,6 +75,9 @@ NotebookTab *Notebook::addPage(QWidget *page, QString title, bool select)
|
|||
|
||||
void Notebook::removePage(QWidget *page)
|
||||
{
|
||||
// Queue up save because: Tab removed
|
||||
getApp()->windows->queueSave();
|
||||
|
||||
for (int i = 0; i < this->items_.count(); i++) {
|
||||
if (this->items_[i].page == page) {
|
||||
if (this->items_.count() == 1) {
|
||||
|
@ -267,6 +273,9 @@ QWidget *Notebook::tabAt(QPoint point, int &index, int maxWidth)
|
|||
|
||||
void Notebook::rearrangePage(QWidget *page, int index)
|
||||
{
|
||||
// Queue up save because: Tab rearranged
|
||||
getApp()->windows->queueSave();
|
||||
|
||||
this->items_.move(this->indexOf(page), index);
|
||||
|
||||
this->performLayout(true);
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "singletons/Fonts.hpp"
|
||||
#include "singletons/Settings.hpp"
|
||||
#include "singletons/Theme.hpp"
|
||||
#include "singletons/WindowManager.hpp"
|
||||
#include "util/Clamp.hpp"
|
||||
#include "util/Helpers.hpp"
|
||||
#include "widgets/Notebook.hpp"
|
||||
|
@ -146,6 +147,9 @@ const QString &NotebookTab::getTitle() const
|
|||
|
||||
void NotebookTab::titleUpdated()
|
||||
{
|
||||
// Queue up save because: Tab title changed
|
||||
getApp()->windows->queueSave();
|
||||
|
||||
this->updateSize();
|
||||
this->update();
|
||||
}
|
||||
|
|
|
@ -224,6 +224,9 @@ void Split::setChannel(IndirectChannel newChannel)
|
|||
this->header_->updateRoomModes();
|
||||
|
||||
this->channelChanged.invoke();
|
||||
|
||||
// Queue up save because: Split channel changed
|
||||
getApp()->windows->queueSave();
|
||||
}
|
||||
|
||||
void Split::setModerationMode(bool value)
|
||||
|
@ -326,6 +329,9 @@ void Split::keyReleaseEvent(QKeyEvent *event)
|
|||
|
||||
void Split::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
// Queue up save because: Split resized
|
||||
getApp()->windows->queueSave();
|
||||
|
||||
BaseWidget::resizeEvent(event);
|
||||
|
||||
this->overlay_->setGeometry(this->rect());
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "widgets/splits/SplitContainer.hpp"
|
||||
|
||||
#include "Application.hpp"
|
||||
#include "common/Common.hpp"
|
||||
#include "debug/AssertInGuiThread.hpp"
|
||||
|
@ -142,6 +143,9 @@ void SplitContainer::insertSplit(Split *split, Direction direction,
|
|||
void SplitContainer::insertSplit(Split *split, Direction direction,
|
||||
Node *relativeTo)
|
||||
{
|
||||
// Queue up save because: Split added
|
||||
getApp()->windows->queueSave();
|
||||
|
||||
assertInGuiThread();
|
||||
|
||||
split->setContainer(this);
|
||||
|
@ -236,6 +240,9 @@ SplitContainer::Position SplitContainer::releaseSplit(Split *split)
|
|||
|
||||
SplitContainer::Position SplitContainer::deleteSplit(Split *split)
|
||||
{
|
||||
// Queue up save because: Split removed
|
||||
getApp()->windows->queueSave();
|
||||
|
||||
assertInGuiThread();
|
||||
assert(split != nullptr);
|
||||
|
||||
|
|
Loading…
Reference in a new issue