Added online badges to touchbar.

This commit is contained in:
23rd 2019-06-09 21:55:15 +03:00 committed by John Preston
parent d9426413dd
commit 77a216cbca
1 changed files with 46 additions and 4 deletions

View File

@ -13,6 +13,7 @@
#include "core/application.h" #include "core/application.h"
#include "core/sandbox.h" #include "core/sandbox.h"
#include "data/data_folder.h" #include "data/data_folder.h"
#include "data/data_peer_values.h"
#include "data/data_session.h" #include "data/data_session.h"
#include "dialogs/dialogs_layout.h" #include "dialogs/dialogs_layout.h"
#include "history/history.h" #include "history/history.h"
@ -98,6 +99,12 @@ inline bool IsSelfPeer(PeerData *peer) {
return (peer && peer->id == Auth().userPeerId()); return (peer && peer->id == Auth().userPeerId());
} }
inline bool IsUserOnline(PeerData *peer) {
return peer
&& peer->isUser()
&& Data::OnlineTextActive(peer->asUser(), unixtime());
}
inline int UnreadCount(PeerData *peer) { inline int UnreadCount(PeerData *peer) {
return (peer return (peer
&& AuthSession::Exists() && AuthSession::Exists()
@ -122,11 +129,11 @@ NSString *FormatTime(int time) {
return stringTime; return stringTime;
} }
void PaintUnreadBadge(Painter &p, PeerData *peer) { bool PaintUnreadBadge(Painter &p, PeerData *peer) {
const auto history = Auth().data().history(peer->id); const auto history = Auth().data().history(peer->id);
const auto count = history->unreadCountForBadge(); const auto count = history->unreadCountForBadge();
if (!count) { if (!count) {
return; return false;
} }
const auto unread = history->unreadMark() const auto unread = history->unreadMark()
? QString() ? QString()
@ -142,6 +149,29 @@ void PaintUnreadBadge(Painter &p, PeerData *peer) {
unreadSt.font->flags(), unreadSt.font->flags(),
unreadSt.font->family()); unreadSt.font->family());
Dialogs::Layout::paintUnreadCount(p, unread, kIdealIconSize, kIdealIconSize - unreadSt.size, unreadSt, nullptr, 2); Dialogs::Layout::paintUnreadCount(p, unread, kIdealIconSize, kIdealIconSize - unreadSt.size, unreadSt, nullptr, 2);
return true;
}
void PaintOnlineCircle(Painter &p) {
PainterHighQualityEnabler hq(p);
// Use constant values to draw online badge regardless of cConfigScale().
const auto size = 8;
const auto paddingSize = 4;
const auto circleSize = size + paddingSize;
const auto offset = size + paddingSize / 2;
p.setPen(Qt::NoPen);
p.setBrush(Qt::black);
p.drawEllipse(
kIdealIconSize - circleSize,
kIdealIconSize - circleSize,
circleSize,
circleSize);
p.setBrush(st::dialogsOnlineBadgeFg);
p.drawEllipse(
kIdealIconSize - offset,
kIdealIconSize - offset,
size,
size);
} }
void SendKeyEvent(int command) { void SendKeyEvent(int command) {
@ -238,7 +268,7 @@ void SendKeyEvent(int command) {
themeChanged themeChanged
) | rpl::filter([=](const Update &update) { ) | rpl::filter([=](const Update &update) {
return update.type == Update::Type::ApplyingTheme return update.type == Update::Type::ApplyingTheme
&& UnreadCount(_peer); && (UnreadCount(_peer) || IsUserOnline(_peer));
}) | rpl::start_with_next([=] { }) | rpl::start_with_next([=] {
[self updateBadge]; [self updateBadge];
}, _lifetime); }, _lifetime);
@ -283,6 +313,15 @@ void SendKeyEvent(int command) {
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
[self updateBadge]; [self updateBadge];
}, _peerChangedLifetime); }, _peerChangedLifetime);
Notify::PeerUpdateViewer(
_peer,
Notify::PeerUpdate::Flag::UserOnlineChanged
) | rpl::filter([=] {
return UnreadCount(_peer) == 0;
}) | rpl::start_with_next([=] {
[self updateBadge];
}, _peerChangedLifetime);
} }
- (void) buttonActionPin:(NSButton *)sender { - (void) buttonActionPin:(NSButton *)sender {
@ -334,9 +373,12 @@ void SendKeyEvent(int command) {
} }
- (void) updateBadge { - (void) updateBadge {
// Draw unread or online badge.
auto pixmap = App::pixmapFromImageInPlace(_userpic.toImage()); auto pixmap = App::pixmapFromImageInPlace(_userpic.toImage());
Painter p(&pixmap); Painter p(&pixmap);
PaintUnreadBadge(p, _peer); if (!PaintUnreadBadge(p, _peer) && IsUserOnline(_peer)) {
PaintOnlineCircle(p);
}
[self updateImage:pixmap]; [self updateImage:pixmap];
} }