-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The sixel implementation is behind the `sixel` feature flag. Additionally to enable it, set in tunables: ```json "image_preview": { "sixel": { "line_count": 10, "cache_path": "/home/gipsy/Downloads/iamb_sixels" } } ``` The default is `"image_preview": "disabled"`.
- Loading branch information
Showing
12 changed files
with
806 additions
and
23 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use std::convert::TryFrom; | ||
|
||
use matrix_sdk::ruma::{ | ||
events::{ | ||
room::{ | ||
message::{MessageType, RoomMessageEventContent}, | ||
MediaSource, | ||
}, | ||
MessageLikeEvent, | ||
SyncMessageLikeEvent, | ||
}, | ||
EventId, | ||
OwnedEventId, | ||
}; | ||
use modalkit::tui::{buffer::Buffer, layout::Rect}; | ||
|
||
use crate::{ | ||
base::IambResult, | ||
config::{ApplicationSettings, ImagePreview}, | ||
}; | ||
|
||
#[cfg(feature = "sixel")] | ||
mod sixel; | ||
|
||
pub enum Preview { | ||
None, | ||
Pending, | ||
Loaded { data: String, placeholder: String }, | ||
} | ||
|
||
pub struct Previewer { | ||
renderer: Box<dyn PreviewFormat + Send + Sync>, | ||
} | ||
|
||
impl Previewer { | ||
pub fn new(settings: &ApplicationSettings) -> Option<Previewer> { | ||
match &settings.tunables.image_preview { | ||
ImagePreview::Disabled => None, | ||
#[cfg(feature = "sixel")] | ||
ImagePreview::Sixel { line_count, cache_path } => { | ||
match sixel::PreviewSixel::new(*line_count, cache_path.clone()) { | ||
Ok(renderer) => Some(Box::new(renderer)), | ||
Err(err) => { | ||
eprintln!("Could not set up sixel renderer: {err}"); | ||
None | ||
}, | ||
} | ||
}, | ||
} | ||
.map(|renderer| Previewer { renderer }) | ||
} | ||
|
||
pub fn load_image( | ||
&self, | ||
PreviewSource { source: _, event_id }: &PreviewSource, | ||
) -> IambResult<Preview> { | ||
let data = self.renderer.load(&event_id)?; | ||
return Ok(Preview::Loaded { data, placeholder: self.renderer.placeholder() }); | ||
} | ||
|
||
pub fn save_image(&self, event_id: &EventId, bytes: Vec<u8>) -> IambResult<Preview> { | ||
let data = self.renderer.save(event_id, bytes)?; | ||
return Ok(Preview::Loaded { data, placeholder: self.renderer.placeholder() }); | ||
} | ||
|
||
pub fn render(&self, preview: &Preview, x: u16, y: u16, area: Rect, buf: &mut Buffer) { | ||
self.renderer.render(preview, x, y, area, buf) | ||
} | ||
} | ||
|
||
pub trait PreviewFormat { | ||
fn placeholder(&self) -> String; | ||
fn load(&self, event_id: &EventId) -> IambResult<String>; | ||
fn save(&self, event_id: &EventId, bytes: Vec<u8>) -> IambResult<String>; | ||
fn render(&self, preview: &Preview, x: u16, y: u16, area: Rect, buf: &mut Buffer); | ||
} | ||
|
||
pub struct PreviewSource { | ||
pub source: MediaSource, | ||
pub event_id: OwnedEventId, | ||
} | ||
|
||
impl TryFrom<&SyncMessageLikeEvent<RoomMessageEventContent>> for PreviewSource { | ||
type Error = &'static str; | ||
fn try_from(ev: &SyncMessageLikeEvent<RoomMessageEventContent>) -> Result<Self, Self::Error> { | ||
if let SyncMessageLikeEvent::Original(ev) = &ev { | ||
if let MessageType::Image(c) = &ev.content.msgtype { | ||
Ok(PreviewSource { | ||
source: c.source.clone(), | ||
event_id: ev.event_id.clone(), | ||
}) | ||
} else { | ||
Err("content message type is not image") | ||
} | ||
} else { | ||
Err("event is not original event") | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<&MessageLikeEvent<RoomMessageEventContent>> for PreviewSource { | ||
type Error = &'static str; | ||
fn try_from(ev: &MessageLikeEvent<RoomMessageEventContent>) -> Result<Self, Self::Error> { | ||
if let MessageLikeEvent::Original(ev) = &ev { | ||
if let MessageType::Image(c) = &ev.content.msgtype { | ||
Ok(PreviewSource { | ||
source: c.source.clone(), | ||
event_id: ev.event_id.clone(), | ||
}) | ||
} else { | ||
Err("content message type is not image") | ||
} | ||
} else { | ||
Err("event is not original event") | ||
} | ||
} | ||
} |
Oops, something went wrong.