Commit 7ffc87fb authored by Daniel Garcia Moreno's avatar Daniel Garcia Moreno

Merge branch 'header_message' into 'master'

messages: add update methode to message widget

See merge request World/fractal!229
parents 0d7526b8 80a3551d
...@@ -177,7 +177,7 @@ impl AppOp { ...@@ -177,7 +177,7 @@ impl AppOp {
{ {
let backend = self.backend.clone(); let backend = self.backend.clone();
let ui = self.ui.clone(); let ui = self.ui.clone();
let mut mb = widgets::MessageBox::new(&ui_msg, backend, &ui); let mut mb = widgets::MessageBox::new(ui_msg, backend, ui);
let entry = msg_entry.clone(); let entry = msg_entry.clone();
mb.username_event_box.set_focus_on_click(false); mb.username_event_box.set_focus_on_click(false);
mb.username_event_box.connect_button_press_event(move |eb, btn| { mb.username_event_box.connect_button_press_event(move |eb, btn| {
...@@ -196,9 +196,8 @@ impl AppOp { ...@@ -196,9 +196,8 @@ impl AppOp {
glib::signal::Inhibit(false) glib::signal::Inhibit(false)
}); });
m = match calc_prev { m = match calc_prev {
Some(ref p) if self.should_group(&msg, p) => mb.small_widget(), Some(ref p) if self.should_group(&msg, p) => mb.create(false),
Some(_) if self.has_small_mtype(&msg) => mb.small_widget(), _ => mb.create(true),
_ => mb.widget(),
}; };
if let Some(ref image) = mb.image { if let Some(ref image) = mb.image {
let msg = msg.clone(); let msg = msg.clone();
...@@ -255,7 +254,7 @@ impl AppOp { ...@@ -255,7 +254,7 @@ impl AppOp {
{ {
let backend = self.backend.clone(); let backend = self.backend.clone();
let ui = self.ui.clone(); let ui = self.ui.clone();
let mut mb = widgets::MessageBox::new(&ui_msg, backend, &ui); let mut mb = widgets::MessageBox::new(ui_msg, backend, ui);
m = mb.tmpwidget(); m = mb.tmpwidget();
if let Some(ref image) = mb.image { if let Some(ref image) = mb.image {
let msg = msg.clone(); let msg = msg.clone();
...@@ -308,7 +307,7 @@ impl AppOp { ...@@ -308,7 +307,7 @@ impl AppOp {
{ {
let backend = self.backend.clone(); let backend = self.backend.clone();
let ui = self.ui.clone(); let ui = self.ui.clone();
let mut mb = widgets::MessageBox::new(&ui_msg, backend, &ui); let mut mb = widgets::MessageBox::new(ui_msg, backend, ui);
m = mb.tmpwidget(); m = mb.tmpwidget();
if let Some(ref image) = mb.image { if let Some(ref image) = mb.image {
println!("i have a image"); println!("i have a image");
......
...@@ -32,19 +32,23 @@ use uitypes::RowType; ...@@ -32,19 +32,23 @@ use uitypes::RowType;
use uitypes::MessageContent as Message; use uitypes::MessageContent as Message;
use uibuilder::UI; use uibuilder::UI;
// Room Message item /* A message row in the room history */
pub struct MessageBox<'a> { #[derive(Clone)]
msg: &'a Message, pub struct MessageBox {
msg: Message,
backend: Sender<BKCommand>, backend: Sender<BKCommand>,
ui: &'a UI, /* FIXME: Remove UI */
ui: UI,
username: gtk::Label, username: gtk::Label,
pub username_event_box: gtk::EventBox, pub username_event_box: gtk::EventBox,
pub row_event_box: gtk::EventBox, widget: gtk::EventBox,
row: Option<gtk::ListBoxRow>,
pub image: Option<gtk::DrawingArea>, pub image: Option<gtk::DrawingArea>,
header: bool,
} }
impl<'a> MessageBox<'a> { impl MessageBox {
pub fn new(msg: &'a Message, backend: Sender<BKCommand>, ui: &'a UI) -> MessageBox<'a> { pub fn new(msg: Message, backend: Sender<BKCommand>, ui: UI) -> MessageBox {
let username = gtk::Label::new(""); let username = gtk::Label::new("");
let eb = gtk::EventBox::new(); let eb = gtk::EventBox::new();
...@@ -66,64 +70,96 @@ impl<'a> MessageBox<'a> { ...@@ -66,64 +70,96 @@ impl<'a> MessageBox<'a> {
ui: ui, ui: ui,
username: username, username: username,
username_event_box: eb, username_event_box: eb,
row_event_box: row_eb, widget: row_eb,
row: None,
image: None, image: None,
header: true,
} }
} }
/* create the message row with or without a header */
pub fn create(&mut self, has_header: bool) -> gtk::ListBoxRow {
let row = gtk::ListBoxRow::new();
self.set_msg_styles(&row);
row.set_selectable(false);
let w = if has_header && self.msg.mtype != RowType::Emote {
row.set_margin_top(12);
self.header = true;
self.widget()
} else {
self.header = false;
self.small_widget()
};
self.widget.add(&w);
row.add(&self.widget);
row.show_all();
self.row = Some(row.clone());
row
}
/* Updates the header of a message row */
#[allow(dead_code)]
pub fn update(&mut self, has_header: bool) -> Option<()> {
/* Update only if some thing changed */
if has_header != self.header {
self.username.destroy();
self.username = gtk::Label::new("");
self.username_event_box.destroy();
self.username_event_box = gtk::EventBox::new();
let row = self.row.clone()?;
let child = self.widget.get_child()?;
self.widget.remove(&child);
let w = if has_header && self.msg.mtype != RowType::Emote {
row.set_margin_top(12);
self.header = true;
self.widget()
} else {
/* we need to reset the margin */
row.set_margin_top(0);
self.header = false;
self.small_widget()
};
self.widget.add(&w);
row.show_all();
}
None
}
pub fn tmpwidget(&mut self) -> gtk::ListBoxRow { pub fn tmpwidget(&mut self) -> gtk::ListBoxRow {
let w = self.widget(); let w = self.create(true);
if let Some(style) = w.get_style_context() { if let Some(style) = w.get_style_context() {
style.add_class("msg-tmp"); style.add_class("msg-tmp");
} }
w w
} }
pub fn widget(&mut self) -> gtk::ListBoxRow { fn widget(&mut self) -> gtk::Box {
// msg // msg
// +--------+---------+ // +--------+---------+
// | avatar | content | // | avatar | content |
// +--------+---------+ // +--------+---------+
let msg_widget = gtk::Box::new(gtk::Orientation::Horizontal, 10); let msg_widget = gtk::Box::new(gtk::Orientation::Horizontal, 10);
let content = self.build_room_msg_content(false); let content = self.build_room_msg_content(false);
let avatar = self.build_room_msg_avatar(); let avatar = self.build_room_msg_avatar();
msg_widget.pack_start(&avatar, false, false, 0); msg_widget.pack_start(&avatar, false, false, 0);
msg_widget.pack_start(&content, true, true, 0); msg_widget.pack_start(&content, true, true, 0);
self.row_event_box.add(&msg_widget); msg_widget
let row = gtk::ListBoxRow::new();
self.set_msg_styles(&row);
row.set_selectable(false);
row.set_margin_top(12);
row.add(&self.row_event_box);
row.show_all();
row
} }
pub fn small_widget(&mut self) -> gtk::ListBoxRow { fn small_widget(&mut self) -> gtk::Box {
// msg // msg
// +--------+---------+ // +--------+---------+
// | | content | // | | content |
// +--------+---------+ // +--------+---------+
let msg_widget = gtk::Box::new(gtk::Orientation::Horizontal, 5); let msg_widget = gtk::Box::new(gtk::Orientation::Horizontal, 5);
let content = self.build_room_msg_content(true); let content = self.build_room_msg_content(true);
msg_widget.pack_start(&content, true, true, 50); msg_widget.pack_start(&content, true, true, 50);
self.row_event_box.add(&msg_widget); msg_widget
let row = gtk::ListBoxRow::new();
self.set_msg_styles(&row);
row.set_selectable(false);
row.add(&self.row_event_box);
row.show_all();
row
} }
fn build_room_msg_content(&mut self, small: bool) -> gtk::Box { fn build_room_msg_content(&mut self, small: bool) -> gtk::Box {
...@@ -134,22 +170,21 @@ impl<'a> MessageBox<'a> { ...@@ -134,22 +170,21 @@ impl<'a> MessageBox<'a> {
// | body | // | body |
// +------+ // +------+
let content = gtk::Box::new(gtk::Orientation::Vertical, 0); let content = gtk::Box::new(gtk::Orientation::Vertical, 0);
let msg = self.msg;
if !small { if !small {
let info = self.build_room_msg_info(self.msg); let info = self.build_room_msg_info(&self.msg);
info.set_margin_top(2); info.set_margin_top(2);
info.set_margin_bottom(3); info.set_margin_bottom(3);
content.pack_start(&info, false, false, 0); content.pack_start(&info, false, false, 0);
} }
let body = match msg.mtype { let body = match self.msg.mtype {
RowType::Sticker => self.build_room_msg_sticker(), RowType::Sticker => self.build_room_msg_sticker(),
RowType::Image => self.build_room_msg_image(), RowType::Image => self.build_room_msg_image(),
RowType::Emote => self.build_room_msg_emote(&msg), RowType::Emote => self.build_room_msg_emote(&self.msg),
RowType::Audio => self.build_room_audio_player(), RowType::Audio => self.build_room_audio_player(),
RowType::Video | RowType::File => self.build_room_msg_file(), RowType::Video | RowType::File => self.build_room_msg_file(),
_ => self.build_room_msg_body(&msg.body), _ => self.build_room_msg_body(&self.msg.body),
}; };
content.pack_start(&body, true, true, 0); content.pack_start(&body, true, true, 0);
...@@ -188,10 +223,7 @@ impl<'a> MessageBox<'a> { ...@@ -188,10 +223,7 @@ impl<'a> MessageBox<'a> {
self.username.clone() self.username.clone()
} }
/// Add classes to the widget depending on the properties: /* Add classes to the widget based on message type */
///
/// * msg-mention: if the message contains a keyword, e.g. the username
/// * msg-emote: if the message is an emote
fn set_msg_styles(&self, w: &gtk::ListBoxRow) { fn set_msg_styles(&self, w: &gtk::ListBoxRow) {
if let Some(style) = w.get_style_context() { if let Some(style) = w.get_style_context() {
match self.msg.mtype { match self.msg.mtype {
...@@ -280,12 +312,11 @@ impl<'a> MessageBox<'a> { ...@@ -280,12 +312,11 @@ impl<'a> MessageBox<'a> {
} }
fn build_room_msg_image(&mut self) -> gtk::Box { fn build_room_msg_image(&mut self) -> gtk::Box {
let msg = self.msg;
let bx = gtk::Box::new(gtk::Orientation::Horizontal, 0); let bx = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let img_path = match msg.thumb { let img_path = match self.msg.thumb {
Some(ref m) => m.clone(), Some(ref m) => m.clone(),
None => msg.url.clone().unwrap_or_default(), None => self.msg.url.clone().unwrap_or_default(),
}; };
let image = widgets::image::Image::new(&self.backend, &img_path) let image = widgets::image::Image::new(&self.backend, &img_path)
.size(Some(globals::MAX_IMAGE_SIZE)).build(); .size(Some(globals::MAX_IMAGE_SIZE)).build();
...@@ -301,11 +332,10 @@ impl<'a> MessageBox<'a> { ...@@ -301,11 +332,10 @@ impl<'a> MessageBox<'a> {
} }
fn build_room_msg_sticker(&self) -> gtk::Box { fn build_room_msg_sticker(&self) -> gtk::Box {
let msg = self.msg;
let bx = gtk::Box::new(gtk::Orientation::Horizontal, 0); let bx = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let backend = self.backend.clone(); let backend = self.backend.clone();
let image = widgets::image::Image::new(&backend, let image = widgets::image::Image::new(&backend,
&msg.url.clone().unwrap_or_default()) &self.msg.url.clone().unwrap_or_default())
.size(Some(globals::MAX_STICKER_SIZE)).build(); .size(Some(globals::MAX_STICKER_SIZE)).build();
let w = image.widget.clone(); let w = image.widget.clone();
w.set_tooltip_text(&self.msg.body[..]); w.set_tooltip_text(&self.msg.body[..]);
...@@ -316,12 +346,11 @@ impl<'a> MessageBox<'a> { ...@@ -316,12 +346,11 @@ impl<'a> MessageBox<'a> {
} }
fn build_room_audio_player(&self) -> gtk::Box { fn build_room_audio_player(&self) -> gtk::Box {
let msg = self.msg;
let bx = gtk::Box::new(gtk::Orientation::Horizontal, 6); let bx = gtk::Box::new(gtk::Orientation::Horizontal, 6);
let player = widgets::AudioPlayerWidget::new(); let player = widgets::AudioPlayerWidget::new();
let name = msg.body.clone(); let name = self.msg.body.clone();
let url = msg.url.clone().unwrap_or_default(); let url = self.msg.url.clone().unwrap_or_default();
let backend = self.backend.clone(); let backend = self.backend.clone();
let (tx, rx): (Sender<String>, Receiver<String>) = channel(); let (tx, rx): (Sender<String>, Receiver<String>) = channel();
...@@ -377,12 +406,11 @@ impl<'a> MessageBox<'a> { ...@@ -377,12 +406,11 @@ impl<'a> MessageBox<'a> {
} }
fn build_room_msg_file(&self) -> gtk::Box { fn build_room_msg_file(&self) -> gtk::Box {
let msg = self.msg;
let bx = gtk::Box::new(gtk::Orientation::Horizontal, 12); let bx = gtk::Box::new(gtk::Orientation::Horizontal, 12);
let btn_bx = gtk::Box::new(gtk::Orientation::Horizontal, 0); let btn_bx = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let name = msg.body.clone(); let name = self.msg.body.clone();
let url = msg.url.clone().unwrap_or_default(); let url = self.msg.url.clone().unwrap_or_default();
let backend = self.backend.clone(); let backend = self.backend.clone();
let name_lbl = gtk::Label::new(name.as_str()); let name_lbl = gtk::Label::new(name.as_str());
name_lbl.set_tooltip_text(name.as_str()); name_lbl.set_tooltip_text(name.as_str());
...@@ -503,7 +531,7 @@ impl<'a> MessageBox<'a> { ...@@ -503,7 +531,7 @@ impl<'a> MessageBox<'a> {
} }
fn connect_right_click_menu(&self, w: gtk::Widget) { fn connect_right_click_menu(&self, w: gtk::Widget) {
let eb = self.row_event_box.clone(); let eb = self.widget.clone();
let backend = self.backend.clone(); let backend = self.backend.clone();
let ui = self.ui.clone(); let ui = self.ui.clone();
let msg = self.msg.clone(); let msg = self.msg.clone();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment