Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a choose config file button #80

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use gtk::{prelude::*, Application, Button};
use gtk::{prelude::*, Application, Button, FileChooserAction, FileChooserDialog};
use hyprparser::parse_config;
use std::{cell::RefCell, env, fs, path::Path, path::PathBuf, rc::Rc};

Expand All @@ -8,6 +8,7 @@ mod widgets;

const CONFIG_PATH: &str = ".config/hypr/hyprland.conf";
const BACKUP_SUFFIX: &str = "-bak";
static CONFIG_PATH_OVERRIDE: std::sync::Mutex<Option<PathBuf>> = std::sync::Mutex::new(None);

fn main() {
let app = Application::builder()
Expand Down Expand Up @@ -62,6 +63,63 @@ fn build_ui(app: &Application) {

let undo_button = Button::with_label("Undo Changes");
let copy_button = Button::with_label("Copyright");
let choose_config_button = Button::with_label("Choose Hyprland Config");

let gui_clone = gui.clone();
choose_config_button.connect_clicked(move |button| {
if let Some(popover) = button.ancestor(gtk::Popover::static_type()) {
if let Some(popover) = popover.downcast_ref::<gtk::Popover>() {
popover.popdown();
}
}

let dialog = FileChooserDialog::new(
Some("Select Hyprland Config"),
Some(&gui_clone.borrow().window),
FileChooserAction::Open,
&[
("Cancel", gtk::ResponseType::Cancel),
("Open", gtk::ResponseType::Accept),
],
);

let gui_clone_inner = gui_clone.clone();
dialog.connect_response(move |dialog, response| {
if response == gtk::ResponseType::Accept {
if let Some(file) = dialog.file() {
if let Some(path) = file.path() {
if let Ok(mut override_path) = CONFIG_PATH_OVERRIDE.lock() {
*override_path = Some(path.clone());
let config_str = match fs::read_to_string(&path) {
Ok(s) => s,
Err(e) => {
gui_clone_inner.borrow_mut().custom_error_popup_critical(
"Reading failed",
&format!(
"Failed to read the configuration file: {}",
e
),
true,
);
String::new()
}
};
let parsed_config = parse_config(&config_str);
gui_clone_inner.borrow_mut().load_config(&parsed_config);
gui_clone_inner
.borrow_mut()
.get_changes()
.borrow_mut()
.clear();
}
}
}
}
dialog.close();
});

dialog.show();
});

let gui_clone = gui.clone();
undo_button.connect_clicked(move |button| {
Expand Down Expand Up @@ -101,6 +159,7 @@ along with this program; if not, see

if let Some(gear_menu_box) = gui.borrow().gear_menu.borrow().child() {
if let Some(box_widget) = gear_menu_box.downcast_ref::<gtk::Box>() {
box_widget.append(&choose_config_button);
box_widget.append(&undo_button);
box_widget.append(&copy_button);
}
Expand Down Expand Up @@ -332,5 +391,10 @@ fn undo_changes(gui: Rc<RefCell<gui::ConfigGUI>>) {
}

fn get_config_path() -> PathBuf {
if let Ok(override_path) = CONFIG_PATH_OVERRIDE.lock() {
if let Some(path) = &*override_path {
return path.clone();
}
}
Path::new(&env::var("HOME").unwrap_or_else(|_| ".".to_string())).join(CONFIG_PATH)
}