Page buttons

This commit is contained in:
2026-03-19 02:16:41 -04:00
parent 897b50fe35
commit b748973384
3 changed files with 81 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
use crate::types; use crate::types;
use crate::util::clamp;
use eframe::egui; use eframe::egui;
use tokio::sync::mpsc; use tokio::sync::mpsc;
@@ -24,6 +25,7 @@ impl Application {
fn render_top(&mut self, ctx: &egui::Context) { fn render_top(&mut self, ctx: &egui::Context) {
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| { egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
// Side panel toggle
let mut side_panel_btn_text = ">>"; let mut side_panel_btn_text = ">>";
if self.show_side_panel { if self.show_side_panel {
side_panel_btn_text = "<<"; side_panel_btn_text = "<<";
@@ -31,7 +33,6 @@ impl Application {
if ui.button(side_panel_btn_text).clicked() { if ui.button(side_panel_btn_text).clicked() {
self.show_side_panel = !self.show_side_panel; self.show_side_panel = !self.show_side_panel;
} }
ui.separator(); ui.separator();
// Search button and input // Search button and input
@@ -61,6 +62,57 @@ impl Application {
} }
ui.separator(); ui.separator();
ui.label(format!(
"Page {} of {}",
self.search_ctx.page, self.search_ctx.total_pages
));
if ui.button(" - ").clicked() {
self.search_ctx.page =
clamp(self.search_ctx.page - 1, 1, self.search_ctx.total_pages);
let query = self.query.clone();
let url = self.server_url.clone();
let page = self.search_ctx.page.clone();
let page_size = self.search_ctx.per_page.clone();
let (tx, rx) = mpsc::channel::<
Result<(Vec<types::FileEntry>, types::Metadata), String>,
>(1);
self.search_ctx.search_rx = Some(rx);
self.search_ctx.is_searching = true;
self.search_ctx.search_results.clear();
self.search_ctx.page = 0;
self.search_ctx.total_pages = 0;
self.search_ctx.total_results = 0;
self.status = "Searching...".to_string();
tokio::spawn(async move {
let res = search_files(url, query, page, page_size).await;
let _ = tx.send(res).await;
});
ctx.forget_all_images();
}
if ui.button("+").clicked() {
self.search_ctx.page =
clamp(self.search_ctx.page + 1, 1, self.search_ctx.total_pages);
let query = self.query.clone();
let url = self.server_url.clone();
let page = self.search_ctx.page.clone();
let page_size = self.search_ctx.per_page.clone();
let (tx, rx) = mpsc::channel::<
Result<(Vec<types::FileEntry>, types::Metadata), String>,
>(1);
self.search_ctx.search_rx = Some(rx);
self.search_ctx.is_searching = true;
self.search_ctx.search_results.clear();
self.search_ctx.page = 0;
self.search_ctx.total_pages = 0;
self.search_ctx.total_results = 0;
self.status = "Searching...".to_string();
tokio::spawn(async move {
let res = search_files(url, query, page, page_size).await;
let _ = tx.send(res).await;
});
}
ui.label(&self.status); ui.label(&self.status);
}); });
}); });
@@ -81,6 +133,23 @@ impl Application {
} }
} }
}); });
ui.horizontal(|ui| {
// Page controls
ui.label(format!("Items per page: {}", self.search_ctx.per_page));
if ui.button(" - ").clicked() {
self.search_ctx.per_page = self.search_ctx.per_page.saturating_sub(1);
self.search_ctx.total_pages =
self.search_ctx.total_results / self.search_ctx.per_page;
ctx.forget_all_images();
}
if ui.button("+").clicked() {
self.search_ctx.per_page = self.search_ctx.per_page.saturating_add(1);
self.search_ctx.total_pages =
self.search_ctx.total_results / self.search_ctx.per_page;
ctx.forget_all_images();
}
});
}); });
} }
@@ -143,7 +212,7 @@ impl eframe::App for Application {
self.render_side_panel(ctx); self.render_side_panel(ctx);
} }
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Hello world"); ui.heading("// TODO: Put shit here");
}); });
} }
} }

View File

@@ -2,6 +2,7 @@
mod app; mod app;
mod types; mod types;
mod util;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {

9
src/util.rs Normal file
View File

@@ -0,0 +1,9 @@
pub fn clamp(val: usize, min: usize, max: usize) -> usize {
if val < min {
min
} else if val > max {
max
} else {
val
}
}