Repository: raphamorim/rio (language: rust)

Issue:
CMD+W open new tab but not new window occasionally
### Discussed in https://github.com/raphamorim/rio/discussions/745

<div type='discussions-op-text'>

<sup>Originally posted by **hilaolu** October 30, 2024</sup>
I am using MacOS and rio 0.1.17. Pressing CMD+W sometimes open a new tab on current focus but not a window. The frequency is quite low and the condition to reproduce is elusive. I try many times but only confirm that it happen in 0.1.17 and branch main, without finding any clues. Both CollapsedTab and NativeTab has the problem.  Is it a feature and work as expected?</div>


Discussion / hints:
Thanks.
I try really hard but fail to reproduce. The chance is extremely low.
Appreciate 0.2.0. May I ask if this is fixed in 0.2.0?
Not yet, sorry about that. I ended up cutting the release ealier
Never mind. Looking forward to 0.2.1.
hi, any update on this bug?

Is this still happening (0.2.2)?

I couldn't repro in a while
I am on 0.2.2. It still happens.
I will try 0.2.3 first
Okey please lemme know 🙏 
It still repro. I am on macOS v12.
Ok, I may figured out the cause and give a solution. 

Here are the debug code to tell what happend.
<img width="916" alt="Image" src="https://github.com/user-attachments/assets/f9a97bde-555d-423a-8dfc-5a4532b6fe4d" />

Step to reproduce.

1.Create several windows.
2.Close the oldest window.
3.Create a new window.

The tab_id of new window is simply allocated by the total number of all existing windows, which collisions with the newest one. Thus, the new window with collision tab_id actually becomes a new tab.

<img width="395" alt="Image" src="https://github.com/user-attachments/assets/51d0a92e-6ba5-4215-a44f-4eb1714a285c" />

All windows should be allocated a unique tab_id.



Fix patch:
diff --git a/frontends/rioterm/src/router/mod.rs b/frontends/rioterm/src/router/mod.rs
index 3f2037ddea..0935ce79a2 100644
--- a/frontends/rioterm/src/router/mod.rs
+++ b/frontends/rioterm/src/router/mod.rs
@@ -167,6 +167,7 @@ pub struct Router<'a> {
     pub font_library: Box<rio_backend::sugarloaf::font::FontLibrary>,
     pub config_route: Option<WindowId>,
     pub clipboard: Rc<RefCell<Clipboard>>,
+    next_tab_id: u64,
 }
 
 impl Router<'_> {
@@ -194,6 +195,7 @@ impl Router<'_> {
             config_route: None,
             font_library: Box::new(font_library),
             clipboard,
+            next_tab_id: 0,
         }
     }
 
@@ -310,7 +312,9 @@ impl Router<'_> {
         open_url: Option<String>,
     ) {
         let tab_id = if config.navigation.is_native() {
-            Some(self.routes.len().to_string())
+            let id = self.next_tab_id;
+            self.next_tab_id = self.next_tab_id.wrapping_add(1);
+            Some(id.to_string())
         } else {
             None
         };


Test patch: