Skip to content

Commit

Permalink
🐛 fix selected entry being not visible after scrolling (#90)
Browse files Browse the repository at this point in the history
This fixes #76
  • Loading branch information
friedow authored Mar 19, 2024
1 parent 134a5e5 commit 1026efc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
16 changes: 15 additions & 1 deletion client/src/component/entry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub fn view(entry: &crate::model::Entry, active: bool) -> iced::Element<'static, crate::Message> {
return iced::widget::container(
iced::widget::row![
iced::widget::text(&entry.title)
iced::widget::text(clipped_title(entry.title.clone()))
.size(1. * crate::REM)
.width(iced::Length::Fill),
iced::widget::text(if active { &entry.action } else { "" }).size(1. * crate::REM)
Expand All @@ -12,6 +12,20 @@ pub fn view(entry: &crate::model::Entry, active: bool) -> iced::Element<'static,
.into();
}

fn clipped_title(title: String) -> String {
if title.char_indices().count() <= 57 {
return title;
}

let mut clipped_title: String = title
.char_indices()
.filter_map(|(_, character)| Some(character))
.take(57)
.collect();
clipped_title.push_str("...");
clipped_title
}

fn style(active: bool) -> iced::theme::Container {
if active {
iced::theme::Container::Custom(Box::new(Style {}))
Expand Down
33 changes: 29 additions & 4 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,36 @@ impl Centerpiece {
}

fn scroll_to_selected_entry(&self) -> iced::Command<Message> {
let total_entries = self.entries().len() as f32;
let offset = (1.0 / (total_entries - 1.0)) * self.active_entry_index as f32;
iced::widget::scrollable::snap_to(
let plugin_index = match self.active_entry_id() {
Some(active_entry_id) => self
.plugins
.iter()
.position(|plugin| {
plugin
.entries
.iter()
.find(|entry| entry.id.eq(active_entry_id))
.is_some()
})
.unwrap_or(0) as f32,
None => 0.0,
};
let entry_index = self.active_entry_index as f32;

// 1.0 REM line height +
// 2x0.5 REM padding +
// 0.3 REM for good luck :D
let entry_height = 2.3 * crate::REM;
// 0.75 REM line height +
// 2x0.5 REM padding +
// 2x0.75 REM padding +
// 0.32 REM for good luck :D
let plugin_header_height = 3.57 * crate::REM;

let offset = (plugin_index * plugin_header_height) + (entry_index * entry_height);
iced::widget::scrollable::scroll_to(
iced::widget::scrollable::Id::new(SCROLLABLE_ID),
iced::widget::scrollable::RelativeOffset { x: 0.0, y: offset },
iced::widget::scrollable::AbsoluteOffset { x: 0.0, y: offset },
)
}

Expand Down

0 comments on commit 1026efc

Please sign in to comment.