Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Chatterino/chatterino2 into danke…
Browse files Browse the repository at this point in the history
…rino
  • Loading branch information
Mm2PL committed Oct 31, 2023
2 parents 2428b5b + 25c776c commit 8223232
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 124 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
- Dev: Replace `boost::optional` with `std::optional`. (#4877)
- Dev: Improve performance by reducing repaints caused by selections. (#4889)
- Dev: Removed direct dependency on Qt 5 compatibility module. (#4906)
- Dev: Refactor `DebugCount` and add copy button to debug popup. (#4921)
- Dev: Changed lifetime of context menus. (#4924)

## 2.4.6

Expand Down
8 changes: 7 additions & 1 deletion src/controllers/filters/lang/FilterParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "controllers/filters/lang/Filter.hpp"
#include "controllers/filters/lang/Types.hpp"

namespace chatterino::filters {
namespace {

using namespace chatterino::filters;

QString explainIllType(const IllTyped &ill)
{
Expand All @@ -18,6 +20,10 @@ QString explainIllType(const IllTyped &ill)
.arg(ill.expr->filterString());
}

} // namespace

namespace chatterino::filters {

FilterParser::FilterParser(const QString &text)
: text_(text)
, tokenizer_(Tokenizer(text))
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/filters/lang/FilterParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace chatterino::filters {
class FilterParser
{
public:
/**
* Take input text & attempt to parse it into a filter
**/
FilterParser(const QString &text);

bool valid() const;
Expand Down
9 changes: 8 additions & 1 deletion src/messages/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace detail {
{
auto sz = frame.image.size();
auto area = sz.width() * sz.height();
auto memory = area * frame.image.depth();
auto memory = area * frame.image.depth() / 8;

usage += memory;
}
Expand Down Expand Up @@ -608,6 +608,13 @@ ImageExpirationPool::ImageExpirationPool()
this->freeTimer_->start(
std::chrono::duration_cast<std::chrono::milliseconds>(
IMAGE_POOL_CLEANUP_INTERVAL));

// configure all debug counts used by images
DebugCount::configure("image bytes", DebugCount::Flag::DataSize);
DebugCount::configure("image bytes (ever loaded)",
DebugCount::Flag::DataSize);
DebugCount::configure("image bytes (ever unloaded)",
DebugCount::Flag::DataSize);
}

ImageExpirationPool &ImageExpirationPool::instance()
Expand Down
111 changes: 109 additions & 2 deletions src/util/DebugCount.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,114 @@
#include "DebugCount.hpp"
#include "util/DebugCount.hpp"

#include "common/UniqueAccess.hpp"

#include <QLocale>
#include <QStringBuilder>

#include <map>

namespace {

using namespace chatterino;

struct Count {
int64_t value = 0;
DebugCount::Flags flags = DebugCount::Flag::None;
};

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
UniqueAccess<std::map<QString, Count>> COUNTS;

} // namespace

namespace chatterino {

UniqueAccess<QMap<QString, int64_t>> DebugCount::counts_;
void DebugCount::configure(const QString &name, Flags flags)
{
auto counts = COUNTS.access();

auto it = counts->find(name);
if (it == counts->end())
{
counts->emplace(name, Count{.flags = flags});
}
else
{
it->second.flags = flags;
}
}

void DebugCount::set(const QString &name, const int64_t &amount)
{
auto counts = COUNTS.access();

auto it = counts->find(name);
if (it == counts->end())
{
counts->emplace(name, Count{amount});
}
else
{
it->second.value = amount;
}
}

void DebugCount::increase(const QString &name, const int64_t &amount)
{
auto counts = COUNTS.access();

auto it = counts->find(name);
if (it == counts->end())
{
counts->emplace(name, Count{amount});
}
else
{
it->second.value += amount;
}
}

void DebugCount::decrease(const QString &name, const int64_t &amount)
{
auto counts = COUNTS.access();

auto it = counts->find(name);
if (it == counts->end())
{
counts->emplace(name, Count{-amount});
}
else
{
it->second.value -= amount;
}
}

QString DebugCount::getDebugText()
{
#if QT_VERSION > QT_VERSION_CHECK(5, 13, 0)
static const QLocale locale(QLocale::English);
#else
static QLocale locale(QLocale::English);
#endif

auto counts = COUNTS.access();

QString text;
for (const auto &[key, count] : *counts)
{
QString formatted;
if (count.flags.has(Flag::DataSize))
{
formatted = locale.formattedDataSize(count.value);
}
else
{
formatted = locale.toString(static_cast<qlonglong>(count.value));
}

text += key % ": " % formatted % '\n';
}
return text;
}

} // namespace chatterino
103 changes: 15 additions & 88 deletions src/util/DebugCount.hpp
Original file line number Diff line number Diff line change
@@ -1,111 +1,38 @@
#pragma once

#include "common/UniqueAccess.hpp"
#include "common/FlagsEnum.hpp"

#include <QMap>
#include <QString>

#include <mutex>
#include <typeinfo>

namespace chatterino {

class DebugCount
{
public:
static void increase(const QString &name)
{
auto counts = counts_.access();

auto it = counts->find(name);
if (it == counts->end())
{
counts->insert(name, 1);
}
else
{
reinterpret_cast<int64_t &>(it.value())++;
}
}
enum class Flag : uint16_t {
None = 0,
/// The value is a data size in bytes
DataSize = 1 << 0,
};
using Flags = FlagsEnum<Flag>;

static void set(const QString &name, const int64_t &amount)
{
auto counts = counts_.access();
static void configure(const QString &name, Flags flags);

auto it = counts->find(name);
if (it == counts->end())
{
counts->insert(name, amount);
}
else
{
reinterpret_cast<int64_t &>(it.value()) = amount;
}
}
static void set(const QString &name, const int64_t &amount);

static void increase(const QString &name, const int64_t &amount)
static void increase(const QString &name, const int64_t &amount);
static void increase(const QString &name)
{
auto counts = counts_.access();

auto it = counts->find(name);
if (it == counts->end())
{
counts->insert(name, amount);
}
else
{
reinterpret_cast<int64_t &>(it.value()) += amount;
}
DebugCount::increase(name, 1);
}

static void decrease(const QString &name, const int64_t &amount);
static void decrease(const QString &name)
{
auto counts = counts_.access();

auto it = counts->find(name);
if (it == counts->end())
{
counts->insert(name, -1);
}
else
{
reinterpret_cast<int64_t &>(it.value())--;
}
}
static void decrease(const QString &name, const int64_t &amount)
{
auto counts = counts_.access();

auto it = counts->find(name);
if (it == counts->end())
{
counts->insert(name, -amount);
}
else
{
reinterpret_cast<int64_t &>(it.value()) -= amount;
}
}

static QString getDebugText()
{
auto counts = counts_.access();

QString text;
for (auto it = counts->begin(); it != counts->end(); it++)
{
text += it.key() + ": " + QString::number(it.value()) + "\n";
}
return text;
}

QString toString()
{
return "";
DebugCount::decrease(name, 1);
}

private:
static UniqueAccess<QMap<QString, int64_t>> counts_;
static QString getDebugText();
};

} // namespace chatterino
11 changes: 2 additions & 9 deletions src/widgets/dialogs/UserInfoPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,8 @@ UserInfoPopup::UserInfoPopup(bool closeAutomatically, QWidget *parent,
return;
}

static QMenu *previousMenu = nullptr;
if (previousMenu != nullptr)
{
previousMenu->deleteLater();
previousMenu = nullptr;
}

auto menu = new QMenu;
previousMenu = menu;
auto *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);

auto avatarUrl = this->avatarUrl_;

Expand Down
23 changes: 5 additions & 18 deletions src/widgets/helper/ChannelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,13 @@ namespace {
MessageElementFlags creatorFlags, QMenu &menu)
{
auto *openAction = menu.addAction("&Open");
auto openMenu = new QMenu;
auto *openMenu = new QMenu(&menu);
openAction->setMenu(openMenu);

auto *copyAction = menu.addAction("&Copy");
auto copyMenu = new QMenu;
auto *copyMenu = new QMenu(&menu);
copyAction->setMenu(copyMenu);

// see if the QMenu actually gets destroyed
QObject::connect(openMenu, &QMenu::destroyed, [] {
QMessageBox(QMessageBox::Information, "xD", "the menu got deleted")
.exec();
});

// Add copy and open links for 1x, 2x, 3x
auto addImageLink = [&](const ImagePtr &image, char scale) {
if (!image->isEmpty())
Expand Down Expand Up @@ -2104,15 +2098,8 @@ void ChannelView::addContextMenuItems(
const MessageLayoutElement *hoveredElement, MessageLayoutPtr layout,
QMouseEvent *event)
{
static QMenu *previousMenu = nullptr;
if (previousMenu != nullptr)
{
previousMenu->deleteLater();
previousMenu = nullptr;
}

auto menu = new QMenu;
previousMenu = menu;
auto *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);

// Add image options if the element clicked contains an image (e.g. a badge or an emote)
this->addImageContextMenuItems(hoveredElement, layout, event, *menu);
Expand Down Expand Up @@ -2421,7 +2408,7 @@ void ChannelView::addCommandExecutionContextMenuItems(

menu.addSeparator();
auto *executeAction = menu.addAction("&Execute command");
auto cmdMenu = new QMenu;
auto *cmdMenu = new QMenu(&menu);
executeAction->setMenu(cmdMenu);

for (auto &cmd : cmds)
Expand Down
Loading

0 comments on commit 8223232

Please sign in to comment.