/* 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_chat.h" #include "settings/settings_common.h" #include "boxes/connection_box.h" #include "boxes/stickers_box.h" #include "boxes/background_box.h" #include "boxes/download_path_box.h" #include "boxes/local_storage_box.h" #include "ui/wrap/vertical_layout.h" #include "ui/wrap/slide_wrap.h" #include "ui/widgets/input_fields.h" #include "ui/widgets/checkbox.h" #include "ui/widgets/labels.h" #include "ui/effects/radial_animation.h" #include "ui/toast/toast.h" #include "ui/image/image.h" #include "lang/lang_keys.h" #include "window/themes/window_theme_editor.h" #include "window/themes/window_theme.h" #include "info/profile/info_profile_button.h" #include "storage/localstorage.h" #include "core/file_utilities.h" #include "data/data_session.h" #include "support/support_common.h" #include "support/support_templates.h" #include "auth_session.h" #include "mainwidget.h" #include "styles/style_settings.h" #include "styles/style_boxes.h" namespace Settings { class BackgroundRow : public Ui::RpWidget { public: BackgroundRow(QWidget *parent); protected: void paintEvent(QPaintEvent *e) override; int resizeGetHeight(int newWidth) override; private: void updateImage(); float64 radialProgress() const; bool radialLoading() const; QRect radialRect() const; void radialStart(); TimeMs radialTimeShift() const; void step_radial(TimeMs ms, bool timer); QPixmap _background; object_ptr _chooseFromGallery; object_ptr _chooseFromFile; Ui::RadialAnimation _radial; }; class DefaultTheme final : public Ui::AbstractCheckView { public: enum class Type { DayBlue, Default, Night, NightGreen, }; struct Scheme { Type type = Type(); QColor background; QColor sent; QColor received; QColor radiobuttonInactive; QColor radiobuttonActive; QString name; QString path; }; DefaultTheme(Scheme scheme, bool checked); QSize getSize() const override; void paint( Painter &p, int left, int top, int outerWidth, TimeMs ms) override; QImage prepareRippleMask() const override; bool checkRippleStartPosition(QPoint position) const override; private: void checkedChangedHook(anim::type animated) override; Scheme _scheme; Ui::RadioView _radio; }; void ChooseFromFile(not_null parent); BackgroundRow::BackgroundRow(QWidget *parent) : RpWidget(parent) , _chooseFromGallery( this, lang(lng_settings_bg_from_gallery), st::settingsLink) , _chooseFromFile(this, lang(lng_settings_bg_from_file), st::settingsLink) , _radial(animation(this, &BackgroundRow::step_radial)) { updateImage(); _chooseFromGallery->addClickHandler([] { Ui::show(Box()); }); _chooseFromFile->addClickHandler([=] { ChooseFromFile(this); }); using Update = const Window::Theme::BackgroundUpdate; base::ObservableViewer( *Window::Theme::Background() ) | rpl::filter([](const Update &update) { return (update.type == Update::Type::New || update.type == Update::Type::Start || update.type == Update::Type::Changed); }) | rpl::start_with_next([=] { updateImage(); }, lifetime()); } void BackgroundRow::paintEvent(QPaintEvent *e) { Painter p(this); bool radial = false; float64 radialOpacity = 0; if (_radial.animating()) { _radial.step(getms()); radial = _radial.animating(); radialOpacity = _radial.opacity(); } if (radial) { const auto backThumb = App::main()->newBackgroundThumb(); if (backThumb->isNull()) { p.drawPixmap(0, 0, _background); } else { const auto &pix = backThumb->pixBlurred( Data::FileOrigin(), st::settingsBackgroundThumb); const auto factor = cIntRetinaFactor(); p.drawPixmap( 0, 0, st::settingsBackgroundThumb, st::settingsBackgroundThumb, pix, 0, (pix.height() - st::settingsBackgroundThumb * factor) / 2, st::settingsBackgroundThumb * factor, st::settingsBackgroundThumb * factor); } const auto outer = radialRect(); const auto inner = QRect( QPoint( outer.x() + (outer.width() - st::radialSize.width()) / 2, outer.y() + (outer.height() - st::radialSize.height()) / 2), st::radialSize); p.setPen(Qt::NoPen); p.setOpacity(radialOpacity); p.setBrush(st::radialBg); { PainterHighQualityEnabler hq(p); p.drawEllipse(inner); } p.setOpacity(1); const auto arc = inner.marginsRemoved(QMargins( st::radialLine, st::radialLine, st::radialLine, st::radialLine)); _radial.draw(p, arc, st::radialLine, st::radialFg); } else { p.drawPixmap(0, 0, _background); } } int BackgroundRow::resizeGetHeight(int newWidth) { auto linkTop = st::settingsFromGalleryTop; auto linkLeft = st::settingsBackgroundThumb + st::settingsThumbSkip; auto linkWidth = newWidth - linkLeft; _chooseFromGallery->resizeToWidth( qMin(linkWidth, _chooseFromGallery->naturalWidth())); _chooseFromFile->resizeToWidth( qMin(linkWidth, _chooseFromFile->naturalWidth())); _chooseFromGallery->moveToLeft(linkLeft, linkTop, newWidth); linkTop += _chooseFromGallery->height() + st::settingsFromFileTop; _chooseFromFile->moveToLeft(linkLeft, linkTop, newWidth); return st::settingsBackgroundThumb; } float64 BackgroundRow::radialProgress() const { return App::main()->chatBackgroundProgress(); } bool BackgroundRow::radialLoading() const { const auto main = App::main(); if (main->chatBackgroundLoading()) { main->checkChatBackground(); if (main->chatBackgroundLoading()) { return true; } else { const_cast(this)->updateImage(); } } return false; } QRect BackgroundRow::radialRect() const { return QRect( 0, 0, st::settingsBackgroundThumb, st::settingsBackgroundThumb); } void BackgroundRow::radialStart() { if (radialLoading() && !_radial.animating()) { _radial.start(radialProgress()); if (const auto shift = radialTimeShift()) { _radial.update( radialProgress(), !radialLoading(), getms() + shift); } } } TimeMs BackgroundRow::radialTimeShift() const { return st::radialDuration; } void BackgroundRow::step_radial(TimeMs ms, bool timer) { const auto updated = _radial.update( radialProgress(), !radialLoading(), ms + radialTimeShift()); if (timer && _radial.animating() && (!anim::Disabled() || updated)) { rtlupdate(radialRect()); } } void BackgroundRow::updateImage() { int32 size = st::settingsBackgroundThumb * cIntRetinaFactor(); QImage back(size, size, QImage::Format_ARGB32_Premultiplied); back.setDevicePixelRatio(cRetinaFactor()); { Painter p(&back); PainterHighQualityEnabler hq(p); const auto &pix = Window::Theme::Background()->pixmap(); const auto sx = (pix.width() > pix.height()) ? ((pix.width() - pix.height()) / 2) : 0; const auto sy = (pix.height() > pix.width()) ? ((pix.height() - pix.width()) / 2) : 0; const auto s = (pix.width() > pix.height()) ? pix.height() : pix.width(); p.drawPixmap( 0, 0, st::settingsBackgroundThumb, st::settingsBackgroundThumb, pix, sx, sy, s, s); } Images::prepareRound(back, ImageRoundRadius::Small); _background = App::pixmapFromImageInPlace(std::move(back)); _background.setDevicePixelRatio(cRetinaFactor()); rtlupdate(radialRect()); if (radialLoading()) { radialStart(); } } DefaultTheme::DefaultTheme(Scheme scheme, bool checked) : AbstractCheckView(st::defaultRadio.duration, checked, nullptr) , _scheme(scheme) , _radio(st::defaultRadio, checked, [=] { update(); }) { _radio.setToggledOverride(_scheme.radiobuttonActive); _radio.setUntoggledOverride(_scheme.radiobuttonInactive); } QSize DefaultTheme::getSize() const { return st::settingsThemePreviewSize; } void DefaultTheme::paint( Painter &p, int left, int top, int outerWidth, TimeMs ms) { const auto received = QRect( st::settingsThemeBubblePosition, st::settingsThemeBubbleSize); const auto sent = QRect( outerWidth - received.width() - st::settingsThemeBubblePosition.x(), received.y() + received.height() + st::settingsThemeBubbleSkip, received.width(), received.height()); const auto radius = st::settingsThemeBubbleRadius; p.fillRect( QRect(QPoint(), st::settingsThemePreviewSize), _scheme.background); PainterHighQualityEnabler hq(p); p.setPen(Qt::NoPen); p.setBrush(_scheme.received); p.drawRoundedRect(rtlrect(received, outerWidth), radius, radius); p.setBrush(_scheme.sent); p.drawRoundedRect(rtlrect(sent, outerWidth), radius, radius); const auto radio = _radio.getSize(); _radio.paint( p, (outerWidth - radio.width()) / 2, getSize().height() - radio.height() - st::settingsThemeRadioBottom, outerWidth, getms()); } QImage DefaultTheme::prepareRippleMask() const { return QImage(); } bool DefaultTheme::checkRippleStartPosition(QPoint position) const { return false; } void DefaultTheme::checkedChangedHook(anim::type animated) { _radio.setChecked(checked(), animated); } void ChooseFromFile(not_null parent) { const auto imgExtensions = cImgExtensions(); auto filters = QStringList( qsl("Theme files (*.tdesktop-theme *.tdesktop-palette *") + imgExtensions.join(qsl(" *")) + qsl(")")); filters.push_back(FileDialog::AllFilesFilter()); const auto callback = [=](const FileDialog::OpenResult &result) { if (result.paths.isEmpty() && result.remoteContent.isEmpty()) { return; } if (!result.paths.isEmpty()) { const auto filePath = result.paths.front(); const auto hasExtension = [&](QLatin1String extension) { return filePath.endsWith(extension, Qt::CaseInsensitive); }; if (hasExtension(qstr(".tdesktop-theme")) || hasExtension(qstr(".tdesktop-palette"))) { Window::Theme::Apply(filePath); return; } } auto image = result.remoteContent.isEmpty() ? App::readImage(result.paths.front()) : App::readImage(result.remoteContent); if (image.isNull() || image.width() <= 0 || image.height() <= 0) { return; } else if (image.width() > 4096 * image.height()) { image = image.copy( (image.width() - 4096 * image.height()) / 2, 0, 4096 * image.height(), image.height()); } else if (image.height() > 4096 * image.width()) { image = image.copy( 0, (image.height() - 4096 * image.width()) / 2, image.width(), 4096 * image.width()); } Window::Theme::Background()->setImage( Window::Theme::kCustomBackground, std::move(image)); Window::Theme::Background()->setTile(false); }; FileDialog::GetOpenPath( parent.get(), lang(lng_choose_image), filters.join(qsl(";;")), crl::guard(parent, callback)); } QString DownloadPathText() { if (Global::DownloadPath().isEmpty()) { return lang(lng_download_path_default); } else if (Global::DownloadPath() == qsl("tmp")) { return lang(lng_download_path_temp); } return QDir::toNativeSeparators(Global::DownloadPath()); } void SetupStickersEmoji(not_null container) { AddDivider(container); AddSkip(container); AddSubsectionTitle(container, lng_settings_stickers_emoji); auto wrap = object_ptr(container); const auto inner = wrap.data(); container->add(object_ptr( container, std::move(wrap), QMargins(0, 0, 0, st::settingsCheckbox.margin.bottom()))); const auto checkbox = [&](LangKey label, bool checked) { return object_ptr( container, lang(label), checked, st::settingsCheckbox); }; const auto add = [&](LangKey label, bool checked, auto &&handle) { inner->add( checkbox(label, checked), st::settingsCheckboxPadding )->checkedChanges( ) | rpl::start_with_next( std::move(handle), inner->lifetime()); }; add( lng_settings_replace_emojis, Global::ReplaceEmoji(), [](bool checked) { Global::SetReplaceEmoji(checked); Global::RefReplaceEmojiChanged().notify(); Local::writeUserSettings(); }); add( lng_settings_suggest_emoji, Global::SuggestEmoji(), [](bool checked) { Global::SetSuggestEmoji(checked); Local::writeUserSettings(); }); add( lng_settings_suggest_by_emoji, Global::SuggestStickersByEmoji(), [](bool checked) { Global::SetSuggestStickersByEmoji(checked); Local::writeUserSettings(); }); AddButton( container, lng_stickers_you_have, st::settingsChatButton, &st::settingsIconStickers, st::settingsChatIconLeft )->addClickHandler([] { Ui::show(Box(StickersBox::Section::Installed)); }); AddSkip(container, st::settingsCheckboxesSkip); } void SetupMessages(not_null container) { AddDivider(container); AddSkip(container); AddSubsectionTitle(container, lng_settings_messages); AddSkip(container, st::settingsSendTypeSkip); using SendByType = Ui::InputSubmitSettings; const auto skip = st::settingsSendTypeSkip; auto wrap = object_ptr(container); const auto inner = wrap.data(); container->add( object_ptr( container, std::move(wrap), QMargins(0, skip, 0, skip))); const auto group = std::make_shared>( Auth().settings().sendSubmitWay()); const auto add = [&](SendByType value, LangKey key) { inner->add( object_ptr>( inner, group, value, lang(key), st::settingsSendType), st::settingsSendTypePadding); }; const auto small = st::settingsSendTypePadding; const auto top = skip; add(SendByType::Enter, lng_settings_send_enter); add( SendByType::CtrlEnter, ((cPlatform() == dbipMac || cPlatform() == dbipMacOld) ? lng_settings_send_cmdenter : lng_settings_send_ctrlenter)); group->setChangedCallback([](SendByType value) { Auth().settings().setSendSubmitWay(value); if (App::main()) { App::main()->ctrlEnterSubmitUpdated(); } Local::writeUserSettings(); }); AddSkip(inner, st::settingsCheckboxesSkip); } void SetupExport(not_null container) { AddButton( container, lng_settings_export_data, st::settingsButton )->addClickHandler([] { Ui::hideSettingsAndLayer(); App::CallDelayed( st::boxDuration, &Auth(), [] { Auth().data().startExport(); }); }); } void SetupLocalStorage(not_null container) { AddButton( container, lng_settings_manage_local_storage, st::settingsButton )->addClickHandler([] { LocalStorageBox::Show(&Auth().data().cache()); }); } void SetupDataStorage(not_null container) { using namespace rpl::mappers; AddDivider(container); AddSkip(container); AddSubsectionTitle(container, lng_settings_data_storage); const auto ask = AddButton( container, lng_download_path_ask, st::settingsButton )->toggleOn(rpl::single(Global::AskDownloadPath())); #ifndef OS_WIN_STORE const auto showpath = Ui::CreateChild>(ask); const auto path = container->add( object_ptr>( container, object_ptr