diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index bbd805496..28859f3d3 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -6,23 +6,22 @@ #include "widgets/settingsdialog.hpp" #include "widgets/split.hpp" -#include -#include #include #include #include -#include namespace chatterino { namespace widgets { -Window::Window(ChannelManager &_channelManager, ColorScheme &_colorScheme, bool _isMainWindow) +Window::Window(const QString &_windowName, ChannelManager &_channelManager, + ColorScheme &_colorScheme, bool _isMainWindow) : BaseWidget(_colorScheme, nullptr) + , windowName(_windowName) + , windowGeometry(this->windowName.toStdString()) , channelManager(_channelManager) , colorScheme(_colorScheme) , notebook(this->channelManager, this, _isMainWindow) , dpi(this->getDpiMultiplier()) -// , windowGeometry("/windows/0/geometry") { this->initAsWindow(); @@ -45,14 +44,7 @@ Window::Window(ChannelManager &_channelManager, ColorScheme &_colorScheme, bool this->refreshTheme(); - if (/*this->windowGeometry->isFilled()*/ false) { - // Load geometry from settings file - // this->setGeometry(this->windowGeometry.getValueRef()); - } else { - // Set default geometry - // Default position is in the middle of the current monitor or the primary monitor - this->resize(1280, 800); - } + this->loadGeometry(); // Initialize program-wide hotkeys { @@ -120,8 +112,12 @@ Notebook &Window::getNotebook() void Window::closeEvent(QCloseEvent *) { - // Save closing window position - // this->windowGeometry = this->geometry(); + const QRect &geom = this->geometry(); + + this->windowGeometry.x = geom.x(); + this->windowGeometry.y = geom.y(); + this->windowGeometry.width = geom.width(); + this->windowGeometry.height = geom.height(); this->closed(); } @@ -133,5 +129,19 @@ void Window::refreshTheme() this->setPalette(palette); } +void Window::loadGeometry() +{ + if (!this->windowGeometry.x.isDefaultValue() && !this->windowGeometry.y.isDefaultValue()) { + this->move(this->windowGeometry.x, this->windowGeometry.y); + } + + if (!this->windowGeometry.width.isDefaultValue() && + !this->windowGeometry.height.isDefaultValue()) { + this->resize(this->windowGeometry.width, this->windowGeometry.height); + } else { + this->resize(1280, 800); + } +} + } // namespace widgets } // namespace chatterino diff --git a/src/widgets/window.hpp b/src/widgets/window.hpp index a9c747e3e..4d5c7a76a 100644 --- a/src/widgets/window.hpp +++ b/src/widgets/window.hpp @@ -1,5 +1,6 @@ #pragma once +#include "util/helpers.hpp" #include "widgets/basewidget.hpp" #include "widgets/notebook.hpp" #include "widgets/titlebar.hpp" @@ -10,8 +11,7 @@ #include #include -#include -#include +#include namespace chatterino { @@ -21,12 +21,32 @@ class CompletionManager; namespace widgets { +struct WindowGeometry { + WindowGeometry(const std::string &key) + : x(fS("/windows/{}/geometry/x", key)) + , y(fS("/windows/{}/geometry/y", key)) + , width(fS("/windows/{}/geometry/width", key)) + , height(fS("/windows/{}/geometry/height", key)) + { + } + + pajlada::Settings::Setting x; + pajlada::Settings::Setting y; + pajlada::Settings::Setting width; + pajlada::Settings::Setting height; +}; + class Window : public BaseWidget { Q_OBJECT + QString windowName; + + WindowGeometry windowGeometry; + public: - explicit Window(ChannelManager &_channelManager, ColorScheme &_colorScheme, bool isMainWindow); + explicit Window(const QString &_windowName, ChannelManager &_channelManager, + ColorScheme &_colorScheme, bool isMainWindow); void repaintVisibleChatWidgets(Channel *channel = nullptr); @@ -48,6 +68,8 @@ private: virtual void refreshTheme() override; + void loadGeometry(); + ChannelManager &channelManager; ColorScheme &colorScheme; @@ -55,92 +77,6 @@ private: bool loaded = false; TitleBar titleBar; - /* - class QRectWrapper : public pajlada::Settings::ISettingData, public QRect - { - public: - QRectWrapper() - : QRect(-1, -1, -1, -1) - { - } - - pajlada::Signals::Signal valueChanged; - - const QRectWrapper &getValueRef() const - { - return *this; - } - - virtual rapidjson::Value marshalInto(rapidjson::Document &d) override - { - using namespace pajlada::Settings; - - rapidjson::Value obj(rapidjson::kObjectType); - - auto _x = serializeToJSON::serialize(this->x(), d.GetAllocator()); - auto _y = serializeToJSON::serialize(this->y(), d.GetAllocator()); - auto _width = serializeToJSON::serialize(this->width(), d.GetAllocator()); - auto _height = serializeToJSON::serialize(this->height(), d.GetAllocator()); - - obj.AddMember("x", _x, d.GetAllocator()); - obj.AddMember("y", _y, d.GetAllocator()); - obj.AddMember("width", _width, d.GetAllocator()); - obj.AddMember("height", _height, d.GetAllocator()); - - return obj; - } - - virtual bool unmarshalFrom(rapidjson::Document &document) override - { - using namespace pajlada::Settings; - - auto vXp = this->getValueWithSuffix("/x", document); - auto vYp = this->getValueWithSuffix("/y", document); - auto vWidthp = this->getValueWithSuffix("/width", document); - auto vHeightp = this->getValueWithSuffix("/height", document); - if (vXp != nullptr) { - this->setX(deserializeJSON::deserialize(*vXp)); - this->filled = true; - } - if (vYp != nullptr) { - this->setY(deserializeJSON::deserialize(*vYp)); - this->filled = true; - } - if (vWidthp != nullptr) { - this->setWidth(deserializeJSON::deserialize(*vWidthp)); - this->filled = true; - } - if (vHeightp != nullptr) { - this->setHeight(deserializeJSON::deserialize(*vHeightp)); - this->filled = true; - } - - return true; - } - - virtual void registerDocument(rapidjson::Document &d) override - { - this->valueChanged.connect([this, &d](const auto &) { - this->marshalInto(d); // - }); - } - - QRectWrapper &operator=(const QRect &rhs) - { - static_cast(*this) = rhs; - - return *this; - } - - void setValue(const QRect &rhs) - { - static_cast(*this) = rhs; - } - }; - */ - - // pajlada::Settings::Setting windowGeometry; - friend class Notebook; }; diff --git a/src/windowmanager.cpp b/src/windowmanager.cpp index 80cd9b4dc..bb3719d1e 100644 --- a/src/windowmanager.cpp +++ b/src/windowmanager.cpp @@ -22,7 +22,7 @@ WindowManager::WindowManager(ChannelManager &_channelManager, ColorScheme &_colo void WindowManager::initMainWindow() { this->selectedWindow = this->mainWindow = - new widgets::Window(this->channelManager, this->colorScheme, true); + new widgets::Window("main", this->channelManager, this->colorScheme, true); } static const std::string &getSettingsPath() @@ -68,7 +68,7 @@ widgets::Window &WindowManager::getSelectedWindow() widgets::Window &WindowManager::createWindow() { - auto *window = new widgets::Window(this->channelManager, this->colorScheme, false); + auto *window = new widgets::Window("external", this->channelManager, this->colorScheme, false); window->loadDefaults();