~nicohman/signal-rs

d2e480133bd6dca0f077022dfccf6995c1fa88c2 — nicohman 9 months ago c755295
Add listbox sorting for messages
6 files changed, 29 insertions(+), 4 deletions(-)

M Cargo.lock
M Cargo.toml
M src/main.rs
M src/signal.rs
M src/widgets/chat_history.rs
M src/widgets/message.rs
M Cargo.lock => Cargo.lock +1 -0
@@ 2216,6 2216,7 @@ dependencies = [
 "gdk-pixbuf",
 "gio",
 "glib",
 "glib-macros",
 "glium",
 "gtk",
 "lazy_static",

M Cargo.toml => Cargo.toml +2 -1
@@ 31,4 31,5 @@ regex = "1.4.1"
reqwest = "0.10.8"
libhandy = "0.7.1"
confy = "0.4.0"
serde_repr = "0.1"
\ No newline at end of file
serde_repr = "0.1"
glib-macros = "0.10.0"
\ No newline at end of file

M src/main.rs => src/main.rs +3 -0
@@ 16,6 16,7 @@ extern crate tokio;
extern crate tokio_tungstenite;
extern crate tungstenite;
extern crate url;
extern crate glib_macros;
#[macro_use]
extern crate lazy_static;
use futures_util::StreamExt;


@@ 290,6 291,7 @@ where
                history.insert_bottom(Element::Message(m.clone()));
            }
        }
        //history.listbox.invalidate_sort();
        self.current_chat.replace(Some(tel.clone()));
        self.last_id.replace(history.get_last_id());
        self.message_box.add(&history.listbox);


@@ 336,6 338,7 @@ where
                        }
                        self.last_id.replace(hist.get_last_id());
                    }
                    //hist.listbox.invalidate_sort();
                }
            }
            _ => {}

M src/signal.rs => src/signal.rs +2 -0
@@ 1,3 1,4 @@
use gio::subclass::BoxedType;
use crate::Message;
use enum_variant_type::EnumVariantType;
use futures_util::sink::SinkExt;


@@ 7,6 8,7 @@ use regex::Regex;
use serde::{Deserialize, Deserializer, Serialize};
use serde_repr::*;
use std::convert::TryFrom;
use glib_macros::*;
use tokio_tungstenite::tungstenite::protocol::Message as TMessage;
use tokio_tungstenite::*;
lazy_static! {

M src/widgets/chat_history.rs => src/widgets/chat_history.rs +16 -2
@@ 1,5 1,7 @@
use crate::*;
use std::collections::VecDeque;


/// A widget that displays and tracks
pub struct ChatHistory {
    pub list: VecDeque<Element>,


@@ 16,6 18,18 @@ impl ChatHistory {
            .selection_mode(SelectionMode::None)
            .activate_on_single_click(false)
            .build();
            listbox.set_sort_func(Some(std::boxed::Box::new(|a: &ListBoxRow, b: &ListBoxRow| {
                let a : String = a.get_property("name").unwrap().get().unwrap().unwrap();
                let b : String = b.get_property("name").unwrap().get().unwrap().unwrap();
                let diff = a.parse::<i64>().unwrap() - b.parse::<i64>().unwrap();
                if diff < 0 {
                    -1
                } else if diff > 0 {
                    1
                } else {
                    0
                }
            })));
        listbox.get_style_context().add_class("history-listbox");
        ChatHistory {
            tel,


@@ 27,12 41,12 @@ impl ChatHistory {
    }
    /// Inserts an element into the history at the top
    pub fn insert_top(&mut self, element: Element) {
        self.listbox.insert(element.get_lbr(), 0);
        self.listbox.add(element.get_lbr());
        self.list.push_back(element);
    }
    /// Inserts an element into the history at the bottom
    pub fn insert_bottom(&mut self, element: Element) {
        self.listbox.insert(element.get_lbr(), -1);
        self.listbox.add(element.get_lbr());
        self.list.push_front(element);
    }
    /// Get the ID of the latest message

M src/widgets/message.rs => src/widgets/message.rs +5 -1
@@ 28,6 28,7 @@ impl MessageUi {
            .selectable(false)
            .activatable(false)
            .build();
            row.set_property("name", &msg.sent_at.to_string()).unwrap();
        row.add(&ui);
        row.show_all();
        MessageUi { row, ui }


@@ 50,8 51,10 @@ impl MessageUi {
            for attachment in attachments.into_iter() {
                match attachment.c_type {
                    AttachmentType::Image => {
                        let image = Image::from_file(&attachment.file);
                        let image = Image::from_pixbuf(None);       
                        msg_box.add(&image);
                        let pixbuf = gdk_pixbuf::Pixbuf::from_file_at_scale(&attachment.file, 300, 200, true).expect("Couldn't construct pixbuf from file");
                        image.set_from_pixbuf(Some(&pixbuf));
                    }
                    _ => {}
                }


@@ 64,6 67,7 @@ impl MessageUi {
                .as_millis()
                - msg.sent_at as u128,
        )));
        //let when_label = Label::new(Some(&msg.sent_at.to_string()));
        add_class(&msg_box, "msg");
        add_class(&when_label, "when-label");
        when_label.set_halign(Align::Start);