Allow share and remove cloud themes.

This commit is contained in:
John Preston 2019-09-08 20:05:26 +03:00
parent c92a9585e1
commit a770b5d4cd
7 changed files with 99 additions and 2 deletions

View File

@ -414,6 +414,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_theme_keep_changes" = "Keep changes";
"lng_theme_revert" = "Revert";
"lng_theme_no_desktop" = "Sorry, this theme doesn't include a version for Telegram Desktop.";
"lng_theme_share" = "Share";
"lng_theme_edit" = "Edit";
"lng_theme_delete" = "Delete";
"lng_theme_delete_sure" = "Are you sure you want to delete this theme?";
"lng_background_header" = "Background preview";
"lng_background_text1" = "Ah, you kids today with techno music! You should enjoy the classics, like Hasselhoff!";
"lng_background_text2" = "I can't even take you seriously right now.";

View File

@ -290,4 +290,19 @@ void CloudThemes::apply(const CloudTheme &theme) {
}
}
void CloudThemes::remove(uint64 cloudThemeId) {
const auto i = ranges::find(_list, cloudThemeId, &CloudTheme::id);
if (i == end(_list)) {
return;
}
_session->api().request(MTPaccount_SaveTheme(
MTP_inputTheme(
MTP_long(i->id),
MTP_long(i->accessHash)),
MTP_bool(true)
)).send();
_list.erase(i);
_updates.fire({});
}
} // namespace Data

View File

@ -40,6 +40,7 @@ public:
[[nodiscard]] rpl::producer<> updated() const;
[[nodiscard]] const std::vector<CloudTheme> &list() const;
void apply(const CloudTheme &data);
void remove(uint64 cloudThemeId);
void applyUpdate(const MTPTheme &theme);

View File

@ -1245,6 +1245,12 @@ void ToggleNightMode(const QString &path) {
Background()->toggleNightMode(path);
}
void ResetToSomeDefault() {
Background()->reapplyWithNightMode(
IsNightMode() ? NightThemePath() : QString(),
IsNightMode());
}
bool LoadFromContent(
const QByteArray &content,
not_null<Instance*> out,

View File

@ -75,6 +75,7 @@ QString NightThemePath();
void SetNightModeValue(bool nightMode);
void ToggleNightMode();
void ToggleNightMode(const QString &themePath);
void ResetToSomeDefault();
[[nodiscard]] bool IsNonDefaultBackground();
void Revert();
@ -198,6 +199,7 @@ private:
friend void SetNightModeValue(bool nightMode);
friend void ToggleNightMode();
friend void ToggleNightMode(const QString &themePath);
friend void ResetToSomeDefault();
friend void KeepApplied();
friend void KeepFromEditor(
const QByteArray &originalContent,

View File

@ -17,12 +17,19 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_document.h"
#include "data/data_session.h"
#include "ui/image/image_prepare.h"
#include "ui/widgets/popup_menu.h"
#include "ui/toast/toast.h"
#include "boxes/confirm_box.h"
#include "lang/lang_keys.h"
#include "main/main_session.h"
#include "core/application.h"
#include "styles/style_settings.h"
#include "styles/style_boxes.h"
#include "styles/style_history.h"
#include <QtGui/QGuiApplication>
#include <QtGui/QClipboard>
namespace Window {
namespace Theme {
namespace {
@ -474,14 +481,19 @@ void CloudList::insert(int index, const Data::CloudTheme &theme) {
button->setAllowTextLines(2);
button->setTextBreakEverywhere();
button->show();
button->addClickHandler([=] {
button->setAcceptBoth(true);
button->addClickHandler([=](Qt::MouseButton button) {
const auto i = ranges::find(_elements, id, &Element::id);
if (i == end(_elements)
|| id == kFakeCloudThemeId
|| i->waiting) {
return;
}
_window->session().data().cloudThemes().showPreview(i->theme);
if (button == Qt::RightButton) {
showMenu(*i);
} else {
_window->session().data().cloudThemes().showPreview(i->theme);
}
});
auto &element = *_elements.insert(
begin(_elements) + index,
@ -523,6 +535,56 @@ void CloudList::refreshColors(Element &element) {
}
}
void CloudList::showMenu(Element &element) {
if (_contextMenu) {
_contextMenu = nullptr;
return;
}
_contextMenu = base::make_unique_q<Ui::PopupMenu>(element.button.get());
const auto cloud = element.theme;
if (const auto slug = element.theme.slug; !slug.isEmpty()) {
_contextMenu->addAction(tr::lng_theme_share(tr::now), [=] {
QGuiApplication::clipboard()->setText(
Core::App().createInternalLinkFull("addtheme/" + slug));
Ui::Toast::Show(tr::lng_background_link_copied(tr::now));
});
}
if (cloud.documentId
&& cloud.createdBy == _window->session().userId()
&& Background()->themeObject().cloud.id == cloud.id) {
_contextMenu->addAction(tr::lng_theme_edit(tr::now), [=] {
StartEditor(&_window->window(), cloud);
});
}
const auto id = cloud.id;
_contextMenu->addAction(tr::lng_theme_delete(tr::now), [=] {
const auto box = std::make_shared<QPointer<BoxContent>>();
const auto remove = [=] {
if (Background()->themeObject().cloud.id == id
|| id == kFakeCloudThemeId) {
if (*box) {
(*box)->closeBox();
}
if (Background()->editingTheme().has_value()) {
Background()->clearEditingTheme(
ClearEditing::KeepChanges);
_window->window().showRightColumn(nullptr);
}
ResetToSomeDefault();
KeepApplied();
}
if (id != kFakeCloudThemeId) {
_window->session().data().cloudThemes().remove(id);
}
};
*box = _window->window().show(Box<ConfirmBox>(
tr::lng_theme_delete_sure(tr::now),
tr::lng_theme_delete(tr::now),
remove));
});
_contextMenu->popup(QCursor::pos());
}
void CloudList::setWaiting(Element &element, bool waiting) {
element.waiting = waiting;
element.button->setPointerCursor(

View File

@ -10,6 +10,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "boxes/generic_box.h"
#include "data/data_cloud_themes.h"
#include "ui/widgets/checkbox.h"
#include "base/unique_qptr.h"
namespace Ui {
class PopupMenu;
} // namespace Ui
namespace Window {
@ -98,6 +103,7 @@ private:
void refreshElementUsing(Element &element, const Data::CloudTheme &data);
void insert(int index, const Data::CloudTheme &theme);
void refreshColors(Element &element);
void showMenu(Element &element);
void refreshColorsFromDocument(
Element &element,
not_null<DocumentData*> document);
@ -119,6 +125,7 @@ private:
std::vector<uint64> _idByGroupValue;
base::flat_map<uint64, int> _groupValueById;
rpl::lifetime _downloadFinishedLifetime;
base::unique_qptr<Ui::PopupMenu> _contextMenu;
};