refactored windowmanager

This commit is contained in:
fourtf 2017-04-13 19:25:33 +02:00
parent 11fed12be1
commit d96c4a5c91
8 changed files with 59 additions and 50 deletions

View file

@ -117,7 +117,7 @@ void Channel::addMessage(std::shared_ptr<Message> message)
this->messageAppended(message); this->messageAppended(message);
WindowManager::repaintVisibleChatWidgets(this); WindowManager::getInstance().repaintVisibleChatWidgets(this);
} }
// private methods // private methods

View file

@ -21,7 +21,7 @@ void ColorScheme::init()
[](const float &) { ColorScheme::getInstance().update(); }); [](const float &) { ColorScheme::getInstance().update(); });
ColorScheme::getInstance().updated.connect( ColorScheme::getInstance().updated.connect(
[] { WindowManager::repaintVisibleChatWidgets(); }); [] { WindowManager::getInstance().repaintVisibleChatWidgets(); });
} }
} }
@ -172,4 +172,4 @@ void ColorScheme::normalizeColor(QColor &color)
// color.setHslF(color.hueF(), s, newL); // color.setHslF(color.hueF(), s, newL);
} }
} } // namespace chatterino

View file

@ -58,7 +58,7 @@ public:
QObject::connect(&_gifUpdateTimer, &QTimer::timeout, [this] { QObject::connect(&_gifUpdateTimer, &QTimer::timeout, [this] {
_gifUpdateTimerSignal(); _gifUpdateTimerSignal();
WindowManager::repaintGifEmotes(); WindowManager::getInstance().repaintGifEmotes();
}); });
} }
@ -93,6 +93,6 @@ private:
void loadFfzEmotes(); void loadFfzEmotes();
void loadBttvEmotes(); void loadBttvEmotes();
}; };
} } // namespace chatterino
#endif // EMOTES_H #endif // EMOTES_H

View file

@ -31,9 +31,9 @@ int main(int argc, char *argv[])
ColorScheme::getInstance().init(); ColorScheme::getInstance().init();
WindowManager::load(); WindowManager::getInstance().load();
MainWindow &w = WindowManager::getMainWindow(); MainWindow &w = WindowManager::getInstance().getMainWindow();
w.show(); w.show();
IrcManager::getInstance().connect(); IrcManager::getInstance().connect();
@ -42,7 +42,7 @@ int main(int argc, char *argv[])
SettingsManager::getInstance().save(); SettingsManager::getInstance().save();
WindowManager::save(); WindowManager::getInstance().save();
return ret; return ret;
} }

View file

@ -93,7 +93,7 @@ void LazyLoadedImage::loadImage()
} }
EmoteManager::getInstance().incGeneration(); EmoteManager::getInstance().incGeneration();
WindowManager::layoutVisibleChatWidgets(); WindowManager::getInstance().layoutVisibleChatWidgets();
reply->deleteLater(); reply->deleteLater();
manager->deleteLater(); manager->deleteLater();
@ -115,5 +115,5 @@ void LazyLoadedImage::gifUpdateTimout()
this->currentPixmap = this->allFrames[this->currentFrame].image; this->currentPixmap = this->allFrames[this->currentFrame].image;
} }
} } // namespace messages
} } // namespace chatterino

View file

@ -152,7 +152,7 @@ void SettingsDialog::addTabs()
QObject::connect(slider, &QSlider::valueChanged, this, [this, &settings](int value) { QObject::connect(slider, &QSlider::valueChanged, this, [this, &settings](int value) {
settings.themeHue.set(value / 1000.0); settings.themeHue.set(value / 1000.0);
WindowManager::updateAll(); WindowManager::getInstance().updateAll();
}); });
group->setLayout(form); group->setLayout(form);

View file

@ -7,6 +7,13 @@
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
namespace chatterino { namespace chatterino {
WindowManager WindowManager::instance;
WindowManager::WindowManager()
: _windowMutex()
, _mainWindow(nullptr)
{
}
static const std::string &getSettingsPath() static const std::string &getSettingsPath()
{ {
@ -15,38 +22,45 @@ static const std::string &getSettingsPath()
return path; return path;
} }
QMutex WindowManager::windowMutex;
widgets::MainWindow *WindowManager::mainWindow(nullptr);
void WindowManager::layoutVisibleChatWidgets(Channel *channel) void WindowManager::layoutVisibleChatWidgets(Channel *channel)
{ {
if (WindowManager::mainWindow != nullptr) { if (_mainWindow != nullptr) {
WindowManager::mainWindow->layoutVisibleChatWidgets(channel); _mainWindow->layoutVisibleChatWidgets(channel);
} }
} }
void WindowManager::repaintVisibleChatWidgets(Channel *channel) void WindowManager::repaintVisibleChatWidgets(Channel *channel)
{ {
if (WindowManager::mainWindow != nullptr) { if (_mainWindow != nullptr) {
WindowManager::mainWindow->repaintVisibleChatWidgets(channel); _mainWindow->repaintVisibleChatWidgets(channel);
} }
} }
void WindowManager::repaintGifEmotes() void WindowManager::repaintGifEmotes()
{ {
if (WindowManager::mainWindow != nullptr) { if (_mainWindow != nullptr) {
WindowManager::mainWindow->repaintGifEmotes(); _mainWindow->repaintGifEmotes();
} }
} }
void WindowManager::updateAll() void WindowManager::updateAll()
{ {
if (WindowManager::mainWindow != nullptr) { if (_mainWindow != nullptr) {
WindowManager::mainWindow->update(); _mainWindow->update();
} }
} }
widgets::MainWindow &WindowManager::getMainWindow()
{
std::lock_guard<std::mutex> lock(_windowMutex);
if (_mainWindow == nullptr) {
_mainWindow = new widgets::MainWindow();
}
return *_mainWindow;
}
void WindowManager::load() void WindowManager::load()
{ {
const auto &settingsPath = getSettingsPath(); const auto &settingsPath = getSettingsPath();
@ -57,7 +71,7 @@ void WindowManager::load()
} catch (const boost::property_tree::json_parser_error &ex) { } catch (const boost::property_tree::json_parser_error &ex) {
qDebug() << "Error using property_tree::readJson: " << QString::fromStdString(ex.message()); qDebug() << "Error using property_tree::readJson: " << QString::fromStdString(ex.message());
WindowManager::getMainWindow().loadDefaults(); getMainWindow().loadDefaults();
return; return;
} }
@ -70,7 +84,7 @@ void WindowManager::load()
const auto &type = v.second.get<std::string>("type", "unknown"); const auto &type = v.second.get<std::string>("type", "unknown");
if (type == "main") { if (type == "main") {
WindowManager::getMainWindow().load(v.second); getMainWindow().load(v.second);
} else { } else {
qDebug() << "Unhandled window type: " << type.c_str(); 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 the main window was not loaded properly, load defaults
if (!WindowManager::getMainWindow().isLoaded()) { if (!getMainWindow().isLoaded()) {
WindowManager::getMainWindow().loadDefaults(); getMainWindow().loadDefaults();
} }
// If there are no windows, create a default main window // If there are no windows, create a default main window
@ -97,7 +111,7 @@ void WindowManager::save()
{ {
// save main window // save main window
auto child = WindowManager::getMainWindow().save(); auto child = getMainWindow().save();
windows.push_back(std::make_pair("", child)); windows.push_back(std::make_pair("", child));
} }

View file

@ -3,40 +3,35 @@
#include "widgets/mainwindow.h" #include "widgets/mainwindow.h"
#include <QMutex> #include <mutex>
namespace chatterino { namespace chatterino {
class WindowManager class WindowManager
{ {
public: public:
static void layoutVisibleChatWidgets(Channel *channel = NULL); static WindowManager &getInstance()
static void repaintVisibleChatWidgets(Channel *channel = NULL);
static void repaintGifEmotes();
static void updateAll();
static widgets::MainWindow &getMainWindow()
{ {
windowMutex.lock(); return instance;
if (mainWindow == nullptr) {
mainWindow = new widgets::MainWindow();
}
windowMutex.unlock();
return *mainWindow;
} }
static void load(); void layoutVisibleChatWidgets(Channel *channel = nullptr);
static void save(); void repaintVisibleChatWidgets(Channel *channel = nullptr);
void repaintGifEmotes();
void updateAll();
widgets::MainWindow &getMainWindow();
void load();
void save();
private: private:
WindowManager() static WindowManager instance;
{
}
static QMutex windowMutex; WindowManager();
static widgets::MainWindow *mainWindow; std::mutex _windowMutex;
widgets::MainWindow *_mainWindow;
}; };
} // namespace chatterino } // namespace chatterino