AudioDeviceManager/src/window/popups/new_device_config_name.rs

93 lines
2.8 KiB
Rust

use std::collections::HashMap;
use adw::gdk::pango;
use adw::ResponseAppearance;
use crate::state_manager::structs::DeviceConfig;
use crate::window::subpages;
use super::*;
pub(in crate::window) async fn new_device_config_name(
main_window: &AudioDeviceManagerWindow,
name_suggestion: Option<String>,
properties: HashMap<String, String>,
profiles: Vec<String>,
selected_profile: Option<String>,
) {
let entry = gtk::Entry::builder()
.placeholder_text("Name")
.activates_default(true)
.build();
let cancel_response = "cancel";
let create_response = "create";
// Create new dialog
let dialog = adw::AlertDialog::builder()
.heading("Device Configuration Name")
.close_response(cancel_response)
.default_response(create_response)
.extra_child(&entry)
.build();
dialog.add_responses(&[(cancel_response, "Cancel"), (create_response, "Create")]);
// Make the dialog button insensitive initially
dialog.set_response_enabled(create_response, false);
dialog.set_response_appearance(create_response, ResponseAppearance::Suggested);
let config_names = main_window.imp().state_manager.borrow_mut().get_device_config_names();
if let Some(init_name) = name_suggestion {
entry.set_text(&init_name);
if init_name.is_empty() || config_names.contains(&init_name) {
entry.add_css_class("error")
} else {
dialog.set_response_enabled(create_response, true)
}
}
// Set entry's css class to "error", when there is no text in it
entry.connect_changed(clone!(
#[weak]
dialog,
move |entry| {
let text = entry.text();
let empty = text.is_empty();
let exists = config_names.contains(&String::from(text));
let err = empty || exists;
dialog.set_response_enabled(create_response, !err);
if err {
entry.add_css_class("error");
} else {
entry.remove_css_class("error");
}
}
));
let response = dialog.choose_future(main_window).await;
// Return if the user chose 'cancel_response'
if response == cancel_response {
println!("Cancel");
return;
};
if response == create_response {
let device_config_name = entry.text().to_string();
main_window.imp().state_manager.borrow_mut().create_new_device_config(
device_config_name.clone(),
properties,
profiles,
selected_profile
);
main_window.add_device_config(&device_config_name);
subpages::device_config::switch_to_device_config_window(main_window, device_config_name);
return;
};
}