diff --git a/Telegram/SourceFiles/core/click_handler_types.h b/Telegram/SourceFiles/core/click_handler_types.h index 69960c408..9157b1783 100644 --- a/Telegram/SourceFiles/core/click_handler_types.h +++ b/Telegram/SourceFiles/core/click_handler_types.h @@ -15,6 +15,7 @@ constexpr auto kPhotoLinkMediaProperty = 0x02; constexpr auto kDocumentLinkMediaProperty = 0x03; constexpr auto kSendReactionEmojiProperty = 0x04; constexpr auto kReactionsCountEmojiProperty = 0x05; +constexpr auto kDocumentFilenameTooltipProperty = 0x06; namespace Ui { class Show; diff --git a/Telegram/SourceFiles/data/data_file_click_handler.cpp b/Telegram/SourceFiles/data/data_file_click_handler.cpp index 87bf28392..d51618481 100644 --- a/Telegram/SourceFiles/data/data_file_click_handler.cpp +++ b/Telegram/SourceFiles/data/data_file_click_handler.cpp @@ -53,6 +53,10 @@ DocumentClickHandler::DocumentClickHandler( reinterpret_cast(_document.get())); } +QString DocumentClickHandler::tooltip() const { + return property(kDocumentFilenameTooltipProperty).value(); +} + DocumentOpenClickHandler::DocumentOpenClickHandler( not_null document, Fn &&callback, diff --git a/Telegram/SourceFiles/data/data_file_click_handler.h b/Telegram/SourceFiles/data/data_file_click_handler.h index 459ebddfe..472d04d15 100644 --- a/Telegram/SourceFiles/data/data_file_click_handler.h +++ b/Telegram/SourceFiles/data/data_file_click_handler.h @@ -33,6 +33,8 @@ public: not_null document, FullMsgId context = FullMsgId()); + QString tooltip() const override; + [[nodiscard]] not_null document() const; private: diff --git a/Telegram/SourceFiles/history/view/media/history_view_document.cpp b/Telegram/SourceFiles/history/view/media/history_view_document.cpp index f2e2e3f81..2134e9b56 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_document.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_document.cpp @@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "media/player/media_player_instance.h" #include "history/history_item_components.h" #include "history/history.h" +#include "core/click_handler_types.h" // kDocumentFilenameTooltipProperty. #include "history/view/history_view_element.h" #include "history/view/history_view_cursor_state.h" #include "history/view/history_view_transcribe_button.h" @@ -178,6 +179,7 @@ Document::Document( createComponents(!caption.isEmpty()); if (const auto named = Get()) { fillNamedFromData(named); + _tooltipFilename.setTooltipText(named->name); } setDocumentLinks(_data, realParent); @@ -647,11 +649,13 @@ void Document::draw( } else if (auto named = Get()) { p.setFont(st::semiboldFont); p.setPen(stm->historyFileNameFg); - if (namewidth < named->namew) { + const auto elided = (namewidth < named->namew); + if (elided) { p.drawTextLeft(nameleft, nametop, width, st::semiboldFont->elided(named->name, namewidth, Qt::ElideMiddle)); } else { p.drawTextLeft(nameleft, nametop, width, named->name, named->namew); } + _tooltipFilename.setElided(elided); } auto statusText = voiceStatusOverride.isEmpty() ? _statusText : voiceStatusOverride; @@ -926,11 +930,15 @@ TextState Document::textState( const auto nameleft = st.padding.left() + st.thumbSize + st.thumbSkip; const auto nametop = st.nameTop - topMinus; const auto nameright = st.padding.right(); + auto namewidth = width - nameleft - nameright; const auto linktop = st.linkTop - topMinus; auto bottom = st.padding.top() + st.thumbSize + st.padding.bottom() - topMinus; const auto rthumb = style::rtlrect(st.padding.left(), st.padding.top() - topMinus, st.thumbSize, st.thumbSize, width); const auto innerSize = st::msgFileLayout.thumbSize; const auto inner = QRect(rthumb.x() + (rthumb.width() - innerSize) / 2, rthumb.y() + (rthumb.height() - innerSize) / 2, innerSize, innerSize); + + const auto filenameMoused = QRect(nameleft, nametop, namewidth, st::semiboldFont->height).contains(point); + _tooltipFilename.setMoused(filenameMoused); if (const auto thumbed = Get()) { if ((_data->loading() || _data->uploading()) && rthumb.contains(point)) { result.link = _cancell; @@ -958,7 +966,6 @@ TextState Document::textState( } const auto voice = Get(); - auto namewidth = width - nameleft - nameright; auto transcribeLength = 0; auto transcribeHeight = 0; auto painth = layout.height(); @@ -1035,8 +1042,10 @@ TextState Document::textState( } else { result.link = _savel; } + _tooltipFilename.updateTooltipForLink(result.link.get()); return result; } + _tooltipFilename.updateTooltipForState(result); return result; } @@ -1388,6 +1397,48 @@ Ui::Text::String Document::createCaption() { return File::createCaption(_realParent); } +void Document::TooltipFilename::setElided(bool value) { + if (_elided != value) { + _elided = value; + _stale = true; + } +} + +void Document::TooltipFilename::setMoused(bool value) { + if (_moused != value) { + _moused = value; + _stale = true; + } +} + +void Document::TooltipFilename::setTooltipText(QString text) { + if (_tooltip != text) { + _tooltip = text; + _stale = true; + } +} + +void Document::TooltipFilename::updateTooltipForLink(ClickHandler *link) { + if (_lastLink != link) { + _lastLink = link; + _stale = true; + } + if (_stale && link) { + _stale = false; + link->setProperty( + kDocumentFilenameTooltipProperty, + (_elided && _moused) ? _tooltip : QString()); + } +} + +void Document::TooltipFilename::updateTooltipForState( + TextState &state) const { + if (_elided && _moused) { + state.customTooltip = true; + state.customTooltipText = _tooltip; + } +} + bool DrawThumbnailAsSongCover( Painter &p, const style::color &colored, diff --git a/Telegram/SourceFiles/history/view/media/history_view_document.h b/Telegram/SourceFiles/history/view/media/history_view_document.h index 643ba24fd..fc9856a6d 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_document.h +++ b/Telegram/SourceFiles/history/view/media/history_view_document.h @@ -157,6 +157,23 @@ private: mutable QImage _iconCache; mutable QImage _cornerDownloadCache; + class TooltipFilename { + public: + void setElided(bool value); + void setMoused(bool value); + void setTooltipText(QString text); + void updateTooltipForLink(ClickHandler *link); + void updateTooltipForState(TextState &state) const; + private: + ClickHandler *_lastLink = nullptr; + bool _elided = false; + bool _moused = false; + bool _stale = false; + QString _tooltip; + }; + + mutable TooltipFilename _tooltipFilename; + }; bool DrawThumbnailAsSongCover(