Commit 786cd981 authored by Eisha Chen-yen-su's avatar Eisha Chen-yen-su

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().
parent 25f4cb94
......@@ -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<String>, t: RowType, highlights: Vec<String>) -> MessageContent {
fn create_ui_message (msg: Message, name: Option<String>, t: RowType, highlights: Vec<String>, 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<String>, t: RowType, highlights
formatted_body: msg.formatted_body,
format: msg.format,
highlights: highlights,
redactable,
}
}
......@@ -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<String>,
pub redactable: bool,
}
/* To-Do: this should be moved to a file collecting all structs used in the UI */
......
......@@ -41,23 +41,20 @@ pub struct MessageBox<'a> {
pub username_event_box: gtk::EventBox,
pub row_event_box: gtk::EventBox,
pub image: Option<gtk::DrawingArea>,
uid: String,
power_level: i32,
}
impl<'a> MessageBox<'a> {
pub fn new(msg: &'a Message, backend: Sender<BKCommand>, ui: &'a UI, uid: String, power_level: i32) -> MessageBox<'a> {
pub fn new(msg: &'a Message, backend: Sender<BKCommand>, 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::<gtk::Widget>(), uid.clone(), power_level);
msg.clone(), None);
menu.show_menu_popover(eb.clone().upcast::<gtk::Widget>());
}
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::<gtk::Widget>(), uid.clone(), power_level);
menu.show_menu_popover(eb.clone().upcast::<gtk::Widget>());
Inhibit(true)
} else {
Inhibit(false)
......
......@@ -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<BKCommand>,
selected_text: Option<SelectedText>,
pub msg: Message,
pub msg: MessageContent,
}
impl MessageMenu {
pub fn new_message_menu(ui: UI,
backend: Sender<BKCommand>,
msg: Message,
msg: MessageContent,
event_widget: Option<&gtk::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 |_| {
......
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