Skip to content

Commit 2bafd7f

Browse files
committed
Show full title tooltip on hover when chat name is truncated
When the chat title in the top bar is too long and gets elided, hovering over it now shows a tooltip with the full name. This works for both regular chats and forum topics. Fixes #1502
1 parent af55019 commit 2bafd7f

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,57 @@ void TopBarWidget::mousePressEvent(QMouseEvent *e) {
801801
}
802802
}
803803

804+
bool TopBarWidget::event(QEvent *e) {
805+
if (e->type() == QEvent::ToolTip) {
806+
const auto pos = static_cast<QHelpEvent*>(e)->pos();
807+
const auto nametop = st::topBarArrowPadding.top();
808+
const auto nameheight = st::msgNameStyle.font->height;
809+
const auto nameleft = _leftTaken;
810+
const auto namewidth = width()
811+
- _rightTaken
812+
- nameleft
813+
- st::topBarNameRightPadding;
814+
const auto nameRect = QRect(
815+
nameleft,
816+
nametop,
817+
namewidth,
818+
nameheight);
819+
_tooltipText = QString();
820+
if (nameRect.contains(pos)) {
821+
const auto topic = _activeChat.key.topic();
822+
if (topic
823+
&& _activeChat.section == Section::Replies) {
824+
const auto &topicName = topic->chatListNameText();
825+
if (topicName.maxWidth() > namewidth) {
826+
_tooltipText = topicName.toString();
827+
}
828+
} else if (!_title.isEmpty()
829+
&& _title.maxWidth() > namewidth) {
830+
_tooltipText = _title.toString();
831+
}
832+
}
833+
if (_tooltipText.isEmpty()) {
834+
Ui::Tooltip::Hide();
835+
} else {
836+
Ui::Tooltip::Show(0, this);
837+
}
838+
return true;
839+
}
840+
return RpWidget::event(e);
841+
}
842+
843+
QString TopBarWidget::tooltipText() const {
844+
return _tooltipText;
845+
}
846+
847+
QPoint TopBarWidget::tooltipPos() const {
848+
return QCursor::pos();
849+
}
850+
851+
bool TopBarWidget::tooltipWindowActive() const {
852+
return Ui::AppInFocus() && Ui::InFocusChain(window());
853+
}
854+
804855
void TopBarWidget::infoClicked() {
805856
const auto key = _activeChat.key;
806857
if (!key) {

Telegram/SourceFiles/history/view/history_view_top_bar_widget.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ For license and copyright information please follow this link:
99

1010
#include "ui/rp_widget.h"
1111
#include "ui/unread_badge.h"
12+
#include "ui/widgets/tooltip.h"
1213
#include "ui/effects/animations.h"
1314
#include "base/timer.h"
1415
#include "base/object_ptr.h"
@@ -46,7 +47,9 @@ class SendActionPainter;
4647

4748
[[nodiscard]] QString SwitchToChooseFromQuery();
4849

49-
class TopBarWidget final : public Ui::RpWidget {
50+
class TopBarWidget final
51+
: public Ui::RpWidget
52+
, private Ui::AbstractTooltipShower {
5053
public:
5154
struct SelectedState {
5255
bool textSelected = false;
@@ -129,6 +132,7 @@ class TopBarWidget final : public Ui::RpWidget {
129132
void paintEvent(QPaintEvent *e) override;
130133
void mousePressEvent(QMouseEvent *e) override;
131134
void resizeEvent(QResizeEvent *e) override;
135+
bool event(QEvent *e) override;
132136
bool eventFilter(QObject *obj, QEvent *e) override;
133137

134138
int resizeGetHeight(int newWidth) override;
@@ -185,6 +189,10 @@ class TopBarWidget final : public Ui::RpWidget {
185189
void updateOnlineDisplayTimer();
186190
void updateOnlineDisplayIn(crl::time timeout);
187191

192+
QString tooltipText() const override;
193+
QPoint tooltipPos() const override;
194+
bool tooltipWindowActive() const override;
195+
188196
void infoClicked();
189197
void backClicked();
190198

@@ -266,6 +274,8 @@ class TopBarWidget final : public Ui::RpWidget {
266274

267275
rpl::lifetime _backLifetime;
268276

277+
QString _tooltipText;
278+
269279
};
270280

271281
} // namespace HistoryView

0 commit comments

Comments
 (0)