Skip to content

Commit

Permalink
search debouncer
Browse files Browse the repository at this point in the history
  • Loading branch information
xou816 committed Nov 24, 2020
1 parent f635cdc commit e060047
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/app/components/details/details.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
.details .details__album-art {
border-radius: 8px;
}

.details .details__like {
font-size: 18px;
}
5 changes: 4 additions & 1 deletion src/app/components/details/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ struct DetailsWidget {
pub artist_label: gtk::Label,
pub album_label: gtk::Label,
pub album_tracks: gtk::ListBox,
pub album_art: gtk::Image
pub album_art: gtk::Image,
pub like_button: gtk::Button
}

impl DetailsWidget {
Expand Down Expand Up @@ -52,9 +53,11 @@ impl Details {
let album = &info.title[..];
let artist = &info.artist[..];
let art = info.art.clone();
let is_liked = false;

self.widget.album_label.set_label(album);
self.widget.artist_label.set_label(artist);
self.widget.like_button.set_label(if is_liked { "♥" } else { "♡" });

let image = self.widget.album_art.clone();
self.worker.send_task(async move {
Expand Down
18 changes: 9 additions & 9 deletions src/app/components/details/details.ui
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@
</packing>
</child>
<child>
<object class="GtkButton">
<object class="GtkButton" id="like_button">
<property name="label" translatable="yes">♥</property>
<property name="width-request">35</property>
<property name="height-request">35</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="relief">none</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="stock">gtk-apply</property>
</object>
</child>
<property name="relief">half</property>
<style>
<class name="circular"/>
<class name="details__like"/>
</style>
</object>
<packing>
<property name="expand">False</property>
Expand Down
39 changes: 36 additions & 3 deletions src/app/components/search/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::rc::Rc;
use std::cell::Cell;
use gtk::prelude::*;
use gio::prelude::*;
use gladis::Gladis;
Expand All @@ -7,6 +8,28 @@ use crate::app::dispatch::Worker;
use crate::app::components::{Component, EventListener, Album, gtypes::AlbumModel};
use super::SearchResultsModel;

struct Debouncer(Rc<Cell<Option<glib::source::SourceId>>>);

impl Debouncer {

fn new() -> Self {
Self(Rc::new(Cell::new(None)))
}

fn debounce<F: Fn() + 'static>(&self, interval_ms: u32, f: F) {
let source_clone = Rc::downgrade(&self.0);
let new_source = glib::timeout_add_local(interval_ms, move || {
f();
if let Some(cell) = source_clone.upgrade() {
cell.set(None);
}
glib::Continue(false)
});
if let Some(previous_source) = self.0.replace(Some(new_source)) {
glib::source_remove(previous_source);
}
}
}

#[derive(Gladis, Clone)]
struct SearchResultsWidget {
Expand All @@ -25,7 +48,8 @@ impl SearchResultsWidget {
pub struct SearchResults {
widget: SearchResultsWidget,
model: Rc<SearchResultsModel>,
album_results_model: gio::ListStore
album_results_model: gio::ListStore,
debouncer: Debouncer
}

impl SearchResults {
Expand Down Expand Up @@ -53,7 +77,7 @@ impl SearchResults {
child.upcast::<gtk::Widget>()
});

Self { widget, model, album_results_model }
Self { widget, model, album_results_model, debouncer: Debouncer::new() }
}

fn update_results(&self) {
Expand All @@ -71,7 +95,16 @@ impl SearchResults {
}

fn update_search_query(&self) {
self.model.fetch_results();

{
let model = Rc::downgrade(&self.model);
self.debouncer.debounce(600, move || {
if let Some(model) = model.upgrade() {
model.fetch_results();
}
});
}

if let Some(query) = self.model.get_query() {
let formatted = format!("Search results for « {} »", *query);
self.widget.results_label.set_label(&formatted[..]);
Expand Down
4 changes: 2 additions & 2 deletions src/app/state/browser_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ impl UpdatableState for BrowserState {

let navigation = &mut self.navigation;
let screen_state = navigation.screen_state(Self::match_search);
println!("{:?}", screen_state);

let mut events = match screen_state {
ScreenState::Current => vec![],
ScreenState::Present => {
navigation.pop_to(Self::match_search);
vec![BrowserEvent::NavigationPoppedTo(ScreenName::Search)]
},
ScreenState::NotPresent => {
navigation.push(BrowserScreen::Search(SearchState::new(query.to_owned())));
navigation.push(BrowserScreen::Search(Default::default()));
vec![BrowserEvent::NavigationPushed(ScreenName::Search)]
}
};
Expand Down
7 changes: 3 additions & 4 deletions src/app/state/screen_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ pub struct SearchState {
pub album_results: Vec<AlbumDescription>
}

impl SearchState {

pub fn new(query: String) -> Self {
Self { query, album_results: vec![] }
impl Default for SearchState {
fn default() -> Self {
Self { query: "".to_owned(), album_results: vec![] }
}
}

Expand Down

0 comments on commit e060047

Please sign in to comment.