From 153ba18d1cc60bad244bfefb74ce1b599851e506 Mon Sep 17 00:00:00 2001 From: KeksNino Date: Mon, 27 Jul 2026 01:52:24 +0200 Subject: [PATCH] fix ui widget alignment, remove add row button, add timer for search request --- src/main.rs | 54 +++++++++++++++++++++++++++++---------------- src/rutracker.rs | 4 ---- ui/app-window.slint | 24 +++++--------------- 3 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/main.rs b/src/main.rs index fb78110..7bbeed3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,34 +9,34 @@ slint::include_modules!(); fn main() -> Result<(), Box> { let ui = AppWindow::new()?; + let ui_weak = ui.as_weak(); let items = Rc::new(VecModel::::from(Vec::new())); let items_clone = items.clone(); ui.on_request_text_input(move |text| { - println!("User input: {}", text); - let response = rutracker::search(&text); + if let Some(ui) = ui_weak.upgrade() { + println!("User input: {}", text); + let now = std::time::Instant::now(); - let new_items: Vec = match response { - Ok(softwares) => softwares - .into_iter() - .map(|s| StandardListViewItem::from(s.title.as_str())) - .collect(), + let response = rutracker::search(&text); - Err(e) => vec![StandardListViewItem::from(format!("Error: {}", e).as_str())], - }; + let elapsed_time = now.elapsed(); + println!("Search took {} seconds", elapsed_time.as_secs()); - items_clone.set_vec(new_items); - }); + let new_items: Vec = match response { + Ok(softwares) => softwares + .into_iter() + .map(|s| StandardListViewItem::from(s.title.as_str())) + .collect(), - let table_model = Rc::new(VecModel::from(Vec::>::new())); - let items_clone = items.clone(); + Err(e) => vec![StandardListViewItem::from(format!("Error: {}", e).as_str())], + }; - ui.set_table_data(table_model.clone().into()); - ui.on_add_row({ - move || { - for item in items_clone.iter() { - table_model.push(ModelRc::new(VecModel::from_slice(&[item.clone()]))); - } + items_clone.set_vec(new_items); + let table_model = Rc::new(VecModel::from(Vec::>::new())); + + ui.set_table_data(table_model.clone().into()); + add_rows(&items_clone, &table_model); } }); @@ -44,3 +44,19 @@ fn main() -> Result<(), Box> { Ok(()) } + +fn add_rows( + items: &Rc>, + table_model: &Rc>>, +) { + for item in items.iter() { + let row = Rc::new(VecModel::from(vec![ + item.clone(), + StandardListViewItem::from("placeholder column 1"), + StandardListViewItem::from("placeholder column 2"), + StandardListViewItem::from("placeholder column 3"), + ])); + + table_model.push(ModelRc::from(row.clone())); + } +} diff --git a/src/rutracker.rs b/src/rutracker.rs index 2e940f3..8ee130a 100644 --- a/src/rutracker.rs +++ b/src/rutracker.rs @@ -12,7 +12,6 @@ pub struct Software { } #[tokio::main] -//pub async fn search(value: &str) -> Result { pub async fn search(value: &str) -> Result, reqwest::Error> { let api_url = "https://api.michijackson.xyz/search?q=".to_owned(); let results = reqwest::get(api_url + value).await?; @@ -26,11 +25,8 @@ pub async fn search(value: &str) -> Result, reqwest::Error> { for item in data.as_array().unwrap() { let software: Software = serde_json::from_value(item.clone()).expect("Failed to deserialize"); - println!("{:?}", software); software_list.push(software); } - //println!("{:?}", data); - //Ok(v) Ok(software_list) } diff --git a/ui/app-window.slint b/ui/app-window.slint index 71d829a..6143cb7 100644 --- a/ui/app-window.slint +++ b/ui/app-window.slint @@ -1,9 +1,6 @@ import { Button, LineEdit, VerticalBox, HorizontalBox, StandardTableView } from "std-widgets.slint"; export component AppWindow inherits Window { - width: 200px; - height: 60px; - in-out property search-text: ""; callback request-text-input(string); @@ -11,11 +8,8 @@ export component AppWindow inherits Window { callback add-row(); VerticalBox { alignment: stretch; - // width: 50%; - // height: 50%; LineEdit { - width: 500px; placeholder-text: "Search for Software"; accepted(text) => { root.request-text-input(self.text); @@ -23,25 +17,17 @@ export component AppWindow inherits Window { } HorizontalBox { + alignment: stretch; table := StandardTableView { - // width: 230px; - // height: 200px; columns: [ - { title: "Post Title" }, - { title: "Author" }, - { title: "Seeders" }, - { title: "Leechers" }, + { title: "Post Title", horizontal_stretch: 0.5 }, + { title: "Author", horizontal_stretch: 0.3 }, + { title: "Seeders", horizontal_stretch: 0.1 }, + { title: "Leechers", horizontal_stretch: 0.1 }, ]; rows: table-data; } - - Button { - text: "Add row"; - clicked => { - add-row(); - } - } } } }