Skip to content

Commit

Permalink
doc(demo): add resize scale to demo
Browse files Browse the repository at this point in the history
  • Loading branch information
TadoTheMiner committed Dec 5, 2024
1 parent 7fe6232 commit 9662f43
Showing 1 changed file with 46 additions and 50 deletions.
96 changes: 46 additions & 50 deletions examples/demo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use ratatui_image::{
protocol::{Protocol, StatefulProtocol},
Image, Resize, StatefulImage,
};
use rustix::path::Arg;

fn main() -> Result<(), Box<dyn Error>> {
#[cfg(feature = "crossterm")]
Expand All @@ -48,21 +47,22 @@ enum ShowImages {
}

struct App<'a> {
pub title: &'a str,
pub should_quit: bool,
pub tick_rate: Duration,
pub background: String,
pub split_percent: u16,
pub show_images: ShowImages,

pub image_source_path: PathBuf,
pub image_static_offset: (u16, u16),

pub picker: Picker,
pub image_source: DynamicImage,
pub image_static: Protocol,
pub image_fit_state: StatefulProtocol,
pub image_crop_state: StatefulProtocol,
title: &'a str,
should_quit: bool,
tick_rate: Duration,
background: String,
split_percent: u16,
show_images: ShowImages,

image_source_path: PathBuf,
image_static_offset: (u16, u16),

picker: Picker,
image_source: DynamicImage,
image_static: Protocol,
image_fit_state: StatefulProtocol,
image_crop_state: StatefulProtocol,
image_scale_state: StatefulProtocol,
}

fn size() -> Rect {
Expand All @@ -81,7 +81,8 @@ impl<'a> App<'a> {
.unwrap();

let image_fit_state = picker.new_resize_protocol(image_source.clone());
let image_crop_state = picker.new_resize_protocol(image_source.clone());
let image_crop_state = image_fit_state.clone();
let image_scale_state = image_fit_state.clone();

let mut background = String::new();

Expand All @@ -101,7 +102,7 @@ impl<'a> App<'a> {
background.push(c);
}

App {
Self {
title,
should_quit: false,
tick_rate: Duration::from_millis(1000),
Expand All @@ -115,6 +116,7 @@ impl<'a> App<'a> {
image_static,
image_fit_state,
image_crop_state,
image_scale_state,

image_static_offset: (0, 0),
}
Expand Down Expand Up @@ -186,6 +188,25 @@ impl<'a> App<'a> {
}

pub fn on_tick(&mut self) {}

fn render_resized_image(&mut self, f: &mut Frame<'_>, resize: Resize, area: Rect) {
let (state, name, color) = match resize {
Resize::Fit(_) => (&mut self.image_fit_state, "Fit", Color::Magenta),
Resize::Crop(_) => (&mut self.image_crop_state, "Crop", Color::Green),
Resize::Scale(_) => (&mut self.image_scale_state, "Scale", Color::Blue),
};
let block = block(name);
let inner_area = block.inner(area);
f.render_widget(paragraph(self.background.as_str().bg(color)), inner_area);
match self.show_images {
ShowImages::Fixed => (),
_ => {
let image = StatefulImage::new(None).resize(resize);
f.render_stateful_widget(image, inner_area, state);
}
};
f.render_widget(block, area);
}
}

fn ui(f: &mut Frame<'_>, app: &mut App) {
Expand All @@ -208,7 +229,10 @@ fn ui(f: &mut Frame<'_>, app: &mut App) {

let block_left_top = block("Fixed");
let area = block_left_top.inner(left_chunks[0]);
f.render_widget(paragraph(app.background.as_str().style(Color::Yellow), area));
f.render_widget(
paragraph(app.background.as_str()).style(Color::Yellow),
area,
);
f.render_widget(block_left_top, left_chunks[0]);
match app.show_images {
ShowImages::Resized => {}
Expand Down Expand Up @@ -248,37 +272,9 @@ fn ui(f: &mut Frame<'_>, app: &mut App) {
}
}
f.render_widget(block_left_bottom, chunks_left_bottom[0]);

let block_middle_bottom = block("Placeholder");
f.render_widget(
paragraph(app.background.as_str()).style(Style::new().bg(Color::Blue)),
block_middle_bottom.inner(chunks_left_bottom[1]),
);
f.render_widget(block_middle_bottom, chunks_left_bottom[1]);

<<<<<<< HEAD
let block_right_top = Block::default()
.borders(Borders::ALL)
.border_style(Color::Blue)
.title("Fit");
=======
let block_right_top = block("Fit");
>>>>>>> f947fb4 (refactor(demo): simplify ui function which is required for updating the demo)
let area = block_right_top.inner(right_chunks[0]);
f.render_widget(paragraph(app.background.as_str()), area);
match app.show_images {
ShowImages::Fixed => {}
_ => {
let image = StatefulImage::new(None).resize(Resize::Fit(None));
f.render_stateful_widget(
image,
block_right_top.inner(right_chunks[0]),
&mut app.image_fit_state,
);
}
}
f.render_widget(block_right_top, right_chunks[0]);

app.render_resized_image(f, Resize::Crop(None), chunks_left_bottom[0]);
app.render_resized_image(f, Resize::Scale(None), chunks_left_bottom[1]);
app.render_resized_image(f, Resize::Fit(None), right_chunks[0]);
let block_right_bottom = block("Help");
let area = block_right_bottom.inner(right_chunks[1]);
f.render_widget(
Expand Down

0 comments on commit 9662f43

Please sign in to comment.