add constructor for menu item
This commit is contained in:
parent
1525803ccc
commit
b18a44fd47
2 changed files with 84 additions and 88 deletions
|
@ -84,12 +84,37 @@ pub struct MenuItem<T: Clone> {
|
||||||
pub action: Option<String>,
|
pub action: Option<String>,
|
||||||
pub sub_elements: Vec<MenuItem<T>>,
|
pub sub_elements: Vec<MenuItem<T>>,
|
||||||
pub working_dir: Option<String>,
|
pub working_dir: Option<String>,
|
||||||
pub initial_sort_score: i64, // todo make this f64
|
pub initial_sort_score: f64,
|
||||||
pub search_sort_score: f64, // todo make this private
|
|
||||||
pub visible: bool, // todo make this private
|
|
||||||
|
|
||||||
/// Allows to store arbitrary additional information
|
/// Allows to store arbitrary additional information
|
||||||
pub data: Option<T>,
|
pub data: Option<T>,
|
||||||
|
|
||||||
|
search_sort_score: f64,
|
||||||
|
visible: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Clone> MenuItem<T> {
|
||||||
|
pub fn new(
|
||||||
|
label: String,
|
||||||
|
icon_path: Option<String>,
|
||||||
|
action: Option<String>,
|
||||||
|
sub_elements: Vec<MenuItem<T>>,
|
||||||
|
working_dir: Option<String>,
|
||||||
|
initial_sort_score: f64,
|
||||||
|
data: Option<T>,
|
||||||
|
) -> Self {
|
||||||
|
MenuItem {
|
||||||
|
label,
|
||||||
|
icon_path,
|
||||||
|
action,
|
||||||
|
sub_elements,
|
||||||
|
working_dir,
|
||||||
|
initial_sort_score,
|
||||||
|
data,
|
||||||
|
search_sort_score: 0.0,
|
||||||
|
visible: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone> AsRef<MenuItem<T>> for MenuItem<T> {
|
impl<T: Clone> AsRef<MenuItem<T>> for MenuItem<T> {
|
||||||
|
@ -671,7 +696,7 @@ where
|
||||||
action: None,
|
action: None,
|
||||||
sub_elements: Vec::new(),
|
sub_elements: Vec::new(),
|
||||||
working_dir: None,
|
working_dir: None,
|
||||||
initial_sort_score: 0,
|
initial_sort_score: 0.0,
|
||||||
search_sort_score: 0.0,
|
search_sort_score: 0.0,
|
||||||
data: None,
|
data: None,
|
||||||
visible: true,
|
visible: true,
|
||||||
|
@ -776,9 +801,9 @@ fn create_menu_row<T: Clone + 'static>(
|
||||||
row.set_halign(Align::Fill);
|
row.set_halign(Align::Fill);
|
||||||
row.set_widget_name("row");
|
row.set_widget_name("row");
|
||||||
|
|
||||||
|
let config_clone = config.clone();
|
||||||
let click = GestureClick::new();
|
let click = GestureClick::new();
|
||||||
click.set_button(gdk::BUTTON_PRIMARY);
|
click.set_button(gdk::BUTTON_PRIMARY);
|
||||||
let config_clone = config.clone();
|
|
||||||
click.connect_pressed(move |_gesture, n_press, _x, _y| {
|
click.connect_pressed(move |_gesture, n_press, _x, _y| {
|
||||||
if n_press == 2 {
|
if n_press == 2 {
|
||||||
if let Err(e) = handle_selected_item(
|
if let Err(e) = handle_selected_item(
|
||||||
|
@ -795,7 +820,6 @@ fn create_menu_row<T: Clone + 'static>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
row.add_controller(click);
|
row.add_controller(click);
|
||||||
|
|
||||||
let row_box = gtk4::Box::new(
|
let row_box = gtk4::Box::new(
|
||||||
|
@ -880,8 +904,7 @@ fn set_menu_visibility_for_search<T: Clone>(
|
||||||
{
|
{
|
||||||
if query.is_empty() {
|
if query.is_empty() {
|
||||||
for menu_item in items.iter_mut() {
|
for menu_item in items.iter_mut() {
|
||||||
// todo make initial score and search score both follow same logic.
|
menu_item.search_sort_score = menu_item.initial_sort_score;
|
||||||
menu_item.search_sort_score = menu_item.initial_sort_score as f64;
|
|
||||||
menu_item.visible = true;
|
menu_item.visible = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -936,9 +959,7 @@ fn set_menu_visibility_for_search<T: Clone>(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// todo turn initial score init f64
|
menu_item.search_sort_score = search_sort_score + menu_item.initial_sort_score;
|
||||||
menu_item.search_sort_score =
|
|
||||||
search_sort_score + menu_item.initial_sort_score as f64;
|
|
||||||
menu_item.visible = visible;
|
menu_item.visible = visible;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -979,17 +1000,15 @@ fn percent_or_absolute(value: Option<&String>, base_value: i32) -> Option<i32> {
|
||||||
|
|
||||||
// highly unlikely that we are dealing with > i64 items
|
// highly unlikely that we are dealing with > i64 items
|
||||||
#[allow(clippy::cast_possible_wrap)]
|
#[allow(clippy::cast_possible_wrap)]
|
||||||
pub fn sort_menu_items_alphabetically_honor_initial_score<T: std::clone::Clone>(
|
pub fn sort_menu_items_alphabetically_honor_initial_score<T: Clone>(items: &mut [MenuItem<T>]) {
|
||||||
items: &mut [MenuItem<T>],
|
let special_score = items.len() as f64;
|
||||||
) {
|
let mut regular_score = 0.0;
|
||||||
let special_score = items.len() as i64;
|
|
||||||
let mut regular_score = 0;
|
|
||||||
items.sort_by(|l, r| l.label.cmp(&r.label));
|
items.sort_by(|l, r| l.label.cmp(&r.label));
|
||||||
|
|
||||||
for item in items.iter_mut() {
|
for item in items.iter_mut() {
|
||||||
if item.initial_sort_score == 0 {
|
if item.initial_sort_score == 0.0 {
|
||||||
item.initial_sort_score += regular_score;
|
item.initial_sort_score += regular_score;
|
||||||
regular_score += 1;
|
regular_score += 1.0;
|
||||||
} else {
|
} else {
|
||||||
item.initial_sort_score += special_score;
|
item.initial_sort_score += special_score;
|
||||||
}
|
}
|
||||||
|
|
117
src/lib/mode.rs
117
src/lib/mode.rs
|
@ -108,19 +108,17 @@ impl<T: Clone + std::marker::Send + std::marker::Sync> DRunProvider<T> {
|
||||||
.map(|s| s.content.clone())
|
.map(|s| s.content.clone())
|
||||||
.or(Some(default_icon.clone()));
|
.or(Some(default_icon.clone()));
|
||||||
|
|
||||||
let sort_score = *self.cache.get(&name).unwrap_or(&0);
|
let sort_score = *self.cache.get(&name).unwrap_or(&0) as f64;
|
||||||
|
|
||||||
let mut entry = MenuItem {
|
let mut entry = MenuItem::new(
|
||||||
label: name.clone(),
|
name.clone(),
|
||||||
icon_path: icon.clone(),
|
icon.clone(),
|
||||||
action: action.clone(),
|
action.clone(),
|
||||||
sub_elements: Vec::new(),
|
Vec::new(),
|
||||||
working_dir: working_dir.clone(),
|
working_dir.clone(),
|
||||||
initial_sort_score: sort_score,
|
sort_score,
|
||||||
search_sort_score: 0.0,
|
Some(self.data.clone()),
|
||||||
data: Some(self.data.clone()),
|
);
|
||||||
visible: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
for (_, action) in &file.actions {
|
for (_, action) in &file.actions {
|
||||||
if let Some(action_name) = lookup_name_with_locale(
|
if let Some(action_name) = lookup_name_with_locale(
|
||||||
|
@ -136,17 +134,15 @@ impl<T: Clone + std::marker::Send + std::marker::Sync> DRunProvider<T> {
|
||||||
.unwrap_or("application-x-executable".to_string());
|
.unwrap_or("application-x-executable".to_string());
|
||||||
|
|
||||||
|
|
||||||
entry.sub_elements.push(MenuItem {
|
entry.sub_elements.push(MenuItem::new(
|
||||||
label: action_name,
|
action_name,
|
||||||
icon_path: Some(action_icon),
|
Some(action_icon),
|
||||||
action: action.exec.clone(),
|
action.exec.clone(),
|
||||||
sub_elements: Vec::new(),
|
Vec::new(),
|
||||||
working_dir: working_dir.clone(),
|
working_dir.clone(),
|
||||||
initial_sort_score: 0,
|
0.0,
|
||||||
search_sort_score: 0.0,
|
Some(self.data.clone()),
|
||||||
data: Some(self.data.clone()),
|
));
|
||||||
visible: true,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -279,37 +275,31 @@ impl<T: Clone> ItemProvider<T> for FileItemProvider<T> {
|
||||||
path_str.push('/');
|
path_str.push('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
items.push(MenuItem {
|
items.push(MenuItem::new(
|
||||||
label: path_str.clone(),
|
path_str.clone(),
|
||||||
icon_path: Some(FileItemProvider::<T>::resolve_icon_for_name(
|
Some(FileItemProvider::<T>::resolve_icon_for_name(&entry.path())),
|
||||||
&entry.path(),
|
Some(format!("xdg-open {path_str}")),
|
||||||
)),
|
vec![],
|
||||||
action: Some(format!("xdg-open {path_str}")),
|
None,
|
||||||
sub_elements: vec![],
|
0.0,
|
||||||
working_dir: None,
|
Some(self.menu_item_data.clone()),
|
||||||
initial_sort_score: 0,
|
));
|
||||||
search_sort_score: 0.0,
|
|
||||||
data: Some(self.menu_item_data.clone()),
|
|
||||||
visible: true,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
items.push({
|
items.push({
|
||||||
MenuItem {
|
MenuItem::new(
|
||||||
label: trimmed_search.clone(),
|
trimmed_search.clone(),
|
||||||
icon_path: Some(FileItemProvider::<T>::resolve_icon_for_name(
|
Some(FileItemProvider::<T>::resolve_icon_for_name(
|
||||||
&PathBuf::from(&trimmed_search),
|
&PathBuf::from(&trimmed_search),
|
||||||
)),
|
)),
|
||||||
action: Some(format!("xdg-open {trimmed_search}")),
|
Some(format!("xdg-open {trimmed_search}")),
|
||||||
sub_elements: vec![],
|
vec![],
|
||||||
working_dir: None,
|
None,
|
||||||
initial_sort_score: 0,
|
0.0,
|
||||||
search_sort_score: 0.0,
|
Some(self.menu_item_data.clone()),
|
||||||
data: Some(self.menu_item_data.clone()),
|
)
|
||||||
visible: true,
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,18 +346,15 @@ impl<T: Clone> ItemProvider<T> for MathProvider<T> {
|
||||||
Err(e) => format!("failed to calculate {e:?}"),
|
Err(e) => format!("failed to calculate {e:?}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let item = MenuItem {
|
let item = MenuItem::new(
|
||||||
label: result,
|
result,
|
||||||
icon_path: None,
|
None,
|
||||||
action: search.map(String::from),
|
search.map(String::from),
|
||||||
sub_elements: vec![],
|
vec![],
|
||||||
working_dir: None,
|
None,
|
||||||
initial_sort_score: 0,
|
0.0,
|
||||||
search_sort_score: 0.0,
|
Some(self.menu_item_data.clone()),
|
||||||
data: Some(self.menu_item_data.clone()),
|
);
|
||||||
visible: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
vec![item]
|
vec![item]
|
||||||
} else {
|
} else {
|
||||||
vec![]
|
vec![]
|
||||||
|
@ -394,17 +381,7 @@ impl DMenuProvider {
|
||||||
let items: Vec<MenuItem<String>> = input
|
let items: Vec<MenuItem<String>> = input
|
||||||
.lines()
|
.lines()
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.map(|s| MenuItem {
|
.map(|s| MenuItem::new(s.clone(), None, None, vec![], None, 0.0, None))
|
||||||
label: s,
|
|
||||||
icon_path: None,
|
|
||||||
action: None,
|
|
||||||
sub_elements: vec![],
|
|
||||||
working_dir: None,
|
|
||||||
initial_sort_score: 0,
|
|
||||||
search_sort_score: 0.0,
|
|
||||||
data: None,
|
|
||||||
visible: true,
|
|
||||||
})
|
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(Self { items })
|
Ok(Self { items })
|
||||||
|
|
Loading…
Add table
Reference in a new issue