Skip to content

Commit

Permalink
lazily initialize settings pages
Browse files Browse the repository at this point in the history
  • Loading branch information
fourtf committed Feb 21, 2020
1 parent 78ca0cb commit 70e5bd1
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 66 deletions.
61 changes: 30 additions & 31 deletions src/widgets/dialogs/SettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ void SettingsDialog::filterElements(const QString &text)
for (auto &&tab : this->tabs_)
{
// filterElements returns true if anything on the page matches the search query
tab->setVisible(tab->page()->filterElements(text));
tab->setVisible(tab->name().contains(text, Qt::CaseInsensitive) ||
tab->page()->filterElements(text));
}

// find next visible page
Expand Down Expand Up @@ -157,36 +158,30 @@ void SettingsDialog::addTabs()

this->ui_.tabContainer->setContentsMargins(0, 20, 0, 20);

this->addTab(new GeneralPage, ":/settings/about.svg");

// clang-format off
this->addTab([]{return new GeneralPage;}, "General", ":/settings/about.svg");
this->ui_.tabContainer->addSpacing(16);

this->addTab(new AccountsPage, ":/settings/accounts.svg");

this->addTab([]{return new AccountsPage;}, "Accounts", ":/settings/accounts.svg");
this->ui_.tabContainer->addSpacing(16);

this->addTab(new CommandPage, ":/settings/commands.svg");
this->addTab(new HighlightingPage, ":/settings/notifications.svg");
this->addTab(new IgnoresPage, ":/settings/ignore.svg");

this->addTab([]{return new CommandPage;}, "Commands", ":/settings/commands.svg");
this->addTab([]{return new HighlightingPage;}, "Highlights", ":/settings/notifications.svg");
this->addTab([]{return new IgnoresPage;}, "Ignores", ":/settings/ignore.svg");
this->ui_.tabContainer->addSpacing(16);

this->addTab(new KeyboardSettingsPage, ":/settings/keybinds.svg");
this->addTab(new ModerationPage, ":/settings/moderation.svg");
this->addTab(new NotificationPage, ":/settings/notification2.svg");
this->addTab(new ExternalToolsPage, ":/settings/externaltools.svg");

this->addTab([]{return new KeyboardSettingsPage;}, "Keybindings", ":/settings/keybinds.svg");
this->addTab([]{return new ModerationPage;}, "Moderation", ":/settings/moderation.svg");
this->addTab([]{return new NotificationPage;}, "Notifications", ":/settings/notification2.svg");
this->addTab([]{return new ExternalToolsPage;}, "External tools", ":/settings/externaltools.svg");
this->ui_.tabContainer->addStretch(1);
this->addTab(new AboutPage, ":/settings/about.svg", Qt::AlignBottom);
this->addTab([]{return new AboutPage;}, "About", ":/settings/about.svg", Qt::AlignBottom);
// clang-format on
}

void SettingsDialog::addTab(SettingsPage *page, const QString &iconPath,
void SettingsDialog::addTab(std::function<SettingsPage *()> page,
const QString &name, const QString &iconPath,
Qt::Alignment alignment)
{
auto tab = new SettingsDialogTab(this, page, iconPath);
page->setTab(tab);
auto tab = new SettingsDialogTab(this, std::move(page), name, iconPath);

this->ui_.pageStack->addWidget(page);
this->ui_.tabContainer->addWidget(tab, 0, alignment);
this->tabs_.push_back(tab);

Expand All @@ -198,6 +193,15 @@ void SettingsDialog::addTab(SettingsPage *page, const QString &iconPath,

void SettingsDialog::selectTab(SettingsDialogTab *tab, bool byUser)
{
// add page if it's not been added yet
[&] {
for (int i = 0; i < this->ui_.pageStack->count(); i++)
if (this->ui_.pageStack->itemAt(i)->widget() == tab->page())
return;

this->ui_.pageStack->addWidget(tab->page());
}();

this->ui_.pageStack->setCurrentWidget(tab->page());

if (this->selectedTab_ != nullptr)
Expand Down Expand Up @@ -231,18 +235,13 @@ SettingsDialogTab *SettingsDialog::tab(SettingsTabId id)
return nullptr;
}

void SettingsDialog::selectPage(SettingsPage *page)
{
assert(page);
assert(page->tab());

this->selectTab(page->tab());
}

void SettingsDialog::showDialog(SettingsDialogPreference preferredTab)
{
static SettingsDialog *instance = new SettingsDialog();
instance->refresh();
static bool hasShownBefore = false;
if (hasShownBefore)
instance->refresh();
hasShownBefore = true;

switch (preferredTab)
{
Expand Down
5 changes: 3 additions & 2 deletions src/widgets/dialogs/SettingsDialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QStackedLayout>
#include <QVBoxLayout>
#include <QWidget>
#include <functional>
#include <pajlada/settings/setting.hpp>
#include "widgets/helper/SettingsDialogTab.hpp"

Expand Down Expand Up @@ -49,10 +50,10 @@ class SettingsDialog : public BaseWindow

void initUi();
void addTabs();
void addTab(SettingsPage *page, const QString &iconPath,
void addTab(std::function<SettingsPage *()> page, const QString &name,
const QString &iconPath,
Qt::Alignment alignment = Qt::AlignTop);
void selectTab(SettingsDialogTab *tab, const bool byUser = true);
void selectPage(SettingsPage *page);
void selectTab(SettingsTabId id);
SettingsDialogTab *tab(SettingsTabId id);
void filterElements(const QString &query);
Expand Down
18 changes: 15 additions & 3 deletions src/widgets/helper/SettingsDialogTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
namespace chatterino {

SettingsDialogTab::SettingsDialogTab(SettingsDialog *_dialog,
SettingsPage *_page, QString imageFileName,
std::function<SettingsPage *()> _lazyPage,
const QString &name, QString imageFileName,
SettingsTabId id)
: BaseWidget(_dialog)
, dialog_(_dialog)
, page_(_page)
, lazyPage_(std::move(_lazyPage))
, id_(id)
, name_(name)
{
this->ui_.labelText = page_->getName();
this->ui_.labelText = name;
this->ui_.icon.addFile(imageFileName);

this->setCursor(QCursor(Qt::PointingHandCursor));
Expand All @@ -38,6 +40,11 @@ void SettingsDialogTab::setSelected(bool _selected)

SettingsPage *SettingsDialogTab::page()
{
if (this->page_)
return this->page_;

this->page_ = this->lazyPage_();
this->page_->setTab(this);
return this->page_;
}

Expand Down Expand Up @@ -74,6 +81,11 @@ void SettingsDialogTab::mousePressEvent(QMouseEvent *event)
this->setFocus();
}

const QString &SettingsDialogTab::name() const
{
return name_;
}

SettingsTabId SettingsDialogTab::id() const
{
return id_;
Expand Down
15 changes: 11 additions & 4 deletions src/widgets/helper/SettingsDialogTab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QIcon>
#include <QPaintEvent>
#include <QWidget>
#include <functional>

namespace chatterino {

Expand All @@ -22,13 +23,17 @@ class SettingsDialogTab : public BaseWidget
Q_OBJECT

public:
SettingsDialogTab(SettingsDialog *dialog_, SettingsPage *page_,
QString imageFileName, SettingsTabId id = {});
SettingsDialogTab(SettingsDialog *dialog_,
std::function<SettingsPage *()> page_,
const QString &name, QString imageFileName,
SettingsTabId id = {});

void setSelected(bool selected_);
SettingsPage *page();
SettingsTabId id() const;

const QString &name() const;

signals:
void selectedChanged(bool);

Expand All @@ -42,9 +47,11 @@ class SettingsDialogTab : public BaseWidget
} ui_;

// Parent settings dialog
SettingsDialog *dialog_;
SettingsPage *page_;
SettingsDialog *dialog_{};
SettingsPage *page_{};
std::function<SettingsPage *()> lazyPage_;
SettingsTabId id_;
QString name_;

bool selected_ = false;
};
Expand Down
1 change: 0 additions & 1 deletion src/widgets/settingspages/AboutPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
namespace chatterino {

AboutPage::AboutPage()
: SettingsPage("About")
{
LayoutCreator<AboutPage> layoutCreator(this);

Expand Down
1 change: 0 additions & 1 deletion src/widgets/settingspages/AccountsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
namespace chatterino {

AccountsPage::AccountsPage()
: SettingsPage("Accounts")
{
auto *app = getApp();

Expand Down
1 change: 0 additions & 1 deletion src/widgets/settingspages/CommandPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace {
} // namespace

CommandPage::CommandPage()
: SettingsPage("Commands")
{
auto app = getApp();

Expand Down
1 change: 0 additions & 1 deletion src/widgets/settingspages/ExternalToolsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace chatterino {

ExternalToolsPage::ExternalToolsPage()
: SettingsPage("External tools")
{
LayoutCreator<ExternalToolsPage> layoutCreator(this);
auto layout = layoutCreator.setLayoutType<QVBoxLayout>();
Expand Down
5 changes: 1 addition & 4 deletions src/widgets/settingspages/GeneralPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ bool SettingsLayout::filterElements(const QString &query)
}

GeneralPage::GeneralPage()
: SettingsPage("General")
{
auto y = new QVBoxLayout;
auto scroll = new QScrollArea;
Expand All @@ -263,9 +262,7 @@ GeneralPage::GeneralPage()
bool GeneralPage::filterElements(const QString &query)
{
if (this->settingsLayout_)
return this->settingsLayout_->filterElements(query) ||
this->name_.contains(query, Qt::CaseInsensitive) ||
query.isEmpty();
return this->settingsLayout_->filterElements(query) || query.isEmpty();
else
return false;
}
Expand Down
1 change: 0 additions & 1 deletion src/widgets/settingspages/HighlightingPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
namespace chatterino {

HighlightingPage::HighlightingPage()
: SettingsPage("Highlights")
{
auto app = getApp();
LayoutCreator<HighlightingPage> layoutCreator(this);
Expand Down
1 change: 0 additions & 1 deletion src/widgets/settingspages/IgnoresPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ static void addUsersTab(IgnoresPage &page, LayoutCreator<QVBoxLayout> box,
QStringListModel &model);

IgnoresPage::IgnoresPage()
: SettingsPage("Ignores")
{
LayoutCreator<IgnoresPage> layoutCreator(this);
auto layout = layoutCreator.setLayoutType<QVBoxLayout>();
Expand Down
1 change: 0 additions & 1 deletion src/widgets/settingspages/KeyboardSettingsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace chatterino {

KeyboardSettingsPage::KeyboardSettingsPage()
: SettingsPage("Keybindings")
{
auto layout =
LayoutCreator<KeyboardSettingsPage>(this).setLayoutType<QVBoxLayout>();
Expand Down
1 change: 0 additions & 1 deletion src/widgets/settingspages/ModerationPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ QString fetchLogDirectorySize()
}

ModerationPage::ModerationPage()
: SettingsPage("Moderation")
{
auto app = getApp();
LayoutCreator<ModerationPage> layoutCreator(this);
Expand Down
1 change: 0 additions & 1 deletion src/widgets/settingspages/NotificationPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace chatterino {

NotificationPage::NotificationPage()
: SettingsPage("Notifications")
{
LayoutCreator<NotificationPage> layoutCreator(this);
auto layout = layoutCreator.emplace<QVBoxLayout>().withoutMargin();
Expand Down
11 changes: 2 additions & 9 deletions src/widgets/settingspages/SettingsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,13 @@ bool filterItemsRec(QObject *object, const QString &query)
return any;
}

SettingsPage::SettingsPage(const QString &name)
: name_(name)
SettingsPage::SettingsPage()
{
}

bool SettingsPage::filterElements(const QString &query)
{
return filterItemsRec(this, query) || query.isEmpty() ||
this->name_.contains(query, Qt::CaseInsensitive);
}

const QString &SettingsPage::getName()
{
return this->name_;
return filterItemsRec(this, query) || query.isEmpty();
}

SettingsDialogTab *SettingsPage::tab() const
Expand Down
5 changes: 1 addition & 4 deletions src/widgets/settingspages/SettingsPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class SettingsPage : public QFrame
Q_OBJECT

public:
SettingsPage(const QString &name);

const QString &getName();
SettingsPage();

virtual bool filterElements(const QString &query);

Expand All @@ -71,7 +69,6 @@ class SettingsPage : public QFrame
}

protected:
QString name_;
SettingsDialogTab *tab_;
pajlada::Signals::NoArgSignal onCancel_;
std::vector<pajlada::Signals::ScopedConnection> managedConnections_;
Expand Down

0 comments on commit 70e5bd1

Please sign in to comment.