~anteater/vgmms

6b16784fc66049ae570a6cbd15ac959d1d5689ba — anteater 3 months ago c370de1
chat_log.rs: rustfmt
1 files changed, 60 insertions(+), 42 deletions(-)

M src/chat_log.rs
M src/chat_log.rs => src/chat_log.rs +60 -42
@@ 114,11 114,12 @@ fn tree_view_interpret_click<'a>(tree_view: &'a TreeView, ev: &gdk::Event) -> Op
		let cellarea = col.cell_area().expect("cell-area property is NULL");
		let x = RefCell::new(None::<CellRenderer>);
		cellarea.foreach(|renderer| {
			*x.borrow_mut() = Some(renderer.clone()); false
			*x.borrow_mut() = Some(renderer.clone());
			false
		});
		let crm = x.into_inner().expect("no cell renderers");
		let crm: &CellRendererMessage = crm.downcast_ref().expect("cell renderer is not CellRendererMessage");
		let area = crm.message_area_at (&message.0, last_message_time, tree_view.upcast_ref(), cell_x, cell_y);
		let area = crm.message_area_at(&message.0, last_message_time, tree_view.upcast_ref(), cell_x, cell_y);
		//let area = owned_message_area_at(items.get_some(), msg_id.get_some().0, cell_x, cell_y);
		//loop{};
		//crm.message_area_at (items, cell_x, cell_y);


@@ 152,14 153,13 @@ fn tree_view_create_menu(tree_view: &TreeView, ev: Option<&gdk::Event>) -> Optio
					add_attachment_menuitems(&gmenu, att_id);
				}*/
				add_message_menuitems(&gmenu, &msg_id);
			}
			},
		}
		return Some((Menu::from_model(&gmenu), rect))
	}
	None
}


impl ChatLog {
	fn generate_column(attachments: AttachmentMapHandle) -> (TreeViewColumn, CellRendererMessage) {
		let col = TreeViewColumn::new();


@@ 176,7 176,10 @@ impl ChatLog {
		let _signal_id = message_cr.connect_local("notify::heights-valid", true, move |_| {
			if !mcr2.property::<bool>("heights-valid") {
				let col3 = col2.clone();
				glib::idle_add_local(move || {col3.queue_resize(); glib::Continue(false)});
				glib::idle_add_local(move || {
					col3.queue_resize();
					glib::Continue(false)
				});
			}
			None
		});


@@ 185,18 188,16 @@ impl ChatLog {
	}

	fn empty_log_model() -> gtk::ListStore {
		ListStore::new(&[
			GMessageId::static_type(),
			GMessage::static_type(),
			glib::types::Type::U64,
		])
		ListStore::new(&[GMessageId::static_type(), GMessage::static_type(), glib::types::Type::U64])
	}

	fn log_model_for_chat(chat: &Chat) -> ListStore {
		let mut cache = LOG_MODEL_CACHE.lock().unwrap();
		cache.entry(chat.clone())
		cache
			.entry(chat.clone())
			.or_insert_with(|| SendWrapper::new(Self::empty_log_model()))
			.clone().take()
			.clone()
			.take()
	}

	/** update the chat's view to match the chat's set of messages in the state. returns whether any changes occurred. */


@@ 209,8 210,7 @@ impl ChatLog {
			($model: expr, $iter: expr, $msg_id: ident) => {
				msg_id_value = $model.value(&$iter, 0);
				let gmsg_id = msg_id_value.get::<&GMessageId>();
				$msg_id = &gmsg_id.as_ref()
					.expect("column 0 is not MessageId").0;
				$msg_id = &gmsg_id.as_ref().expect("column 0 is not MessageId").0;
			};
		}



@@ 218,8 218,7 @@ impl ChatLog {
			($model: expr, $iter: expr, $msg: ident) => {
				msg_value = $model.value(&$iter, 1);
				let gmsg = msg_value.get::<&GMessage>();
				$msg = &gmsg.as_ref()
					.expect("column 1 is not MessageInfo").0;
				$msg = &gmsg.as_ref().expect("column 1 is not MessageInfo").0;
			};
		}



@@ 248,7 247,10 @@ impl ChatLog {

		let mut state_iter = match state.chat_messages(chat) {
			Some(iter) => iter,
			None => {view.clear(); return true}, /* if no messages, sync by clearing view and return */
			None => {
				view.clear();
				return true
			}, /* if no messages, sync by clearing view and return */
		};

		let mut time_iter = {


@@ 285,7 287,7 @@ impl ChatLog {
					last_time = msg.time;
				}
				return true;
			}
			},
		};

		/* index of the current row */


@@ 296,14 298,20 @@ impl ChatLog {

		/* iterate over the state and view in lockstep in reverse to find a difference */
		match state_vs_view {
			Greater|Less => {
			Greater | Less => {
				/* skip until IDs don't match */
				while {
					/* read both iters, advance state iter */
					get_msg_id!(view, &view_iter, view_msg_id);
					match state_iter.next_back() {
						Some(state_item) => {state_msg_id = state_item.0; state_msg = state_item.1; },
						None => {state_msg_id = &invalid_msg_id; state_msg = &invalid_msg;},
						Some(state_item) => {
							state_msg_id = state_item.0;
							state_msg = state_item.1;
						},
						None => {
							state_msg_id = &invalid_msg_id;
							state_msg = &invalid_msg;
						},
					};
					/* update last_time */
					last_time = time_iter.next_back().unwrap_or(0);


@@ 315,7 323,7 @@ impl ChatLog {
					view.iter_previous(&view_iter);
					view_idx -= 1;
				}
			}
			},
			Equal => {
				/* skip until values don't match (IDs should always match) */
				while {


@@ 323,8 331,14 @@ impl ChatLog {
					get_msg_id!(view, &view_iter, view_msg_id);
					get_msg!(view, &view_iter, view_msg);
					match state_iter.next_back() {
						Some(state_item) => {state_msg_id = state_item.0; state_msg = state_item.1; },
						None => {eprintln!("underflow in state message iter--this should not happen!"); return true},
						Some(state_item) => {
							state_msg_id = state_item.0;
							state_msg = state_item.1;
						},
						None => {
							eprintln!("underflow in state message iter--this should not happen!");
							return true
						},
					};
					/* update last_time */
					last_time = time_iter.next_back().unwrap_or(0);


@@ 344,7 358,7 @@ impl ChatLog {
						return false;
					}
				}
			}
			},
		}

		/* either insert into the view, edit a row in the view, or delete from the view */


@@ 366,8 380,14 @@ impl ChatLog {
					]);

					match state_iter.next_back() {
						Some(state_item) => {state_msg_id = state_item.0; state_msg = state_item.1; },
						None => {view.clear(); break},
						Some(state_item) => {
							state_msg_id = state_item.0;
							state_msg = state_item.1;
						},
						None => {
							view.clear();
							break
						},
					};
					/* update last_time */
					last_time = time_iter.next_back().unwrap_or(0);


@@ 377,9 397,7 @@ impl ChatLog {
				/* update value in store */
				#[cfg(log_model_debug)]
				println!("updating {}", view_idx);
				view.set(&view_iter,
					&[(1, &Value::from(&GMessage(state_msg.clone()))),
				]);
				view.set(&view_iter, &[(1, &Value::from(&GMessage(state_msg.clone())))]);
			},
			Less => {
				/* message[s] deleted */


@@ 411,7 429,7 @@ impl ChatLog {
								#[cfg(log_model_debug)]
								println!("all gone!");
								return true
							}
							},
						}
					}



@@ 419,9 437,7 @@ impl ChatLog {
				}
				/* update next one's last_time */
				view.iter_next(&view_iter);
				view.set(&view_iter,
					&[(2, &Value::from(&last_time))
				]);
				view.set(&view_iter, &[(2, &Value::from(&last_time))]);
			},
		}
		true


@@ 440,7 456,6 @@ fn add_message_cell_renderer(tree_view: &gtk::TreeView, attachments: AttachmentM
		message_cr.clear_image_cache();
		None
	});

}

impl Component for ChatLog {


@@ 451,23 466,26 @@ impl Component for ChatLog {
		let log_model = Self::log_model_for_chat(&props.chat);
		Self::update_log_model(&props.chat, &props.state.read().unwrap(), &log_model);

		ChatLog {
			props,
			log_model,
		}
		ChatLog { props, log_model }
	}

	fn change(&mut self, props: Self::Properties) -> UpdateAction<Self> {
		let mut changed = false;
		if self.props.chat != props.chat {
			println!("chatlog@{:x} ({:?}) change() -> {:?}", self as *const _ as usize, self.props.chat.numbers, props.chat.numbers);
			println!(
				"chatlog@{:x} ({:?}) change() -> {:?}",
				self as *const _ as usize, self.props.chat.numbers, props.chat.numbers
			);
			self.props = props;
			self.log_model = Self::log_model_for_chat(&self.props.chat);
			changed = true;
		}

		changed |= Self::update_log_model(&self.props.chat, &self.props.state.read().unwrap(), &self.log_model);
		println!("chatlog@{:x} ({:?}) change() (changed={})", self as *const _ as usize, self.props.chat.numbers, changed);
		println!(
			"chatlog@{:x} ({:?}) change() (changed={})",
			self as *const _ as usize, self.props.chat.numbers, changed
		);
		if changed {
			UpdateAction::Render
		} else {


@@ 481,7 499,7 @@ impl Component for ChatLog {
			Send(draft_items) => {
				self.props.on_send.send((self.props.chat.clone(), draft_items));
				UpdateAction::Render
			}
			},
		}
	}