2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Redesign auto download box.

This commit is contained in:
John Preston
2018-12-06 19:47:28 +04:00
parent 8e54ac4dcf
commit a0c6104fae
19 changed files with 298 additions and 219 deletions

View File

@@ -0,0 +1,167 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "boxes/auto_download_box.h"
#include "lang/lang_keys.h"
#include "auth_session.h"
#include "data/data_session.h"
#include "info/profile/info_profile_button.h"
#include "ui/widgets/continuous_sliders.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/wrap/wrap.h"
#include "storage/localstorage.h"
#include "settings/settings_common.h"
#include "export/view/export_view_settings.h"
#include "styles/style_boxes.h"
#include "styles/style_settings.h"
namespace {
constexpr auto kMegabyte = 1024 * 1024;
constexpr auto kDefaultLimit = 10 * kMegabyte;
} // namespace
AutoDownloadBox::AutoDownloadBox(
QWidget*,
Data::AutoDownload::Source source)
: _source(source) {
}
void AutoDownloadBox::prepare() {
setupContent();
}
void AutoDownloadBox::setupContent() {
using namespace Settings;
using namespace Data::AutoDownload;
using namespace rpl::mappers;
using Type = Data::AutoDownload::Type;
constexpr auto kLegacyLimit = 10 * 1024 * 1024;
setTitle(langFactory(lng_media_auto_title));
const auto settings = &Auth().settings().autoDownload();
const auto checked = [=](Source source, Type type) {
return (settings->bytesLimit(source, type) > 0);
};
auto wrap = object_ptr<Ui::VerticalLayout>(this);
const auto content = wrap.data();
setInnerWidget(object_ptr<Ui::OverrideMargins>(
this,
std::move(wrap)));
const auto values = Ui::CreateChild<base::flat_map<Type, int>>(content);
const auto add = [&](Type type, LangKey label) {
const auto value = settings->bytesLimit(_source, type);
AddButton(
content,
label,
st::settingsButton
)->toggleOn(
rpl::single(value > 0)
)->toggledChanges(
) | rpl::start_with_next([=](bool enabled) {
(*values)[type] = enabled ? 1 : 0;
}, content->lifetime());
values->emplace(type, value);
};
add(Type::Photo, lng_media_photo_title);
add(Type::VoiceMessage, lng_media_audio_title);
add(Type::VideoMessage, lng_media_video_messages_title);
add(Type::Video, lng_media_video_title);
add(Type::File, lng_media_file_title);
add(Type::Music, lng_media_music_title);
add(Type::GIF, lng_media_animation_title);
const auto limits = Ui::CreateChild<rpl::event_stream<int>>(content);
using Pair = base::flat_map<Type, int>::value_type;
const auto settingsLimit = ranges::max_element(
*values,
std::less<>(),
[](Pair pair) { return pair.second; })->second;
const auto initialLimit = settingsLimit ? settingsLimit : kDefaultLimit;
const auto limit = Ui::CreateChild<int>(content, initialLimit);
AddButtonWithLabel(
content,
lng_media_size_limit,
limits->events_starting_with_copy(
initialLimit
) | rpl::map([](int value) {
return lng_media_size_up_to(
lt_size,
QString::number(value / kMegabyte) + " MB");
}),
st::autoDownloadLimitButton
)->setAttribute(Qt::WA_TransparentForMouseEvents);
const auto slider = content->add(
object_ptr<Ui::MediaSlider>(content, st::autoDownloadLimitSlider),
st::autoDownloadLimitPadding);
slider->resize(st::autoDownloadLimitSlider.seekSize);
slider->setPseudoDiscrete(
Export::View::kSizeValueCount,
Export::View::SizeLimitByIndex,
*limit,
[=](int value) {
*limit = value;
limits->fire_copy(value);
});
const auto save = [=](
Type type,
std::pair<bool, bool> pair) {
const auto limit = [](bool checked) {
return checked ? kMaxBytesLimit : 0;
};
settings->setBytesLimit(Source::User, type, limit(pair.first));
settings->setBytesLimit(Source::Group, type, limit(pair.second));
settings->setBytesLimit(Source::Channel, type, limit(pair.second));
};
addButton(langFactory(lng_connection_save), [=] {
auto allowMore = ranges::view::all(
*values
) | ranges::view::filter([&](Pair pair) {
const auto [type, enabled] = pair;
const auto value = enabled ? *limit : 0;
const auto old = settings->bytesLimit(_source, type);
return (old < value);
}) | ranges::view::transform([](Pair pair) {
return pair.first;
});
const auto allowMoreTypes = base::flat_set<Type>(
allowMore.begin(),
allowMore.end());
const auto changed = ranges::find_if(*values, [&](Pair pair) {
const auto [type, enabled] = pair;
const auto value = enabled ? *limit : 0;
return settings->bytesLimit(_source, type) != value;
}) != end(*values);
if (changed) {
for (const auto [type, enabled] : *values) {
const auto value = enabled ? *limit : 0;
settings->setBytesLimit(_source, type, value);
}
Local::writeUserSettings();
}
if (allowMoreTypes.contains(Type::Photo)) {
Auth().data().photoLoadSettingsChanged();
}
if (ranges::find_if(allowMoreTypes, _1 != Type::Photo)
!= allowMoreTypes.end()) {
Auth().data().documentLoadSettingsChanged();
}
closeBox();
});
addButton(langFactory(lng_cancel), [=] { closeBox(); });
setDimensionsToContent(st::boxWidth, content);
}

View File

@@ -0,0 +1,30 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "boxes/abstract_box.h"
namespace Data {
namespace AutoDownload {
enum class Source;
} // namespace AutoDownload
} // namespace Data
class AutoDownloadBox : public BoxContent {
public:
AutoDownloadBox(QWidget*, Data::AutoDownload::Source source);
protected:
void prepare() override;
private:
void setupContent();
Data::AutoDownload::Source _source;
};

View File

@@ -533,6 +533,10 @@ aboutLabel: FlatLabel(defaultFlatLabel) {
autoDownloadTopDelta: 10px;
autoDownloadTitlePosition: point(23px, 18px);
autoDownloadTitleFont: font(15px semibold);
autoDownloadLimitSlider: MediaSlider(defaultContinuousSlider) {
seekSize: size(15px, 15px);
}
autoDownloadLimitPadding: margins(22px, 8px, 22px, 8px);
confirmCaptionArea: InputField(defaultInputField) {
textMargins: margins(1px, 26px, 31px, 4px);

View File

@@ -7,37 +7,24 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "boxes/connection_box.h"
#include "data/data_photo.h"
#include "data/data_document.h"
#include "boxes/confirm_box.h"
#include "lang/lang_keys.h"
#include "storage/localstorage.h"
#include "base/qthelp_url.h"
#include "mainwidget.h"
#include "messenger.h"
#include "mainwindow.h"
#include "auth_session.h"
#include "data/data_session.h"
#include "mtproto/connection.h"
#include "ui/widgets/checkbox.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/input_fields.h"
#include "ui/widgets/labels.h"
#include "ui/widgets/dropdown_menu.h"
#include "ui/wrap/fade_wrap.h"
#include "ui/wrap/padding_wrap.h"
#include "ui/wrap/slide_wrap.h"
#include "ui/wrap/vertical_layout.h"
#include "ui/toast/toast.h"
#include "ui/effects/radial_animation.h"
#include "ui/text_options.h"
#include "history/history_location_manager.h"
#include "settings/settings_common.h"
#include "application.h"
#include "styles/style_boxes.h"
#include "styles/style_chat_helpers.h"
#include "styles/style_info.h"
#include "styles/style_settings.h"
namespace {
@@ -936,135 +923,6 @@ void ProxyBox::addLabel(
} // namespace
AutoDownloadBox::AutoDownloadBox(QWidget *parent) {
}
void AutoDownloadBox::prepare() {
setupContent();
}
void AutoDownloadBox::setupContent() {
using namespace Settings;
using namespace Data::AutoDownload;
using Type = Data::AutoDownload::Type;
constexpr auto kLegacyLimit = 10 * 1024 * 1024;
setTitle(langFactory(lng_media_auto_title));
const auto settings = &Auth().settings().autoDownload();
const auto checked = [=](Source source, Type type) {
return (settings->bytesLimit(source, type) > 0);
};
auto wrap = object_ptr<Ui::VerticalLayout>(this);
const auto content = wrap.data();
setInnerWidget(object_ptr<Ui::OverrideMargins>(
this,
std::move(wrap)));
using pair = std::pair<Ui::Checkbox*, Ui::Checkbox*>;
const auto pairChecked = [](pair checkboxes) {
return std::make_pair(
checkboxes.first->checked(),
checkboxes.second->checked());
};
const auto enabledSomething = [=](
Type type,
std::pair<bool, bool> pair) {
return (!checked(Source::User, type) && pair.first)
|| (!checked(Source::Group, type) && pair.second);
};
const auto changedSomething = [=](
Type type,
std::pair<bool, bool> pair) {
return (checked(Source::User, type) != pair.first)
|| (checked(Source::Group, type) != pair.second);
};
const auto save = [=](
Type type,
std::pair<bool, bool> pair) {
const auto limit = [](bool checked) {
return checked ? kMaxBytesLimit : 0;
};
settings->setBytesLimit(Source::User, type, limit(pair.first));
settings->setBytesLimit(Source::Group, type, limit(pair.second));
settings->setBytesLimit(Source::Channel, type, limit(pair.second));
};
const auto addCheckbox = [&](Type type, Source source) {
const auto label = (source == Source::User)
? lng_media_auto_private_chats
: lng_media_auto_groups;
return content->add(
object_ptr<Ui::Checkbox>(
content,
lang(label),
checked(source, type),
st::settingsSendType),
st::settingsSendTypePadding);
};
const auto addPair = [&](Type type) {
const auto first = addCheckbox(type, Source::User);
const auto second = addCheckbox(type, Source::Group);
return pair(first, second);
};
AddSubsectionTitle(content, lng_media_photo_title);
const auto photo = addPair(Type::Photo);
AddSkip(content);
AddSkip(content);
AddSubsectionTitle(content, lng_media_audio_title);
const auto audio = addPair(Type::VoiceMessage);
AddSkip(content);
AddSkip(content);
AddSubsectionTitle(content, lng_media_gif_title);
const auto gif = addPair(Type::GIF);
AddSkip(content);
addButton(langFactory(lng_connection_save), [=] {
const auto photoChecked = pairChecked(photo);
const auto audioChecked = pairChecked(audio);
const auto gifChecked = pairChecked(gif);
const auto photosEnabled = enabledSomething(
Type::Photo,
photoChecked);
const auto audioEnabled = enabledSomething(
Type::VoiceMessage,
audioChecked);
const auto gifEnabled = enabledSomething(
Type::GIF,
gifChecked);
const auto photosChanged = changedSomething(
Type::Photo,
photoChecked);
const auto documentsChanged = changedSomething(
Type::VoiceMessage,
audioChecked) || changedSomething(Type::GIF, gifChecked);
if (photosChanged || documentsChanged) {
save(Type::Photo, photoChecked);
save(Type::VoiceMessage, audioChecked);
save(Type::GIF, gifChecked);
save(Type::VideoMessage, gifChecked);
Local::writeUserSettings();
}
if (photosEnabled) {
Auth().data().photoLoadSettingsChanged();
}
if (audioEnabled) {
Auth().data().voiceLoadSettingsChanged();
}
if (gifEnabled) {
Auth().data().animationLoadSettingsChanged();
}
closeBox();
});
addButton(langFactory(lng_cancel), [=] { closeBox(); });
setDimensionsToContent(st::boxWideWidth, content);
}
ProxiesBoxController::ProxiesBoxController()
: _saveTimer([] { Local::writeSettings(); }) {
_list = ranges::view::all(
@@ -1524,7 +1382,7 @@ void ProxiesBoxController::share(const ProxyData &proxy) {
? "&pass=" + qthelp::url_encode(proxy.password) : "")
+ ((proxy.type == Type::Mtproto && !proxy.password.isEmpty())
? "&secret=" + proxy.password : "");
Application::clipboard()->setText(link);
QApplication::clipboard()->setText(link);
Ui::Toast::Show(lang(lng_username_copied));
}

View File

@@ -22,18 +22,6 @@ template <typename Enum>
class Radioenum;
} // namespace Ui
class AutoDownloadBox : public BoxContent {
public:
AutoDownloadBox(QWidget *parent);
protected:
void prepare() override;
private:
void setupContent();
};
class ProxiesBoxController : public base::Subscriber {
public:
using Type = ProxyData::Type;