From 484878c33f42fc215d0be439f53b4e6d29f5542a Mon Sep 17 00:00:00 2001 From: felikcat <29991266+felikcat@users.noreply.github.com> Date: Sun, 8 Dec 2024 16:31:03 -0800 Subject: [PATCH] Update gui.rs --- src/gui.rs | 367 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 211 insertions(+), 156 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index 85bf100..4321209 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -21,12 +21,12 @@ use fltk::{ }; use fltk_theme::{ColorTheme, color_themes}; use std::{ - borrow::BorrowMut, - cell::RefCell, + borrow::{Borrow, BorrowMut}, + cell::{Ref, RefCell}, error::Error, mem, ops::DerefMut, - process::{Command, exit}, + process::{exit, Command}, rc::Rc, }; use windows::Win32::{ @@ -37,7 +37,7 @@ use windows::Win32::{ }; use crate::common::center; -type MyCheckboxes = [CheckButton; 9]; +type MyCheckboxes = [RefCell; 9]; pub fn draw_gui() -> Result<(), Box> { let app = app::App::default().with_scheme(app::Scheme::Gtk); @@ -62,120 +62,138 @@ pub fn draw_gui() -> Result<(), Box> { titlebar_close.set_frame(enums::FrameType::NoBox); titlebar_close.set_callback(move |_| exit(0)); - let mut apply = Button::new( + let apply = Rc::new(RefCell::new(Button::new( 0, 0, (wind.width() - 6) / 2, (wind.height() * 14) / 100, "Apply W11Boost", - ) - .center_of(&wind); + ).center_of(&wind))); + + let apply = Rc::clone(&apply); - let mut remove = Button::new( + let mut apply_mut = (*apply).borrow_mut(); + + let apply_height = apply_mut.clone().height(); + apply_mut.set_pos(2, wind.height() - apply_height - 2); // Put button at the bottom + + let remove = Rc::new(RefCell::new(Button::new( wind.width() / 2, 0, (wind.width() - 6) / 2, (wind.height() * 14) / 100, "Remove W11Boost", - ) - .center_of(&wind); - - apply.set_pos(2, wind.height() - apply.height() - 2); // Put button at the bottom - - remove.set_pos( - wind.width() - remove.width() - 2, - wind.height() - remove.height() - 2, + ))); + + let remove = Rc::clone(&remove); + let mut remove_mut = (*remove).borrow_mut(); + + remove_mut.clone().center_of(&wind); + + let remove_width = remove_mut.width(); + let remove_height = remove_mut.height(); + remove_mut.set_pos( + wind.width() - remove_width - 2, + wind.height() - remove_height - 2, ); - apply.set_label_font(enums::Font::by_name(&font)); - apply.set_label_size(16); - remove.set_label_font(enums::Font::by_name(&font)); - remove.set_label_size(16); + remove_mut.set_label_font(enums::Font::by_name(&font)); + remove_mut.set_label_size(16); + + apply_mut.set_label_font(enums::Font::by_name(&font)); + apply_mut.set_label_size(16); let checkbox_height = wind.height() / 12; let mut my_checkboxes: MyCheckboxes = [ - CheckButton::new( + RefCell::new(CheckButton::new( 0, titlebar.height(), wind.width(), checkbox_height, "Reduce local data / forensics to a minimum", - ), - CheckButton::new( + )), + RefCell::new(CheckButton::new( 0, titlebar.height() + checkbox_height + 2, wind.width(), checkbox_height, "Reduce online activity to a minimum", - ), - CheckButton::new( + )), + RefCell::new(CheckButton::new( 0, titlebar.height() + checkbox_height * 2 + 4, wind.width(), checkbox_height, "Create a system restore point", - ), - CheckButton::new( + )), + RefCell::new(CheckButton::new( 0, titlebar.height() + checkbox_height * 3 + 6, wind.width(), checkbox_height, "Install the Microsoft Store", - ), - CheckButton::new( + )), + RefCell::new(CheckButton::new( 0, titlebar.height() + checkbox_height * 4 + 8, wind.width(), checkbox_height, "Install support for .appx/.appxbundle and WinGet", - ), - CheckButton::new( + )), + RefCell::new(CheckButton::new( 0, titlebar.height() + checkbox_height * 5 + 10, wind.width(), checkbox_height, "Disable Defender and SmartScreen", - ), - CheckButton::new( + )), + RefCell::new(CheckButton::new( 0, titlebar.height() + checkbox_height * 6 + 12, wind.width(), checkbox_height, "Disable sleep and hibernate", - ), - CheckButton::new( + )), + RefCell::new(CheckButton::new( 0, titlebar.height() + checkbox_height * 7 + 14, wind.width(), checkbox_height, "Disable Virtualization Based Security", - ), - CheckButton::new( + )), + RefCell::new(CheckButton::new( 0, titlebar.height() + checkbox_height * 8 + 16, wind.width(), checkbox_height, "Add in non-intrusive tweaks", - ), + )), ]; - for checkbox in &mut my_checkboxes { + let my_checkboxes = my_checkboxes.clone(); + + for checkbox in &my_checkboxes { + let mut checkbox = checkbox.borrow_mut(); checkbox.set_label_font(enums::Font::by_name(&font)); checkbox.set_label_size(16); } - my_checkboxes[2].set_value(true); - my_checkboxes[8].set_value(true); + my_checkboxes[2].borrow_mut().set_value(true); + my_checkboxes[8].borrow_mut().set_value(true); - let frame0 = RefCell::new(Widget::default() - .with_size(wind.width(), wind.height() - titlebar.height()) - .with_pos(0, titlebar.height())); + let frame0 = RefCell::new( + Widget::default() + .with_size(wind.width(), wind.height() - titlebar.height()) + .with_pos(0, titlebar.height()), + ); - let frame0_clone = frame0.clone(); + let frame0 = frame0.clone(); - frame0_clone.borrow_mut().set_frame(enums::FrameType::BorderBox); - frame0_clone.borrow_mut().draw(move |f| { + frame0 + .borrow_mut() + .set_frame(enums::FrameType::BorderBox); + frame0.borrow_mut().draw(move |f| { let label = f.label(); let txt = label.split(" ").nth(0).unwrap(); let x = f.x(); @@ -190,8 +208,10 @@ pub fn draw_gui() -> Result<(), Box> { draw::draw_text2(txt, x, y - 16, w, h, f.align()); draw::pop_clip(); }); - frame0_clone.borrow_mut().set_label("Applying W11Boost, please wait..."); - frame0_clone.borrow_mut().hide(); + frame0 + .borrow_mut() + .set_label("Applying W11Boost, please wait..."); + frame0.borrow_mut().hide(); wind.end(); wind.show(); @@ -241,16 +261,18 @@ pub fn draw_gui() -> Result<(), Box> { fn hide_elements( frame0: &mut impl WidgetExt, - apply_clone: &mut impl ButtonExt, - my_checkboxes: &mut [impl WidgetExt], + apply: &mut impl ButtonExt, + remove: &mut impl ButtonExt, + my_checkboxes: &[RefCell], ) { frame0.show(); frame0.top_window(); // So these aren't accidentally clicked. - apply_clone.hide(); + apply.hide(); + remove.hide(); for checkbox in my_checkboxes { - checkbox.hide(); + checkbox.borrow_mut().hide(); } // Force window to redraw to display frame0. @@ -260,125 +282,158 @@ pub fn draw_gui() -> Result<(), Box> { fn show_elements( frame0: &mut impl WidgetExt, - apply_clone: &mut impl ButtonExt, - my_checkboxes: &mut [impl WidgetExt], + apply: &mut impl ButtonExt, + remove: &mut impl ButtonExt, + my_checkboxes: &[RefCell], ) { // Does not require a manual redraw. frame0.hide(); - apply_clone.show(); + apply.show(); + remove.show(); for checkbox in my_checkboxes { - checkbox.show(); + checkbox.borrow_mut().show(); } } - - let mut apply_clone = apply.clone(); - - apply.set_callback( move |_| { - let choice = dialog::choice2( - center().0, - center().1, - "Are you sure you want to apply W11Boost?", - "&Yes", - "&No", - "", - ); - if choice == Some(0) { - hide_elements( - &mut *frame0_clone.borrow_mut(), - &mut *apply_clone.borrow_mut(), - &mut *my_checkboxes.borrow_mut(), + (*apply_mut).set_callback({ + let frame0 = frame0.clone(); + let apply = apply.clone(); + let remove = remove.clone(); + let my_checkboxes = my_checkboxes.clone(); + move |_| { + let choice = dialog::choice2( + center().0, + center().1, + "Are you sure you want to apply W11Boost?", + "&Yes", + "&No", + "", ); - // Has to be first! - if my_checkboxes[2].is_checked() { - create_system_restore_point::run() - .expect("create_system_restore_point::run failed"); - } - - if my_checkboxes[0].is_checked() { - reduce_forensics::run().expect("reduce_forensics::run failed"); - } - if my_checkboxes[1].is_checked() { - reduce_online_data_collection::run() - .expect("reduce_online_data_collection::run failed"); - } - if my_checkboxes[3].is_checked() { - Command::new("wsreset.exe") - .output() - .expect("wsreset.exe failed to execute"); - } - if my_checkboxes[4].is_checked() { - appx_support::install().expect("appx_support::install failed"); - } - - if my_checkboxes[5].is_checked() { - disable_defender_and_smartscreen::run() - .expect("disable_defender_and_smartscreen::run failed"); - } - - if my_checkboxes[6].is_checked() { - disable_sleep::run().expect("disable_sleep::run failed"); - } - - if my_checkboxes[7].is_checked() { - disable_vbs::run().expect("disable_vbs::run failed"); - } - - if my_checkboxes[8].is_checked() { - defaults::run().expect("defaults::run failed"); - } - - if my_checkboxes.iter().all(|checkbox| !checkbox.is_checked()) { - dialog::message( - center().0, - center().1, - "No options were selected, therefore nothing has changed.", + if choice == Some(0) { + hide_elements( + &mut *frame0.borrow_mut(), + &mut *(*apply).borrow_mut(), + &mut *(*remove).borrow_mut(), + &my_checkboxes, ); - } else { - dialog::message( - center().0, - center().1, - "W11Boost applied your preferences successfully, please reboot.", + // Has to be first! + if let Ok(checkbox) = my_checkboxes[2].try_borrow_mut() { + if checkbox.is_checked() { + create_system_restore_point::run() + .expect("create_system_restore_point::run failed"); + } + } + + if let Ok(checkbox) = my_checkboxes[0].try_borrow_mut() { + if checkbox.is_checked() { + reduce_forensics::run().expect("reduce_forensics::run failed"); + } + } + if let Ok(checkbox) = my_checkboxes[1].try_borrow_mut() { + if checkbox.is_checked() { + reduce_online_data_collection::run() + .expect("reduce_online_data_collection::run failed"); + } + } + if let Ok(checkbox) = my_checkboxes[3].try_borrow_mut() { + if checkbox.is_checked() { + Command::new("wsreset.exe") + .output() + .expect("wsreset.exe failed to execute"); + } + } + if let Ok(checkbox) = my_checkboxes[4].try_borrow_mut() { + if checkbox.is_checked() { + appx_support::install().expect("appx_support::install failed"); + } + } + + if let Ok(checkbox) = my_checkboxes[5].try_borrow_mut() { + if checkbox.is_checked() { + disable_defender_and_smartscreen::run() + .expect("disable_defender_and_smartscreen::run failed"); + } + } + + if let Ok(mut checkbox) = my_checkboxes[6].try_borrow_mut() { + if checkbox.is_checked() { + disable_sleep::run().expect("disable_sleep::run failed"); + } + } + + if let Ok(mut checkbox) = my_checkboxes[7].try_borrow_mut() { + if checkbox.is_checked() { + disable_vbs::run().expect("disable_vbs::run failed"); + } + } + + if let Ok(mut checkbox) = my_checkboxes[8].try_borrow_mut() { + if checkbox.is_checked() { + defaults::run().expect("defaults::run failed"); + } + } + + if my_checkboxes.iter().all(|checkbox| checkbox.try_borrow_mut().map_or(false, |mut cb| !cb.is_checked())) { + dialog::message( + center().0, + center().1, + "No options were selected, therefore nothing has changed.", + ); + } else { + dialog::message( + center().0, + center().1, + "W11Boost applied your preferences successfully, please reboot.", + ); + } + + show_elements( + &mut *frame0.borrow_mut(), + &mut *(*apply).borrow_mut(), + &mut *(*remove).borrow_mut(), + &my_checkboxes, ); } - - show_elements( - &mut *frame0_clone.borrow_mut(), - &mut *apply_clone.borrow_mut(), - &mut *my_checkboxes.borrow_mut(), - ); } }); - remove.set_callback(move |_| { - let choice = dialog::choice2( - center().0, - center().1, - "Are you sure you want to remove W11Boost?", - "&Yes", - "&No", - "", - ); - if choice == Some(0) { - hide_elements( - &mut *frame0_clone.borrow_mut(), - &mut apply_clone, - &mut my_checkboxes, + (*remove_mut).borrow_mut().set_callback({ + let frame0 = frame0.clone(); + let apply = apply.clone(); + let remove = remove.clone(); + let my_checkboxes = my_checkboxes.clone(); + move |_| { + let choice = dialog::choice2( + center().0, + center().1, + "Are you sure you want to remove W11Boost?", + "&Yes", + "&No", + "", ); - if let Ok(_) = remove_w11boost::run() { - dialog::message( - center().0, - center().1, - "W11Boost applied your preferences successfully, please reboot.", + if choice == Some(0) { + hide_elements( + &mut *frame0.borrow_mut(), + &mut *(*apply).borrow_mut(), + &mut *(*remove).borrow_mut(), + &my_checkboxes, + ); + if let Ok(_) = remove_w11boost::run() { + dialog::message( + center().0, + center().1, + "W11Boost applied your preferences successfully, please reboot.", + ); + } else { + eprintln!("remove_w11boost::run failed"); + } + + show_elements( + &mut *frame0.borrow_mut(), + &mut *(*apply).borrow_mut(), + &mut *(*remove).borrow_mut(), + &my_checkboxes, ); - } else { - eprintln!("remove_w11boost::run failed"); } - - show_elements( - &mut *frame0_clone.borrow_mut(), - &mut apply_clone, - &mut my_checkboxes, - ); } });