diff --git a/channel.cpp b/channel.cpp index 859c9725c..838ad4f69 100644 --- a/channel.cpp +++ b/channel.cpp @@ -117,7 +117,7 @@ void Channel::addMessage(std::shared_ptr message) this->messageAppended(message); - WindowManager::repaintVisibleChatWidgets(this); + WindowManager::getInstance().repaintVisibleChatWidgets(this); } // private methods diff --git a/colorscheme.cpp b/colorscheme.cpp index 024436255..4800b98b1 100644 --- a/colorscheme.cpp +++ b/colorscheme.cpp @@ -21,7 +21,7 @@ void ColorScheme::init() [](const float &) { ColorScheme::getInstance().update(); }); ColorScheme::getInstance().updated.connect( - [] { WindowManager::repaintVisibleChatWidgets(); }); + [] { WindowManager::getInstance().repaintVisibleChatWidgets(); }); } } @@ -172,4 +172,4 @@ void ColorScheme::normalizeColor(QColor &color) // color.setHslF(color.hueF(), s, newL); } -} +} // namespace chatterino diff --git a/emotemanager.h b/emotemanager.h index be8a4a987..3097d3953 100644 --- a/emotemanager.h +++ b/emotemanager.h @@ -58,7 +58,7 @@ public: QObject::connect(&_gifUpdateTimer, &QTimer::timeout, [this] { _gifUpdateTimerSignal(); - WindowManager::repaintGifEmotes(); + WindowManager::getInstance().repaintGifEmotes(); }); } @@ -93,6 +93,6 @@ private: void loadFfzEmotes(); void loadBttvEmotes(); }; -} +} // namespace chatterino #endif // EMOTES_H diff --git a/main.cpp b/main.cpp index ba59cecf9..c7847221d 100644 --- a/main.cpp +++ b/main.cpp @@ -31,9 +31,9 @@ int main(int argc, char *argv[]) ColorScheme::getInstance().init(); - WindowManager::load(); + WindowManager::getInstance().load(); - MainWindow &w = WindowManager::getMainWindow(); + MainWindow &w = WindowManager::getInstance().getMainWindow(); w.show(); IrcManager::getInstance().connect(); @@ -42,7 +42,7 @@ int main(int argc, char *argv[]) SettingsManager::getInstance().save(); - WindowManager::save(); + WindowManager::getInstance().save(); return ret; } diff --git a/messages/lazyloadedimage.cpp b/messages/lazyloadedimage.cpp index 422e9d0d7..b929a549e 100644 --- a/messages/lazyloadedimage.cpp +++ b/messages/lazyloadedimage.cpp @@ -93,7 +93,7 @@ void LazyLoadedImage::loadImage() } EmoteManager::getInstance().incGeneration(); - WindowManager::layoutVisibleChatWidgets(); + WindowManager::getInstance().layoutVisibleChatWidgets(); reply->deleteLater(); manager->deleteLater(); @@ -115,5 +115,5 @@ void LazyLoadedImage::gifUpdateTimout() this->currentPixmap = this->allFrames[this->currentFrame].image; } -} -} +} // namespace messages +} // namespace chatterino diff --git a/widgets/settingsdialog.cpp b/widgets/settingsdialog.cpp index b495fbc8e..7ccb75ef6 100644 --- a/widgets/settingsdialog.cpp +++ b/widgets/settingsdialog.cpp @@ -152,7 +152,7 @@ void SettingsDialog::addTabs() QObject::connect(slider, &QSlider::valueChanged, this, [this, &settings](int value) { settings.themeHue.set(value / 1000.0); - WindowManager::updateAll(); + WindowManager::getInstance().updateAll(); }); group->setLayout(form); diff --git a/windowmanager.cpp b/windowmanager.cpp index 01c064df2..da8828143 100644 --- a/windowmanager.cpp +++ b/windowmanager.cpp @@ -7,6 +7,13 @@ #include namespace chatterino { +WindowManager WindowManager::instance; + +WindowManager::WindowManager() + : _windowMutex() + , _mainWindow(nullptr) +{ +} static const std::string &getSettingsPath() { @@ -15,38 +22,45 @@ static const std::string &getSettingsPath() return path; } -QMutex WindowManager::windowMutex; - -widgets::MainWindow *WindowManager::mainWindow(nullptr); - void WindowManager::layoutVisibleChatWidgets(Channel *channel) { - if (WindowManager::mainWindow != nullptr) { - WindowManager::mainWindow->layoutVisibleChatWidgets(channel); + if (_mainWindow != nullptr) { + _mainWindow->layoutVisibleChatWidgets(channel); } } void WindowManager::repaintVisibleChatWidgets(Channel *channel) { - if (WindowManager::mainWindow != nullptr) { - WindowManager::mainWindow->repaintVisibleChatWidgets(channel); + if (_mainWindow != nullptr) { + _mainWindow->repaintVisibleChatWidgets(channel); } } void WindowManager::repaintGifEmotes() { - if (WindowManager::mainWindow != nullptr) { - WindowManager::mainWindow->repaintGifEmotes(); + if (_mainWindow != nullptr) { + _mainWindow->repaintGifEmotes(); } } void WindowManager::updateAll() { - if (WindowManager::mainWindow != nullptr) { - WindowManager::mainWindow->update(); + if (_mainWindow != nullptr) { + _mainWindow->update(); } } +widgets::MainWindow &WindowManager::getMainWindow() +{ + std::lock_guard lock(_windowMutex); + + if (_mainWindow == nullptr) { + _mainWindow = new widgets::MainWindow(); + } + + return *_mainWindow; +} + void WindowManager::load() { const auto &settingsPath = getSettingsPath(); @@ -57,7 +71,7 @@ void WindowManager::load() } catch (const boost::property_tree::json_parser_error &ex) { qDebug() << "Error using property_tree::readJson: " << QString::fromStdString(ex.message()); - WindowManager::getMainWindow().loadDefaults(); + getMainWindow().loadDefaults(); return; } @@ -70,7 +84,7 @@ void WindowManager::load() const auto &type = v.second.get("type", "unknown"); if (type == "main") { - WindowManager::getMainWindow().load(v.second); + getMainWindow().load(v.second); } else { qDebug() << "Unhandled window type: " << type.c_str(); } @@ -80,8 +94,8 @@ void WindowManager::load() } // if the main window was not loaded properly, load defaults - if (!WindowManager::getMainWindow().isLoaded()) { - WindowManager::getMainWindow().loadDefaults(); + if (!getMainWindow().isLoaded()) { + getMainWindow().loadDefaults(); } // If there are no windows, create a default main window @@ -97,7 +111,7 @@ void WindowManager::save() { // save main window - auto child = WindowManager::getMainWindow().save(); + auto child = getMainWindow().save(); windows.push_back(std::make_pair("", child)); } diff --git a/windowmanager.h b/windowmanager.h index b0f82b556..9b4b15da3 100644 --- a/windowmanager.h +++ b/windowmanager.h @@ -3,40 +3,35 @@ #include "widgets/mainwindow.h" -#include +#include namespace chatterino { class WindowManager { public: - static void layoutVisibleChatWidgets(Channel *channel = NULL); - static void repaintVisibleChatWidgets(Channel *channel = NULL); - static void repaintGifEmotes(); - static void updateAll(); - - static widgets::MainWindow &getMainWindow() + static WindowManager &getInstance() { - windowMutex.lock(); - if (mainWindow == nullptr) { - mainWindow = new widgets::MainWindow(); - } - windowMutex.unlock(); - - return *mainWindow; + return instance; } - static void load(); - static void save(); + void layoutVisibleChatWidgets(Channel *channel = nullptr); + void repaintVisibleChatWidgets(Channel *channel = nullptr); + void repaintGifEmotes(); + void updateAll(); + + widgets::MainWindow &getMainWindow(); + + void load(); + void save(); private: - WindowManager() - { - } + static WindowManager instance; - static QMutex windowMutex; + WindowManager(); - static widgets::MainWindow *mainWindow; + std::mutex _windowMutex; + widgets::MainWindow *_mainWindow; }; } // namespace chatterino