making the text in the library smarter

This commit is contained in:
Chris Cochrun 2025-03-20 07:16:21 -05:00
parent 40065d2c5a
commit 9b9c337c1b
8 changed files with 65 additions and 22 deletions

View file

@ -7,4 +7,5 @@ pub trait Content {
fn kind(&self) -> ServiceItemKind;
fn to_service_item(&self) -> ServiceItem;
fn background(&self) -> Option<Background>;
fn subtext(&self) -> String;
}

View file

@ -53,6 +53,17 @@ impl Content for Image {
None
}
}
fn subtext(&self) -> String {
if self.path.exists() {
self.path
.file_name()
.map(|f| f.to_string_lossy().to_string())
.unwrap_or("Missing image".into())
} else {
"Missing image".into()
}
}
}
impl From<Value> for Image {

View file

@ -65,6 +65,17 @@ impl Content for Presentation {
None
}
}
fn subtext(&self) -> String {
if self.path.exists() {
self.path
.file_name()
.map(|f| f.to_string_lossy().to_string())
.unwrap_or("Missing presentation".into())
} else {
"Missing presentation".into()
}
}
}
impl From<Value> for Presentation {

View file

@ -59,6 +59,10 @@ impl Content for Song {
fn background(&self) -> Option<Background> {
self.background.clone()
}
fn subtext(&self) -> String {
self.author.clone().unwrap_or("Author missing".into())
}
}
impl ServiceTrait for Song {

View file

@ -58,6 +58,17 @@ impl Content for Video {
None
}
}
fn subtext(&self) -> String {
if self.path.exists() {
self.path
.file_name()
.map(|f| f.to_string_lossy().to_string())
.unwrap_or("Missing video".into())
} else {
"Missing video".into()
}
}
}
impl From<Value> for Video {

View file

@ -690,7 +690,7 @@ impl cosmic::Application for App {
)
.center_y(Length::Fill)
.align_right(Length::Fill)
.width(Length::FillPortion(2)),
.width(Length::FillPortion(1)),
Container::new(slide_preview)
.center_y(Length::Fill)
.width(Length::FillPortion(3)),
@ -705,7 +705,7 @@ impl cosmic::Application for App {
)
.center_y(Length::Fill)
.align_left(Length::Fill)
.width(Length::FillPortion(2)),
.width(Length::FillPortion(1)),
library.width(Length::FillPortion(2))
]
.width(Length::Fill)

View file

@ -1,9 +1,10 @@
use cosmic::{
iced::{
alignment::Vertical, futures::FutureExt, Background, Border,
Length,
Color, Length,
},
iced_widget::{column, row as rowm, text as textm},
theme,
widget::{
button, container, horizontal_space, icon, mouse_area,
responsive, row, scrollable, text, text_input, Container,
@ -275,18 +276,28 @@ impl<'a> Library {
let mut row = row::<Message>().spacing(5);
match &model.kind {
LibraryKind::Song => {
row = row
.push(icon::from_name("folder-music-symbolic"));
row = row
.push(textm!("Songs").align_y(Vertical::Center));
}
LibraryKind::Video => {
row = row
.push(icon::from_name("folder-videos-symbolic"));
row = row
.push(textm!("Videos").align_y(Vertical::Center));
}
LibraryKind::Image => {
row = row.push(icon::from_name(
"folder-pictures-symbolic",
));
row = row
.push(textm!("Images").align_y(Vertical::Center));
}
LibraryKind::Presentation => {
row = row.push(icon::from_name(
"x-office-presentation-symbolic",
));
row = row.push(
textm!("Presentations").align_y(Vertical::Center),
);
@ -431,25 +442,19 @@ impl<'a> Library {
}))
.center_y(20)
.center_x(Length::Fill);
let icon = icon::from_name({
match model.kind {
LibraryKind::Song => "folder-music-symbolic",
LibraryKind::Video => "folder-videos-symbolic",
LibraryKind::Image => "folder-pictures-symbolic",
LibraryKind::Presentation => {
"x-office-presentation-symbolic"
}
}
});
let subtext = container(responsive(|size| {
let background = if let Some(text) = item.background() {
text.path.to_string_lossy().to_string()
let color: Color = if item.background().is_some() {
theme::active().cosmic().accent_text_color().into()
} else {
"Background does not exist...".to_string()
theme::active()
.cosmic()
.destructive_text_color()
.into()
};
text::body(elide_text(background, size.width))
text::body(elide_text(item.subtext(), size.width))
.center()
.wrapping(textm::Wrapping::None)
.class(color)
.into()
}))
.center_y(20)
@ -458,7 +463,7 @@ impl<'a> Library {
let texts = column([text.into(), subtext.into()]);
Container::new(
rowm![horizontal_space().width(0), icon, texts]
rowm![horizontal_space().width(0), texts]
.spacing(10)
.align_y(Vertical::Center),
)
@ -561,8 +566,9 @@ async fn add_db() -> Result<SqlitePool> {
SqlitePool::connect(&db_url).await.into_diagnostic()
}
fn elide_text(text: String, width: f32) -> String {
fn elide_text(text: impl AsRef<str>, width: f32) -> String {
const CHAR_SIZE: f32 = 8.0;
let text: String = text.as_ref().to_owned();
let text_length = text.len() as f32 * CHAR_SIZE;
if text_length > width {
format!(

View file

@ -11,9 +11,8 @@ use cosmic::{
theme,
widget::{
button, column, combo_box, container, dropdown,
horizontal_space, icon, scrollable,
svg::{self, Handle},
text, text_editor, text_input, Svg,
horizontal_space, icon, scrollable, svg::Handle, text,
text_editor, text_input, Svg,
},
Element, Task,
};