Optimize theme edit data clearing.

This commit is contained in:
John Preston 2019-09-06 18:30:44 +03:00
parent 469c6770fb
commit 95ee17bd54
4 changed files with 27 additions and 14 deletions

View File

@ -483,6 +483,10 @@ SendMediaReady PrepareWallPaper(const QImage &image) {
0); 0);
} }
void ClearEditingPalette() {
QFile(EditingPalettePath()).remove();
}
} // namespace } // namespace
ChatBackground::AdjustableColor::AdjustableColor(style::color data) ChatBackground::AdjustableColor::AdjustableColor(style::color data)
@ -739,13 +743,20 @@ std::optional<Data::CloudTheme> ChatBackground::editingTheme() const {
return _editingTheme; return _editingTheme;
} }
void ChatBackground::setEditingTheme( void ChatBackground::setEditingTheme(const Data::CloudTheme &editing) {
std::optional<Data::CloudTheme> editing) { _editingTheme = editing;
if (!_editingTheme && !editing) { }
void ChatBackground::clearEditingTheme(ClearEditing clear) {
if (!_editingTheme) {
return; return;
} }
_editingTheme = editing; _editingTheme = std::nullopt;
if (!_editingTheme) { if (clear == ClearEditing::Temporary) {
return;
}
ClearEditingPalette();
if (clear == ClearEditing::RevertChanges) {
reapplyWithNightMode(std::nullopt, _nightMode); reapplyWithNightMode(std::nullopt, _nightMode);
KeepApplied(); KeepApplied();
} }
@ -1235,10 +1246,6 @@ QString EditingPalettePath() {
return cWorkingDir() + "tdata/editing-theme.tdesktop-palette"; return cWorkingDir() + "tdata/editing-theme.tdesktop-palette";
} }
void ClearEditingPalette() {
QFile(EditingPalettePath()).remove();
}
QColor CountAverageColor(const QImage &image) { QColor CountAverageColor(const QImage &image) {
Expects(image.format() == QImage::Format_ARGB32_Premultiplied); Expects(image.format() == QImage::Format_ARGB32_Premultiplied);

View File

@ -70,7 +70,6 @@ void ToggleNightMode(const QString &themePath);
void Revert(); void Revert();
[[nodiscard]] QString EditingPalettePath(); [[nodiscard]] QString EditingPalettePath();
void ClearEditingPalette();
bool LoadFromFile( bool LoadFromFile(
const QString &file, const QString &file,
@ -103,6 +102,12 @@ struct BackgroundUpdate {
bool tiled; bool tiled;
}; };
enum class ClearEditing {
Temporary,
RevertChanges,
KeepChanges,
};
class ChatBackground class ChatBackground
: public base::Observable<BackgroundUpdate> : public base::Observable<BackgroundUpdate>
, private base::Subscriber { , private base::Subscriber {
@ -121,7 +126,8 @@ public:
void setThemeObject(const Object &object); void setThemeObject(const Object &object);
[[nodiscard]] const Object &themeObject() const; [[nodiscard]] const Object &themeObject() const;
[[nodiscard]] std::optional<Data::CloudTheme> editingTheme() const; [[nodiscard]] std::optional<Data::CloudTheme> editingTheme() const;
void setEditingTheme(std::optional<Data::CloudTheme> editing); void setEditingTheme(const Data::CloudTheme &editing);
void clearEditingTheme(ClearEditing clear = ClearEditing::Temporary);
void reset(); void reset();
void setTestingTheme(Instance &&theme); void setTestingTheme(Instance &&theme);

View File

@ -664,7 +664,7 @@ Editor::Editor(
_close->setClickedCallback([=] { _close->setClickedCallback([=] {
const auto box = std::make_shared<QPointer<BoxContent>>(); const auto box = std::make_shared<QPointer<BoxContent>>();
const auto close = crl::guard(this, [=] { const auto close = crl::guard(this, [=] {
ClearEditingPalette(); Background()->clearEditingTheme(ClearEditing::RevertChanges);
closeEditor(); closeEditor();
if (*box) { if (*box) {
(*box)->closeBox(); (*box)->closeBox();
@ -791,7 +791,7 @@ void Editor::paintEvent(QPaintEvent *e) {
void Editor::closeEditor() { void Editor::closeEditor() {
if (const auto window = App::wnd()) { if (const auto window = App::wnd()) {
window->showRightColumn(nullptr); window->showRightColumn(nullptr);
Background()->setEditingTheme(std::nullopt); Background()->clearEditingTheme();
} }
} }

View File

@ -482,6 +482,7 @@ Fn<void()> SavePreparedTheme(
const auto finish = [=](const MTPTheme &result) { const auto finish = [=](const MTPTheme &result) {
done(); done();
Background()->clearEditingTheme(ClearEditing::KeepChanges);
const auto cloud = result.match([&](const MTPDtheme &data) { const auto cloud = result.match([&](const MTPDtheme &data) {
const auto result = Data::CloudTheme::Parse(session, data); const auto result = Data::CloudTheme::Parse(session, data);
@ -503,7 +504,6 @@ Fn<void()> SavePreparedTheme(
Apply(std::move(preview)); Apply(std::move(preview));
KeepApplied(); KeepApplied();
} }
Background()->setEditingTheme(std::nullopt);
}; };
const auto createTheme = [=](const MTPDocument &data) { const auto createTheme = [=](const MTPDocument &data) {