Skip to content

Commit

Permalink
feat: add borders to images, refurbish making blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Chleba committed Jun 11, 2024
1 parent a5fffda commit 199705e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .data_bak/slides.json5
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{
"type": "Image",
"content": ".data/images/bomb.png",
"rect": { "x": 0, "y": 0, "width": 15, "height": 15 }
"rect": { "x": 0, "y": 0, "width": 15, "height": 3 }
},
{
"type": "Image",
Expand Down
68 changes: 49 additions & 19 deletions src/components/slides.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Read;
use std::path::Path;

use block::Position;
use color_eyre::{eyre::Result, owo_colors::OwoColorize};
use ratatui::{
prelude::*,
Expand All @@ -16,7 +17,9 @@ use crate::{
action::Action,
enums::{ContentJson, ReturnSlideWidget, SlideContentType, SlideJson, SlidesJson},
layout::{get_slides_layout, CONTENT_HEIGHT, CONTENT_WIDTH},
slide_builder::{get_slide_content_string, make_slide_content, make_slide_image},
slide_builder::{
get_slide_content_string, make_slide_block, make_slide_content, make_slide_image,
},
};

pub struct Slides {
Expand Down Expand Up @@ -140,23 +143,43 @@ impl Slides {
big_title.unwrap()
}

fn make_block(&self) -> Block {
let s_index = self.slide_index + 1;
Block::default()
fn make_block(title: Option<Line>) -> Block {
let s_content = ContentJson {
type_: SlideContentType::Block,
content: None,
rect: None,
color: Some("#FFDDDD".to_string()),
};
let block = make_slide_block(s_content);
if let ReturnSlideWidget::Block(mut b) = block {
if let Some(t) = title {
b = b.title(Title::from(t));
}
return b;
}

// -- default
let mut block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Rgb(100, 100, 100)))
.border_type(BorderType::Double)
.title(
Title::from(Line::from(vec![
"|".yellow(),
s_index.to_string().green(),
"/".yellow(),
self.slide_count.to_string().green(),
"|".yellow(),
]))
.alignment(Alignment::Right)
.position(block::Position::Bottom),
)
.border_style(Style::default().fg(Color::Rgb(100, 100, 100)));
if let Some(t) = title {
block = block.title(Title::from(t));
}
block
}

fn make_content_block(&self) -> Block {
let s_index = self.slide_index + 1;
let title = Line::from(vec![
"|".yellow(),
s_index.to_string().green(),
"/".yellow(),
self.slide_count.to_string().green(),
"|".yellow(),
]);
Self::make_block(None)
.title_bottom(title)
.title_alignment(Alignment::Right)
}

fn make_slide_items<'a>(
Expand Down Expand Up @@ -220,7 +243,7 @@ impl Component for Slides {

let slide_items = Self::make_slide_items(&slide, self.json_slides.clone());
let title = Self::make_title(&slide);
let block = self.make_block();
let block = self.make_content_block();

f.render_widget(title, title_rect);
f.render_widget(block, rect.content);
Expand All @@ -240,8 +263,15 @@ impl Component for Slides {
f.render_widget(s, slide_rect);
}
ReturnSlideWidget::Image(s) => {
// -- block
let block = Self::make_block(None);
let mut b_rect = slide_rect;
b_rect.x -= 1;
b_rect.width += 2;
b_rect.y -= 1;
f.render_widget(block, b_rect);

let mut img_static = self.images[img_index].clone();
// let mut img_static = self.picker.new_resize_protocol(s);
let img = StatefulImage::new(None).resize(Resize::Fit(None));
f.render_stateful_widget(img, slide_rect, &mut img_static);
img_index += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/slide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn make_slide_image<'a>(slide: ContentJson, slide_path: String) -> ReturnSli
ReturnSlideWidget::Image(dyn_img)
}

fn make_slide_block<'a>(slide: ContentJson) -> ReturnSlideWidget<'a> {
pub fn make_slide_block<'a>(slide: ContentJson) -> ReturnSlideWidget<'a> {
let content = get_slide_content_string(slide.clone());
let color = get_slide_content_color(slide);
ReturnSlideWidget::Block(
Expand Down
8 changes: 4 additions & 4 deletions talk_example/slides.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"box_size": {
"width": 50,
"width": 75,
"height": 30
},

Expand All @@ -11,18 +11,18 @@
{
"type": "Image",
"content": "./images/ratatui.png",
"rect": { "x": 15, "y": 6, "width": 20, "height": 20 }
"rect": { "x": 15, "y": 7, "width": 21, "height": 15 }
},
{
"type": "Line",
"content": "A Rust library for creating",
"rect": { "x": 12, "y": 17, "width": 30, "height": 1 },
"rect": { "x": 12, "y": 21, "width": 30, "height": 1 },
"color": "#FFFFee"
},
{
"type": "Line",
"content": "TERMINAL USER INTERFACES",
"rect": { "x": 14, "y": 18, "width": 30, "height": 1 },
"rect": { "x": 14, "y": 22, "width": 30, "height": 1 },
"color": "#FFFF00"
}
]
Expand Down

0 comments on commit 199705e

Please sign in to comment.