Add 'Export theme' / 'Show palette file' menu.

This commit is contained in:
John Preston 2019-09-09 17:44:08 +03:00
parent c1ab1acd44
commit 8bf6013342
6 changed files with 118 additions and 37 deletions

View File

@ -1628,6 +1628,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
"lng_theme_editor_save_title" = "Save theme";
"lng_theme_editor_link_about" = "Your theme will be updated for all users each time you change it. Anyone can install it using this link.\n\nTheme links must be longer than 5 characters and use a-z, 0-9 and underscores.";
"lng_theme_editor_menu_export" = "Export theme";
"lng_theme_editor_menu_show" = "Show palette file";
"lng_payments_not_supported" = "Sorry, Telegram Desktop doesn't support payments yet. Please use one of our mobile apps to do this.";
"lng_payments_receipt_label" = "Receipt";
"lng_payments_receipt_label_test" = "Test receipt";

View File

@ -885,6 +885,21 @@ themesScroll: ScrollArea(defaultScrollArea) {
bottomsh: 0px;
topsh: 0px;
}
themesMenuToggle: IconButton(defaultIconButton) {
width: 44px;
height: 44px;
icon: icon {{ "title_menu_dots", menuIconFg }};
iconOver: icon {{ "title_menu_dots", menuIconFgOver }};
iconPosition: point(-1px, -1px);
rippleAreaPosition: point(4px, 4px);
rippleAreaSize: 36px;
ripple: RippleAnimation(defaultRippleAnimation) {
color: windowBgOver;
}
}
themesMenuPosition: point(-2px, 25px);
createPollField: InputField(defaultInputField) {
font: boxTextFont;

View File

@ -16,13 +16,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "mainwindow.h"
#include "storage/localstorage.h"
#include "boxes/confirm_box.h"
#include "styles/style_window.h"
#include "styles/style_dialogs.h"
#include "styles/style_boxes.h"
#include "ui/widgets/scroll_area.h"
#include "ui/widgets/shadow.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/multi_select.h"
#include "ui/widgets/dropdown_menu.h"
#include "ui/toast/toast.h"
#include "base/parse_helper.h"
#include "base/zlib_help.h"
@ -30,6 +28,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "core/application.h"
#include "boxes/edit_color_box.h"
#include "lang/lang_keys.h"
#include "styles/style_window.h"
#include "styles/style_dialogs.h"
#include "styles/style_boxes.h"
namespace Window {
namespace Theme {
@ -621,29 +622,6 @@ void Editor::Inner::applyEditing(const QString &name, const QString &copyOf, QCo
_paletteContent = newContent;
}
//void ThemeExportBox::exportTheme() {
// App::CallDelayed(st::defaultRippleAnimation.hideDuration, this, [this] {
// auto caption = tr::lng_theme_editor_choose_name(tr::now);
// auto filter = "Themes (*.tdesktop-theme)";
// auto name = "awesome.tdesktop-theme";
// FileDialog::GetWritePath(this, caption, filter, name, crl::guard(this, [this](const QString &path) {
// QFile f(path);
// if (!f.open(QIODevice::WriteOnly)) {
// LOG(("Theme Error: could not open zip-ed theme file '%1' for writing").arg(path));
// Ui::show(Box<InformBox>(tr::lng_theme_editor_error(tr::now)));
// return;
// }
// if (f.write(result) != result.size()) {
// LOG(("Theme Error: could not write zip-ed theme to file '%1'").arg(path));
// Ui::show(Box<InformBox>(tr::lng_theme_editor_error(tr::now)));
// return;
// }
// Ui::hideLayer();
// Ui::Toast::Show(tr::lng_theme_editor_done(tr::now));
// }));
// });
//}
Editor::Editor(
QWidget*,
not_null<Window::Controller*> window,
@ -652,6 +630,7 @@ Editor::Editor(
, _cloud(cloud)
, _scroll(this, st::themesScroll)
, _close(this, st::contactsMultiSelect.fieldCancel)
, _menuToggle(this, st::themesMenuToggle)
, _select(this, st::contactsMultiSelect, tr::lng_country_ph())
, _leftShadow(this)
, _topShadow(this)
@ -680,6 +659,9 @@ Editor::Editor(
_inner->setScrollCallback([this](int top, int bottom) {
_scroll->scrollToY(top, bottom);
});
_menuToggle->setClickedCallback([=] {
showMenu();
});
_close->setClickedCallback([=] {
closeWithConfirmation();
});
@ -693,6 +675,63 @@ Editor::Editor(
resizeToWidth(st::windowMinWidth);
}
void Editor::showMenu() {
if (_menu) {
return;
}
_menu.create(this);
_menu->setHiddenCallback([weak = make_weak(this), menu = _menu.data()]{
menu->deleteLater();
if (weak && weak->_menu == menu) {
weak->_menu = nullptr;
weak->_menuToggle->setForceRippled(false);
}
});
_menu->setShowStartCallback(crl::guard(this, [this, menu = _menu.data()]{
if (_menu == menu) {
_menuToggle->setForceRippled(true);
}
}));
_menu->setHideStartCallback(crl::guard(this, [this, menu = _menu.data()]{
if (_menu == menu) {
_menuToggle->setForceRippled(false);
}
}));
_menuToggle->installEventFilter(_menu);
_menu->addAction(tr::lng_theme_editor_menu_export(tr::now), [=] {
App::CallDelayed(st::defaultRippleAnimation.hideDuration, this, [=] {
exportTheme();
});
});
_menu->addAction(tr::lng_theme_editor_menu_show(tr::now), [=] {
File::ShowInFolder(EditingPalettePath());
});
_menu->moveToRight(st::themesMenuPosition.x(), st::themesMenuPosition.y());
_menu->showAnimated(Ui::PanelAnimation::Origin::TopRight);
}
void Editor::exportTheme() {
auto caption = tr::lng_theme_editor_choose_name(tr::now);
auto filter = "Themes (*.tdesktop-theme)";
auto name = "awesome.tdesktop-theme";
FileDialog::GetWritePath(this, caption, filter, name, crl::guard(this, [=](const QString &path) {
const auto result = CollectForExport(_inner->paletteContent());
QFile f(path);
if (!f.open(QIODevice::WriteOnly)) {
LOG(("Theme Error: could not open zip-ed theme file '%1' for writing").arg(path));
Ui::show(Box<InformBox>(tr::lng_theme_editor_error(tr::now)));
return;
}
if (f.write(result) != result.size()) {
LOG(("Theme Error: could not write zip-ed theme to file '%1'").arg(path));
Ui::show(Box<InformBox>(tr::lng_theme_editor_error(tr::now)));
return;
}
Ui::Toast::Show(tr::lng_theme_editor_done(tr::now));
}));
}
QByteArray Editor::ColorizeInContent(
QByteArray content,
const Colorizer &colorizer) {
@ -714,6 +753,7 @@ void Editor::save() {
void Editor::resizeEvent(QResizeEvent *e) {
_save->resizeToWidth(width());
_close->moveToRight(0, 0);
_menuToggle->moveToRight(_close->width(), 0);
_select->resizeToWidth(width());
_select->moveToLeft(0, _close->height());

View File

@ -15,6 +15,8 @@ class ScrollArea;
class CrossButton;
class MultiSelect;
class PlainShadow;
class DropdownMenu;
class IconButton;
} // namespace Ui
namespace Window {
@ -61,6 +63,8 @@ protected:
private:
void save();
void showMenu();
void exportTheme();
void closeEditor();
void closeWithConfirmation();
@ -71,6 +75,8 @@ private:
class Inner;
QPointer<Inner> _inner;
object_ptr<Ui::CrossButton> _close;
object_ptr<Ui::IconButton> _menuToggle;
object_ptr<Ui::DropdownMenu> _menu = { nullptr };
object_ptr<Ui::MultiSelect> _select;
object_ptr<Ui::PlainShadow> _leftShadow;
object_ptr<Ui::PlainShadow> _topShadow;

View File

@ -550,7 +550,6 @@ Fn<void()> SavePreparedTheme(
const auto creating = !fields.id
|| (fields.createdBy != session->userId());
const auto oldDocumentId = creating ? 0 : fields.documentId;
const auto changed = (parsed.background != originalParsed.background)
|| (parsed.tiled != originalParsed.tiled)
|| PaletteChanged(parsed.palette, originalParsed.palette, fields);
@ -814,13 +813,14 @@ void SaveTheme(
}
}
void SaveThemeBox(
not_null<GenericBox*> box,
not_null<Window::Controller*> window,
const Data::CloudTheme &cloud,
const QByteArray &palette) {
Expects(window->account().sessionExists());
struct CollectedData {
QByteArray originalContent;
ParsedTheme originalParsed;
ParsedTheme parsed;
QImage background;
};
[[nodiscard]] CollectedData CollectData(const QByteArray &palette) {
const auto original = Local::ReadThemeContent();
const auto originalContent = original.content;
@ -847,6 +847,21 @@ void SaveThemeBox(
parsed.background = originalParsed.background;
parsed.isPng = originalParsed.isPng;
}
return { originalContent, originalParsed, parsed, background };
}
QByteArray CollectForExport(const QByteArray &palette) {
return PackTheme(CollectData(palette).parsed);
}
void SaveThemeBox(
not_null<GenericBox*> box,
not_null<Window::Controller*> window,
const Data::CloudTheme &cloud,
const QByteArray &palette) {
Expects(window->account().sessionExists());
const auto collected = CollectData(palette);
box->setTitle(tr::lng_theme_editor_save_title(Ui::Text::WithEntities));
@ -902,8 +917,8 @@ void SaveThemeBox(
const auto back = box->addRow(
object_ptr<BackgroundSelector>(
box,
background,
parsed),
collected.background,
collected.parsed),
style::margins(
st::boxRowPadding.left(),
st::themesSmallSkip,
@ -963,8 +978,8 @@ void SaveThemeBox(
*cancel = SavePreparedTheme(
window,
back->result(),
originalContent,
originalParsed,
collected.originalContent,
collected.originalParsed,
fields,
done,
fail);

View File

@ -44,5 +44,7 @@ void SaveThemeBox(
const QByteArray &editorPalette,
const Data::CloudTheme &cloud);
[[nodiscard]] QByteArray CollectForExport(const QByteArray &palette);
} // namespace Theme
} // namespace Window