-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmessage_row.rs
More file actions
215 lines (196 loc) · 6.77 KB
/
message_row.rs
File metadata and controls
215 lines (196 loc) · 6.77 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
use std::io::Read;
use adw::prelude::*;
use adw::subclass::prelude::*;
use chrono::{Local, TimeZone};
use gtk::{gdk, gio, glib};
use ntfy_daemon::models;
use tracing::error;
use crate::error::*;
mod imp {
use super::*;
#[derive(Debug, Default)]
pub struct MessageRow {}
#[glib::object_subclass]
impl ObjectSubclass for MessageRow {
const NAME: &'static str = "MessageRow";
type Type = super::MessageRow;
type ParentType = gtk::Grid;
}
impl ObjectImpl for MessageRow {}
impl WidgetImpl for MessageRow {}
impl GridImpl for MessageRow {}
}
glib::wrapper! {
pub struct MessageRow(ObjectSubclass<imp::MessageRow>)
@extends gtk::Widget, gtk::Grid;
}
impl MessageRow {
pub fn new(msg: models::ReceivedMessage) -> Self {
let this: Self = glib::Object::new();
this.build_ui(msg);
this
}
fn build_ui(&self, msg: models::ReceivedMessage) {
self.set_margin_top(8);
self.set_margin_bottom(8);
self.set_margin_start(8);
self.set_margin_end(8);
self.set_column_spacing(8);
self.set_row_spacing(8);
let mut row = 0;
let time = gtk::Label::builder()
.label(
&Local
.timestamp_opt(msg.time as i64, 0)
.earliest()
.map(|time| time.format("%Y-%m-%d %H:%M:%S").to_string())
.unwrap_or_default(),
)
.xalign(0.0)
.build();
time.add_css_class("caption");
self.attach(&time, 0, row, 1, 1);
if let Some(p) = msg.priority {
let text = format!(
"Priority: {}",
match p {
5 => "Max",
4 => "High",
3 => "Medium",
2 => "Low",
1 => "Min",
_ => "Invalid",
}
);
let priority = gtk::Label::builder().label(&text).xalign(0.0).build();
priority.add_css_class("caption");
priority.add_css_class("chip");
if p == 5 {
priority.add_css_class("chip--danger")
} else if p == 4 {
priority.add_css_class("chip--warning")
}
priority.set_halign(gtk::Align::End);
self.attach(&priority, 1, 0, 2, 1);
}
row += 1;
if let Some(title) = msg.display_title() {
let label = gtk::Label::builder()
.label(&title)
.wrap_mode(gtk::pango::WrapMode::WordChar)
.xalign(0.0)
.wrap(true)
.selectable(true)
.build();
label.add_css_class("heading");
self.attach(&label, 0, row, 3, 1);
row += 1;
}
if let Some(message) = msg.display_message() {
let label = gtk::Label::builder()
.label(&message)
.wrap_mode(gtk::pango::WrapMode::WordChar)
.xalign(0.0)
.wrap(true)
.selectable(true)
.hexpand(true)
.build();
self.attach(&label, 0, row, 3, 1);
row += 1;
}
if let Some(attachment) = msg.attachment {
if attachment.is_image() {
self.attach(&self.build_image(attachment.url.to_string()), 0, row, 3, 1);
row += 1;
}
}
if msg.actions.len() > 0 {
let action_btns = gtk::FlowBox::builder()
.row_spacing(8)
.column_spacing(8)
.homogeneous(true)
.selection_mode(gtk::SelectionMode::None)
.build();
for a in msg.actions {
let btn = self.build_action_btn(a);
action_btns.append(&btn);
}
self.attach(&action_btns, 0, row, 3, 1);
row += 1;
}
if msg.tags.len() > 0 {
let mut tags_text = String::from("tags: ");
tags_text.push_str(&msg.tags.join(", "));
let tags = gtk::Label::builder()
.label(&tags_text)
.xalign(0.0)
.wrap(true)
.wrap_mode(gtk::pango::WrapMode::WordChar)
.build();
self.attach(&tags, 0, row, 3, 1);
}
}
fn fetch_image_bytes(url: &str) -> anyhow::Result<Vec<u8>> {
let path = glib::user_cache_dir().join("com.ranfdev.Notify").join(&url);
let bytes = if path.exists() {
std::fs::read(&path)?
} else {
let mut bytes = vec![];
ureq::get(&url)
.call()?
.into_reader()
.take(5 * 1_000_000) // 5 MB
.read_to_end(&mut bytes)?;
bytes
};
Ok(bytes)
}
fn build_image(&self, url: String) -> gtk::Picture {
let (s, r) = async_channel::unbounded();
gio::spawn_blocking(move || {
if let Err(e) = Self::fetch_image_bytes(&url).and_then(|bytes| {
let t = gdk::Texture::from_bytes(&glib::Bytes::from_owned(bytes))?;
s.send_blocking(t)?;
Ok(())
}) {
error!(error = %e)
}
glib::ControlFlow::Break
});
let picture = gtk::Picture::new();
picture.set_can_shrink(true);
picture.set_height_request(350);
let picturec = picture.clone();
self.error_boundary().spawn(async move {
let t = r.recv().await?;
picturec.set_paintable(Some(&t));
Ok(())
});
picture
}
fn build_action_btn(&self, action: models::Action) -> gtk::Button {
let btn = gtk::Button::new();
match &action {
models::Action::View { label, url, .. } => {
btn.set_label(&label);
btn.set_tooltip_text(Some(&format!("Go to {url}")));
btn.set_action_name(Some("app.message-action"));
btn.set_action_target_value(Some(&serde_json::to_string(&action).unwrap().into()));
}
models::Action::Http {
label, method, url, ..
} => {
btn.set_label(&label);
btn.set_tooltip_text(Some(&format!("Send HTTP {method} to {url}")));
btn.set_action_name(Some("app.message-action"));
btn.set_action_target_value(Some(&serde_json::to_string(&action).unwrap().into()));
}
models::Action::Broadcast { label, .. } => {
btn.set_label(&label);
btn.set_sensitive(false);
btn.set_tooltip_text(Some("Broadcast action only available on Android"));
}
}
btn
}
}