set preferred terminal WIP - need to fix flatpak

This commit is contained in:
Dvlv 2024-02-16 13:30:50 +00:00
parent cb430426b8
commit 7dd8a922a6
No known key found for this signature in database
GPG key ID: 1F4BD7220B7FDCFA
4 changed files with 194 additions and 52 deletions

Binary file not shown.

View file

@ -2,7 +2,7 @@
<schema id="io.github.dvlv.boxbuddyrs" path="/io/github/dvlv/boxbuddyrs/" gettext-domain="boxbuddyrs">
<key name="default-terminal" type="s">
<default>"kgx"</default>
<default>"GNOME Console"</default>
<summary>Default Terminal to launch</summary>
<description>
The terminal which should be checked for first when performing an action which spawns a terminal window.

View file

@ -1324,21 +1324,117 @@ fn show_incorrect_binary_file_popup(window: &ApplicationWindow, file_type: Binar
}
fn show_preferred_terminal_popup(window: &ApplicationWindow) {
let terms = get_supported_terminals();
let settings = Settings::new(APP_ID);
let default_term = settings.string("default-terminal");
let mut selected_term_idx: u32 = 0;
let message_body = gettext(format!("You like {}", default_term));
let d = adw::MessageDialog::new(
Some(window),
//TRANSLATORS: Popup Heading
Some(&gettext("WIP")),
Some(&message_body),
for (idx, term) in terms.iter().enumerate() {
if term.name == default_term {
selected_term_idx = idx as u32;
break;
}
}
let term_pref_popup = gtk::Window::new();
term_pref_popup.set_transient_for(Some(window));
term_pref_popup.set_modal(true);
term_pref_popup.set_default_size(500, 250);
// TRANSLATORS: Popup Header
let title_lbl = gtk::Label::new(Some(&gettext("Preferred Terminal")));
title_lbl.add_css_class("header");
// TRANSLATORS: Button Label
let save_btn = gtk::Button::with_label(&gettext("Save"));
save_btn.add_css_class("suggested-action");
// TRANSLATORS: Button Label
let cancel_btn = gtk::Button::with_label(&gettext("Cancel"));
cancel_btn.connect_clicked(move |btn| {
let win = btn.root().and_downcast::<gtk::Window>().unwrap();
win.destroy();
});
let term_pref_titlebar = adw::HeaderBar::builder().title_widget(&title_lbl).build();
term_pref_titlebar.pack_end(&save_btn);
term_pref_titlebar.pack_start(&cancel_btn);
term_pref_popup.set_titlebar(Some(&term_pref_titlebar));
let main_box = gtk::Box::new(gtk::Orientation::Vertical, 20);
main_box.set_margin_top(20);
// TRANSLATORS: Instructions label
let instruction_label = gtk::Label::new(Some(&gettext("Select your preferred terminal")));
instruction_label.add_css_class("title-2");
let exp = gtk::PropertyExpression::new(
gtk::StringObject::static_type(),
None::<gtk::Expression>,
"string",
);
d.set_transient_for(Some(window));
//TRANSLATORS: Button Label
d.add_response("ok", &gettext("Ok"));
d.set_default_response(Some("ok"));
d.set_close_response("ok");
d.present()
let term_names_as_refs: Vec<&str> = terms.iter().map(|t| t.name.as_ref()).collect();
let term_names_strlist = gtk::StringList::new(&term_names_as_refs);
let terms_dropdown = gtk::DropDown::new(Some(term_names_strlist), Some(exp));
terms_dropdown.set_selected(selected_term_idx);
terms_dropdown.set_enable_search(true);
terms_dropdown.set_search_match_mode(gtk::StringFilterMatchMode::Substring);
terms_dropdown.set_width_request(400);
let terms_dd_row = adw::ActionRow::new();
// TRANSLATORS: Label for Dropdown of terminals available
terms_dd_row.set_title(&gettext("Terminal"));
terms_dd_row.set_activatable_widget(Some(&terms_dropdown));
terms_dd_row.add_suffix(&terms_dropdown);
let dd_clone = terms_dropdown.clone();
let popup_clone = term_pref_popup.clone();
let win_clone = window.clone();
save_btn.connect_clicked(move |_btn| {
let term_name = dd_clone
.selected_item()
.unwrap()
.downcast::<gtk::StringObject>()
.unwrap()
.string()
.to_string();
let settings = gio::Settings::new(APP_ID);
match settings.set_string("default-terminal", term_name.as_ref()) {
Ok(_) => {
// TRANSLATORS: Success Message
let toast = adw::Toast::new(&gettext("Terminal Preference Saved!"));
if let Some(child) = win_clone.clone().child() {
let toast_area = child.downcast::<ToastOverlay>();
toast_area.unwrap().add_toast(toast);
}
popup_clone.destroy();
delayed_rerender(&win_clone, None);
}
Err(_) => {
// TRANSLATORS: Error Message
let toast = adw::Toast::new(&gettext("Sorry, Preference Could Not Be Saved"));
if let Some(child) = win_clone.clone().child() {
let toast_area = child.downcast::<ToastOverlay>();
toast_area.unwrap().add_toast(toast);
}
popup_clone.destroy();
delayed_rerender(&win_clone, None);
}
}
});
main_box.append(&instruction_label);
main_box.append(&terms_dd_row);
term_pref_popup.set_child(Some(&main_box));
term_pref_popup.present();
}

View file

@ -1,17 +1,26 @@
use adw::StyleManager;
use gettextrs::*;
use gtk::gio::Settings;
use gtk::prelude::SettingsExt;
use std::collections::HashMap;
use std::env;
use std::path::Path;
use std::process::Command;
use crate::get_all_distroboxes;
use crate::APP_ID;
pub struct FilesystemAccess {
pub home: bool,
pub host: bool,
}
pub struct TerminalOption {
pub name: String,
pub executable_name: String,
pub separator_arg: String,
}
impl FilesystemAccess {
fn new() -> Self {
FilesystemAccess {
@ -166,58 +175,95 @@ pub fn has_distrobox_installed() -> bool {
true
}
pub fn get_supported_terminals() -> Vec<TerminalOption> {
vec![
TerminalOption {
name: String::from("GNOME Console"),
executable_name: String::from("kgx"),
separator_arg: String::from("--"),
},
TerminalOption {
name: String::from("GNOME Terminal"),
executable_name: String::from("gnome-terminal"),
separator_arg: String::from("--"),
},
TerminalOption {
name: String::from("Konsole"),
executable_name: String::from("konsole"),
separator_arg: String::from("-e"),
},
TerminalOption {
name: String::from("Tilix"),
executable_name: String::from("tilix"),
separator_arg: String::from("-e"),
},
TerminalOption {
name: String::from("Kitty"),
executable_name: String::from("kitty"),
separator_arg: String::from(""),
},
TerminalOption {
name: String::from("Alacritty"),
executable_name: String::from("alacritty"),
separator_arg: String::from("-e"),
},
TerminalOption {
name: String::from("Xterm"),
executable_name: String::from("xterm"),
separator_arg: String::from("-e"),
},
]
}
pub fn get_terminal_and_separator_arg() -> (String, String) {
let mut output = get_command_output(String::from("which"), Some(&["kgx"]));
let settings = Settings::new(APP_ID);
let chosen_term = settings.string("default-terminal");
// gnome console
if !output.contains("no kgx in") && !output.is_empty() {
return (String::from("kgx"), String::from("--"));
// first iter through supported terms and find the exec name of their default
let supported_terminals = get_supported_terminals();
let mut chosen_term_obj = &supported_terminals[0];
for term in &supported_terminals {
if term.name == chosen_term {
chosen_term_obj = term;
break;
}
}
// gnome terminal
output = get_command_output(String::from("which"), Some(&["gnome-terminal"]));
if !output.contains("no gnome-terminal in") && !output.is_empty() {
return (String::from("gnome-terminal"), String::from("--"));
let mut output = get_command_output(
String::from("which"),
Some(&[&chosen_term_obj.executable_name]),
);
let mut potential_error_msg = format!("no {} in", chosen_term_obj.executable_name);
// if their chosen term is available, return its details
if !output.contains(&potential_error_msg) && !output.is_empty() {
return (
chosen_term_obj.executable_name.clone(),
chosen_term_obj.separator_arg.clone(),
);
}
// konsole
output = get_command_output(String::from("which"), Some(&["konsole"]));
if !output.contains("no konsole in") && !output.is_empty() {
return (String::from("konsole"), String::from("-e"));
}
// if chosen term is NOT available, iter through list as before
for term in &supported_terminals {
output = get_command_output(String::from("which"), Some(&[&term.executable_name]));
potential_error_msg = format!("no {} in", term.executable_name);
// tilix
output = get_command_output(String::from("which"), Some(&["tilix"]));
if !output.contains("no tilix in") && !output.is_empty() {
return (String::from("tilix"), String::from("-e"));
}
//kitty
// kitty doesnt have an arg, just `kitty distrobox enter`
output = get_command_output(String::from("which"), Some(&["kitty"]));
if !output.contains("no kitty in") && !output.is_empty() {
return (String::from("kitty"), String::from(""));
}
//alacritty
output = get_command_output(String::from("which"), Some(&["alacritty"]));
if !output.contains("no alacritty in") && !output.is_empty() {
return (String::from("alacritty"), String::from("-e"));
}
//xterm
output = get_command_output(String::from("which"), Some(&["xterm"]));
if !output.contains("no xterm in") && !output.is_empty() {
return (String::from("xterm"), String::from("-e"));
if !output.contains(&potential_error_msg) && !output.is_empty() {
return (term.executable_name.clone(), term.separator_arg.clone());
}
}
(String::from(""), String::from(""))
}
pub fn get_supported_terminals_list() -> String {
String::from(
"- GNOME Terminal\n- GNOME Console\n- Konsole\n- Tilix\n- Kitty\n- Alacritty\n- Xterm",
)
let terms = get_supported_terminals();
terms
.iter()
.map(|t| format!("- {}", t.name))
.collect::<Vec<String>>()
.join("\n")
}
pub fn get_container_runtime() -> String {