Fix tab navigation to respect user choice and prevent jumping back to localhost

- Add user_navigated_away flag to track manual navigation
- Only auto-switch to localhost if user hasn't manually navigated away
- Reset flag when host disconnects to allow auto-selection
- Preserves user's tab navigation choices while still prioritizing localhost initially
This commit is contained in:
Christoffer Martinsson 2025-10-19 11:21:59 +02:00
parent bf2f066029
commit ca160c9627

View File

@ -57,6 +57,8 @@ pub struct TuiApp {
host_index: usize,
/// Should quit application
should_quit: bool,
/// Track if user manually navigated away from localhost
user_navigated_away: bool,
}
impl TuiApp {
@ -67,6 +69,7 @@ impl TuiApp {
available_hosts: Vec::new(),
host_index: 0,
should_quit: false,
user_navigated_away: false,
}
}
@ -159,22 +162,23 @@ impl TuiApp {
self.available_hosts = sorted_hosts;
// Always prioritize localhost if it becomes available, even if another host is selected
// Prioritize localhost if it becomes available, but respect user navigation
let localhost = gethostname::gethostname().to_string_lossy().to_string();
if !self.available_hosts.is_empty() {
if self.available_hosts.contains(&localhost) {
// Localhost is available - always switch to it
if self.available_hosts.contains(&localhost) && !self.user_navigated_away {
// Localhost is available and user hasn't navigated away - switch to it
self.current_host = Some(localhost);
self.host_index = 0; // localhost is always first in sorted_hosts
} else if self.current_host.is_none() {
// No localhost, no current host - select first available
// No current host - select first available (which is localhost if available)
self.current_host = Some(self.available_hosts[0].clone());
self.host_index = 0;
} else if let Some(ref current) = self.current_host {
if !self.available_hosts.contains(current) {
// Current host disconnected - select first available
// Current host disconnected - select first available and reset navigation flag
self.current_host = Some(self.available_hosts[0].clone());
self.host_index = 0;
self.user_navigated_away = false; // Reset since we're forced to switch
} else if let Some(index) = self.available_hosts.iter().position(|h| h == current) {
// Update index for current host
self.host_index = index;
@ -227,6 +231,17 @@ impl TuiApp {
}
self.current_host = Some(self.available_hosts[self.host_index].clone());
// Check if user navigated away from localhost
let localhost = gethostname::gethostname().to_string_lossy().to_string();
if let Some(ref current) = self.current_host {
if current != &localhost {
self.user_navigated_away = true;
} else {
self.user_navigated_away = false; // User navigated back to localhost
}
}
info!("Switched to host: {}", self.current_host.as_ref().unwrap());
}