-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathsingle_notification_layer.c
More file actions
242 lines (207 loc) · 8.45 KB
/
single_notification_layer.c
File metadata and controls
242 lines (207 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* single_notification_layer.c
* Renders a single notification.
* RebbleOS
*
* Author: Joshua Wise <joshua@joshuawise.com>
*
* based off of a design by @lavender
*
* Renders *just* the notification itself. If you want to scroll it, you
* need to put it in a scroll-layer yourself.
*/
#include "librebble.h"
#include "ngfxwrap.h"
#include "notification_manager.h"
#include "platform_res.h"
#include "single_notification_layer.h"
#include "minilib.h"
#include "status_bar_layer.h"
static void single_notification_layer_update_proc(Layer *layer, GContext *ctx);
#define X_PADDING 4
#define APPNAME_HEIGHT 28
#define APPNAME_PADDING 8
#define ELEMENT_PADDING 8
#define APPNAME_FONT FONT_KEY_GOTHIC_24_BOLD
#define TITLE_FONT FONT_KEY_GOTHIC_18_BOLD
#define SUBTITLE_FONT FONT_KEY_GOTHIC_18
#define BODY_FONT FONT_KEY_GOTHIC_24_BOLD
#define TIMESTAMP_FONT FONT_KEY_GOTHIC_14
void single_notification_layer_ctor(SingleNotificationLayer *l, GRect frame) {
layer_ctor(&l->layer, frame);
layer_set_update_proc(&l->layer, single_notification_layer_update_proc);
l->title = l->subtitle = l->body = l->timestamp = NULL;
l->icon = NULL;
}
void single_notification_layer_dtor(SingleNotificationLayer *l) {
free(l->title);
free(l->subtitle);
free(l->body);
free(l->timestamp);
gbitmap_destroy(l->icon);
layer_dtor(&l->layer);
}
extern void single_notification_layer_set_notification(SingleNotificationLayer *l, rebble_notification *notif) {
free(l->title);
free(l->subtitle);
free(l->body);
free(l->timestamp);
if (l->icon)
gbitmap_destroy(l->icon);
const char *sender = NULL, *subject = NULL, *message = NULL;
uint32_t sourcetype = 0;
rebble_attribute *a;
list_foreach(a, ¬if->attributes, rebble_attribute, node)
{
switch (a->timeline_attribute.attribute_id) {
case TimelineAttributeType_Sender: sender = (const char *)a->data; break;
case TimelineAttributeType_Subject: subject = (const char *)a->data; break;
case TimelineAttributeType_Message: message = (const char *)a->data; break;
case TimelineAttributeType_SourceType:
sourcetype = *(uint32_t *)a->data;
break;
default:
/* we don't care */
;
}
}
if (sender && strlen(sender)) {
l->title = strdup(sender);
if (subject && strlen(subject))
l->subtitle = strdup(subject);
l->body = message ? strdup(message) : NULL;
} else {
if (subject && strlen(subject))
l->title = strdup(subject);
l->body = message ? strdup(message) : NULL;
}
// since the 'source' attribute is useful just to set its respective icon, there's no need
// to hard code the app name using it. 'subject' will be used for that instead.
switch (sourcetype) {
case TimelineNotificationSource_SMS:
l->icon = gbitmap_create_with_resource(RESOURCE_ID_NOTIFICATION);
break;
case TimelineNotificationSource_Email:
l->icon = gbitmap_create_with_resource(RESOURCE_ID_NOTIFICATION); // Change to proper Email icon.
break;
case TimelineNotificationSource_Twitter:
l->icon = gbitmap_create_with_resource(RESOURCE_ID_UNKNOWN); // Change to Twitter icon.
break;
case TimelineNotificationSource_Facebook:
l->icon = gbitmap_create_with_resource(RESOURCE_ID_NOTIFICATION); // Change to Facebook icon.
break;
default:
l->icon = gbitmap_create_with_resource(RESOURCE_ID_UNKNOWN);
break;
}
time_t now = rcore_get_time();
time_t ts = notif->timeline_item.timestamp;
struct tm tm_ts;
localtime_r(&ts, &tm_ts);
char buf[32];
if (ts > now)
l->timestamp = strdup("The future");
else if (ts > (now - 60 * 60)) {
sfmt(buf, sizeof(buf), "%d min ago", (ts - now) / 60);
l->timestamp = strdup(buf);
} else if (ts > (now - 24 * 60 * 60)) {
sfmt(buf, sizeof(buf), "%d hours ago", (ts - now) / (60 * 60));
l->timestamp = strdup(buf);
} else if (ts > (now - 7 * 24 * 60 * 60)) {
char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
sfmt(buf, sizeof(buf), "%s, %02d:%02d", days[tm_ts.tm_mday], tm_ts.tm_hour, tm_ts.tm_min);
l->timestamp = strdup(buf);
} else {
char *mons[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
sfmt(buf, sizeof(buf), "%s %d, %02d:%02d", mons[tm_ts.tm_mon], tm_ts.tm_mday, tm_ts.tm_hour, tm_ts.tm_min);
l->timestamp = strdup(buf);
}
layer_mark_dirty(&l->layer);
}
Layer *single_notification_layer_get_layer(SingleNotificationLayer *l) {
return &l->layer;
}
#ifdef PBL_RECT
uint16_t single_notification_layer_height(SingleNotificationLayer *l) {
uint16_t height = 0;
GRect szrect = layer_get_frame(&l->layer);
szrect.size.h = 1000;
szrect.size.w -= X_PADDING * 2;
height += APPNAME_HEIGHT;
height += APPNAME_PADDING;
if (l->title)
height += n_graphics_text_layout_get_content_size_with_attributes(
l->title, fonts_get_system_font(TITLE_FONT),
szrect, GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, 0).h
+ ELEMENT_PADDING;
if (l->subtitle)
height += n_graphics_text_layout_get_content_size_with_attributes(
l->subtitle, fonts_get_system_font(SUBTITLE_FONT),
szrect, GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, 0).h
+ ELEMENT_PADDING;
if (l->body)
height += n_graphics_text_layout_get_content_size_with_attributes(
l->body, fonts_get_system_font(BODY_FONT),
szrect, GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, 0).h
+ ELEMENT_PADDING;
height += n_graphics_text_layout_get_content_size_with_attributes(
l->timestamp, fonts_get_system_font(TIMESTAMP_FONT),
szrect, GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, 0).h
+ ELEMENT_PADDING;
return height;
}
static void single_notification_layer_update_proc(Layer *layer, GContext *ctx) {
SingleNotificationLayer *l = container_of(layer, SingleNotificationLayer, layer);
GRect szrect = layer_get_frame(layer);
GSize outsz;
graphics_context_set_fill_color(ctx, GColorWhite);
graphics_fill_rect(ctx, szrect, 0, GCornerNone);
graphics_context_set_text_color(ctx, GColorBlack);
szrect.origin.x = 0;
szrect.origin.y = STATUS_BAR_LAYER_HEIGHT;
szrect.origin.x += X_PADDING;
szrect.size.w -= X_PADDING * 2;
GRect tmpsz = szrect;
tmpsz.size.h = 25;
tmpsz.size.w = 25;
graphics_context_set_compositing_mode(ctx, GCompOpSet);
graphics_draw_bitmap_in_rect(ctx, l->icon, tmpsz);
tmpsz = szrect;
tmpsz.size.h = APPNAME_HEIGHT; // Align text vertically
tmpsz.size.w -= APPNAME_HEIGHT + ELEMENT_PADDING;
tmpsz.origin.x += APPNAME_HEIGHT;
if (l->title) {
graphics_draw_text(ctx, l->title, fonts_get_system_font(APPNAME_FONT),
tmpsz, GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, 0);
szrect.origin.y += APPNAME_HEIGHT;
szrect.size.h -= APPNAME_HEIGHT;
}
graphics_context_set_stroke_color(ctx, GColorBlack);
graphics_context_set_stroke_width(ctx, 1);
/* XXX: make this a drawrect */
graphics_draw_line(ctx,
GPoint(szrect.origin.x, szrect.origin.y),
GPoint(szrect.origin.x + szrect.size.w, szrect.origin.y));
szrect.origin.y += 0;
szrect.size.h -= APPNAME_PADDING;
if (l->subtitle) {
graphics_draw_text_ex(ctx,
l->subtitle, fonts_get_system_font(SUBTITLE_FONT),
szrect, GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, 0, &outsz);
szrect.origin.y += outsz.h;
szrect.size.h -= outsz.h;
} if (l->body) {
graphics_draw_text_ex(ctx,
l->body, fonts_get_system_font(BODY_FONT),
szrect, GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, 0, &outsz);
szrect.origin.y += outsz.h + ELEMENT_PADDING;
szrect.size.h -= outsz.h + ELEMENT_PADDING;
}
graphics_draw_text_ex(ctx,
l->timestamp, fonts_get_system_font(TIMESTAMP_FONT),
szrect, GTextOverflowModeTrailingEllipsis, GTextAlignmentLeft, 0, &outsz);
szrect.origin.y += outsz.h + ELEMENT_PADDING;
szrect.size.h -= outsz.h + ELEMENT_PADDING;
}
#else !PBL_RECT
# error single_notification_layer not implemented on non-rectangular Pebbles
#endif