From acc2c277703db3aa12b76f870e9159c4b4ae4a47 Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Thu, 25 Jul 2024 10:43:51 +0200 Subject: [PATCH] fixed dragging tab disappear if mouse too far --- calcifer.project | 2 +- src/core/ui.rs | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/calcifer.project b/calcifer.project index 3dd015b..96d7541 100644 --- a/calcifer.project +++ b/calcifer.project @@ -1 +1 @@ -{"categories":[{"name":"to do","content":[{"name":"update workflow .yml","description":"make a workflow compiling the calcifer and put the linux in calcifer-{version}\nand the windows in calcifer_windows_{version}\n\nupdate nix\nupdate jiji","id":5}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"move .project file","description":"// Hello there","id":4},{"name":"move config","description":"config from .calcifer/save.json\nto .config/calcifer/state.json","id":1},{"name":"add id to textarea per tab","description":"to improve undo, make each code area of each tab have a unique id (no more undo into another tab)","id":1},{"name":"file tree id ?","description":"// Hello there","id":1},{"name":"open dir in tree ?","description":"// Hello there","id":2},{"name":"fix tab title","description":"// Hello there","id":2},{"name":"when closing last tab","description":"close tab and THEN close calcifer (to save no tab in save.json)","id":1},{"name":"draggable tabs","description":"// Hello there","id":2},{"name":"repair build.rs","description":"// Hello there","id":1},{"name":"export copy paste fix","description":"// Hello there","id":1}]},{"name":"+","content":[]}]} \ No newline at end of file +{"categories":[{"name":"to do","content":[{"name":"update workflow .yml","description":"make a workflow compiling the calcifer and put the linux in calcifer-{version}\nand the windows in calcifer_windows_{version}\n\nupdate nix\nupdate jiji","id":5},{"name":"draggable item for project mode","description":"// Hello there","id":2}]},{"name":"in progress","content":[]},{"name":"done","content":[{"name":"move .project file","description":"// Hello there","id":4},{"name":"move config","description":"config from .calcifer/save.json\nto .config/calcifer/state.json","id":1},{"name":"add id to textarea per tab","description":"to improve undo, make each code area of each tab have a unique id (no more undo into another tab)","id":1},{"name":"file tree id ?","description":"// Hello there","id":1},{"name":"open dir in tree ?","description":"// Hello there","id":2},{"name":"fix tab title","description":"// Hello there","id":2},{"name":"when closing last tab","description":"close tab and THEN close calcifer (to save no tab in save.json)","id":1},{"name":"draggable tabs","description":"// Hello there","id":2},{"name":"repair build.rs","description":"// Hello there","id":1},{"name":"export copy paste fix","description":"// Hello there","id":1}]},{"name":"+","content":[]}]} \ No newline at end of file diff --git a/src/core/ui.rs b/src/core/ui.rs index 027d611..2c7899f 100644 --- a/src/core/ui.rs +++ b/src/core/ui.rs @@ -451,9 +451,9 @@ impl Calcifer { if let Some(pos) = ctx.input(|i| i.pointer.interact_pos()) { match self.mouse_holder { panels::MouseHolder::TabHolder(index) => { - egui::Area::new(egui::Id::new("mouse_holder")) - .fixed_pos(pos) - .show(ctx, |ui| { + let snapped_pos = egui::Pos2::new(pos.x, (self.tab_rect.max.y + self.tab_rect.min.y) / 2.0); + + egui::Area::new(egui::Id::new("mouse_holder")).fixed_pos(snapped_pos).show(ctx, |ui| { let (bg_color, text_color) = if self.selected_tab == index { (core::hex_str_to_color(self.theme.functions), core::hex_str_to_color(self.theme.bg)) } else { @@ -462,7 +462,7 @@ impl Calcifer { let rect = egui::Rect::from_center_size( - egui::Pos2::new(pos.x, (self.tab_rect.max.y + self.tab_rect.min.y) / 2.0), + snapped_pos, egui::Vec2::new((self.tab_rect.max.x - self.tab_rect.min.x) / usize_to_f32(self.tab_area_size()), self.tab_rect.max.y - self.tab_rect.min.y) ); @@ -495,7 +495,8 @@ impl Calcifer { match self.mouse_holder { panels::MouseHolder::TabHolder(initial_index) => { if let Some(pos) = ctx.input(|i| i.pointer.interact_pos()) { - if self.tab_rect.distance_to_pos(pos) == 0.0 { + let snapped_pos = egui::Pos2::new(pos.x, (self.tab_rect.max.y + self.tab_rect.min.y) / 2.0); + if self.tab_rect.distance_to_pos(snapped_pos) == 0.0 { let hover_pos : f32 = (pos.x - self.tab_rect.min.x) / ((self.tab_rect.max.x - self.tab_rect.min.x) / usize_to_f32(self.tab_area_size())); if let Some(final_index) = floor_f32(hover_pos) { @@ -561,19 +562,19 @@ pub fn format_path(path: &Path) -> String { fn usize_to_f32(value: usize) -> f32 { - const MAX_F32: f32 = f32::MAX; + const MAX_F32: f32 = f32::MAX; - if value as f64 > MAX_F32 as f64 { - MAX_F32 - } else { - value as f32 - } + if value as f64 > MAX_F32 as f64 { + MAX_F32 + } else { + value as f32 + } } fn floor_f32(value: f32) -> Option { - if value.is_nan() || value < 0.0 || value > usize::MAX as f32 { - None - } else { - Some(value.floor() as usize) - } + if value.is_nan() || value < 0.0 || value > usize::MAX as f32 { + None + } else { + Some(value.floor() as usize) + } } \ No newline at end of file