Skip to content

Commit

Permalink
Render incoming typing notifications
Browse files Browse the repository at this point in the history
We're not sending typing notification ourselves,
but at least now we can see them coming
from the good clients that do.
  • Loading branch information
albel727 committed Sep 17, 2014
1 parent 775fb7f commit 5097511
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions friend.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ void friend_addmessage(FRIEND *f, void *data)
}
}

void friend_set_typing(FRIEND *f, int typing) {
f->typing = typing;
messages_set_typing(&messages_friend, &f->msg, typing);
}

void friend_addid(uint8_t *id, char_t *msg, uint16_t msg_length)
{
void *data = malloc(TOX_FRIEND_ADDRESS_SIZE + msg_length * sizeof(char_t));
Expand Down
41 changes: 41 additions & 0 deletions messages.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
#include "main.h"

// Ideally this function would have returned an array of simultaneously
// typing friends, e.g. in a groupchat. But since groupchats don't support
// notifications, it simply returns the friend associated with
// given MESSAGES, or NULL if he is not typing.
static FRIEND* get_typers(MESSAGES *m) {
// Currently only friends support typing notifications
if(sitem->item == ITEM_FRIEND) {
FRIEND *f = sitem->data;
// check just in case that we're given the messages panel for
// the currently selected friend
if(&f->msg == m->data) {
if(f->typing) return f;
}
}
return NULL;
}

void messages_draw(MESSAGES *m, int x, int y, int width, int height)
{
setcolor(0);
Expand Down Expand Up @@ -192,6 +209,18 @@ void messages_draw(MESSAGES *m, int x, int y, int width, int height)

y += MESSAGES_SPACING;
}

if(i == n) {
// Last message is visible. Append typing notifications, if needed.
FRIEND *f = get_typers(m);
if(f) {
setfont(FONT_TEXT);
setcolor(C_GRAY2);
drawtextwidth_right(x, MESSAGES_X - NAME_OFFSET, y, f->name, f->name_length);
// TODO: i18n
drawtextwidth(x + MESSAGES_X, x + width, y, "is typing...", sizeof("is typing...") - 1);
}
}
}

_Bool messages_mmove(MESSAGES *m, int px, int py, int width, int height, int mx, int my, int dx, int dy)
Expand Down Expand Up @@ -765,6 +794,10 @@ void messages_updateheight(MESSAGES *m)
height += msg->height;
i++;
}
if(get_typers(m)) {
// Add space for typing notification
height += font_small_lineheight + MESSAGES_SPACING;
}

m->height = height;
data->height = height;
Expand Down Expand Up @@ -837,6 +870,14 @@ void message_add(MESSAGES *m, MESSAGE *msg, MSG_DATA *p)
message_setheight(m, msg, p);
}

void messages_set_typing(MESSAGES *m, MSG_DATA *p, int typing) {
if(m->data == p) {
// MSG_DATA associated with typing notification
// corresponds to given MESSAGES, so update their height.
messages_updateheight(m);
}
}

_Bool messages_char(uint32_t ch)
{
MESSAGES *m;
Expand Down
5 changes: 4 additions & 1 deletion tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,14 +1301,17 @@ void tox_message(uint8_t msg, uint16_t param1, uint16_t param2, void *data)

case FRIEND_TYPING: {
FRIEND *f = &friend[param1];
f->typing = param2;
friend_set_typing(f, param2);
updatefriend(f);
break;
}

case FRIEND_ONLINE: {
FRIEND *f = &friend[param1];
f->online = param2;
if(!f->online) {
friend_set_typing(f, 0);
}
updatefriend(f);
break;
}
Expand Down

0 comments on commit 5097511

Please sign in to comment.