Commit c010b24c69

c010b24c6936488f7cfb557b52f0bf873e19c762

parent: 362a323a5b

Unregistered key

vaw <git@nlih.de> · 2026-09-04 23:26 UTC
committer: <noreply@github.com>

Use `SlicedProtocol` to render images that are partially off-screen (#667)

Layout: unified · split

Cargo.lock +57 −5
@@ -1020,6 +1020,7 @@ dependencies = [
10201020 "itoa",
10211021 "rustversion",
10221022 "ryu",
1023 "serde",
10231024 "static_assertions",
10241025]
10251026
@@ -1180,6 +1181,23 @@ version = "0.8.21"
11801181source = "registry+https://github.com/rust-lang/crates.io-index"
11811182checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
11821183
1184[[package]]
1185name = "crossterm"
1186version = "0.28.1"
1187source = "registry+https://github.com/rust-lang/crates.io-index"
1188checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6"
1189dependencies = [
1190 "bitflags 2.13.0",
1191 "crossterm_winapi",
1192 "mio",
1193 "parking_lot 0.12.5",
1194 "rustix 0.38.44",
1195 "serde",
1196 "signal-hook",
1197 "signal-hook-mio",
1198 "winapi",
1199]
1200
11831201[[package]]
11841202name = "crossterm"
11851203version = "0.29.0"
@@ -1193,6 +1211,7 @@ dependencies = [
11931211 "mio",
11941212 "parking_lot 0.12.5",
11951213 "rustix 1.1.4",
1214 "serde",
11961215 "signal-hook",
11971216 "signal-hook-mio",
11981217 "winapi",
@@ -3701,7 +3720,7 @@ dependencies = [
37013720 "anymap2",
37023721 "arboard",
37033722 "bitflags 2.13.0",
3704 "crossterm",
3723 "crossterm 0.29.0",
37053724 "derive_more 2.1.1",
37063725 "editor-types",
37073726 "intervaltree",
@@ -3722,7 +3741,7 @@ version = "0.0.26"
37223741source = "registry+https://github.com/rust-lang/crates.io-index"
37233742checksum = "a9070725ef25a76d7dd3431c5ee4d108780dfc42d097d194d397721843b8ac30"
37243743dependencies = [
3725 "crossterm",
3744 "crossterm 0.29.0",
37263745 "intervaltree",
37273746 "libc",
37283747 "modalkit",
@@ -3903,6 +3922,12 @@ dependencies = [
39033922 "libc",
39043923]
39053924
3925[[package]]
3926name = "numtoa"
3927version = "0.2.4"
3928source = "registry+https://github.com/rust-lang/crates.io-index"
3929checksum = "6aa2c4e539b869820a2b82e1aef6ff40aa85e65decdd5185e83fb4b1249cd00f"
3930
39063931[[package]]
39073932name = "oauth2"
39083933version = "5.0.0"
@@ -4826,6 +4851,7 @@ dependencies = [
48264851 "ratatui-crossterm",
48274852 "ratatui-macros",
48284853 "ratatui-termina",
4854 "ratatui-termion",
48294855 "ratatui-termwiz",
48304856 "ratatui-widgets",
48314857 "serde",
@@ -4860,16 +4886,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
48604886checksum = "567584a3b0e6a8203c23de40b4861497266725eb5363dbfd18a1edd603cca9f0"
48614887dependencies = [
48624888 "cfg-if",
4863 "crossterm",
4889 "crossterm 0.28.1",
4890 "crossterm 0.29.0",
48644891 "instability",
48654892 "ratatui-core",
48664893]
48674894
48684895[[package]]
48694896name = "ratatui-image"
4870version = "11.0.6"
4897version = "11.0.8"
48714898source = "registry+https://github.com/rust-lang/crates.io-index"
4872checksum = "e000b7a22eae639460bc6ec8bb1cc689ecae5b0ed21935cd7d7dd52d38270c86"
4899checksum = "399baa06251282b0c1bbb5762848cf98a6b7b84f44af7f0daf3213ace16df4f0"
48734900dependencies = [
48744901 "base64-simd",
48754902 "icy_sixel",
@@ -4905,6 +4932,17 @@ dependencies = [
49054932 "termina",
49064933]
49074934
4935[[package]]
4936name = "ratatui-termion"
4937version = "0.1.2"
4938source = "registry+https://github.com/rust-lang/crates.io-index"
4939checksum = "87c732202fa5a71a9da0991013f0853e53f87048f45198e3a1ca3ee722accc2f"
4940dependencies = [
4941 "instability",
4942 "ratatui-core",
4943 "termion",
4944]
4945
49084946[[package]]
49094947name = "ratatui-termwiz"
49104948version = "0.1.2"
@@ -6058,6 +6096,17 @@ dependencies = [
60586096 "phf_codegen 0.11.3",
60596097]
60606098
6099[[package]]
6100name = "termion"
6101version = "4.0.6"
6102source = "registry+https://github.com/rust-lang/crates.io-index"
6103checksum = "f44138a9ae08f0f502f24104d82517ef4da7330c35acd638f1f29d3cd5475ecb"
6104dependencies = [
6105 "libc",
6106 "numtoa",
6107 "serde",
6108]
6109
60616110[[package]]
60626111name = "termios"
60636112version = "0.3.3"
@@ -6092,6 +6141,7 @@ dependencies = [
60926141 "pest",
60936142 "pest_derive",
60946143 "phf 0.11.3",
6144 "serde",
60956145 "sha2",
60966146 "signal-hook",
60976147 "siphasher",
@@ -7101,6 +7151,7 @@ checksum = "692daff6d93d94e29e4114544ef6d5c942a7ed998b37abdc19b17136ea428eb7"
71017151dependencies = [
71027152 "getrandom 0.3.4",
71037153 "mac_address",
7154 "serde",
71047155 "sha2",
71057156 "thiserror 1.0.69",
71067157 "uuid",
@@ -7115,6 +7166,7 @@ dependencies = [
71157166 "csscolorparser",
71167167 "deltae",
71177168 "lazy_static 1.5.0",
7169 "serde",
71187170 "wezterm-dynamic",
71197171]
71207172
Cargo.toml +2 −2
@@ -54,7 +54,7 @@ mime_guess = "2.0.4"
5454nom = "8.0"
5555open = "5.0.0"
5656rand = "0.10"
57ratatui = "0.30.2"
57ratatui = { version = "0.30.2", features = ["serde"] }
5858regex = "1.5"
5959rpassword = "7.2"
6060serde = "1.0"
@@ -107,7 +107,7 @@ version = "1.24.1"
107107features = ["macros", "net", "rt-multi-thread", "sync", "time"]
108108
109109[dependencies.ratatui-image]
110version = "11.0.6"
110version = "11.0.8"
111111default-features = false
112112features = ["serde", "crossterm", "image-defaults"]
113113
src/config.rs +4 −15
@@ -17,6 +17,7 @@ use matrix_sdk::authentication::matrix::MatrixSession;
1717use matrix_sdk::media::MediaRetentionPolicy;
1818use matrix_sdk::reqwest::header::{HeaderMap, HeaderValue};
1919use matrix_sdk::ruma::{OwnedDeviceId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, UserId};
20use ratatui::layout::Size;
2021use ratatui::style::{Color, Modifier as StyleModifier, Style};
2122use ratatui::text::Span;
2223use ratatui_image::FilterType;
@@ -695,7 +696,7 @@ pub struct Notifications {
695696pub struct ImagePreviewValues {
696697 pub enabled: bool,
697698 pub lazy_load: bool,
698 pub size: ImagePreviewSize,
699 pub size: Size,
699700 pub protocol: ImagePreviewProtocolValues,
700701}
701702
@@ -703,7 +704,7 @@ pub struct ImagePreviewValues {
703704pub struct ImagePreview {
704705 pub enabled: Option<bool>,
705706 pub lazy_load: Option<bool>,
706 pub size: Option<ImagePreviewSize>,
707 pub size: Option<Size>,
707708 pub protocol: Option<ImagePreviewProtocolValues>,
708709}
709710
@@ -712,24 +713,12 @@ impl ImagePreview {
712713 ImagePreviewValues {
713714 enabled: self.enabled.unwrap_or(true),
714715 lazy_load: self.lazy_load.unwrap_or(true),
715 size: self.size.unwrap_or_default(),
716 size: self.size.unwrap_or(Size { width: 66, height: 10 }),
716717 protocol: self.protocol.unwrap_or_default(),
717718 }
718719 }
719720}
720721
721#[derive(Clone, Copy, Deserialize, Debug)]
722pub struct ImagePreviewSize {
723 pub width: usize,
724 pub height: usize,
725}
726
727impl Default for ImagePreviewSize {
728 fn default() -> Self {
729 ImagePreviewSize { width: 66, height: 10 }
730 }
731}
732
733722#[derive(Clone, Debug, Deserialize, Default)]
734723pub struct ImagePreviewProtocolValues {
735724 pub r#type: Option<ProtocolType>,
src/message/mod.rs +39 −20
@@ -17,7 +17,9 @@ use matrix_sdk::ruma::events::room::message::RoomMessageEventContentWithoutRelat
1717use matrix_sdk::ruma::events::sticker::{OriginalStickerEvent, RedactedStickerEvent, StickerEvent};
1818use matrix_sdk::ruma::events::{AnyRedactionEvent, MessageLikeEvent};
1919use matrix_sdk::send_queue::SendHandle;
20use ratatui::layout::Size;
2021use ratatui::style::Color;
22use ratatui_image::sliced::SlicedProtocol;
2123use unicode_width::UnicodeWidthStr;
2224
2325use matrix_sdk::ruma::{
@@ -59,10 +61,8 @@ use ratatui::{
5961
6062use modalkit::editing::cursor::Cursor;
6163use modalkit::prelude::*;
62use ratatui_image::protocol::Protocol;
6364
6465use crate::base::MessageEdits;
65use crate::config::ImagePreviewSize;
6666use crate::preview::{ImageStatus, PreviewKind, PreviewManager};
6767use crate::{
6868 base::RoomInfo,
@@ -80,7 +80,7 @@ pub use self::compose::{text_to_message, text_to_text_message_event_content};
8080use self::state::{body_cow_state, html_state};
8181pub use html::TreeGenState;
8282
83type ProtocolPreview<'a> = (&'a Protocol, u16, u16);
83type ProtocolPreview<'a> = (&'a SlicedProtocol, u16, u16);
8484
8585/// The key used for uniquely identifying messages within a room and its threads.
8686///
@@ -184,17 +184,17 @@ fn hash_message_id(id: &MessageId) -> Option<usize> {
184184fn placeholder_frame(
185185 text: Option<&str>,
186186 outer_width: usize,
187 image_preview_size: &ImagePreviewSize,
187 image_preview_size: &Size,
188188) -> Option<String> {
189 let ImagePreviewSize { width, height } = image_preview_size;
190 let width = usize::min(*width, outer_width);
189 let Size { width, height } = image_preview_size;
190 let width = usize::min(*width as usize, outer_width);
191191 if width < 2 || *height < 2 {
192192 return None;
193193 }
194194 let mut placeholder = "\u{230c}".to_string();
195195 placeholder.push_str(&" ".repeat(width - 2));
196196 placeholder.push('\u{230d}');
197 placeholder.push_str(&"\n".repeat((height - 1) / 2));
197 placeholder.push_str(&"\n".repeat((*height as usize - 1) / 2));
198198
199199 if *height > 2 &&
200200 let Some(text) = text &&
@@ -204,7 +204,7 @@ fn placeholder_frame(
204204 placeholder.push_str(text);
205205 }
206206
207 placeholder.push_str(&"\n".repeat(height / 2));
207 placeholder.push_str(&"\n".repeat(*height as usize / 2));
208208 placeholder.push('\u{230e}');
209209 placeholder.push_str(&" ".repeat(width - 2));
210210 placeholder.push_str("\u{230f}\n");
@@ -1053,6 +1053,25 @@ impl Message {
10531053
10541054 !prev.timestamp.same_day(self.timestamp)
10551055 }
1056 pub fn message_column_width(
1057 viewctx: &ViewportContext<MessageCursor>,
1058 settings: &ApplicationSettings,
1059 ) -> usize {
1060 let width = viewctx.get_width();
1061 let user_gutter = settings.tunables.user_gutter_width;
1062
1063 if user_gutter + TIME_GUTTER + READ_GUTTER + MIN_MSG_LEN <= width &&
1064 settings.tunables.read_receipt_display
1065 {
1066 width - user_gutter - TIME_GUTTER - READ_GUTTER
1067 } else if user_gutter + TIME_GUTTER + MIN_MSG_LEN <= width {
1068 width - user_gutter - TIME_GUTTER
1069 } else if user_gutter + MIN_MSG_LEN <= width {
1070 width - user_gutter
1071 } else {
1072 width.saturating_sub(2)
1073 }
1074 }
10561075
10571076 fn get_render_format<'a>(
10581077 &'a self,
@@ -1215,7 +1234,7 @@ impl Message {
12151234 style: Style,
12161235 settings: &'a ApplicationSettings,
12171236 previews: &'a PreviewManager,
1218 ) -> (Text<'a>, Option<&'a Protocol>) {
1237 ) -> (Text<'a>, Option<&'a SlicedProtocol>) {
12191238 let mut proto = None;
12201239 let placeholder = match self
12211240 .image_preview()
@@ -1230,7 +1249,7 @@ impl Message {
12301249 },
12311250 Some(ImageStatus::Loaded(backend)) => {
12321251 proto = Some(backend);
1233 placeholder_frame(Some("No Space..."), width, &backend.size().into())
1252 placeholder_frame(None, width, &backend.size())
12341253 },
12351254 Some(ImageStatus::Error(err)) => Some(format!("[Image error: {err}]\n")),
12361255 };
@@ -1582,7 +1601,7 @@ pub mod tests {
15821601 }
15831602
15841603 assert_eq!(
1585 placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 4 }),
1604 placeholder_frame(None, 4, &Size { width: 4, height: 4 }),
15861605 pretty_frame_test(
15871606 r#"
15881607⌌ ⌍
@@ -1594,7 +1613,7 @@ pub mod tests {
15941613 );
15951614
15961615 assert_eq!(
1597 placeholder_frame(None, 2, &ImagePreviewSize { width: 4, height: 4 }),
1616 placeholder_frame(None, 2, &Size { width: 4, height: 4 }),
15981617 pretty_frame_test(
15991618 r#"
16001619⌌⌍
@@ -1604,12 +1623,12 @@ pub mod tests {
16041623"#
16051624 )
16061625 );
1607 assert_eq!(placeholder_frame(None, 4, &ImagePreviewSize { width: 1, height: 4 }), None);
1626 assert_eq!(placeholder_frame(None, 4, &Size { width: 1, height: 4 }), None);
16081627
1609 assert_eq!(placeholder_frame(None, 4, &ImagePreviewSize { width: 4, height: 1 }), None);
1628 assert_eq!(placeholder_frame(None, 4, &Size { width: 4, height: 1 }), None);
16101629
16111630 assert_eq!(
1612 placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 4 }),
1631 placeholder_frame(Some("OK"), 4, &Size { width: 4, height: 4 }),
16131632 pretty_frame_test(
16141633 r#"
16151634⌌ ⌍
@@ -1620,7 +1639,7 @@ pub mod tests {
16201639 )
16211640 );
16221641 assert_eq!(
1623 placeholder_frame(Some("OK"), 6, &ImagePreviewSize { width: 6, height: 6 }),
1642 placeholder_frame(Some("OK"), 6, &Size { width: 6, height: 6 }),
16241643 pretty_frame_test(
16251644 r#"
16261645⌌ ⌍
@@ -1633,7 +1652,7 @@ pub mod tests {
16331652 )
16341653 );
16351654 assert_eq!(
1636 placeholder_frame(Some("OK"), 6, &ImagePreviewSize { width: 6, height: 7 }),
1655 placeholder_frame(Some("OK"), 6, &Size { width: 6, height: 7 }),
16371656 pretty_frame_test(
16381657 r#"
16391658⌌ ⌍
@@ -1647,7 +1666,7 @@ pub mod tests {
16471666 )
16481667 );
16491668 assert_eq!(
1650 placeholder_frame(Some("idontfit"), 4, &ImagePreviewSize { width: 4, height: 4 }),
1669 placeholder_frame(Some("idontfit"), 4, &Size { width: 4, height: 4 }),
16511670 pretty_frame_test(
16521671 r#"
16531672⌌ ⌍
@@ -1658,7 +1677,7 @@ pub mod tests {
16581677 )
16591678 );
16601679 assert_eq!(
1661 placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 4, height: 2 }),
1680 placeholder_frame(Some("OK"), 4, &Size { width: 4, height: 2 }),
16621681 pretty_frame_test(
16631682 r#"
16641683⌌ ⌍
@@ -1667,7 +1686,7 @@ pub mod tests {
16671686 )
16681687 );
16691688 assert_eq!(
1670 placeholder_frame(Some("OK"), 4, &ImagePreviewSize { width: 2, height: 3 }),
1689 placeholder_frame(Some("OK"), 4, &Size { width: 2, height: 3 }),
16711690 pretty_frame_test(
16721691 r#"
16731692⌌⌍
src/preview.rs +16 −46
@@ -5,20 +5,21 @@ use matrix_sdk::{
55 media::{MediaFormat, MediaRequestParameters, UniqueKey},
66 ruma::events::room::MediaSource,
77};
8use ratatui::layout::{Rect, Size};
9use ratatui_image::{FilterType, Resize, picker::Picker, protocol::Protocol};
8use ratatui::layout::Size;
9use ratatui_image::sliced::SlicedProtocol;
10use ratatui_image::{FilterType, Resize, picker::Picker};
1011use tokio::sync::Semaphore;
1112
1213use crate::{
1314 base::{AsyncProgramStore, IambError},
14 config::{ApplicationSettings, ImagePreviewSize, ImagePreviewValues},
15 config::{ApplicationSettings, ImagePreviewValues},
1516 worker::Requester,
1617};
1718
1819pub enum ImageStatus {
19 Queued(ImagePreviewSize),
20 Downloading(ImagePreviewSize),
21 Loaded(Protocol),
20 Queued(Size),
21 Downloading(Size),
22 Loaded(SlicedProtocol),
2223 Error(String),
2324}
2425
@@ -29,10 +30,10 @@ pub enum PreviewKind {
2930}
3031
3132impl PreviewKind {
32 fn image_size(self, image_preview: &ImagePreviewValues) -> ImagePreviewSize {
33 fn image_size(self, image_preview: &ImagePreviewValues) -> Size {
3334 match self {
3435 Self::Message => image_preview.size,
35 Self::Reaction => ImagePreviewSize { width: 2, height: 1 },
36 Self::Reaction => Size { width: 2, height: 1 },
3637 }
3738 }
3839}
@@ -108,7 +109,6 @@ impl PreviewManager {
108109 }
109110}
110111
111#[cfg(not(windows))]
112112fn picker_from_query() -> Picker {
113113 // XXX: documentation says to use this query on alternate screen but it seems to be fine
114114 Picker::from_query_stdio().unwrap_or_else(|e| {
@@ -117,16 +117,11 @@ fn picker_from_query() -> Picker {
117117 })
118118}
119119
120#[cfg(windows)]
121fn picker_from_query() -> Picker {
122 tracing::error!(
123 "\"image_preview\" requires \"protocol\" with \"type\" and \"font_size\" options on Windows."
124 );
125 Picker::halfblocks()
126}
127
128120fn picker_from_settings(settings: &ApplicationSettings) -> Picker {
129 let mut picker = if let Some(font_size) = settings.tunables.image_preview.protocol.font_size {
121 let mut picker = if !settings.tunables.image_preview.enabled {
122 // Skip any auto-detection and use halfblocks when disabled:
123 Picker::halfblocks()
124 } else if let Some(font_size) = settings.tunables.image_preview.protocol.font_size {
130125 #[expect(deprecated, reason = "from_query_stdio doesn't work on windows")]
131126 Picker::from_fontsize(font_size.into())
132127 } else {
@@ -141,30 +136,6 @@ fn picker_from_settings(settings: &ApplicationSettings) -> Picker {
141136 picker
142137}
143138
144impl From<ImagePreviewSize> for Rect {
145 fn from(value: ImagePreviewSize) -> Self {
146 Rect::new(0, 0, value.width as _, value.height as _)
147 }
148}
149
150impl From<ImagePreviewSize> for Size {
151 fn from(value: ImagePreviewSize) -> Self {
152 Size::new(value.width as _, value.height as _)
153 }
154}
155
156impl From<Rect> for ImagePreviewSize {
157 fn from(rect: Rect) -> Self {
158 ImagePreviewSize { width: rect.width as _, height: rect.height as _ }
159 }
160}
161
162impl From<Size> for ImagePreviewSize {
163 fn from(size: Size) -> Self {
164 ImagePreviewSize { width: size.width as _, height: size.height as _ }
165 }
166}
167
168139pub async fn load_image(
169140 store: AsyncProgramStore,
170141 media: Media,
@@ -172,14 +143,14 @@ pub async fn load_image(
172143 kind: PreviewKind,
173144 picker: Arc<Picker>,
174145 permits: Arc<Semaphore>,
175 size: ImagePreviewSize,
146 size: Size,
176147) {
177148 async fn load_image_inner(
178149 media: Media,
179150 source: MediaSource,
180151 picker: Arc<Picker>,
181152 permits: Arc<Semaphore>,
182 size: ImagePreviewSize,
153 size: Size,
183154 filter: FilterType,
184155 ) -> Result<ImageStatus, IambError> {
185156 let reader = media
@@ -198,8 +169,7 @@ pub async fn load_image(
198169 let handle = tokio::task::spawn_blocking(move || {
199170 let image = reader.decode().map_err(IambError::Image)?;
200171
201 picker
202 .new_protocol(image, size.into(), Resize::Fit(Some(filter)))
172 SlicedProtocol::new_with_resize(&picker, image, size, Resize::Fit(Some(filter)))
203173 .map_err(|err| IambError::Preview(err.to_string()))
204174 });
205175
src/windows/room/scrollback.rs +42 −12
@@ -1,5 +1,5 @@
11//! Message scrollback
2use ratatui_image::Image;
2use ratatui_image::sliced::{SignedPosition, SlicedImage};
33use regex::Regex;
44
55use matrix_sdk::ruma::{OwnedEventId, OwnedRoomId};
@@ -1411,6 +1411,8 @@ impl StatefulWidget for Scrollback<'_> {
14111411 }
14121412
14131413 let previews = &self.store.application.previews;
1414 let mut image_previews = vec![];
1415
14141416 for (key, item) in thread.range(&corner_key..) {
14151417 let sel = key == cursor_key;
14161418
@@ -1429,7 +1431,13 @@ impl StatefulWidget for Scrollback<'_> {
14291431 }
14301432
14311433 if key == &corner_key && row < corner.text_row {
1432 // Skip rows above the viewport corner.
1434 // Skip rows above the viewport corner but keep image previews.
1435 let y = area.top() as i16 + row as i16 - corner.text_row as i16;
1436 let line_previews = msg_previews
1437 .extract_if(.., |(_, _, y)| *y as usize == row)
1438 .map(|(backend, msg_x, _)| (area.left() + msg_x, y, backend));
1439 image_previews.extend(line_previews);
1440
14331441 continue;
14341442 }
14351443
@@ -1446,7 +1454,16 @@ impl StatefulWidget for Scrollback<'_> {
14461454
14471455 if lines.len() > height {
14481456 let n = lines.len() - height;
1449 let _ = lines.drain(..n);
1457 let previews =
1458 lines
1459 .drain(..n)
1460 .zip(-(n as i16)..)
1461 .flat_map(|((_, _, _, line_previews, _), y)| {
1462 line_previews.into_iter().map(move |(backend, msg_x, _)| {
1463 (area.left() + msg_x, area.top() as i16 + y, backend)
1464 })
1465 });
1466 image_previews.extend(previews);
14501467 }
14511468
14521469 if let Some((key, row, _, _, _)) = lines.first() {
@@ -1457,11 +1474,12 @@ impl StatefulWidget for Scrollback<'_> {
14571474 let mut y = area.top();
14581475 let x = area.left();
14591476
1460 let mut image_previews = vec![];
14611477 for (key, row, txt, line_preview, includes_date_line) in lines.into_iter() {
14621478 let _ = buf.set_line(x, y, &txt, area.width);
14631479 image_previews.extend(
1464 line_preview.into_iter().map(|(backend, msg_x, _)| (x + msg_x, y, backend)),
1480 line_preview
1481 .into_iter()
1482 .map(|(backend, msg_x, _)| (x + msg_x, y as i16, backend)),
14651483 );
14661484
14671485 if key == cursor_key && row == usize::from(includes_date_line) {
@@ -1470,16 +1488,28 @@ impl StatefulWidget for Scrollback<'_> {
14701488
14711489 y += 1;
14721490 }
1491
1492 let msg_width = Message::message_column_width(&state.viewctx, settings);
1493
14731494 // Render image previews after all text lines have been drawn, as the render might draw below the current
14741495 // line.
14751496 for (x, y, backend) in image_previews {
1476 let image_widget = Image::new(backend);
1477 let mut rect: Rect = backend.size().into();
1478 rect.x = x;
1479 rect.y = y;
1480 // Don't render outside of scrollback area
1481 if rect.bottom() <= area.bottom() && rect.right() <= area.right() {
1482 image_widget.render(rect, buf);
1497 if backend.size().height as i16 + y >= area.y as i16 {
1498 let hidden_lines = (area.y as i16 - y).max(0);
1499
1500 let position = SignedPosition { x: 0, y: -hidden_lines };
1501 let image_widget = SlicedImage::new(backend, position);
1502 let mut rect: Rect = backend.size().into();
1503 rect.x = x;
1504 rect.y = (y + hidden_lines) as u16;
1505
1506 rect.height -= hidden_lines as u16;
1507 rect.width = rect.width.min(msg_width as u16);
1508
1509 let rect = rect.intersection(area);
1510 if !rect.is_empty() {
1511 image_widget.render(rect, buf);
1512 }
14831513 }
14841514 }
14851515
src/worker.rs +4 −3
@@ -18,6 +18,7 @@ use matrix_sdk::ruma::events::key::verification::ready::{
1818 ToDeviceKeyVerificationReadyEvent,
1919};
2020use matrix_sdk_base::RoomStateFilter;
21use ratatui::layout::Size;
2122use ratatui_image::picker::Picker;
2223use tokio::sync::Semaphore;
2324use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel};
@@ -92,7 +93,7 @@ use modalkit::errors::UIError;
9293use modalkit::prelude::{EditInfo, InfoMessage};
9394
9495use crate::base::{EchoLocation, MessageNeed};
95use crate::config::{ImagePreviewSize, ProxyUrl};
96use crate::config::ProxyUrl;
9697use crate::message::{Message, MessageEvent, MessageId, MessageKey};
9798use crate::notifications::register_notifications;
9899use crate::preview::PreviewKind;
@@ -736,7 +737,7 @@ pub enum WorkerTask {
736737 Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>),
737738 SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>),
738739 TypingNotice(OwnedRoomId),
739 LoadImage(MediaSource, PreviewKind, ImagePreviewSize, Arc<Picker>, Arc<Semaphore>),
740 LoadImage(MediaSource, PreviewKind, Size, Arc<Picker>, Arc<Semaphore>),
740741}
741742
742743impl Debug for WorkerTask {
@@ -970,7 +971,7 @@ impl Requester {
970971 &self,
971972 source: MediaSource,
972973 kind: PreviewKind,
973 size: ImagePreviewSize,
974 size: Size,
974975 picker: Arc<Picker>,
975976 permits: Arc<Semaphore>,
976977 ) {