2023-02-21 12:31:55 +00:00
|
|
|
/*
|
|
|
|
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 "settings/settings_power_saving.h"
|
|
|
|
|
2023-02-27 14:09:12 +00:00
|
|
|
#include "base/battery_saving.h"
|
2023-02-21 12:31:55 +00:00
|
|
|
#include "boxes/peers/edit_peer_permissions_box.h"
|
|
|
|
#include "core/application.h"
|
2023-02-27 14:09:12 +00:00
|
|
|
#include "core/core_settings.h"
|
2023-02-21 12:31:55 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2023-02-25 12:17:48 +00:00
|
|
|
#include "settings/settings_common.h"
|
2023-02-21 12:31:55 +00:00
|
|
|
#include "ui/layers/generic_box.h"
|
2023-02-27 14:09:12 +00:00
|
|
|
#include "ui/toasts/common_toasts.h"
|
2023-02-25 12:17:48 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2023-02-21 12:31:55 +00:00
|
|
|
#include "ui/power_saving.h"
|
2023-02-25 12:17:48 +00:00
|
|
|
#include "styles/style_menu_icons.h"
|
|
|
|
#include "styles/style_layers.h"
|
|
|
|
#include "styles/style_settings.h"
|
2023-02-21 12:31:55 +00:00
|
|
|
|
|
|
|
namespace Settings {
|
2023-02-27 14:09:12 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kForceDisableTooltipDuration = 3 * crl::time(1000);
|
|
|
|
|
|
|
|
} // namespace
|
2023-02-21 12:31:55 +00:00
|
|
|
|
|
|
|
void PowerSavingBox(not_null<Ui::GenericBox*> box) {
|
2023-02-25 12:17:48 +00:00
|
|
|
box->setStyle(st::layerBox);
|
2023-02-21 12:31:55 +00:00
|
|
|
box->setTitle(tr::lng_settings_power_title());
|
2023-02-25 12:17:48 +00:00
|
|
|
box->setWidth(st::boxWideWidth);
|
|
|
|
|
|
|
|
const auto container = box->verticalLayout();
|
2023-02-27 14:09:12 +00:00
|
|
|
const auto ignore = Core::App().settings().ignoreBatterySaving();
|
|
|
|
const auto batterySaving = Core::App().batterySaving().enabled();
|
2023-02-25 12:17:48 +00:00
|
|
|
|
|
|
|
// Force top shadow visibility.
|
|
|
|
box->setPinnedToTopContent(
|
|
|
|
object_ptr<Ui::FixedHeightWidget>(box, st::lineWidth));
|
|
|
|
|
2023-02-27 14:09:12 +00:00
|
|
|
const auto subtitle = AddSubsectionTitle(
|
2023-02-25 12:17:48 +00:00
|
|
|
container,
|
|
|
|
tr::lng_settings_power_subtitle(),
|
|
|
|
st::powerSavingSubtitlePadding);
|
2023-02-21 12:31:55 +00:00
|
|
|
|
2023-02-27 14:09:12 +00:00
|
|
|
struct State {
|
|
|
|
rpl::variable<QString> forceDisabledMessage;
|
|
|
|
};
|
|
|
|
const auto state = container->lifetime().make_state<State>();
|
|
|
|
state->forceDisabledMessage = (batterySaving.value_or(false) && !ignore)
|
|
|
|
? tr::lng_settings_power_turn_off(tr::now)
|
|
|
|
: QString();
|
|
|
|
|
2023-02-21 12:31:55 +00:00
|
|
|
auto [checkboxes, getResult, changes] = CreateEditPowerSaving(
|
|
|
|
box,
|
2023-02-27 14:09:12 +00:00
|
|
|
PowerSaving::kAll & ~PowerSaving::Current(),
|
|
|
|
state->forceDisabledMessage.value());
|
2023-02-21 12:31:55 +00:00
|
|
|
|
2023-02-27 14:09:12 +00:00
|
|
|
const auto controlsRaw = checkboxes.data();
|
2023-02-21 12:31:55 +00:00
|
|
|
box->addRow(std::move(checkboxes), {});
|
|
|
|
|
2023-02-25 12:17:48 +00:00
|
|
|
auto automatic = (Ui::SettingsButton*)nullptr;
|
2023-02-27 14:09:12 +00:00
|
|
|
if (batterySaving.has_value()) {
|
2023-02-25 12:17:48 +00:00
|
|
|
AddSkip(container);
|
|
|
|
AddDivider(container);
|
|
|
|
AddSkip(container);
|
2023-02-27 14:09:12 +00:00
|
|
|
automatic = AddButton(
|
2023-02-25 12:17:48 +00:00
|
|
|
container,
|
|
|
|
tr::lng_settings_power_auto(),
|
|
|
|
st::powerSavingButtonNoIcon
|
2023-02-27 14:09:12 +00:00
|
|
|
)->toggleOn(rpl::single(!ignore));
|
2023-02-25 12:17:48 +00:00
|
|
|
AddSkip(container);
|
|
|
|
AddDividerText(container, tr::lng_settings_power_auto_about());
|
2023-02-27 14:09:12 +00:00
|
|
|
|
|
|
|
state->forceDisabledMessage = rpl::combine(
|
|
|
|
automatic->toggledValue(),
|
|
|
|
Core::App().batterySaving().value()
|
|
|
|
) | rpl::map([=](bool dontIgnore, bool saving) {
|
|
|
|
return (saving && dontIgnore)
|
|
|
|
? tr::lng_settings_power_turn_off()
|
|
|
|
: rpl::single(QString());
|
|
|
|
}) | rpl::flatten_latest();
|
|
|
|
|
|
|
|
const auto disabler = Ui::CreateChild<Ui::AbstractButton>(container.get());
|
|
|
|
disabler->setClickedCallback([=] {
|
|
|
|
Ui::ShowMultilineToast({
|
|
|
|
.parentOverride = container,
|
2023-02-27 18:05:10 +00:00
|
|
|
.text = { tr::lng_settings_power_turn_off(tr::now) },
|
2023-02-27 14:09:12 +00:00
|
|
|
.duration = kForceDisableTooltipDuration,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
disabler->paintRequest() | rpl::start_with_next([=](QRect clip) {
|
|
|
|
auto color = st::boxBg->c;
|
|
|
|
color.setAlpha(96);
|
|
|
|
QPainter(disabler).fillRect(clip, color);
|
|
|
|
}, disabler->lifetime());
|
|
|
|
rpl::combine(
|
|
|
|
subtitle->geometryValue(),
|
|
|
|
controlsRaw->geometryValue()
|
|
|
|
) | rpl::start_with_next([=](QRect subtitle, QRect controls) {
|
|
|
|
disabler->setGeometry(subtitle.united(controls));
|
|
|
|
}, disabler->lifetime());
|
|
|
|
disabler->showOn(state->forceDisabledMessage.value(
|
|
|
|
) | rpl::map([=](const QString &value) {
|
|
|
|
return !value.isEmpty();
|
|
|
|
}));
|
2023-02-25 12:17:48 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 17:38:54 +00:00
|
|
|
box->addButton(tr::lng_settings_save(), [=, collect = getResult] {
|
2023-02-27 14:09:12 +00:00
|
|
|
const auto ignore = automatic
|
|
|
|
? !automatic->toggled()
|
|
|
|
: Core::App().settings().ignoreBatterySaving();
|
|
|
|
const auto batterySaving = Core::App().batterySaving().enabled();
|
|
|
|
if (ignore || !batterySaving.value_or(false)) {
|
|
|
|
Set(PowerSaving::kAll & ~collect());
|
|
|
|
}
|
|
|
|
Core::App().settings().setIgnoreBatterySavingValue(ignore);
|
2023-02-21 12:31:55 +00:00
|
|
|
Core::App().saveSettingsDelayed();
|
|
|
|
box->closeBox();
|
|
|
|
});
|
|
|
|
box->addButton(tr::lng_cancel(), [=] { box->closeBox(); });
|
|
|
|
}
|
|
|
|
|
2023-02-25 12:17:48 +00:00
|
|
|
EditFlagsDescriptor<PowerSaving::Flags> PowerSavingLabels() {
|
2023-02-21 12:31:55 +00:00
|
|
|
using namespace PowerSaving;
|
2023-02-25 12:17:48 +00:00
|
|
|
using Label = EditFlagsLabel<Flags>;
|
|
|
|
|
2023-02-21 12:31:55 +00:00
|
|
|
auto stickers = std::vector<Label>{
|
2023-02-25 12:17:48 +00:00
|
|
|
{
|
|
|
|
kStickersPanel,
|
|
|
|
tr::lng_settings_power_stickers_panel(tr::now),
|
|
|
|
&st::menuIconStickers,
|
|
|
|
},
|
2023-02-21 12:31:55 +00:00
|
|
|
{ kStickersChat, tr::lng_settings_power_stickers_chat(tr::now) },
|
|
|
|
};
|
|
|
|
auto emoji = std::vector<Label>{
|
2023-02-25 12:17:48 +00:00
|
|
|
{
|
|
|
|
kEmojiPanel,
|
|
|
|
tr::lng_settings_power_emoji_panel(tr::now),
|
|
|
|
&st::menuIconEmoji,
|
|
|
|
},
|
2023-02-21 12:31:55 +00:00
|
|
|
{ kEmojiReactions, tr::lng_settings_power_emoji_reactions(tr::now) },
|
|
|
|
{ kEmojiChat, tr::lng_settings_power_emoji_chat(tr::now) },
|
2023-03-10 06:44:36 +00:00
|
|
|
{ kEmojiStatus, tr::lng_settings_power_emoji_status(tr::now) },
|
2023-02-21 12:31:55 +00:00
|
|
|
};
|
|
|
|
auto chat = std::vector<Label>{
|
2023-02-25 12:17:48 +00:00
|
|
|
{
|
|
|
|
kChatBackground,
|
|
|
|
tr::lng_settings_power_chat_background(tr::now),
|
|
|
|
&st::menuIconChatBubble,
|
|
|
|
},
|
2023-02-21 12:31:55 +00:00
|
|
|
{ kChatSpoiler, tr::lng_settings_power_chat_spoiler(tr::now) },
|
|
|
|
};
|
|
|
|
auto calls = std::vector<Label>{
|
2023-02-25 12:17:48 +00:00
|
|
|
{
|
|
|
|
kCalls,
|
|
|
|
tr::lng_settings_power_calls(tr::now),
|
|
|
|
&st::menuIconPhone,
|
|
|
|
},
|
2023-02-21 12:31:55 +00:00
|
|
|
};
|
|
|
|
auto animations = std::vector<Label>{
|
2023-02-25 12:17:48 +00:00
|
|
|
{
|
|
|
|
kAnimations,
|
|
|
|
tr::lng_settings_power_ui(tr::now),
|
|
|
|
&st::menuIconStartStream,
|
|
|
|
},
|
2023-02-21 12:31:55 +00:00
|
|
|
};
|
2023-02-25 12:17:48 +00:00
|
|
|
return { .labels = {
|
2023-02-21 12:31:55 +00:00
|
|
|
{ tr::lng_settings_power_stickers(), std::move(stickers) },
|
|
|
|
{ tr::lng_settings_power_emoji(), std::move(emoji) },
|
|
|
|
{ tr::lng_settings_power_chat(), std::move(chat) },
|
|
|
|
{ std::nullopt, std::move(calls) },
|
2023-02-25 12:17:48 +00:00
|
|
|
{ std::nullopt, std::move(animations), },
|
|
|
|
}, .st = &st::powerSavingButton };
|
2023-02-21 12:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Settings
|