updated tracker_id list to not clear all items, and remove/add smartly

This commit is contained in:
Nickiel12 2024-05-30 11:43:52 -07:00
parent f6b9c50e7e
commit a2d303fdfe
2 changed files with 73 additions and 52 deletions

View file

@ -74,7 +74,7 @@ impl LiveViewPanel {
click_handler.connect_pressed(move |gesture, _id, x, y| {
gesture.set_state(gtk::EventSequenceState::Claimed);
LiveViewPanel::gesture_callback(&handler_picture, &tracker_state, x, y)
LiveViewPanel::click_gesture_callback(&handler_picture, &tracker_state, x, y)
});
overlay_box.add_controller(click_handler);
@ -101,24 +101,7 @@ impl LiveViewPanel {
}
}
fn gesture_callback(
overlay: &Picture,
tracker_state: &Arc<Mutex<TrackerState>>,
x_coord: f64,
y_coord: f64,
) {
let x_size = overlay.size(gtk::Orientation::Horizontal);
let y_size = overlay.size(gtk::Orientation::Vertical);
let x_coord = x_coord as f32 / x_size as f32;
let y_coord = y_coord as f32 / y_size as f32;
if let Ok(mut ts) = tracker_state.lock() {
ts.highlighted_id = calc_box_under_mouse(&ts.identity_boxes, x_coord, y_coord);
}
}
fn motion_callback(
fn click_gesture_callback(
overlay: &Picture,
tracker_state: &Arc<Mutex<TrackerState>>,
x_coord: f64,
@ -136,6 +119,22 @@ impl LiveViewPanel {
}
}
fn motion_callback(
overlay: &Picture,
tracker_state: &Arc<Mutex<TrackerState>>,
x_coord: f64,
y_coord: f64,
) {
let x_size = overlay.size(gtk::Orientation::Horizontal);
let y_size = overlay.size(gtk::Orientation::Vertical);
let x_coord = x_coord as f32 / x_size as f32;
let y_coord = y_coord as f32 / y_size as f32;
if let Ok(mut ts) = tracker_state.lock() {
ts.highlighted_id = calc_box_under_mouse(&ts.identity_boxes, x_coord, y_coord);
}
}
pub fn get_top_level(&self) -> &Box {
&self.top_level
}
@ -149,10 +148,14 @@ impl LiveViewPanel {
}
}
fn calc_box_under_mouse(boxes: &Vec<NormalizedBoxCoords>, x_coord: f32, y_coord: f32) -> Option<u32> {
fn calc_box_under_mouse(
boxes: &Vec<NormalizedBoxCoords>,
x_coord: f32,
y_coord: f32,
) -> Option<u32> {
let mut mouse_over: Vec<NormalizedBoxCoords> = vec![];
for nb in boxes.iter() {
for nb in boxes.iter() {
if nb.x1 < x_coord as f32
&& nb.y1 < y_coord as f32
&& nb.x2 > x_coord as f32
@ -161,11 +164,7 @@ fn calc_box_under_mouse(boxes: &Vec<NormalizedBoxCoords>, x_coord: f32, y_coord:
mouse_over.push(nb.clone());
}
}
if mouse_over.len() == 1 {
// the length is one, why would the unwrap fail
let new_id = mouse_over.first().expect("Length one vec had no first");
return Some(new_id.id);
} else if mouse_over.len() > 1 {
if mouse_over.len() > 1 {
let mut x_coords = mouse_over
.iter()
.flat_map(|b| [b.x1, b.x2])
@ -184,29 +183,25 @@ fn calc_box_under_mouse(boxes: &Vec<NormalizedBoxCoords>, x_coord: f32, y_coord:
let overlap_area =
(median_two_x[1] - median_two_x[0]) * (median_two_y[1] - median_two_y[0]);
// We want the one with the largest percentage of it's area as overlap
// to be the highlighted one
mouse_over.sort_by(|a, b| {
// a is valid for cutting in line if the
// overlap area is greater than 75% of a's area
let a_overlap = overlap_area >= 0.75 * a.area();
let b_overlap = overlap_area >= 0.75 * b.area();
match (a_overlap, b_overlap) {
// if the areas are not valid for cutting in line or both are
// do the sorting based on the id, and let the hover affect
// force the user to decide
(true, true) | (false, false) => {
if a.id > b.id {
Ordering::Greater
} else {
Ordering::Less
}
}
(false, true) => Ordering::Less,
(true, false) => Ordering::Greater,
let result = ((a.area() - overlap_area) / a.area()) - ((b.area() - overlap_area) / b.area());
if 0.0001 > result && result > -0.0001 {
Ordering::Equal
} else if result > 0.0 {
Ordering::Greater
} else {
Ordering::Less
}
});
});
}
mouse_over.iter().map(|x| x.id).collect::<Vec<u32>>().first().copied()
}
mouse_over
.iter()
.map(|x| x.id)
.collect::<Vec<u32>>()
.first()
.copied()
}

View file

@ -215,10 +215,32 @@ pub fn build_ui(app: &Application, config: Arc<RwLock<AppConfig>>, runtime: Hand
if let Some(id) = current_id {
id_label.set_text(id.to_string().as_str());
}
if let Some(mut ids) = ids {
let mut to_delete_indexes: Vec<u32> = vec![];
// find all indexes where matching item does not exist in
// the ids, list, and remove all items in the ids list
// that already exists in the stringList
for i in 0..items.n_items() {
let item = items.string(i).unwrap_or("Empty".into()).to_string();
if !ids.contains(&item) {
to_delete_indexes.push(i);
} else {
if let Some(pos) = ids.iter().position(|x| **x == item) {
ids.remove(pos);
}
}
}
if let Some (ids) = ids {
let old_len = items.n_items();
items.splice(0, old_len, &ids.iter().map(|x| x.as_str()).collect::<Vec<&str>>()[0..])
// invert the order so we don't have to adjust after each deletion
to_delete_indexes.sort();
to_delete_indexes.reverse();
to_delete_indexes.iter().for_each(|x| {
items.remove(*x);
});
items.splice(items.n_items(), 0, &ids.iter().map(|x| x.as_str()).collect::<Vec<&str>>()[0..])
}
glib::ControlFlow::Continue
@ -327,6 +349,10 @@ fn draw_boxes(width: i32, height: i32, ctx: &Context, tracker_state: &Arc<Mutex<
} else {
ctx.set_source_rgb(0.0, 0.0, 1.0);
}
if nb.id == active && nb.id == highlighted_id {
ctx.set_source_rgb(1.0, 1.0, 0.0);
}
let b = nb.into_relative(width, height);
ctx.rectangle(