diff --git a/Telegram/SourceFiles/core/core_settings.cpp b/Telegram/SourceFiles/core/core_settings.cpp index efd0ba4d5..16b2595de 100644 --- a/Telegram/SourceFiles/core/core_settings.cpp +++ b/Telegram/SourceFiles/core/core_settings.cpp @@ -220,7 +220,7 @@ QByteArray Settings::serialize() const { + Serialize::bytearraySize(ivPosition) + Serialize::stringSize(noWarningExtensions) + Serialize::stringSize(_customFontFamily) - + sizeof(qint32) * 2; + + sizeof(qint32) * 3; auto result = QByteArray(); result.reserve(size); @@ -372,7 +372,8 @@ QByteArray Settings::serialize() const { qRound(_dialogsNoChatWidthRatio.current() * 1000000), 0, 1000000)) - << qint32(_systemUnlockEnabled ? 1 : 0); + << qint32(_systemUnlockEnabled ? 1 : 0) + << qint32(!_weatherInCelsius ? 0 : *_weatherInCelsius ? 1 : 2); } Ensures(result.size() == size); @@ -493,6 +494,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) { QByteArray ivPosition; QString customFontFamily = _customFontFamily; qint32 systemUnlockEnabled = _systemUnlockEnabled ? 1 : 0; + qint32 weatherInCelsius = !_weatherInCelsius ? 0 : *_weatherInCelsius ? 1 : 2; stream >> themesAccentColors; if (!stream.atEnd()) { @@ -793,6 +795,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) { if (!stream.atEnd()) { stream >> systemUnlockEnabled; } + if (!stream.atEnd()) { + stream >> weatherInCelsius; + } if (stream.status() != QDataStream::Ok) { LOG(("App Error: " "Bad data for Core::Settings::constructFromSerialized()")); @@ -1001,6 +1006,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) { } _customFontFamily = customFontFamily; _systemUnlockEnabled = (systemUnlockEnabled == 1); + _weatherInCelsius = !weatherInCelsius + ? std::optional() + : (weatherInCelsius == 1); } QString Settings::getSoundPath(const QString &key) const { diff --git a/Telegram/SourceFiles/core/core_settings.h b/Telegram/SourceFiles/core/core_settings.h index 3412c4588..0ac4e94a5 100644 --- a/Telegram/SourceFiles/core/core_settings.h +++ b/Telegram/SourceFiles/core/core_settings.h @@ -891,6 +891,13 @@ public: _systemUnlockEnabled = enabled; } + [[nodiscard]] std::optional weatherInCelsius() const { + return _weatherInCelsius; + } + void setWeatherInCelsius(bool value) { + _weatherInCelsius = value; + } + [[nodiscard]] static bool ThirdColumnByDefault(); [[nodiscard]] static float64 DefaultDialogsWidthRatio(); @@ -1022,6 +1029,7 @@ private: WindowPosition _ivPosition; QString _customFontFamily; bool _systemUnlockEnabled = false; + std::optional _weatherInCelsius; bool _tabbedReplacedWithInfo = false; // per-window rpl::event_stream _tabbedReplacedWithInfoValue; // per-window diff --git a/Telegram/SourceFiles/media/stories/media_stories_controller.cpp b/Telegram/SourceFiles/media/stories/media_stories_controller.cpp index ca558abfd..fc75e366d 100644 --- a/Telegram/SourceFiles/media/stories/media_stories_controller.cpp +++ b/Telegram/SourceFiles/media/stories/media_stories_controller.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "media/stories/media_stories_controller.h" +#include "base/platform/base_platform_info.h" #include "base/power_save_blocker.h" #include "base/qt_signal_producer.h" #include "base/unixtime.h" @@ -128,6 +129,13 @@ struct SameDayRange { int(base::SafeRound(asin * point.x() + acos * point.y()))); } +[[nodiscard]] bool ResolveWeatherInCelsius() { + const auto saved = Core::App().settings().weatherInCelsius(); + return saved.value_or(!ranges::contains( + std::array{ u"US"_q, u"BS"_q, u"KY"_q, u"LR"_q, u"BZ"_q }, + Platform::SystemCountry().toUpper())); +} + } // namespace class Controller::PhotoPlayback final { @@ -284,7 +292,8 @@ Controller::Controller(not_null delegate) , _slider(std::make_unique(this)) , _replyArea(std::make_unique(this)) , _reactions(std::make_unique(this)) -, _recentViews(std::make_unique(this)) { +, _recentViews(std::make_unique(this)) +, _weatherInCelsius(ResolveWeatherInCelsius()){ initLayout(); using namespace rpl::mappers; @@ -1272,16 +1281,16 @@ ClickHandlerPtr Controller::lookupAreaHandler(QPoint point) const { }); } for (const auto &weather : _weatherAreas) { - auto widget = _reactions->makeWeatherAreaWidget(weather); - const auto raw = widget.get(); _areas.push_back({ .original = weather.area.geometry, .radiusOriginal = weather.area.radius, .rotation = weather.area.rotation, .handler = std::make_shared([=] { - raw->toggleMode(); + toggleWeatherMode(); }), - .view = std::move(widget), + .view = _reactions->makeWeatherAreaWidget( + weather, + _weatherInCelsius.value()), }); } rebuildActiveAreas(*layout); @@ -1300,6 +1309,13 @@ ClickHandlerPtr Controller::lookupAreaHandler(QPoint point) const { return nullptr; } +void Controller::toggleWeatherMode() const { + const auto now = !_weatherInCelsius.current(); + Core::App().settings().setWeatherInCelsius(now); + Core::App().saveSettingsDelayed(); + _weatherInCelsius = now; +} + void Controller::maybeMarkAsRead(const Player::TrackState &state) { const auto length = state.length; const auto position = Player::IsStoppedAtEnd(state.state) diff --git a/Telegram/SourceFiles/media/stories/media_stories_controller.h b/Telegram/SourceFiles/media/stories/media_stories_controller.h index 590dee3f6..860908b96 100644 --- a/Telegram/SourceFiles/media/stories/media_stories_controller.h +++ b/Telegram/SourceFiles/media/stories/media_stories_controller.h @@ -229,6 +229,7 @@ private: void updatePlayingAllowed(); void setPlayingAllowed(bool allowed); void rebuildActiveAreas(const Layout &layout) const; + void toggleWeatherMode() const; void hideSiblings(); void showSiblings(not_null session); @@ -307,6 +308,7 @@ private: std::vector _urlAreas; std::vector _weatherAreas; mutable std::vector _areas; + mutable rpl::variable _weatherInCelsius; std::vector _cachedSourcesList; int _cachedSourceIndex = -1; diff --git a/Telegram/SourceFiles/media/stories/media_stories_reactions.cpp b/Telegram/SourceFiles/media/stories/media_stories_reactions.cpp index f5e2582ef..a8e044cc9 100644 --- a/Telegram/SourceFiles/media/stories/media_stories_reactions.cpp +++ b/Telegram/SourceFiles/media/stories/media_stories_reactions.cpp @@ -75,7 +75,6 @@ public: void setAreaGeometry(QRect geometry, float64 radius) override; void updateReactionsCount(int count) override; void playEffect() override; - void toggleMode() override; bool contains(QPoint point) override; private: @@ -137,12 +136,12 @@ public: WeatherView( QWidget *parent, not_null session, - const Data::WeatherArea &data); + const Data::WeatherArea &data, + rpl::producer weatherInCelsius); void setAreaGeometry(QRect geometry, float64 radius) override; void updateReactionsCount(int count) override; void playEffect() override; - void toggleMode() override; bool contains(QPoint point) override; private: @@ -349,10 +348,6 @@ void ReactionView::playEffect() { } } -void ReactionView::toggleMode() { - Unexpected("ReactionView::toggleMode."); -} - bool ReactionView::contains(QPoint point) { const auto circle = _apiGeometry; const auto radius = std::min(circle.width(), circle.height()) / 2; @@ -537,7 +532,8 @@ void ReactionView::cacheBackground() { WeatherView::WeatherView( QWidget *parent, not_null session, - const Data::WeatherArea &data) + const Data::WeatherArea &data, + rpl::producer weatherInCelsius) : RpWidget(parent) , _session(session) , _data(data) @@ -546,6 +542,12 @@ WeatherView::WeatherView( watchForSticker(); setAttribute(Qt::WA_TransparentForMouseEvents); show(); + + std::move(weatherInCelsius) | rpl::start_with_next([=](bool celsius) { + _celsius = celsius; + _background = {}; + update(); + }, lifetime()); } void WeatherView::watchForSticker() { @@ -565,7 +567,7 @@ void WeatherView::watchForSticker() { ) | rpl::start_with_next([=](not_null document) { setStickerFrom(document); update(); - }, _lifetime); + }, lifetime()); } } @@ -598,12 +600,6 @@ void WeatherView::playEffect() { Unexpected("WeatherView::playEffect."); } -void WeatherView::toggleMode() { - _celsius = !_celsius; - _background = {}; - update(); -} - bool WeatherView::contains(QPoint point) { const auto geometry = _rect.translated(pos()).toRect(); const auto angle = -_data.area.rotation; @@ -683,7 +679,7 @@ void WeatherView::setStickerFrom(not_null document) { } _sticker->setRepaintCallback([=] { update(); }); update(); - }, _lifetime); + }, lifetime()); } void WeatherView::cacheBackground() { @@ -1087,12 +1083,15 @@ auto Reactions::makeSuggestedReactionWidget( reaction); } -auto Reactions::makeWeatherAreaWidget(const Data::WeatherArea &data) +auto Reactions::makeWeatherAreaWidget( + const Data::WeatherArea &data, + rpl::producer weatherInCelsius) -> std::unique_ptr { return std::make_unique( _controller->wrap(), &_controller->uiShow()->session(), - data); + data, + std::move(weatherInCelsius)); } void Reactions::setReplyFieldState( diff --git a/Telegram/SourceFiles/media/stories/media_stories_reactions.h b/Telegram/SourceFiles/media/stories/media_stories_reactions.h index b17e2e19d..11da4c456 100644 --- a/Telegram/SourceFiles/media/stories/media_stories_reactions.h +++ b/Telegram/SourceFiles/media/stories/media_stories_reactions.h @@ -49,7 +49,6 @@ public: virtual void setAreaGeometry(QRect geometry, float64 radius) = 0; virtual void updateReactionsCount(int count) = 0; virtual void playEffect() = 0; - virtual void toggleMode() = 0; virtual bool contains(QPoint point) = 0; }; @@ -83,7 +82,9 @@ public: [[nodiscard]] auto makeSuggestedReactionWidget( const Data::SuggestedReaction &reaction) -> std::unique_ptr; - [[nodiscard]] auto makeWeatherAreaWidget(const Data::WeatherArea &data) + [[nodiscard]] auto makeWeatherAreaWidget( + const Data::WeatherArea &data, + rpl::producer weatherInCelsius) -> std::unique_ptr; void setReplyFieldState(