forge
Explore
Log in

Don't you dare go Hollow

layout: Focus prev column when removing just created column

Browse files at this commit

51df1aba019dde896d32f32d7a8b21b71064ada3Ivan MolodetskikhParent: b3b1e08
This works similar to tabs in Firefox: when you create a tab and then
close it without focusing any other tabs, the focus returns to the
previously-focused tab, rather than the usual next tab. The logic is
that in this case, the new tab was likely transient, and the user wants
to get right back to what they were doing.

In niri's case, this also serves as a workaround for wl-clipboard
spawning and destroying a window, messing up the focus.
src/layout.rsmodified+28 -2
@@ -145,6 +145,16 @@ pub struct Workspace<W: LayoutElement> {
145 145
146 /// Animation of the view offset, if one is currently ongoing.146 /// Animation of the view offset, if one is currently ongoing.
147 view_offset_anim: Option<Animation>,147 view_offset_anim: Option<Animation>,
148+
149+ /// Whether to activate the previous, rather than the next, column upon column removal.
150+ ///
151+ /// When a new column is created and removed with no focus changes in-between, it is more
152+ /// natural to activate the previously-focused column. This variable tracks that.
153+ ///
154+ /// Since we only create-and-activate columns immediately to the right of the active column (in
155+ /// contrast to tabs in Firefox, for example), we can track this as a bool, rather than an
156+ /// index of the previous column to activate.
157+ activate_prev_column_on_removal: bool,
148 }158 }
149 159
150 /// Width of a column.160 /// Width of a column.
@@ -1303,6 +1313,7 @@ impl<W: LayoutElement> Workspace<W> {
1303 active_column_idx: 0,1313 active_column_idx: 0,
1304 view_offset: 0,1314 view_offset: 0,
1305 view_offset_anim: None,1315 view_offset_anim: None,
1316+ activate_prev_column_on_removal: false,
1306 }1317 }
1307 }1318 }
1308 1319
@@ -1315,6 +1326,7 @@ impl<W: LayoutElement> Workspace<W> {
1315 active_column_idx: 0,1326 active_column_idx: 0,
1316 view_offset: 0,1327 view_offset: 0,
1317 view_offset_anim: None,1328 view_offset_anim: None,
1329+ activate_prev_column_on_removal: false,
1318 }1330 }
1319 }1331 }
1320 1332
@@ -1387,6 +1399,9 @@ impl<W: LayoutElement> Workspace<W> {
1387 1399
1388 self.active_column_idx = idx;1400 self.active_column_idx = idx;
1389 1401
1402+ // A different column was activated; reset the flag.
1403+ self.activate_prev_column_on_removal = false;
1404+
1390 let new_x = self.column_x(idx) - PADDING;1405 let new_x = self.column_x(idx) - PADDING;
1391 let new_view_offset = compute_new_view_offset(1406 let new_view_offset = compute_new_view_offset(
1392 current_x,1407 current_x,
@@ -1441,6 +1456,7 @@ impl<W: LayoutElement> Workspace<W> {
1441 1456
1442 if activate {1457 if activate {
1443 self.activate_column(idx);1458 self.activate_column(idx);
1459+ self.activate_prev_column_on_removal = true;
1444 }1460 }
1445 }1461 }
1446 1462
@@ -1459,6 +1475,12 @@ impl<W: LayoutElement> Workspace<W> {
1459 let window_idx = column.windows.iter().position(|win| win == window).unwrap();1475 let window_idx = column.windows.iter().position(|win| win == window).unwrap();
1460 column.windows.remove(window_idx);1476 column.windows.remove(window_idx);
1461 if column.windows.is_empty() {1477 if column.windows.is_empty() {
1478+ if column_idx + 1 == self.active_column_idx {
1479+ // The previous column, that we were going to activate upon removal of the active
1480+ // column, has just been itself removed.
1481+ self.activate_prev_column_on_removal = false;
1482+ }
1483+
1462 // FIXME: activate_column below computes current view position to compute the new view1484 // FIXME: activate_column below computes current view position to compute the new view
1463 // position, which can include the column we're removing here. This leads to unwanted1485 // position, which can include the column we're removing here. This leads to unwanted
1464 // view jumps.1486 // view jumps.
@@ -1467,9 +1489,13 @@ impl<W: LayoutElement> Workspace<W> {
1467 return;1489 return;
1468 }1490 }
1469 1491
1470- if self.active_column_idx > column_idx {1492+ if self.active_column_idx > column_idx
1493+ || (self.active_column_idx == column_idx && self.activate_prev_column_on_removal)
1494+ {
1471 // A column to the left was removed; preserve the current position.1495 // A column to the left was removed; preserve the current position.
1472- self.activate_column(self.active_column_idx - 1);1496+ // FIXME: preserve activate_prev_column_on_removal.
1497+ // Or, the active column was removed, and we needed to activate the previous column.
1498+ self.activate_column(self.active_column_idx.saturating_sub(1));
1473 } else {1499 } else {
1474 self.activate_column(min(self.active_column_idx, self.columns.len() - 1));1500 self.activate_column(min(self.active_column_idx, self.columns.len() - 1));
1475 }1501 }