Skip to content

Commit 717c116

Browse files
Licapulesjfhsjfh
authored andcommitted
feat: support searching in local charts
1 parent 41857a9 commit 717c116

File tree

1 file changed

+77
-55
lines changed

1 file changed

+77
-55
lines changed

phira/src/page/library.rs

Lines changed: 77 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,16 @@ impl LibraryPage {
239239
fn sync_local(&mut self, s: &SharedState) {
240240
let list = self.tabs.selected_mut();
241241
if list.ty == ChartListType::Local {
242-
list.view.set(
243-
s.t,
244-
std::iter::once(ChartDisplayItem::new(None, None))
245-
.chain(s.charts_local.iter().map(|it| ChartDisplayItem::new(Some(it.clone()), None)))
246-
.collect(),
242+
let search = self.search_str.clone();
243+
let mut charts = Vec::new();
244+
charts.push(ChartDisplayItem::new(None, None));
245+
charts.append(&mut s.charts_local
246+
.iter()
247+
.filter(|it| it.info.name.contains(&search))
248+
.map(|it| ChartDisplayItem::new(Some(it.clone()), None))
249+
.collect::<Vec<ChartDisplayItem>>()
247250
);
251+
list.view.set(s.t, charts);
248252
}
249253
}
250254
}
@@ -310,6 +314,16 @@ impl Page for LibraryPage {
310314
request_file("_import");
311315
return Ok(true);
312316
}
317+
if !self.search_str.is_empty() && self.search_clr_btn.touch(touch) {
318+
button_hit();
319+
self.search_str.clear();
320+
self.sync_local(s);
321+
return Ok(true);
322+
}
323+
if !self.search_clr_btn.contains(touch.position) && self.search_btn.touch(touch, t) {
324+
request_input("search", &self.search_str);
325+
return Ok(true);
326+
}
313327
}
314328
ChartListType::Ranked | ChartListType::Special | ChartListType::Unstable => {
315329
if !self.search_str.is_empty() && self.search_clr_btn.touch(touch) {
@@ -346,18 +360,15 @@ impl Page for LibraryPage {
346360
self.tags.update(t);
347361
self.rating.update(t);
348362

363+
let is_local = self.tabs.selected().ty == ChartListType::Local;
349364
if self.tabs.changed() {
350365
self.tabs.selected_mut().view.reset_scroll();
351-
match self.tabs.selected().ty {
352-
ChartListType::Local => {
353-
self.online_task = None;
354-
self.sync_local(s);
355-
}
356-
_ => {
357-
self.online_task = None;
358-
self.current_page = 0;
359-
self.load_online();
360-
}
366+
self.online_task = None;
367+
if is_local {
368+
self.sync_local(s);
369+
} else {
370+
self.current_page = 0;
371+
self.load_online();
361372
}
362373
}
363374
if self.tabs.selected_mut().view.clicked_special {
@@ -416,8 +427,12 @@ impl Page for LibraryPage {
416427
if let Some((id, text)) = take_input() {
417428
if id == "search" {
418429
self.search_str = text;
419-
self.current_page = 0;
420-
self.load_online();
430+
if is_local {
431+
self.sync_local(s);
432+
} else {
433+
self.current_page = 0;
434+
self.load_online();
435+
}
421436
} else {
422437
return_input(id, text);
423438
}
@@ -447,47 +462,55 @@ impl Page for LibraryPage {
447462
Ok(())
448463
})
449464
})?;
450-
match chosen {
451-
ChartListType::Local => {
452-
s.render_fader(ui, |ui| {
453-
let w = 0.24;
454-
let r = Rect::new(r.right() - w, -ui.top + 0.04, w, r.y + ui.top - 0.06);
455-
self.import_btn.render_text(ui, r, t, tl!("import"), 0.6, false);
465+
if chosen != ChartListType::Popular {
466+
s.render_fader(ui, |ui| {
467+
let empty = self.search_str.is_empty();
468+
let w = 0.53;
469+
let mut r = Rect::new(r.right() - w, -ui.top + 0.04, w, r.y + ui.top - 0.06);
470+
if empty {
471+
r.x += r.h;
472+
r.w -= r.h;
473+
}
474+
let rt = r.right();
475+
self.search_btn.render_shadow(ui, r, t, |ui, path| {
476+
ui.fill_path(&path, semi_black(0.4));
456477
});
457-
}
458-
ChartListType::Ranked | ChartListType::Special | ChartListType::Unstable => {
459-
s.render_fader(ui, |ui| {
460-
let empty = self.search_str.is_empty();
461-
let w = 0.53;
462-
let mut r = Rect::new(r.right() - w, -ui.top + 0.04, w, r.y + ui.top - 0.06);
463-
if empty {
464-
r.x += r.h;
465-
r.w -= r.h;
466-
}
467-
let rt = r.right();
468-
self.search_btn.render_shadow(ui, r, t, |ui, path| {
478+
let mut r = r.feather(-0.01);
479+
r.w = r.h;
480+
if !empty {
481+
ui.fill_rect(r, (*self.icons.close, r, ScaleType::Fit));
482+
self.search_clr_btn.set(ui, r);
483+
r.x += r.w;
484+
}
485+
ui.fill_rect(r, (*self.icons.search, r, ScaleType::Fit));
486+
ui.text(&self.search_str)
487+
.pos(r.right() + 0.01, r.center().y)
488+
.anchor(0., 0.5)
489+
.no_baseline()
490+
.size(0.6)
491+
.max_width(rt - r.right() - 0.02)
492+
.draw();
493+
let mut r = r.feather(0.01);
494+
// TODO: better shifting
495+
r.x = 1. - w - r.w - 0.05;
496+
if empty {
497+
r.x += r.w;
498+
}
499+
if chosen == ChartListType::Local {
500+
let w = 0.24;
501+
r.x = r.x + r.w - w;
502+
r.w = w;
503+
let ct = r.center();
504+
self.import_btn.render_shadow(ui, r, t, |ui, path| {
469505
ui.fill_path(&path, semi_black(0.4));
470506
});
471-
let mut r = r.feather(-0.01);
472-
r.w = r.h;
473-
if !empty {
474-
ui.fill_rect(r, (*self.icons.close, r, ScaleType::Fit));
475-
self.search_clr_btn.set(ui, r);
476-
r.x += r.w;
477-
}
478-
ui.fill_rect(r, (*self.icons.search, r, ScaleType::Fit));
479-
ui.text(&self.search_str)
480-
.pos(r.right() + 0.01, r.center().y)
481-
.anchor(0., 0.5)
507+
ui.text(tl!("import"))
508+
.pos(ct.x, ct.y)
509+
.anchor(0.5, 0.5)
482510
.no_baseline()
483511
.size(0.6)
484-
.max_width(rt - r.right() - 0.02)
485512
.draw();
486-
let mut r = r.feather(0.01);
487-
r.x = 1. - w - r.w - 0.05;
488-
if empty {
489-
r.x += r.w;
490-
}
513+
} else {
491514
self.order_btn.render_shadow(ui, r, t, |ui, path| {
492515
ui.fill_path(&path, semi_black(0.4));
493516
ui.fill_rect(r, (*self.icons.order, r, ScaleType::Fit));
@@ -504,9 +527,8 @@ impl Page for LibraryPage {
504527
let cr = r.feather(-0.005);
505528
ui.fill_rect(cr, (*self.icons.filter, cr, ScaleType::Fit));
506529
});
507-
});
508-
}
509-
ChartListType::Popular => {}
530+
}
531+
});
510532
}
511533
if chosen != ChartListType::Local {
512534
let total_page = self.total_page(s);

0 commit comments

Comments
 (0)