Skip to content

Commit

Permalink
cleanup SQUASHME
Browse files Browse the repository at this point in the history
  • Loading branch information
benjajaja committed Oct 19, 2023
1 parent faf1b60 commit 2edc335
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 38 deletions.
18 changes: 9 additions & 9 deletions docs/example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
}
},
"default_room": "#iamb-users:0x.badd.cafe",
"image_preview": {
"protocol": {
"type": "sixel"
},
"size": {
"width": 66,
"height": 10
}
}
"image_preview": {
"protocol": {
"type": "sixel"
},
"size": {
"width": 66,
"height": 10
}
}
},
"dirs": {
"cache": "/home/user/.cache/iamb/",
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
src = ./.;
cargoLock = {
lockFile = ./Cargo.lock;
# Remove this once modalkit gets pinned by version again.
outputHashes = {
"modalkit-0.0.16" = "sha256-mjAD1v0r2+SzPdoB2wZ/5iJ1NZK+3OSvCYcUZ5Ef38Y=";
};
Expand Down
11 changes: 8 additions & 3 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use modalkit::{
},
};

use crate::message::ImageBackend;
use crate::message::ImageStatus;
use crate::preview::{source_from_event, spawn_insert_preview};
use crate::{
message::{Message, MessageEvent, MessageKey, MessageTimeStamp, Messages},
Expand Down Expand Up @@ -616,6 +616,7 @@ impl RoomInfo {
self.messages.get(self.get_message_key(event_id)?)
}

/// Get an event for an identifier as mutable.
pub fn get_event_mut(&mut self, event_id: &EventId) -> Option<&mut Message> {
self.messages.get_mut(self.keys.get(event_id)?.to_message_key()?)
}
Expand Down Expand Up @@ -710,12 +711,14 @@ impl RoomInfo {
}
}

/// Insert a new message event, and spawn a task for image-preview if it has an image
/// attachment.
pub fn insert_with_preview(
&mut self,
room_id: OwnedRoomId,
store: AsyncProgramStore,
picker: Option<Picker>,
ev: matrix_sdk::ruma::events::MessageLikeEvent<RoomMessageEventContent>,
ev: RoomMessageEvent,
settings: &mut ApplicationSettings,
media: matrix_sdk::Media,
) {
Expand All @@ -726,7 +729,7 @@ impl RoomInfo {
if let (Some(msg), Some(image_preview)) =
(self.get_event_mut(&event_id), &settings.tunables.image_preview)
{
msg.image_backend = ImageBackend::Downloading(image_preview.size.clone());
msg.image_preview = ImageStatus::Downloading(image_preview.size.clone());
spawn_insert_preview(
store,
room_id,
Expand Down Expand Up @@ -892,6 +895,8 @@ pub struct ChatStore {

/// Information gathered by the background thread.
pub sync_info: SyncInfo,

/// Image preview "protocol" picker.
pub picker: Option<Picker>,
}

Expand Down
11 changes: 5 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ pub struct ImagePreviewValues {
pub protocol: Option<ImagePreviewProtocolValues>,
}
#[derive(Clone, Deserialize)]
pub struct ImagePreviewSize {
pub width: usize,
pub height: usize,
}
#[derive(Clone, Deserialize)]
pub struct ImagePreviewProtocolValues {
pub r#type: Option<ProtocolType>,
pub font_size: Option<(u16, u16)>,
Expand All @@ -266,12 +271,6 @@ impl Default for ImagePreviewValues {
}
}

#[derive(Clone, Deserialize)]
pub struct ImagePreviewSize {
pub width: usize,
pub height: usize,
}

#[derive(Clone)]
pub struct TunableValues {
pub log_level: Level,
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ impl Application {

let mut locked = store.lock().await;

#[allow(unused_variables)]
if let Some(image_preview) = settings.tunables.image_preview.as_ref() {
let picker_from_settings = match image_preview.protocol.as_ref() {
// User forced type and font_size: use that.
Expand Down
26 changes: 12 additions & 14 deletions src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,9 @@ impl<'a> MessageFormatter<'a> {
}
}

pub enum ImageBackend {
pub enum ImageStatus {
None,
Downloading(ImagePreviewSize),
Preparing(ImagePreviewSize),
Loaded(Box<dyn Protocol>),
Error(String),
}
Expand All @@ -601,7 +600,7 @@ pub struct Message {
pub timestamp: MessageTimeStamp,
pub downloaded: bool,
pub html: Option<StyleTree>,
pub image_backend: ImageBackend,
pub image_preview: ImageStatus,
}

impl Message {
Expand All @@ -615,7 +614,7 @@ impl Message {
timestamp,
downloaded,
html,
image_backend: ImageBackend::None,
image_preview: ImageStatus::None,
}
}

Expand Down Expand Up @@ -699,6 +698,7 @@ impl Message {
}
}

/// Get the image preview Protocol and x,y offset, based on get_render_format.
pub fn line_preview<'a>(
&'a self,
prev: Option<&Message>,
Expand All @@ -717,10 +717,10 @@ impl Message {
Some(prev) if !prev.timestamp.same_day(&self.timestamp) => 1,
_ => 0,
};
if let ImageBackend::Loaded(backend) = &self.image_backend {
if let ImageStatus::Loaded(backend) = &self.image_preview {
return Some((backend.as_ref(), x, date_y));
} else if let Some(reply) = self.reply_to().and_then(|e| info.get_event(&e)) {
if let ImageBackend::Loaded(backend) = &reply.image_backend {
if let ImageStatus::Loaded(backend) = &reply.image_preview {
// The reply should be offset a bit:
return Some((backend.as_ref(), x + 2, date_y + 1));
}
Expand Down Expand Up @@ -838,18 +838,15 @@ impl Message {
msg.to_mut().push_str(" \u{2705}");
}

if let Some(placeholder) = match &self.image_backend {
ImageBackend::None => None,
ImageBackend::Downloading(image_preview_size) => {
if let Some(placeholder) = match &self.image_preview {
ImageStatus::None => None,
ImageStatus::Downloading(image_preview_size) => {
Some(Message::placeholder_frame(Some("Downloading..."), image_preview_size))
},
ImageBackend::Preparing(image_preview_size) => {
Some(Message::placeholder_frame(Some("Preparing..."), image_preview_size))
},
ImageBackend::Loaded(backend) => {
ImageStatus::Loaded(backend) => {
Some(Message::placeholder_frame(None, &backend.rect().into()))
},
ImageBackend::Error(err) => Some(format!("[Image error: {err}]")),
ImageStatus::Error(err) => Some(format!("[Image error: {err}]")),
} {
msg.to_mut().insert_str(0, &placeholder);
}
Expand All @@ -866,6 +863,7 @@ impl Message {
settings.get_user_span(self.sender.as_ref(), info)
}

/// Before the image is loaded, already display a placeholder frame of the image size.
fn placeholder_frame(text: Option<&str>, image_preview_size: &ImagePreviewSize) -> String {
let ImagePreviewSize { width, height } = image_preview_size;
let mut placeholder = "\u{230c}".to_string();
Expand Down
9 changes: 4 additions & 5 deletions src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ratatui_image::Resize;
use crate::{
base::{AsyncProgramStore, ChatStore, IambError},
config::ImagePreviewSize,
message::ImageBackend,
message::ImageStatus,
};

pub fn source_from_event(
Expand All @@ -50,7 +50,7 @@ impl From<Rect> for ImagePreviewSize {
}
}

// Download and prepare the preview, and then lock the store to insert it.
/// Download and prepare the preview, and then lock the store to insert it.
pub fn spawn_insert_preview(
store: AsyncProgramStore,
room_id: OwnedRoomId,
Expand Down Expand Up @@ -99,7 +99,6 @@ pub fn spawn_insert_preview(
))
})
.and_then(|(picker, msg, image_preview)| {
msg.image_backend = ImageBackend::Preparing(image_preview.size.clone());
picker
.new_static_fit(img, image_preview.size.into(), Resize::Fit)
.map_err(|err| IambError::Preview(format!("{err:?}")))
Expand All @@ -109,7 +108,7 @@ pub fn spawn_insert_preview(
try_set_msg_preview_error(&mut locked.application, room_id, event_id, err);
},
Ok((backend, msg)) => {
msg.image_backend = ImageBackend::Loaded(backend);
msg.image_preview = ImageStatus::Loaded(backend);
},
}
},
Expand All @@ -130,7 +129,7 @@ fn try_set_msg_preview_error(
.get_event_mut(&event_id)
.ok_or_else(|| IambError::Preview("Message not found".to_string()))
{
Ok(msg) => msg.image_backend = ImageBackend::Error(format!("{err:?}")),
Ok(msg) => msg.image_preview = ImageStatus::Error(format!("{err:?}")),
Err(err) => {
tracing::error!(
"Failed to set error on msg.image_backend for event {}, room {}: {}",
Expand Down

0 comments on commit 2edc335

Please sign in to comment.