diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index d2f259f7f5..4081dc951d 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -1051,6 +1051,8 @@ PRIVATE ui/search_field_controller.h ui/special_buttons.cpp ui/special_buttons.h + ui/text/format_song_document_name.cpp + ui/text/format_song_document_name.h ui/unread_badge.cpp ui/unread_badge.h window/main_window.cpp diff --git a/Telegram/SourceFiles/boxes/edit_caption_box.cpp b/Telegram/SourceFiles/boxes/edit_caption_box.cpp index fea44c1c95..e295a6e111 100644 --- a/Telegram/SourceFiles/boxes/edit_caption_box.cpp +++ b/Telegram/SourceFiles/boxes/edit_caption_box.cpp @@ -47,6 +47,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/input_fields.h" #include "ui/widgets/checkbox.h" #include "ui/widgets/checkbox.h" +#include "ui/text/format_song_document_name.h" #include "ui/text/format_values.h" #include "ui/text/text_options.h" #include "ui/chat/attach/attach_prepare.h" @@ -220,7 +221,7 @@ EditCaptionBox::EditCaptionBox( const auto document = _documentMedia->owner(); const auto nameString = document->isVoiceMessage() ? tr::lng_media_audio(tr::now) - : document->composeNameString(); + : Ui::Text::FormatSongNameFor(document).string(); setName(nameString, document->size); _isImage = document->isImage(); _isAudio = document->isVoiceMessage() @@ -549,10 +550,10 @@ void EditCaptionBox::updateEditPreview() { if (shouldAsDoc) { auto nameString = filename; if (const auto song = std::get_if(fileMedia)) { - nameString = Ui::ComposeNameString( + nameString = Ui::Text::FormatSongName( filename, song->title, - song->performer); + song->performer).string(); _isAudio = true; if (auto cover = song->cover; !cover.isNull()) { diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 7592eecd44..a687acc1fb 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -40,7 +40,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/confirm_box.h" #include "ui/image/image.h" #include "ui/text/text_utilities.h" -#include "ui/text/format_values.h" #include "base/base_file_utilities.h" #include "mainwindow.h" #include "core/application.h" @@ -1464,16 +1463,6 @@ uint8 DocumentData::cacheTag() const { return 0; } -QString DocumentData::composeNameString() const { - if (auto songData = song()) { - return Ui::ComposeNameString( - _filename, - songData->title, - songData->performer); - } - return Ui::ComposeNameString(_filename, QString(), QString()); -} - LocationType DocumentData::locationType() const { return isVoiceMessage() ? AudioFileLocation diff --git a/Telegram/SourceFiles/data/data_document.h b/Telegram/SourceFiles/data/data_document.h index 7ee523c0dd..42eb6c2bf0 100644 --- a/Telegram/SourceFiles/data/data_document.h +++ b/Telegram/SourceFiles/data/data_document.h @@ -228,8 +228,6 @@ public: [[nodiscard]] Storage::Cache::Key cacheKey() const; [[nodiscard]] uint8 cacheTag() const; - [[nodiscard]] QString composeNameString() const; - [[nodiscard]] bool canBeStreamed() const; [[nodiscard]] auto createStreamingLoader( Data::FileOrigin origin, diff --git a/Telegram/SourceFiles/data/data_media_types.cpp b/Telegram/SourceFiles/data/data_media_types.cpp index 61ecbd313a..6382e20ef1 100644 --- a/Telegram/SourceFiles/data/data_media_types.cpp +++ b/Telegram/SourceFiles/data/data_media_types.cpp @@ -26,6 +26,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/media/history_view_slot_machine.h" #include "history/view/media/history_view_dice.h" #include "ui/image/image.h" +#include "ui/text/format_song_document_name.h" #include "ui/text/format_values.h" #include "ui/text/text_options.h" #include "ui/text/text_utilities.h" @@ -506,6 +507,7 @@ QString MediaFile::chatListText() const { return Media::chatListText(); } const auto type = [&] { + using namespace Ui::Text; if (_document->isVideoMessage()) { return tr::lng_in_dlg_video_message(tr::now); } else if (_document->isAnimation()) { @@ -514,7 +516,7 @@ QString MediaFile::chatListText() const { return tr::lng_in_dlg_video(tr::now); } else if (_document->isVoiceMessage()) { return tr::lng_in_dlg_audio(tr::now); - } else if (const auto name = _document->composeNameString(); + } else if (const auto name = FormatSongNameFor(_document).string(); !name.isEmpty()) { return name; } else if (_document->isAudioFile()) { @@ -576,7 +578,7 @@ QString MediaFile::pinnedTextSubstring() const { TextForMimeData MediaFile::clipboardText() const { const auto attachType = [&] { - const auto name = _document->composeNameString(); + const auto name = Ui::Text::FormatSongNameFor(_document).string(); const auto addName = !name.isEmpty() ? qstr(" : ") + name : QString(); diff --git a/Telegram/SourceFiles/history/view/media/history_view_document.cpp b/Telegram/SourceFiles/history/view/media/history_view_document.cpp index 50629f724a..ede2ea5aed 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_document.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_document.cpp @@ -18,6 +18,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/media/history_view_media_common.h" #include "ui/image/image.h" #include "ui/text/format_values.h" +#include "ui/text/format_song_document_name.h" #include "ui/cached_round_corners.h" #include "ui/ui_utility.h" #include "layout.h" // FullSelection @@ -226,7 +227,7 @@ void Document::createComponents(bool caption) { void Document::fillNamedFromData(HistoryDocumentNamed *named) { const auto nameString = named->_name = CleanTagSymbols( - _data->composeNameString()); + Ui::Text::FormatSongNameFor(_data).string()); named->_namew = st::semiboldFont->width(nameString); } diff --git a/Telegram/SourceFiles/media/player/media_player_widget.cpp b/Telegram/SourceFiles/media/player/media_player_widget.cpp index 093e4154f6..020ddb064d 100644 --- a/Telegram/SourceFiles/media/player/media_player_widget.cpp +++ b/Telegram/SourceFiles/media/player/media_player_widget.cpp @@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/buttons.h" #include "ui/effects/ripple_animation.h" #include "ui/text/format_values.h" +#include "ui/text/format_song_document_name.h" #include "lang/lang_keys.h" #include "media/audio/media_audio.h" #include "media/view/media_view_playback_progress.h" @@ -565,26 +566,8 @@ void Widget::handleSongChange() { textWithEntities.text = tr::lng_media_audio(tr::now); } } else { - const auto song = document->song(); - if (!song || song->performer.isEmpty()) { - textWithEntities.text = (!song || song->title.isEmpty()) - ? (document->filename().isEmpty() - ? qsl("Unknown Track") - : document->filename()) - : song->title; - } else { - auto title = song->title.isEmpty() - ? qsl("Unknown Track") - : TextUtilities::Clean(song->title); - auto dash = QString::fromUtf8(" \xe2\x80\x93 "); - textWithEntities.text = song->performer + dash + title; - textWithEntities.entities.append({ - EntityType::Semibold, - 0, - song->performer.size(), - QString() - }); - } + textWithEntities = Ui::Text::FormatSongNameFor(document) + .textWithEntities(true); } _nameLabel->setMarkedText(textWithEntities); diff --git a/Telegram/SourceFiles/overview/overview_layout.cpp b/Telegram/SourceFiles/overview/overview_layout.cpp index 402bdcf7aa..bae062b053 100644 --- a/Telegram/SourceFiles/overview/overview_layout.cpp +++ b/Telegram/SourceFiles/overview/overview_layout.cpp @@ -38,6 +38,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/qt_adapters.h" #include "ui/effects/round_checkbox.h" #include "ui/image/image.h" +#include "ui/text/format_song_document_name.h" #include "ui/text/format_values.h" #include "ui/text/text_options.h" #include "ui/cached_round_corners.h" @@ -56,38 +57,6 @@ TextParseOptions _documentNameOptions = { Qt::LayoutDirectionAuto, // dir }; -TextWithEntities ComposeNameWithEntities(DocumentData *document) { - TextWithEntities result; - const auto song = document->song(); - if (!song || (song->title.isEmpty() && song->performer.isEmpty())) { - result.text = document->filename().isEmpty() - ? qsl("Unknown File") - : document->filename(); - result.entities.push_back({ - EntityType::Semibold, - 0, - result.text.size() - }); - } else if (song->performer.isEmpty()) { - result.text = song->title; - result.entities.push_back({ - EntityType::Semibold, - 0, - result.text.size() - }); - } else { - result.text = song->performer - + QString::fromUtf8(" \xe2\x80\x93 ") - + (song->title.isEmpty() ? qsl("Unknown Track") : song->title); - result.entities.push_back({ - EntityType::Semibold, - 0, - song->performer.size() - }); - } - return result; -} - } // namespace class Checkbox { @@ -935,7 +904,10 @@ Document::Document( , _date(langDateTime(base::unixtime::parse(_data->date))) , _datew(st::normalFont->width(_date)) , _colorIndex(documentColorIndex(_data, _ext)) { - _name.setMarkedText(st::defaultTextStyle, ComposeNameWithEntities(_data), _documentNameOptions); + _name.setMarkedText( + st::defaultTextStyle, + Ui::Text::FormatSongNameFor(_data).textWithEntities(), + _documentNameOptions); AddComponents(Info::Bit()); diff --git a/Telegram/SourceFiles/platform/linux/linux_mpris_support.cpp b/Telegram/SourceFiles/platform/linux/linux_mpris_support.cpp index 9cd3824257..59031d38db 100644 --- a/Telegram/SourceFiles/platform/linux/linux_mpris_support.cpp +++ b/Telegram/SourceFiles/platform/linux/linux_mpris_support.cpp @@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "mainwindow.h" #include "mainwidget.h" +#include "ui/text/format_song_document_name.h" #include #include @@ -108,30 +109,21 @@ auto CreateMetadata( kFakeTrackPath.utf8().constData())); result["mpris:length"] = Glib::Variant::create( state.length * 1000); - result["xesam:title"] = Glib::Variant::create( - "Unknown Track"); - const auto audioData = state.id.audio(); + const auto audioData = state.id.audio() + ? state.id.audio() + : trackView + ? trackView->owner().get() + : nullptr; if (audioData) { - if (!audioData->filename().isEmpty()) { - result["xesam:title"] = Glib::Variant< - Glib::ustring - >::create(audioData->filename().toStdString()); - } - - if (audioData->isSong()) { - const auto songData = audioData->song(); - if (!songData->performer.isEmpty()) { - result["xesam:artist"] = Glib::Variant< - std::vector - >::create({ songData->performer.toStdString() }); - } - if (!songData->title.isEmpty()) { - result["xesam:title"] = Glib::Variant< - Glib::ustring - >::create(songData->title.toStdString()); - } - } + const auto &[title, performer] = + Ui::Text::FormatSongNameFor(audioData).composedName(); + result["xesam:title"] = Glib::Variant< + Glib::ustring + >::create(title.toStdString()); + result["xesam:artist"] = Glib::Variant< + std::vector + >::create({ performer.toStdString() }); } if (trackView) { diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_single_file_preview.cpp b/Telegram/SourceFiles/ui/chat/attach/attach_single_file_preview.cpp index 1c93cdc896..1d04b85784 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_single_file_preview.cpp +++ b/Telegram/SourceFiles/ui/chat/attach/attach_single_file_preview.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/chat/attach/attach_single_file_preview.h" #include "ui/chat/attach/attach_prepare.h" +#include "ui/text/format_song_name.h" #include "ui/text/format_values.h" #include "ui/text/text_options.h" #include "ui/widgets/buttons.h" @@ -115,7 +116,8 @@ void SingleFilePreview::preparePreview(const PreparedFile &file) { } } - _name = ComposeNameString(filename, songTitle, songPerformer); + _name = Text::FormatSongName(filename, songTitle, songPerformer) + .string(); _statusText = FormatSizeText(fileinfo.size()); } const auto &st = !isThumbedLayout() diff --git a/Telegram/SourceFiles/ui/text/format_song_document_name.cpp b/Telegram/SourceFiles/ui/text/format_song_document_name.cpp new file mode 100644 index 0000000000..8324585da1 --- /dev/null +++ b/Telegram/SourceFiles/ui/text/format_song_document_name.cpp @@ -0,0 +1,23 @@ +/* +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 "ui/text/format_song_document_name.h" + +#include "data/data_document.h" + +namespace Ui::Text { + +FormatSongName FormatSongNameFor(not_null document) { + const auto song = document->song(); + + return FormatSongName( + document->filename(), + song ? song->title : QString(), + song ? song->performer : QString()); +} + +} // namespace Ui::Text diff --git a/Telegram/SourceFiles/ui/text/format_song_document_name.h b/Telegram/SourceFiles/ui/text/format_song_document_name.h new file mode 100644 index 0000000000..3318ed95de --- /dev/null +++ b/Telegram/SourceFiles/ui/text/format_song_document_name.h @@ -0,0 +1,19 @@ +/* +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 "ui/text/format_song_name.h" + +class DocumentData; + +namespace Ui::Text { + +[[nodiscard]] FormatSongName FormatSongNameFor( + not_null document); + +} // namespace Ui::Text diff --git a/Telegram/SourceFiles/ui/text/format_song_name.cpp b/Telegram/SourceFiles/ui/text/format_song_name.cpp new file mode 100644 index 0000000000..388112e1c2 --- /dev/null +++ b/Telegram/SourceFiles/ui/text/format_song_name.cpp @@ -0,0 +1,76 @@ +/* +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 "ui/text/format_song_name.h" + +namespace Ui::Text { + +namespace { + +FormatSongName::ComposedName ComputeComposedName( + const QString &filename, + const QString &songTitle, + const QString &songPerformer) { + const auto unknown = u"Unknown Track"_q; + if (songTitle.isEmpty() && songPerformer.isEmpty()) { + return { + .title = filename.isEmpty() ? unknown : filename, + .performer = QString(), + }; + } + + if (songPerformer.isEmpty()) { + return { + .title = songTitle, + .performer = QString(), + }; + } + + return { + .title = (songTitle.isEmpty() ? unknown : songTitle), + .performer = songPerformer, + }; +} + +} // namespace + +FormatSongName::FormatSongName( + const QString &filename, + const QString &songTitle, + const QString &songPerformer) +: _composedName(ComputeComposedName(filename, songTitle, songPerformer)) { +} + +FormatSongName::ComposedName FormatSongName::composedName() const { + return _composedName; +} + +QString FormatSongName::string() const { + const auto &[title, performer] = _composedName; + const auto dash = (title.isEmpty() || performer.isEmpty()) + ? QString() + : QString::fromUtf8(" \xe2\x80\x93 "); + return performer + dash + title; +} + +TextWithEntities FormatSongName::textWithEntities( + bool boldOnlyPerformer) const { + TextWithEntities result; + result.text = string(); + if (!boldOnlyPerformer || !_composedName.performer.isEmpty()) { + result.entities.push_back({ + EntityType::Semibold, + 0, + _composedName.performer.isEmpty() + ? result.text.size() + : _composedName.performer.size(), + }); + } + return result; +} + +} // namespace Ui::Text diff --git a/Telegram/SourceFiles/ui/text/format_song_name.h b/Telegram/SourceFiles/ui/text/format_song_name.h new file mode 100644 index 0000000000..c87f100c61 --- /dev/null +++ b/Telegram/SourceFiles/ui/text/format_song_name.h @@ -0,0 +1,34 @@ +/* +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 + +namespace Ui::Text { + +class FormatSongName final { +public: + struct ComposedName { + QString title; + QString performer; + }; + + FormatSongName( + const QString &filename, + const QString &songTitle, + const QString &songPerformer); + + [[nodiscard]] ComposedName composedName() const; + [[nodiscard]] QString string() const; + [[nodiscard]] TextWithEntities textWithEntities( + bool boldOnlyPerformer = false) const; + +private: + const ComposedName _composedName; + +}; + +} // namespace Ui::Text diff --git a/Telegram/SourceFiles/ui/text/format_values.cpp b/Telegram/SourceFiles/ui/text/format_values.cpp index 6e3bd7722e..d34d926254 100644 --- a/Telegram/SourceFiles/ui/text/format_values.cpp +++ b/Telegram/SourceFiles/ui/text/format_values.cpp @@ -363,20 +363,4 @@ CurrencyRule LookupCurrencyRule(const QString ¤cy) { return result; } -QString ComposeNameString( - const QString &filename, - const QString &songTitle, - const QString &songPerformer) { - if (songTitle.isEmpty() && songPerformer.isEmpty()) { - return filename.isEmpty() ? u"Unknown File"_q : filename; - } - - if (songPerformer.isEmpty()) { - return songTitle; - } - - auto trackTitle = (songTitle.isEmpty() ? u"Unknown Track"_q : songTitle); - return songPerformer + QString::fromUtf8(" \xe2\x80\x93 ") + trackTitle; -} - } // namespace Ui diff --git a/Telegram/SourceFiles/ui/text/format_values.h b/Telegram/SourceFiles/ui/text/format_values.h index 2886dc0c98..ea443fb986 100644 --- a/Telegram/SourceFiles/ui/text/format_values.h +++ b/Telegram/SourceFiles/ui/text/format_values.h @@ -44,9 +44,4 @@ struct CurrencyRule { char decimal, char thousands); -[[nodiscard]] QString ComposeNameString( - const QString &filename, - const QString &songTitle, - const QString &songPerformer); - } // namespace Ui diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index 28d27d2dfd..3ecc52983b 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -137,6 +137,8 @@ PRIVATE ui/controls/invite_link_label.h ui/controls/send_button.cpp ui/controls/send_button.h + ui/text/format_song_name.cpp + ui/text/format_song_name.h ui/text/format_values.cpp ui/text/format_values.h ui/text/text_options.cpp