From 9b9c337c1b6fc45f89f26dff616fc1e26a751c68 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Thu, 20 Mar 2025 07:16:21 -0500 Subject: [PATCH] making the text in the library smarter --- src/core/content.rs | 1 + src/core/images.rs | 11 +++++++++++ src/core/presentations.rs | 11 +++++++++++ src/core/songs.rs | 4 ++++ src/core/videos.rs | 11 +++++++++++ src/main.rs | 4 ++-- src/ui/library.rs | 40 ++++++++++++++++++++++----------------- src/ui/song_editor.rs | 5 ++--- 8 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/core/content.rs b/src/core/content.rs index b117c5e..44d3a8a 100644 --- a/src/core/content.rs +++ b/src/core/content.rs @@ -7,4 +7,5 @@ pub trait Content { fn kind(&self) -> ServiceItemKind; fn to_service_item(&self) -> ServiceItem; fn background(&self) -> Option; + fn subtext(&self) -> String; } diff --git a/src/core/images.rs b/src/core/images.rs index 25f7a55..c04bf3c 100644 --- a/src/core/images.rs +++ b/src/core/images.rs @@ -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 for Image { diff --git a/src/core/presentations.rs b/src/core/presentations.rs index a2cc842..3ee3ecb 100644 --- a/src/core/presentations.rs +++ b/src/core/presentations.rs @@ -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 for Presentation { diff --git a/src/core/songs.rs b/src/core/songs.rs index 46441db..b6984c8 100644 --- a/src/core/songs.rs +++ b/src/core/songs.rs @@ -59,6 +59,10 @@ impl Content for Song { fn background(&self) -> Option { self.background.clone() } + + fn subtext(&self) -> String { + self.author.clone().unwrap_or("Author missing".into()) + } } impl ServiceTrait for Song { diff --git a/src/core/videos.rs b/src/core/videos.rs index 19d447f..34f8b03 100644 --- a/src/core/videos.rs +++ b/src/core/videos.rs @@ -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 for Video { diff --git a/src/main.rs b/src/main.rs index c5f154d..fc034fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) diff --git a/src/ui/library.rs b/src/ui/library.rs index 55523f8..345bd29 100644 --- a/src/ui/library.rs +++ b/src/ui/library.rs @@ -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::().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::connect(&db_url).await.into_diagnostic() } -fn elide_text(text: String, width: f32) -> String { +fn elide_text(text: impl AsRef, 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!( diff --git a/src/ui/song_editor.rs b/src/ui/song_editor.rs index a2c5563..47aaed7 100644 --- a/src/ui/song_editor.rs +++ b/src/ui/song_editor.rs @@ -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, };