Skip to content

Commit 3c4c512

Browse files
authored
Fix broken egui_router transition and improve swiping (#91)
* Fix broken egui_router transition and improve swiping * Disable by default * Comment * Fix
1 parent 8452ba0 commit 3c4c512

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

crates/egui_router/src/router.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::transition::{ActiveTransition, ActiveTransitionResult};
55
use crate::{
66
CurrentTransition, Request, RouteState, RouterError, RouterResult, TransitionConfig, ID,
77
};
8-
use egui::{scroll_area, Id, Sense, Ui};
8+
use egui::{scroll_area, Id, NumExt, Sense, Ui};
99
use matchit::MatchError;
1010
use std::borrow::Cow;
1111
use std::collections::BTreeMap;
@@ -396,13 +396,16 @@ impl<State: 'static, H: History + Default> EguiRouter<State, H> {
396396

397397
// Start a manual backward transition
398398
if self.current_transition.is_none() {
399-
self.current_transition = Some(CurrentTransition {
400-
active_transition: ActiveTransition::backward_manual(
399+
let mut transition = CurrentTransition {
400+
active_transition: ActiveTransition::manual(
401401
self.backward_transition.clone(),
402402
)
403403
.with_default_duration(self.default_duration),
404404
leaving_route: None,
405-
});
405+
};
406+
// Initialize progress to 1.0 (fully showing current page)
407+
transition.active_transition.set_progress(1.0);
408+
self.current_transition = Some(transition);
406409
}
407410
}
408411
}
@@ -422,7 +425,8 @@ impl<State: 'static, H: History + Default> EguiRouter<State, H> {
422425
// Update the transition progress
423426
if let Some(transition) = &mut self.current_transition {
424427
let screen_width = content_rect.width();
425-
let progress = (new_distance / screen_width).min(1.0);
428+
// let progress = 1.0 - (new_distance / screen_width).at_most(1.0);
429+
let progress = 1.0 - (new_distance / screen_width).at_most(1.0);
426430
transition.active_transition.set_progress(progress);
427431
}
428432
}
@@ -442,15 +446,17 @@ impl<State: 'static, H: History + Default> EguiRouter<State, H> {
442446
|| velocity >= FLICK_VELOCITY_THRESHOLD;
443447

444448
if should_navigate_back {
449+
let popped = self.history.pop();
445450
// Complete the back navigation
446451
if let Some(transition) = &mut self.current_transition {
447-
transition.active_transition.resume_automatic();
452+
let progress = transition.active_transition.progress();
453+
transition.active_transition =
454+
ActiveTransition::backward(self.backward_transition.clone());
455+
transition.active_transition.set_progress(1.0 - progress);
456+
transition.leaving_route = popped;
448457
}
449458
// Actually perform the back navigation
450459
self.history_kind.back().ok();
451-
if self.history.len() > 1 {
452-
self.history.pop();
453-
}
454460
} else {
455461
// Cancel the gesture - animate back to the current page
456462
self.current_transition = None;

crates/egui_router/src/router_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<State: 'static, H: History + Default> RouterBuilder<State, H> {
5252
loading_ui: Arc::new(Box::new(|ui, _| {
5353
ui.spinner();
5454
})),
55-
swipe_back_gesture_enabled: true,
55+
swipe_back_gesture_enabled: false,
5656
swipe_back_edge_width: 40.0,
5757
swipe_back_threshold: 0.4,
5858
}
@@ -241,7 +241,7 @@ impl<State: 'static, H: History + Default> RouterBuilder<State, H> {
241241
self
242242
}
243243

244-
/// Enable or disable the iOS-style swipe-to-go-back gesture (enabled by default)
244+
/// Enable or disable the iOS-style swipe-to-go-back gesture (disabled by default)
245245
pub fn swipe_back_gesture(mut self, enabled: bool) -> Self {
246246
self.swipe_back_gesture_enabled = enabled;
247247
self

crates/egui_router/src/transition.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -193,24 +193,24 @@ impl ActiveTransition {
193193
}
194194
}
195195

196-
pub fn backward_manual(config: TransitionConfig) -> Self {
196+
pub fn manual(config: TransitionConfig) -> Self {
197197
Self {
198198
duration: config.duration,
199199
easing: config.easing,
200200
progress: 0.0,
201201
in_: config.in_,
202202
out: config.out,
203-
backward: true,
203+
backward: false,
204204
manual_control: true,
205205
}
206206
}
207207

208-
pub fn set_progress(&mut self, progress: f32) {
209-
self.progress = progress.clamp(0.0, 1.0);
208+
pub fn progress(&self) -> f32 {
209+
self.progress
210210
}
211211

212-
pub fn resume_automatic(&mut self) {
213-
self.manual_control = false;
212+
pub fn set_progress(&mut self, progress: f32) {
213+
self.progress = progress.clamp(0.0, 1.0);
214214
}
215215

216216
pub fn with_default_duration(mut self, duration: Option<f32>) -> Self {
@@ -242,32 +242,36 @@ impl ActiveTransition {
242242
(self.easing)(t)
243243
};
244244

245+
let eased_t_rev = if self.manual_control {
246+
1.0 - t
247+
} else {
248+
(self.easing)(1.0 - t)
249+
};
250+
245251
if self.backward {
246-
// Render previous page first (underneath, revealed as current slides away)
252+
with_temp_auto_id(ui, in_id, |ui| {
253+
let mut out_ui =
254+
self.out
255+
.create_child_ui(ui, eased_t, Id::new("router_child").with(in_id));
256+
content_in(&mut out_ui, state);
257+
});
258+
247259
if let Some((out_id, content_out)) = content_out {
248260
with_temp_auto_id(ui, out_id, |ui| {
249-
let mut out_ui =
250-
self.out
251-
.create_child_ui(ui, eased_t, Id::new("router_child").with(out_id));
252-
content_out(&mut out_ui, state);
261+
let mut in_ui = self.in_.create_child_ui(
262+
ui,
263+
eased_t_rev,
264+
Id::new("router_child").with(out_id),
265+
);
266+
content_out(&mut in_ui, state);
253267
});
254268
}
255-
256-
// Render current page second (on top, sliding to the right)
257-
with_temp_auto_id(ui, in_id, |ui| {
258-
let mut in_ui = self.in_.create_child_ui(
259-
ui,
260-
1.0 - eased_t,
261-
Id::new("router_child").with(in_id),
262-
);
263-
content_in(&mut in_ui, state);
264-
});
265269
} else {
266270
if let Some((out_id, content_out)) = content_out {
267271
with_temp_auto_id(ui, out_id, |ui| {
268272
let mut out_ui = self.out.create_child_ui(
269273
ui,
270-
1.0 - eased_t,
274+
eased_t_rev,
271275
Id::new("router_child").with(out_id),
272276
);
273277
content_out(&mut out_ui, state);
@@ -282,7 +286,7 @@ impl ActiveTransition {
282286
});
283287
}
284288

285-
if self.progress >= 1.0 {
289+
if self.progress >= 1.0 && !self.manual_control {
286290
ActiveTransitionResult::Done
287291
} else {
288292
ActiveTransitionResult::Continue

0 commit comments

Comments
 (0)