From 786cd981b7ffb0732479eb3cd9d505af976a8227 Mon Sep 17 00:00:00 2001 From: Eisha Chen-yen-su Date: Thu, 20 Sep 2018 10:48:28 +0200 Subject: [PATCH] fractal-gtk: Cleanup code for hiding Delete button This simplifies the code for hiding the messages "Delete" button as there were a lot of things added to pass the power level as an argument to show_menu_popover(). --- fractal-gtk/src/appop/message.rs | 36 +++++++++---------------- fractal-gtk/src/uitypes.rs | 1 + fractal-gtk/src/widgets/message.rs | 19 +++++-------- fractal-gtk/src/widgets/message_menu.rs | 24 ++++++++--------- 4 files changed, 32 insertions(+), 48 deletions(-) diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs index f29e7dde..51203b19 100644 --- a/fractal-gtk/src/appop/message.rs +++ b/fractal-gtk/src/appop/message.rs @@ -175,15 +175,9 @@ impl AppOp { if let Some(r) = self.rooms.get(&self.active_room.clone().unwrap_or_default()) { let m; { - let uid = self.uid.clone().unwrap_or_default(); - let power_level = match self.uid.clone().and_then(|uid| r.power_levels.get(&uid)) { - Some(&pl) => pl, - None => 0 - }; - let backend = self.backend.clone(); let ui = self.ui.clone(); - let mut mb = widgets::MessageBox::new(&ui_msg, backend, &ui, uid, power_level); + let mut mb = widgets::MessageBox::new(&ui_msg, backend, &ui); let entry = msg_entry.clone(); mb.username_event_box.set_focus_on_click(false); mb.username_event_box.connect_button_press_event(move |eb, btn| { @@ -259,15 +253,9 @@ impl AppOp { if let Some(r) = self.rooms.get(&self.active_room.clone().unwrap_or_default()) { let m; { - let uid = self.uid.clone().unwrap_or_default(); - let power_level = match self.uid.clone().and_then(|uid| r.power_levels.get(&uid)) { - Some(&pl) => pl, - None => 0 - }; - let backend = self.backend.clone(); let ui = self.ui.clone(); - let mut mb = widgets::MessageBox::new(&ui_msg, backend, &ui, uid, power_level); + let mut mb = widgets::MessageBox::new(&ui_msg, backend, &ui); m = mb.tmpwidget(); if let Some(ref image) = mb.image { let msg = msg.clone(); @@ -318,15 +306,9 @@ impl AppOp { if let Some(ui_msg) = self.create_new_room_message(&t.msg) { let m; { - let uid = self.uid.clone().unwrap_or_default(); - let power_level = match self.uid.clone().and_then(|uid| r.power_levels.get(&uid)) { - Some(&pl) => pl, - None => 0 - }; - let backend = self.backend.clone(); let ui = self.ui.clone(); - let mut mb = widgets::MessageBox::new(&ui_msg, backend, &ui, uid, power_level); + let mut mb = widgets::MessageBox::new(&ui_msg, backend, &ui); m = mb.tmpwidget(); if let Some(ref image) = mb.image { println!("i have a image"); @@ -732,12 +714,19 @@ impl AppOp { None }; - Some(create_ui_message(msg.clone(), name, t, highlights)) + let uid = self.uid.clone().unwrap_or_default(); + let power_level = match self.uid.clone().and_then(|uid| room.power_levels.get(&uid)) { + Some(&pl) => pl, + None => 0, + }; + let redactable = power_level != 0 || uid == msg.sender; + + Some(create_ui_message(msg.clone(), name, t, highlights, redactable)) } } /* FIXME: don't convert msg to ui messages here, we should later get a ui message from storage */ -fn create_ui_message (msg: Message, name: Option, t: RowType, highlights: Vec) -> MessageContent { +fn create_ui_message (msg: Message, name: Option, t: RowType, highlights: Vec, redactable: bool) -> MessageContent { MessageContent { msg: msg.clone(), id: msg.id.unwrap_or(String::from("")), @@ -751,5 +740,6 @@ fn create_ui_message (msg: Message, name: Option, t: RowType, highlights formatted_body: msg.formatted_body, format: msg.format, highlights: highlights, + redactable, } } diff --git a/fractal-gtk/src/uitypes.rs b/fractal-gtk/src/uitypes.rs index 417df5bc..18e8d4c8 100644 --- a/fractal-gtk/src/uitypes.rs +++ b/fractal-gtk/src/uitypes.rs @@ -19,6 +19,7 @@ pub struct MessageContent { /* in some places we still need the backend message type (e.g. media viewer) */ pub msg: Message, pub highlights: Vec, + pub redactable: bool, } /* To-Do: this should be moved to a file collecting all structs used in the UI */ diff --git a/fractal-gtk/src/widgets/message.rs b/fractal-gtk/src/widgets/message.rs index 85236c49..21a95b4b 100644 --- a/fractal-gtk/src/widgets/message.rs +++ b/fractal-gtk/src/widgets/message.rs @@ -41,23 +41,20 @@ pub struct MessageBox<'a> { pub username_event_box: gtk::EventBox, pub row_event_box: gtk::EventBox, pub image: Option, - uid: String, - power_level: i32, } impl<'a> MessageBox<'a> { - pub fn new(msg: &'a Message, backend: Sender, ui: &'a UI, uid: String, power_level: i32) -> MessageBox<'a> { + pub fn new(msg: &'a Message, backend: Sender, ui: &'a UI) -> MessageBox<'a> { let username = gtk::Label::new(""); let eb = gtk::EventBox::new(); let row_eb = gtk::EventBox::new(); - let source_msg = msg.msg.clone(); - row_eb.connect_button_press_event(clone!(source_msg, backend, ui, uid => move |eb, btn| { + row_eb.connect_button_press_event(clone!(msg, backend, ui => move |eb, btn| { if btn.get_button() == 3 { let menu = MessageMenu::new_message_menu(ui.clone(), backend.clone(), - source_msg.clone(), None); - menu.show_menu_popover(eb.clone().upcast::(), uid.clone(), power_level); + msg.clone(), None); + menu.show_menu_popover(eb.clone().upcast::()); } Inhibit(false) @@ -71,8 +68,6 @@ impl<'a> MessageBox<'a> { username_event_box: eb, row_event_box: row_eb, image: None, - uid, - power_level, } } @@ -511,15 +506,13 @@ impl<'a> MessageBox<'a> { let eb = self.row_event_box.clone(); let backend = self.backend.clone(); let ui = self.ui.clone(); - let msg = self.msg.msg.clone(); - let uid = self.uid.clone(); - let power_level = self.power_level; + let msg = self.msg.clone(); w.connect_button_press_event(move |w, btn| { if btn.get_button() == 3 { let menu = MessageMenu::new_message_menu(ui.clone(), backend.clone(), msg.clone(), Some(w)); - menu.show_menu_popover(eb.clone().upcast::(), uid.clone(), power_level); + menu.show_menu_popover(eb.clone().upcast::()); Inhibit(true) } else { Inhibit(false) diff --git a/fractal-gtk/src/widgets/message_menu.rs b/fractal-gtk/src/widgets/message_menu.rs index 6302f653..ed8afecf 100644 --- a/fractal-gtk/src/widgets/message_menu.rs +++ b/fractal-gtk/src/widgets/message_menu.rs @@ -19,7 +19,7 @@ use backend::BKCommand; use i18n::i18n; use uibuilder::UI; -use types::Message; +use uitypes::{MessageContent, RowType}; #[derive(Clone)] struct SelectedText { @@ -35,13 +35,13 @@ pub struct MessageMenu { ui: UI, backend: Sender, selected_text: Option, - pub msg: Message, + pub msg: MessageContent, } impl MessageMenu { pub fn new_message_menu(ui: UI, backend: Sender, - msg: Message, + msg: MessageContent, event_widget: Option<>k::Widget>) -> MessageMenu { let builder = gtk::Builder::new(); builder.add_from_resource("/org/gnome/Fractal/ui/message_menu.ui") @@ -61,7 +61,7 @@ impl MessageMenu { menu } - pub fn show_menu_popover(&self, w: gtk::Widget, uid: String, power_level: i32) { + pub fn show_menu_popover(&self, w: gtk::Widget) { let copy_button: gtk::Widget = self.builder .get_object("copy_selected_text_button") .expect("Can't find copy_selected_text_button"); @@ -73,28 +73,28 @@ impl MessageMenu { let message_menu_separator: gtk::Widget = self.builder .get_object("message_menu_separator") .expect("Can't find message_menu_separator"); - delete_button.set_visible(power_level != 0 || uid == self.msg.sender); - message_menu_separator.set_visible(power_level != 0 || uid == self.msg.sender); + delete_button.set_visible(self.msg.redactable); + message_menu_separator.set_visible(self.msg.redactable); let open_with_button: gtk::Widget = self.builder .get_object("open_with_button") .expect("Can't find open_with_button"); - open_with_button.set_visible(self.msg.mtype == "m.image"); + open_with_button.set_visible(self.msg.mtype == RowType::Image); let save_image_as_button: gtk::Widget = self.builder .get_object("save_image_as_button") .expect("Can't find save_image_as_button"); - save_image_as_button.set_visible(self.msg.mtype == "m.image"); + save_image_as_button.set_visible(self.msg.mtype == RowType::Image); let copy_image_button: gtk::Widget = self.builder .get_object("copy_image_button") .expect("Can't find copy_image_button"); - copy_image_button.set_visible(self.msg.mtype == "m.image"); + copy_image_button.set_visible(self.msg.mtype == RowType::Image); let copy_text_button: gtk::Widget = self.builder .get_object("copy_text_button") .expect("Can't find copy_text_button"); - copy_text_button.set_visible(self.msg.mtype != "m.image"); + copy_text_button.set_visible(self.msg.mtype != RowType::Image); gdk::Display::get_default() .and_then(|disp| disp.get_default_seat()) @@ -226,7 +226,7 @@ impl MessageMenu { .get_object("msg_src_window") .expect("Can't find msg_src_window in ui file."); - source_buffer.set_text(self.msg.source.clone() + source_buffer.set_text(self.msg.msg.source.clone() .unwrap_or("This message has no source.".to_string()) .as_str()); @@ -295,7 +295,7 @@ impl MessageMenu { let backend = self.backend.clone(); delete_message_button.connect_clicked(clone!(this => move |_| { - backend.send(BKCommand::SendMsgRedaction(this.borrow().msg.clone())).unwrap(); + backend.send(BKCommand::SendMsgRedaction(this.borrow().msg.msg.clone())).unwrap(); })); view_source_button.connect_clicked(clone!(this => move |_| {