From af534224988423509d454888aeccd9f9a0eccea3 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Sat, 14 Dec 2024 22:55:12 -0600 Subject: [PATCH] Centering text by default Since the text widget has no way of doing TextAlignment, we need to split every text by lines and make each their own text element and then align them ourselves. This will likely need to be pulled out into their own functions so that we can do it based off of the TextAlignment from the users input --- src/ui/presenter.rs | 53 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/src/ui/presenter.rs b/src/ui/presenter.rs index 9238da5..0691f66 100644 --- a/src/ui/presenter.rs +++ b/src/ui/presenter.rs @@ -5,6 +5,7 @@ use std::{ use cosmic::{ dialog::ashpd::url::Url, iced::{ + alignment::Horizontal, font::{Family, Stretch, Style, Weight}, Background, Border, Color, ContentFit, Font, Length, Shadow, Vector, @@ -19,7 +20,7 @@ use cosmic::{ prelude::*, widget::{ container, image, mouse_area, responsive, scrollable, text, - Container, Id, Responsive, Row, Space, + Column, Container, Id, Responsive, Row, Space, Text, }, Task, }; @@ -282,10 +283,40 @@ impl Presenter { } else { 50.0 }; - let text = text(self.current_slide.text()) - .size(font_size) - .font(font); - let text = Container::new(text).center(Length::Fill); + let slide_text = self.current_slide.text(); + let lines = slide_text.lines(); + let line_size = lines.clone().count(); + // debug!(?lines); + let text: Vec> = lines + .map(|t| { + text(t.to_string()) + .size(font_size) + .font(font) + .width(Length::Fill) + .align_x(Horizontal::Center) + .into() + }) + .collect(); + let texts = Column::with_children(text); + // let text = text(self.current_slide.text()) + // .size(font_size) + // .font(font) + // .align_x(Horizontal::Center); + let text = Container::new(texts).center(Length::Fill); + let text_background = Container::new(Space::new(0, 0)) + .style(|_| { + container::background( + Background::Color(Color::BLACK) + .scale_alpha(0.3), + ) + }) + .width(size.width) + .height( + font_size * line_size as f32 * line_size as f32, + ); + let text_stack = stack!(text_background, text); + let text_container = + Container::new(text_stack).center(Length::Fill); let black = Container::new(Space::new(0, 0)) .style(|_| { container::background(Background::Color( @@ -321,10 +352,14 @@ impl Presenter { } } }; - stack!(black, container.center(Length::Fill), text) - .width(Length::Fill) - .height(Length::Fill) - .into() + stack!( + black, + container.center(Length::Fill), + text_container + ) + .width(Length::Fill) + .height(Length::Fill) + .into() }) .into() }