fix ui widget alignment, remove add row button, add timer for search request
This commit is contained in:
+35
-19
@@ -9,34 +9,34 @@ slint::include_modules!();
|
|||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let ui = AppWindow::new()?;
|
let ui = AppWindow::new()?;
|
||||||
|
let ui_weak = ui.as_weak();
|
||||||
let items = Rc::new(VecModel::<StandardListViewItem>::from(Vec::new()));
|
let items = Rc::new(VecModel::<StandardListViewItem>::from(Vec::new()));
|
||||||
|
|
||||||
let items_clone = items.clone();
|
let items_clone = items.clone();
|
||||||
ui.on_request_text_input(move |text| {
|
ui.on_request_text_input(move |text| {
|
||||||
println!("User input: {}", text);
|
if let Some(ui) = ui_weak.upgrade() {
|
||||||
let response = rutracker::search(&text);
|
println!("User input: {}", text);
|
||||||
|
let now = std::time::Instant::now();
|
||||||
|
|
||||||
let new_items: Vec<StandardListViewItem> = match response {
|
let response = rutracker::search(&text);
|
||||||
Ok(softwares) => softwares
|
|
||||||
.into_iter()
|
|
||||||
.map(|s| StandardListViewItem::from(s.title.as_str()))
|
|
||||||
.collect(),
|
|
||||||
|
|
||||||
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<StandardListViewItem> = match response {
|
||||||
});
|
Ok(softwares) => softwares
|
||||||
|
.into_iter()
|
||||||
|
.map(|s| StandardListViewItem::from(s.title.as_str()))
|
||||||
|
.collect(),
|
||||||
|
|
||||||
let table_model = Rc::new(VecModel::from(Vec::<ModelRc<StandardListViewItem>>::new()));
|
Err(e) => vec![StandardListViewItem::from(format!("Error: {}", e).as_str())],
|
||||||
let items_clone = items.clone();
|
};
|
||||||
|
|
||||||
ui.set_table_data(table_model.clone().into());
|
items_clone.set_vec(new_items);
|
||||||
ui.on_add_row({
|
let table_model = Rc::new(VecModel::from(Vec::<ModelRc<StandardListViewItem>>::new()));
|
||||||
move || {
|
|
||||||
for item in items_clone.iter() {
|
ui.set_table_data(table_model.clone().into());
|
||||||
table_model.push(ModelRc::new(VecModel::from_slice(&[item.clone()])));
|
add_rows(&items_clone, &table_model);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -44,3 +44,19 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_rows(
|
||||||
|
items: &Rc<VecModel<StandardListViewItem>>,
|
||||||
|
table_model: &Rc<VecModel<ModelRc<StandardListViewItem>>>,
|
||||||
|
) {
|
||||||
|
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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ pub struct Software {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
//pub async fn search(value: &str) -> Result<Value, reqwest::Error> {
|
|
||||||
pub async fn search(value: &str) -> Result<Vec<Software>, reqwest::Error> {
|
pub async fn search(value: &str) -> Result<Vec<Software>, reqwest::Error> {
|
||||||
let api_url = "https://api.michijackson.xyz/search?q=".to_owned();
|
let api_url = "https://api.michijackson.xyz/search?q=".to_owned();
|
||||||
let results = reqwest::get(api_url + value).await?;
|
let results = reqwest::get(api_url + value).await?;
|
||||||
@@ -26,11 +25,8 @@ pub async fn search(value: &str) -> Result<Vec<Software>, reqwest::Error> {
|
|||||||
for item in data.as_array().unwrap() {
|
for item in data.as_array().unwrap() {
|
||||||
let software: Software =
|
let software: Software =
|
||||||
serde_json::from_value(item.clone()).expect("Failed to deserialize");
|
serde_json::from_value(item.clone()).expect("Failed to deserialize");
|
||||||
println!("{:?}", software);
|
|
||||||
|
|
||||||
software_list.push(software);
|
software_list.push(software);
|
||||||
}
|
}
|
||||||
//println!("{:?}", data);
|
|
||||||
//Ok(v)
|
|
||||||
Ok(software_list)
|
Ok(software_list)
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-19
@@ -1,9 +1,6 @@
|
|||||||
import { Button, LineEdit, VerticalBox, HorizontalBox, StandardTableView } from "std-widgets.slint";
|
import { Button, LineEdit, VerticalBox, HorizontalBox, StandardTableView } from "std-widgets.slint";
|
||||||
|
|
||||||
export component AppWindow inherits Window {
|
export component AppWindow inherits Window {
|
||||||
width: 200px;
|
|
||||||
height: 60px;
|
|
||||||
|
|
||||||
in-out property <string> search-text: "";
|
in-out property <string> search-text: "";
|
||||||
callback request-text-input(string);
|
callback request-text-input(string);
|
||||||
|
|
||||||
@@ -11,11 +8,8 @@ export component AppWindow inherits Window {
|
|||||||
callback add-row();
|
callback add-row();
|
||||||
VerticalBox {
|
VerticalBox {
|
||||||
alignment: stretch;
|
alignment: stretch;
|
||||||
// width: 50%;
|
|
||||||
// height: 50%;
|
|
||||||
|
|
||||||
LineEdit {
|
LineEdit {
|
||||||
width: 500px;
|
|
||||||
placeholder-text: "Search for Software";
|
placeholder-text: "Search for Software";
|
||||||
accepted(text) => {
|
accepted(text) => {
|
||||||
root.request-text-input(self.text);
|
root.request-text-input(self.text);
|
||||||
@@ -23,25 +17,17 @@ export component AppWindow inherits Window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
HorizontalBox {
|
HorizontalBox {
|
||||||
|
alignment: stretch;
|
||||||
table := StandardTableView {
|
table := StandardTableView {
|
||||||
// width: 230px;
|
|
||||||
// height: 200px;
|
|
||||||
|
|
||||||
columns: [
|
columns: [
|
||||||
{ title: "Post Title" },
|
{ title: "Post Title", horizontal_stretch: 0.5 },
|
||||||
{ title: "Author" },
|
{ title: "Author", horizontal_stretch: 0.3 },
|
||||||
{ title: "Seeders" },
|
{ title: "Seeders", horizontal_stretch: 0.1 },
|
||||||
{ title: "Leechers" },
|
{ title: "Leechers", horizontal_stretch: 0.1 },
|
||||||
];
|
];
|
||||||
rows: table-data;
|
rows: table-data;
|
||||||
}
|
}
|
||||||
|
|
||||||
Button {
|
|
||||||
text: "Add row";
|
|
||||||
clicked => {
|
|
||||||
add-row();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user