From e70c67123543276878870bedac8f7937402555fe Mon Sep 17 00:00:00 2001 From: Julian Sparber Date: Fri, 21 Sep 2018 18:55:06 +0200 Subject: [PATCH] messages: add update methode to message widget - Add the functionality to update a row, for it allowes only to update the header - clean up some duplicated code --- fractal-gtk/src/appop/message.rs | 5 +- fractal-gtk/src/widgets/message.rs | 88 +++++++++++++++++++----------- 2 files changed, 57 insertions(+), 36 deletions(-) diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs index 51203b19..6ec9579d 100644 --- a/fractal-gtk/src/appop/message.rs +++ b/fractal-gtk/src/appop/message.rs @@ -196,9 +196,8 @@ impl AppOp { glib::signal::Inhibit(false) }); m = match calc_prev { - Some(ref p) if self.should_group(&msg, p) => mb.small_widget(), - Some(_) if self.has_small_mtype(&msg) => mb.small_widget(), - _ => mb.widget(), + Some(ref p) if self.should_group(&msg, p) => mb.create(false), + _ => mb.create(true), }; if let Some(ref image) = mb.image { let msg = msg.clone(); diff --git a/fractal-gtk/src/widgets/message.rs b/fractal-gtk/src/widgets/message.rs index 21a95b4b..e4b26c2f 100644 --- a/fractal-gtk/src/widgets/message.rs +++ b/fractal-gtk/src/widgets/message.rs @@ -32,14 +32,15 @@ use uitypes::RowType; use uitypes::MessageContent as Message; use uibuilder::UI; -// Room Message item +/* A message row in the room history */ pub struct MessageBox<'a> { msg: &'a Message, backend: Sender, ui: &'a UI, username: gtk::Label, pub username_event_box: gtk::EventBox, - pub row_event_box: gtk::EventBox, + widget: gtk::EventBox, + row: Option, pub image: Option, } @@ -66,64 +67,88 @@ impl<'a> MessageBox<'a> { ui: ui, username: username, username_event_box: eb, - row_event_box: row_eb, + widget: row_eb, + row: None, image: None, } } + /* 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.widget() + } else { + 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<()> { + 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.widget() + } else { + /* we need to reset the margin */ + row.set_margin_top(0); + self.small_widget() + }; + + self.widget.add(&w); + row.show_all(); + None + } + pub fn tmpwidget(&mut self) -> gtk::ListBoxRow { - let w = self.widget(); + let w = self.create(true); if let Some(style) = w.get_style_context() { style.add_class("msg-tmp"); } w } - pub fn widget(&mut self) -> gtk::ListBoxRow { + fn widget(&mut self) -> gtk::Box { // msg // +--------+---------+ // | avatar | content | // +--------+---------+ let msg_widget = gtk::Box::new(gtk::Orientation::Horizontal, 10); - let content = self.build_room_msg_content(false); let avatar = self.build_room_msg_avatar(); msg_widget.pack_start(&avatar, false, false, 0); msg_widget.pack_start(&content, true, true, 0); - self.row_event_box.add(&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 + msg_widget } - pub fn small_widget(&mut self) -> gtk::ListBoxRow { + fn small_widget(&mut self) -> gtk::Box { // msg // +--------+---------+ // | | content | // +--------+---------+ let msg_widget = gtk::Box::new(gtk::Orientation::Horizontal, 5); - let content = self.build_room_msg_content(true); - msg_widget.pack_start(&content, true, true, 50); - self.row_event_box.add(&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 + msg_widget } fn build_room_msg_content(&mut self, small: bool) -> gtk::Box { @@ -188,10 +213,7 @@ impl<'a> MessageBox<'a> { self.username.clone() } - /// Add classes to the widget depending on the properties: - /// - /// * msg-mention: if the message contains a keyword, e.g. the username - /// * msg-emote: if the message is an emote + /* Add classes to the widget based on message type */ fn set_msg_styles(&self, w: >k::ListBoxRow) { if let Some(style) = w.get_style_context() { match self.msg.mtype { @@ -503,7 +525,7 @@ impl<'a> MessageBox<'a> { } 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 ui = self.ui.clone(); let msg = self.msg.clone();