diff --git a/Cargo.toml b/Cargo.toml index 727fb6c..da96622 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ reqwest = {version = "0.11.11", features = ["blocking"]} iui = "0.3.0" serde_json = "1.0" tokio = {version="1", features=["full"]} +druid = "0.7.0" [build-dependencies] embed-resource = "1.6" diff --git a/assets/logo.png b/assets/logo.png index 6563a2e..f3214d4 100644 Binary files a/assets/logo.png and b/assets/logo.png differ diff --git a/src/main.rs b/src/main.rs index bd26c9e..4882e5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,24 @@ +use std::cell::RefCell; use std::path::{Path, PathBuf}; +use std::rc::Rc; use std::str; -use std::thread; use std::*; +use druid::widget::*; +use druid::{ + commands, AppDelegate, AppLauncher, Application, Data, DelegateCtx, Env, FileDialogOptions, + Handled, Target, WidgetExt, WindowDesc, +}; use fs_extra::{copy_items, dir}; -use iui::controls::{Button, Group, Label, VerticalBox}; -use iui::prelude::*; use regex::Regex; +struct Delegate; + +#[derive(Data, Clone)] +struct AppData { + pub among_us_path: Rc>, +} + static AMONG_US_APPID: &'static str = "945360"; #[derive(Ord, PartialOrd, Eq, PartialEq)] @@ -111,9 +122,39 @@ async fn get_latest_updater_version() -> Result<(String, String), reqwest::Error Ok((String::from("no"), String::from("yes"))) } +impl AppDelegate for Delegate { + fn command( + &mut self, + _ctx: &mut DelegateCtx, + _target: Target, + cmd: &druid::Command, + data: &mut AppData, + _env: &Env, + ) -> Handled { + if let Some(file_info) = cmd.get(commands::OPEN_FILE) { + println!("{:?}", file_info); + let among_us_folder = file_info.path(); + let mut buf = among_us_folder.to_path_buf(); + buf.pop(); + let mut borrow = data.among_us_path.borrow_mut(); + *borrow = String::from(buf.to_str().unwrap()); + + // Pop the selected file off the end of the path + // among_us_folder.pop(); + println!("{}", buf.to_str().unwrap()); + println!("Handled!"); + Application::global().quit(); + return Handled::Yes; + } + Handled::No + } +} + #[tokio::main] async fn main() { let version = env!("CARGO_PKG_VERSION"); + let title_string: String = format!("Town of Us Updater - {}", version); + println!("Updater Version: {}", version); get_latest_updater_version().await.unwrap(); //let _version_check_thread_handle = thread::spawn(move || get_latest_updater_version()); @@ -127,26 +168,6 @@ async fn main() { installs_path.push("installs"); fs::create_dir(installs_path.clone()).unwrap_or(()); - let ui = UI::init().expect("UI failed to init"); - - let mut win = Window::new(&ui, "Town of Us Updater", 600, 400, WindowType::NoMenubar); - - let mut vbox = VerticalBox::new(&ui); - - vbox.set_padded(&ui, true); - - // let mut button = Button::new(&ui, "Button"); - // button.on_clicked(&ui, { - // let ui = ui.clone(); - // move |btn| { - // let folder_opt = detect_among_us_folder(); - // if folder_opt.is_some() { - // btn.set_text(&ui, "Amogus"); - // ui.quit(); - // } - // } - // }); - // DETERMINE AMONG US VERSION let mut among_us_folder = path::PathBuf::new(); @@ -162,13 +183,37 @@ async fn main() { if folder_opt.is_some() { among_us_folder.push(folder_opt.unwrap()); } else { - win.modal_msg(&ui, "Find Among Us", "On the following Open File dialog, navigate to your Among Us folder and select Among Us.exe"); - let open_window = win.open_file(&ui); + let path_rc = Rc::new(RefCell::new(String::from(""))); + let data: AppData = AppData { + among_us_path: path_rc.clone(), + }; + let open_button = Button::new("Locate Among Us").fix_height(45.0).on_click( + move |ctx, _: &mut AppData, _| { + ctx.submit_command( + druid::commands::SHOW_OPEN_PANEL.with(FileDialogOptions::new()), + ); + }, + ); + let flex: Flex = Flex::column().with_flex_child(open_button, 1.0); + let open_dialog = WindowDesc::new(|| flex) + .title("Among Us Not Found!") + .window_size((400.0, 400.0)); + AppLauncher::with_window(open_dialog) + .delegate(Delegate) + .launch(data) + .unwrap_or_default(); + + among_us_folder = path::PathBuf::from(Rc::try_unwrap(path_rc).unwrap().take()); + + println!("{}", among_us_folder.to_str().unwrap()); + + // win.modal_msg(&ui, "Find Among Us", "On the following Open File dialog, navigate to your Among Us folder and select Among Us.exe"); + // let open_window = win.open_file(&ui); // println!("{}", open_window.unwrap().to_str().unwrap()); - among_us_folder = open_window.unwrap().clone(); + // among_us_folder = open_window.unwrap().clone(); // Pop the selected file off the end of the path - among_us_folder.pop(); + // among_us_folder.pop(); } fs::write(existing_file_path, among_us_folder.to_str().unwrap()).unwrap(); } @@ -260,59 +305,77 @@ async fn main() { // Find existing installs, make buttons for them let install_iter = fs::read_dir(installs_path.clone()); + let mut root_column: druid::widget::Flex = druid::widget::Flex::column(); - match install_iter { - Ok(iter) => { - let mut collection: Vec> = iter.collect(); - collection.reverse(); - for i in collection { - // for i in iter { - let existing_ver_smash = i.unwrap().file_name(); - let mut ver_smash_split = existing_ver_smash.to_str().unwrap().split("-"); - let auv = ver_smash_split.next().unwrap(); - let button_string: String = format!( - "Among Us {} Town of Us {}", - auv, - ver_smash_split.next().unwrap() - ); - let mut group_vbox = VerticalBox::new(&ui); - let mut group = Group::new(&ui, auv.clone()); + if let Ok(iter) = install_iter { + let mut collection: Vec> = iter.collect(); + collection.reverse(); - let mut button = Button::new(&ui, button_string.as_str()); - button.on_clicked(&ui, { - let ui = ui.clone(); - let installs_path = installs_path.clone(); - let existing_ver_smash = existing_ver_smash.clone(); - move |btn| { - let mut new_path = installs_path.clone(); - new_path.push(existing_ver_smash.clone()); - println!("{}", new_path.clone().to_str().unwrap()); - attempt_run_among_us(&new_path); - btn.set_text(&ui, "Launching Among Us..."); - } - }); - group_vbox.append(&ui, button, LayoutStrategy::Stretchy); - group.set_child(&ui, group_vbox); - vbox.append(&ui, group, LayoutStrategy::Stretchy); - println!("{}", existing_ver_smash.clone().to_str().unwrap()); + // Iterate first to find labels: + + let mut flexbox_array: Vec> = Vec::new(); + let mut auv_array: Vec = Vec::new(); + + for i in &collection { + let existing_ver_smash = i.as_ref().unwrap().file_name(); + let mut ver_smash_split = existing_ver_smash.to_str().unwrap().split("-"); + let auv = ver_smash_split.next().unwrap(); + if !auv_array.contains(&auv.to_string()) { + let label_text = format!("Among Us {}", auv); + let flex: druid::widget::Flex = druid::widget::Flex::column() + .with_flex_child( + druid::widget::Label::new(label_text.as_str()).with_text_size(24.0), + 1.0, + ) + .with_default_spacer(); + flexbox_array.push(flex); + auv_array.push(auv.to_string()); } } - _ => (), + + for i in collection { + let existing_ver_smash = i.unwrap().file_name(); + let mut ver_smash_split = existing_ver_smash.to_str().unwrap().split("-"); + let auv = ver_smash_split.next().unwrap(); + let button_string: String = format!("Town of Us {}", ver_smash_split.next().unwrap()); + + let mut index = 0; + for j in &auv_array { + if j == auv { + let mut clone = installs_path.clone(); + clone.push(existing_ver_smash.clone()); + + let fybutton = druid::widget::Button::new(button_string.as_str()) + .fix_height(45.0) + .on_click(move |_, _: &mut u32, _| { + attempt_run_among_us(&clone); + }); + + flexbox_array + .get_mut(index) + .unwrap() + .add_flex_child(fybutton, 1.0); + + println!("{}", existing_ver_smash.clone().to_str().unwrap()); + } + index += 1; + } + } + for i in flexbox_array { + root_column.add_flex_child(i, 1.0); + } } // println!("Checking for updater updates..."); // let vals: (String, String) = version_check_thread_handle.join().unwrap(); - // let mut update_button = Button::new(&ui, "Check for New Version"); - // update_button.on_clicked(&ui, { - // let ui = ui.clone(); - // move |btn| {} - // }); - // vbox.append(&ui, update_button, LayoutStrategy::Stretchy); + println!("Launching main window..."); + + let main_window = WindowDesc::new(|| root_column) + .title(title_string) + .window_size((400.0, 400.0)); + AppLauncher::with_window(main_window).launch(1).unwrap(); - win.set_child(&ui, vbox); - win.show(&ui); - ui.main(); // let mut install_folder_path = installs_path.clone(); // install_folder_path.push(version_smash); // fs::create_dir(install_folder_path.clone()).unwrap_or(());