ashbat/src/main.rs

86 lines
2.4 KiB
Rust
Raw Normal View History

2022-11-03 15:23:59 +01:00
use gtk::prelude::*;
use gtk::{ScrolledWindow, Label, Align, Button, Application, LinkButton, PasswordEntry, ApplicationWindow, Box, Orientation, PolicyType};
use html_parser::{Dom, Node, Result};
const APP_ID: &str = "org.antopie.AshBat";
fn parse() -> Result<Vec<String>> {
let html = include_str!("test.html");
let dom = Dom::parse(html)?;
let iter = dom.children.get(0).unwrap().into_iter();
let hrefs = iter.filter_map(|item| match item {
Node::Element(ref element) if element.name == "a" => element.attributes["href"].clone(),
_ => None,
});
println!("\nThe following links where found:");
let mut vec: Vec<String> = Vec::new();
for (index, href) in hrefs.enumerate() {
println!("{}: {}", index + 1, &href);
vec.push(href);
}
Ok(vec)
}
fn build_ui(app: &Application) {
let vec = parse().expect("Nope");
let link_button = LinkButton::new("matrix:r/antopie-synapse:matrix.antopie.org");
let password_entry = PasswordEntry::new();
password_entry.set_show_peek_icon(true);
let text = Label::new(Some("If characters in @str are preceded by an underscore, they are underlined. If you need a literal underscore character in a label, use __ (two underscores). The first underlined character represents a keyboard accelerator called a mnemonic. The <mnemonic> key can be used to activate another widget, chosen automatically, or explicitly using set_mnemonic_widget()."));
Label::set_wrap(&text, true);
let button = Button::from_icon_name("go-next");
let gtk_box = Box::builder()
.margin_top(12)
.margin_bottom(12)
.margin_start(12)
.margin_end(12)
.valign(Align::Center)
.halign(Align::Center)
.spacing(12)
.orientation(Orientation::Vertical)
.build();
gtk_box.append(&link_button);
gtk_box.append(&password_entry);
gtk_box.append(&text);
gtk_box.append(&button);
let mut vec_button = Vec::new();
for x in &vec {
vec_button.push(LinkButton::new(x));
}
for x in &vec_button {
gtk_box.append(x);
}
let scrolled_window = ScrolledWindow::builder()
.hscrollbar_policy(PolicyType::Never) // Disable horizontal scrolling
.min_content_width(700)
.min_content_height(950)
.child(&gtk_box)
.build();
let window = ApplicationWindow::builder()
.application(app)
.title("AshBat")
.child(&scrolled_window)
.build();
window.present();
gtk_box.append(&Button::from_icon_name("go-next"));
}
fn main() {
let app = Application::builder().application_id(APP_ID).build();
app.connect_activate(build_ui);
app.run();
}