Commit b9d7a5a9 authored by Julian Sparber's avatar Julian Sparber

message_menu: remove appOP dependecy from message_menu

We are moving away from the appOp therefore this removes it form the
message_menu. Also the use of different gtk:builder to load .ui files is
the better way.
parent 12e14c89
......@@ -49,7 +49,6 @@ fractal-gtk/src/app/connect/load_more.rs
fractal-gtk/src/app/connect/login.rs
fractal-gtk/src/app/connect/markdown.rs
fractal-gtk/src/app/connect/media_viewer.rs
fractal-gtk/src/app/connect/message_menu.rs
fractal-gtk/src/app/connect/mod.rs
fractal-gtk/src/app/connect/new_room.rs
fractal-gtk/src/app/connect/roomlist_search.rs
......
extern crate gtk;
use self::gtk::prelude::*;
use app::App;
use backend::BKCommand;
impl App {
pub fn connect_message_menu(&self) {
let reply_button: gtk::ModelButton = self.ui.builder
.get_object("reply_button")
.expect("Can't find reply_button in ui file.");
let copy_text_button: gtk::ModelButton = self.ui.builder
.get_object("copy_text_button")
.expect("Can't find copy_text_button in ui file.");
let delete_message_button: gtk::ModelButton = self.ui.builder
.get_object("delete_message_button")
.expect("Can't find delete_message_button in ui file.");
let view_source_button: gtk::ModelButton = self.ui.builder
.get_object("view_source_button")
.expect("Can't find view_source_button in ui file.");
let message_menu = self.op.lock().unwrap().message_menu.clone();
reply_button.connect_clicked(move |_| {
if let Some(message_menu) = message_menu.read().unwrap().clone() {
message_menu.insert_quote();
}
});
let message_menu = self.op.lock().unwrap().message_menu.clone();
copy_text_button.connect_clicked(move |_| {
if let Some(message_menu) = message_menu.read().unwrap().clone() {
message_menu.copy_text();
}
});
let message_menu = self.op.lock().unwrap().message_menu.clone();
let bk = self.op.lock().unwrap().backend.clone();
delete_message_button.connect_clicked(move |_| {
if let Some(message_menu) = message_menu.read().unwrap().clone() {
bk.send(BKCommand::SendMsgRedaction(message_menu.msg.clone())).unwrap();
}
});
let message_menu = self.op.lock().unwrap().message_menu.clone();
view_source_button.connect_clicked(move |_| {
if let Some(message_menu) = message_menu.read().unwrap().clone() {
message_menu.display_source_dialog();
}
});
}
}
......@@ -15,7 +15,6 @@ mod load_more;
mod login;
mod markdown;
mod media_viewer;
mod message_menu;
mod new_room;
mod roomlist_search;
mod scroll;
......@@ -81,7 +80,6 @@ impl App {
self.connect_markdown();
self.connect_media_viewer_headerbar();
self.connect_media_viewer_box();
self.connect_message_menu();
//self.connect_stickers();
self.connect_autocomplete();
self.connect_spellcheck();
......
extern crate gtk;
use std::sync::{Arc, RwLock};
use std::sync::mpsc::Sender;
use std::collections::HashMap;
......@@ -52,8 +51,6 @@ pub use self::message::LastViewed;
pub use self::room::RoomPanel;
use self::member::SearchType;
use self::media_viewer::MediaViewer;
use self::widgets::message_menu::MessageMenu;
pub struct AppOp {
pub ui: uibuilder::UI,
......@@ -101,8 +98,6 @@ pub struct AppOp {
pub directory: Vec<Room>,
pub media_viewer: Option<MediaViewer>,
pub message_menu: Arc<RwLock<Option<MessageMenu>>>,
}
impl PasswordStorage for AppOp {}
......@@ -154,8 +149,6 @@ impl AppOp {
directory: vec![],
media_viewer: None,
message_menu: Arc::new(RwLock::new(None)),
}
}
......
......@@ -34,8 +34,6 @@ impl UI {
.expect("Can't load ui file: stickers_popover.ui");
builder.add_from_resource("/org/gnome/Fractal/ui/media_viewer_menu.ui")
.expect("Can't load ui file: media_viewer_menu.ui");
builder.add_from_resource("/org/gnome/Fractal/ui/message_menu.ui")
.expect("Can't load ui file: message_menu.ui");
builder.add_from_resource("/org/gnome/Fractal/ui/main_window.ui")
.expect("Can't load ui file: main_window.ui");
......
extern crate gtk;
extern crate sourceview;
extern crate chrono;
extern crate pango;
extern crate glib;
......@@ -49,12 +50,21 @@ impl<'a> MessageBox<'a> {
let eb = gtk::EventBox::new();
let row_eb = gtk::EventBox::new();
let message_menu = op.message_menu.clone();
let ui = op.ui.clone();
row_eb.connect_button_press_event(clone!(msg => move |eb, btn| {
let backend = op.backend.clone();
let msg_entry: sourceview::View = op.ui.builder
.get_object("msg_entry")
.expect("Can't find msg_entry in ui file.");
let source_dialog: gtk::MessageDialog = op.ui.builder
.get_object("source_dialog")
.expect("Can't find source_dialog in ui file.");
let buffer: sourceview::Buffer = op.ui.builder
.get_object("msg_source_buffer")
.expect("Can't find msg_source_buffer in ui file.");
row_eb.connect_button_press_event(clone!(msg_entry, source_dialog, buffer, msg => move |eb, btn| {
if btn.get_button() == 3 {
*message_menu.write().unwrap() = Some(MessageMenu::new_message_menu(ui.clone(), msg.clone()));
message_menu.read().unwrap().clone().unwrap().show_menu_popover(eb.clone().upcast::<gtk::Widget>());
let menu = MessageMenu::new_message_menu(source_dialog.clone(), buffer.clone(), msg_entry.clone(), backend.clone(), msg.clone());
menu.show_menu_popover(eb.clone().upcast::<gtk::Widget>());
}
Inhibit(false)
......@@ -557,15 +567,22 @@ impl<'a> MessageBox<'a> {
fn connect_right_click_menu(&self, w: gtk::Widget) {
let eb = self.row_event_box.clone();
let message_menu = self.op.message_menu.clone();
let ui = self.op.ui.clone();
let msg = self.msg.clone();
let backend = self.op.backend.clone();
let msg_entry: sourceview::View = self.op.ui.builder
.get_object("msg_entry")
.expect("Can't find msg_entry in ui file.");
let source_dialog: gtk::MessageDialog = self.op.ui.builder
.get_object("source_dialog")
.expect("Can't find source_dialog in ui file.");
let buffer: sourceview::Buffer = self.op.ui.builder
.get_object("msg_source_buffer")
.expect("Can't find msg_source_buffer in ui file.");
w.connect_button_press_event(move |_, btn| {
if btn.get_button() == 3 {
*message_menu.write().unwrap() = Some(MessageMenu::new_message_menu(ui.clone(), msg.clone()));
message_menu.read().unwrap().clone().unwrap().show_menu_popover(eb.clone().upcast::<gtk::Widget>());
let menu = MessageMenu::new_message_menu(source_dialog.clone(), buffer.clone(), msg_entry.clone(), backend.clone(), msg.clone());
menu.show_menu_popover(eb.clone().upcast::<gtk::Widget>());
Inhibit(true)
} else {
Inhibit(false)
......
......@@ -2,26 +2,44 @@ extern crate gdk;
extern crate gtk;
extern crate sourceview;
use self::gdk::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
use self::gtk::prelude::*;
use self::gdk::prelude::*;
use self::sourceview::prelude::*;
use backend::BKCommand;
use std::sync::mpsc::Sender;
use types::Message;
use uibuilder::UI;
#[derive(Clone)]
pub struct MessageMenu {
ui: UI,
builder: gtk::Builder,
backend: Sender<BKCommand>,
source_dialog: gtk::MessageDialog,
source_buffer: sourceview::Buffer,
msg_entry: sourceview::View,
pub msg: Message,
}
impl MessageMenu {
pub fn new_message_menu(ui: UI, msg: Message) -> MessageMenu {
MessageMenu {
ui,
/* FIXME: Remove widget references, but I'm not sure how we can do that */
pub fn new_message_menu(source_dialog: gtk::MessageDialog, source_buffer: sourceview::Buffer, msg_entry: sourceview::View, backend: Sender<BKCommand>, msg: Message) -> MessageMenu {
let builder = gtk::Builder::new();
builder.add_from_resource("/org/gnome/Fractal/ui/message_menu.ui")
.expect("Can't load ui file: message_menu.ui");
let menu = MessageMenu {
builder,
backend,
source_dialog,
source_buffer,
msg_entry,
msg,
}
};
menu.connect_message_menu();
menu
}
pub fn show_menu_popover(&self, w: gtk::Widget) {
......@@ -32,7 +50,7 @@ impl MessageMenu {
let win = w.get_window()?;
let (_, x, y, _) = win.get_device_position(&ptr);
let menu_popover: gtk::Popover = self.ui.builder
let menu_popover: gtk::Popover = self.builder
.get_object("message_menu_popover")
.expect("Can't find message_menu_popover in ui file.");
let rect = gtk::Rectangle {
......@@ -53,18 +71,14 @@ impl MessageMenu {
}
pub fn insert_quote(&self) {
let msg_entry: sourceview::View = self.ui.builder
.get_object("msg_entry")
.expect("Can't find msg_entry in ui file.");
if let Some(buffer) = msg_entry.get_buffer() {
if let Some(buffer) = self.msg_entry.get_buffer() {
let quote = self.msg.body.lines().map(|l| "> ".to_owned() + l + "\n")
.collect::<Vec<String>>().join("\n") + "\n";
let mut start = buffer.get_start_iter();
buffer.insert(&mut start, &quote);
msg_entry.grab_focus();
self.msg_entry.grab_focus();
}
}
......@@ -76,35 +90,29 @@ impl MessageMenu {
}
pub fn display_source_dialog(&self) {
let dialog: gtk::MessageDialog = self.ui.builder
.get_object("source_dialog")
.expect("Can't find source_dialog in ui file.");
let json_lang = sourceview::LanguageManager::get_default()
.map_or(None, |lm| lm.get_language("json"));
let buffer: sourceview::Buffer = self.ui.builder
.get_object("msg_source_buffer")
.expect("Can't find msg_source_buffer in ui file.");
buffer.set_highlight_matching_brackets(false);
self.source_buffer.set_highlight_matching_brackets(false);
if let Some(json_lang) = json_lang.clone() {
buffer.set_language(&json_lang);
buffer.set_highlight_syntax(true);
self.source_buffer.set_language(&json_lang);
self.source_buffer.set_highlight_syntax(true);
}
buffer.set_text(self.msg.source.clone()
self.source_buffer.set_text(self.msg.source.clone()
.unwrap_or("This message has no source.".to_string())
.as_str());
dialog.connect_response(move |d, res| {
let source_buffer = self.source_buffer.clone();
self.source_dialog.connect_response(move |d, res| {
if gtk::ResponseType::from(res) == gtk::ResponseType::Accept {
let atom = gdk::Atom::intern("CLIPBOARD");
let clipboard = gtk::Clipboard::get(&atom);
let start_iter = buffer.get_start_iter();
let end_iter = buffer.get_end_iter();
let start_iter = source_buffer.get_start_iter();
let end_iter = source_buffer.get_end_iter();
if let Some(src) = buffer.get_text(&start_iter, &end_iter, false) {
if let Some(src) = source_buffer.get_text(&start_iter, &end_iter, false) {
clipboard.set_text(&src);
}
} else {
......@@ -112,6 +120,44 @@ impl MessageMenu {
}
});
dialog.show();
self.source_dialog.show();
}
pub fn connect_message_menu(&self) {
let reply_button: gtk::ModelButton = self.builder
.get_object("reply_button")
.expect("Can't find reply_button in ui file.");
let copy_text_button: gtk::ModelButton = self.builder
.get_object("copy_text_button")
.expect("Can't find copy_text_button in ui file.");
let delete_message_button: gtk::ModelButton = self.builder
.get_object("delete_message_button")
.expect("Can't find delete_message_button in ui file.");
let view_source_button: gtk::ModelButton = self.builder
.get_object("view_source_button")
.expect("Can't find view_source_button in ui file.");
/* since this is used only by the main thread we can just use a simple Rc<RefCell> */
let this: Rc<RefCell<MessageMenu>> = Rc::new(RefCell::new(self.clone()));
reply_button.connect_clicked(clone!(this => move |_| {
this.borrow().insert_quote();
}));
copy_text_button.connect_clicked(clone!(this => move |_| {
this.borrow().copy_text();
}));
let backend = self.backend.clone();
delete_message_button.connect_clicked(clone!(this => move |_| {
backend.send(BKCommand::SendMsgRedaction(this.borrow().msg.clone())).unwrap();
}));
view_source_button.connect_clicked(clone!(this => move |_| {
this.borrow().display_source_dialog();
}));
}
}
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