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
This commit is contained in:
Chris Cochrun 2024-12-14 22:55:12 -06:00
parent 32f91c8d69
commit af53422498

View file

@ -5,6 +5,7 @@ use std::{
use cosmic::{ use cosmic::{
dialog::ashpd::url::Url, dialog::ashpd::url::Url,
iced::{ iced::{
alignment::Horizontal,
font::{Family, Stretch, Style, Weight}, font::{Family, Stretch, Style, Weight},
Background, Border, Color, ContentFit, Font, Length, Shadow, Background, Border, Color, ContentFit, Font, Length, Shadow,
Vector, Vector,
@ -19,7 +20,7 @@ use cosmic::{
prelude::*, prelude::*,
widget::{ widget::{
container, image, mouse_area, responsive, scrollable, text, container, image, mouse_area, responsive, scrollable, text,
Container, Id, Responsive, Row, Space, Column, Container, Id, Responsive, Row, Space, Text,
}, },
Task, Task,
}; };
@ -282,10 +283,40 @@ impl Presenter {
} else { } else {
50.0 50.0
}; };
let text = text(self.current_slide.text()) let slide_text = self.current_slide.text();
.size(font_size) let lines = slide_text.lines();
.font(font); let line_size = lines.clone().count();
let text = Container::new(text).center(Length::Fill); // debug!(?lines);
let text: Vec<Element<Message>> = 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)) let black = Container::new(Space::new(0, 0))
.style(|_| { .style(|_| {
container::background(Background::Color( container::background(Background::Color(
@ -321,10 +352,14 @@ impl Presenter {
} }
} }
}; };
stack!(black, container.center(Length::Fill), text) stack!(
.width(Length::Fill) black,
.height(Length::Fill) container.center(Length::Fill),
.into() text_container
)
.width(Length::Fill)
.height(Length::Fill)
.into()
}) })
.into() .into()
} }