From 9233143a9c419ffef304a33a2d4ca4f1ece06fe7 Mon Sep 17 00:00:00 2001 From: lomirus Date: Tue, 31 May 2022 02:07:18 +0800 Subject: [PATCH] fix(sidebar): last message cannot update --- Cargo.lock | 4 +- Cargo.toml | 4 +- src/app/main/sidebar/chat_item.rs | 79 +++++++++++++------------------ src/app/main/sidebar/mod.rs | 2 - 4 files changed, 38 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ab575df..cbaf36d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1625,7 +1625,7 @@ checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64" [[package]] name = "relm4" version = "0.5.0-beta.1" -source = "git+https://github.com/Relm4/Relm4.git?rev=74659719607331a591dfabf90dd33b7c590bc2fd#74659719607331a591dfabf90dd33b7c590bc2fd" +source = "git+https://github.com/Relm4/Relm4.git?rev=968d09c9417dce6f84446948523e3befc4954d50#968d09c9417dce6f84446948523e3befc4954d50" dependencies = [ "async-broadcast", "async-oneshot", @@ -1642,7 +1642,7 @@ dependencies = [ [[package]] name = "relm4-macros" version = "0.5.0-beta.1" -source = "git+https://github.com/Relm4/Relm4.git?rev=74659719607331a591dfabf90dd33b7c590bc2fd#74659719607331a591dfabf90dd33b7c590bc2fd" +source = "git+https://github.com/Relm4/Relm4.git?rev=968d09c9417dce6f84446948523e3befc4954d50#968d09c9417dce6f84446948523e3befc4954d50" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 6a6be91..9a583ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,8 @@ edition = "2021" [dependencies.relm4] git = "https://github.com/Relm4/Relm4.git" -# next, 2022/05/24 -rev = "74659719607331a591dfabf90dd33b7c590bc2fd" +# next, 2022/05/25 +rev = "968d09c9417dce6f84446948523e3befc4954d50" features = [ "macros", "libadwaita", diff --git a/src/app/main/sidebar/chat_item.rs b/src/app/main/sidebar/chat_item.rs index 19c257c..5465904 100644 --- a/src/app/main/sidebar/chat_item.rs +++ b/src/app/main/sidebar/chat_item.rs @@ -2,7 +2,7 @@ use relm4::factory::{DynamicIndex, FactoryComponent}; use relm4::{adw, gtk, Sender}; use adw::{prelude::*, Avatar}; -use gtk::{Align, Box, Label, ListBox, ListBoxRow, Orientation}; +use gtk::{Align, Box, Label, ListBox, Orientation}; use crate::handler::{FRIEND_LIST, GROUP_LIST}; @@ -16,15 +16,43 @@ pub struct ChatItem { pub last_message: String, } +#[relm4::factory(pub)] impl FactoryComponent for ChatItem { - /// (account, is_group, last_message) type InitParams = (i64, bool, String); - type Widgets = (); + type Widgets = ChatItemWidgets; type Input = (); type Output = (); type Command = (); type CommandOutput = (); - type Root = Box; + + view! { + root = Box { + set_margin_top: 8, + set_margin_bottom: 8, + Avatar { + set_text: Some(&self.name), + set_show_initials: true, + set_size: 48, + set_margin_end: 8 + }, + Box { + set_orientation: Orientation::Vertical, + set_halign: Align::Start, + set_spacing: 8, + Label { + set_xalign: 0.0, + set_text: self.name.as_str(), + add_css_class: "heading" + }, + #[name = "last_message"] + Label { + set_text: self.last_message.as_str(), + add_css_class: "caption", + set_xalign: 0.0, + }, + }, + } + } fn init_model( init_params: Self::InitParams, @@ -60,46 +88,7 @@ impl FactoryComponent for ChatItem { } } - fn init_root(&self) -> Self::Root { - Box::default() - } - - fn init_widgets( - &mut self, - _index: &DynamicIndex, - root: &Self::Root, - _returned_widget: &ListBoxRow, - _input: &Sender, - _output: &Sender, - ) -> Self::Widgets { - relm4::view! { - item = Box { - set_margin_top: 8, - set_margin_bottom: 8, - append = &Avatar { - set_text: Some(&self.name), - set_show_initials: true, - set_size: 48, - set_margin_end: 8 - }, - append = &Box { - set_orientation: Orientation::Vertical, - set_halign: Align::Start, - set_spacing: 8, - append = &Label { - set_xalign: 0.0, - set_text: self.name.as_str(), - add_css_class: "heading" - }, - append = &Label { - set_text: self.last_message.as_str(), - add_css_class: "caption", - set_xalign: 0.0, - }, - }, - } - } - - root.append(&item); + fn pre_view() { + widgets.last_message.set_label(&self.last_message); } } diff --git a/src/app/main/sidebar/mod.rs b/src/app/main/sidebar/mod.rs index cba6e97..ad2a8b5 100644 --- a/src/app/main/sidebar/mod.rs +++ b/src/app/main/sidebar/mod.rs @@ -36,13 +36,11 @@ impl SidebarModel { break; } } - chats_list.render_changes(); } fn insert_chat_item(&self, account: i64, is_group: bool, last_message: String) { let mut chats_list = self.chats_list.borrow_mut(); chats_list.push_front((account, is_group, last_message)); - chats_list.render_changes(); } }