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

View File

@ -70,7 +70,6 @@ void ToggleNightMode(const QString &themePath);
void Revert();
[[nodiscard]] QString EditingPalettePath();
void ClearEditingPalette();
bool LoadFromFile(
const QString &file,
@ -103,6 +102,12 @@ struct BackgroundUpdate {
bool tiled;
};
enum class ClearEditing {
Temporary,
RevertChanges,
KeepChanges,
};
class ChatBackground
: public base::Observable<BackgroundUpdate>
, private base::Subscriber {
@ -121,7 +126,8 @@ public:
void setThemeObject(const Object &object);
[[nodiscard]] const Object &themeObject() 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 setTestingTheme(Instance &&theme);

View File

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

View File

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