forge
Explore
Log in

Don't you dare go Hollow

Change horizontal gesture to focus furthest window

Browse files at this commit

5f416abcf9053cf61323921b6218d72a77e2dcebIvan MolodetskikhParent: 66c1272
src/input.rsmodified+4 -1
@@ -1269,7 +1269,10 @@ impl State {
1269 handled = true;1269 handled = true;
1270 }1270 }
1271 1271
1272- let res = self.niri.layout.view_offset_gesture_update(delta_x);1272+ let res = self
1273+ .niri
1274+ .layout
1275+ .view_offset_gesture_update(delta_x, timestamp);
1273 if let Some(output) = res {1276 if let Some(output) = res {
1274 if let Some(output) = output {1277 if let Some(output) = output {
1275 self.niri.queue_redraw(output);1278 self.niri.queue_redraw(output);
src/layout/mod.rsmodified+9 -4
@@ -1660,7 +1660,11 @@ impl<W: LayoutElement> Layout<W> {
1660 }1660 }
1661 }1661 }
1662 1662
1663- pub fn view_offset_gesture_update(&mut self, delta_x: f64) -> Option<Option<Output>> {1663+ pub fn view_offset_gesture_update(
1664+ &mut self,
1665+ delta_x: f64,
1666+ timestamp: Duration,
1667+ ) -> Option<Option<Output>> {
1664 let monitors = match &mut self.monitor_set {1668 let monitors = match &mut self.monitor_set {
1665 MonitorSet::Normal { monitors, .. } => monitors,1669 MonitorSet::Normal { monitors, .. } => monitors,
1666 MonitorSet::NoOutputs { .. } => return None,1670 MonitorSet::NoOutputs { .. } => return None,
@@ -1668,7 +1672,7 @@ impl<W: LayoutElement> Layout<W> {
1668 1672
1669 for monitor in monitors {1673 for monitor in monitors {
1670 for ws in &mut monitor.workspaces {1674 for ws in &mut monitor.workspaces {
1671- if let Some(refresh) = ws.view_offset_gesture_update(delta_x) {1675+ if let Some(refresh) = ws.view_offset_gesture_update(delta_x, timestamp) {
1672 if refresh {1676 if refresh {
1673 return Some(Some(monitor.output.clone()));1677 return Some(Some(monitor.output.clone()));
1674 } else {1678 } else {
@@ -2036,6 +2040,7 @@ mod tests {
2036 ViewOffsetGestureUpdate {2040 ViewOffsetGestureUpdate {
2037 #[proptest(strategy = "arbitrary_view_offset_gesture_delta()")]2041 #[proptest(strategy = "arbitrary_view_offset_gesture_delta()")]
2038 delta: f64,2042 delta: f64,
2043+ timestamp: Duration,
2039 },2044 },
2040 ViewOffsetGestureEnd,2045 ViewOffsetGestureEnd,
2041 WorkspaceSwitchGestureBegin {2046 WorkspaceSwitchGestureBegin {
@@ -2293,8 +2298,8 @@ mod tests {
2293 2298
2294 layout.view_offset_gesture_begin(&output);2299 layout.view_offset_gesture_begin(&output);
2295 }2300 }
2296- Op::ViewOffsetGestureUpdate { delta } => {2301+ Op::ViewOffsetGestureUpdate { delta, timestamp } => {
2297- layout.view_offset_gesture_update(delta);2302+ layout.view_offset_gesture_update(delta, timestamp);
2298 }2303 }
2299 Op::ViewOffsetGestureEnd => {2304 Op::ViewOffsetGestureEnd => {
2300 // We don't handle cancels in this gesture.2305 // We don't handle cancels in this gesture.
src/layout/workspace.rsmodified+42 -70
@@ -17,6 +17,7 @@ use super::{LayoutElement, Options};
17 use crate::animation::Animation;17 use crate::animation::Animation;
18 use crate::niri_render_elements;18 use crate::niri_render_elements;
19 use crate::render_helpers::renderer::NiriRenderer;19 use crate::render_helpers::renderer::NiriRenderer;
20+use crate::swipe_tracker::SwipeTracker;
20 use crate::utils::output_size;21 use crate::utils::output_size;
21 22
22 #[derive(Debug)]23 #[derive(Debug)]
@@ -90,6 +91,8 @@ enum ViewOffsetAdjustment {
90 #[derive(Debug)]91 #[derive(Debug)]
91 struct ViewGesture {92 struct ViewGesture {
92 current_view_offset: f64,93 current_view_offset: f64,
94+ tracker: SwipeTracker,
95+ delta_from_tracker: f64,
93 }96 }
94 97
95 /// Width of a column.98 /// Width of a column.
@@ -1230,39 +1233,51 @@ impl<W: LayoutElement> Workspace<W> {
1230 1233
1231 let gesture = ViewGesture {1234 let gesture = ViewGesture {
1232 current_view_offset: self.view_offset as f64,1235 current_view_offset: self.view_offset as f64,
1236+ tracker: SwipeTracker::new(),
1237+ delta_from_tracker: self.view_offset as f64,
1233 };1238 };
1234 self.view_offset_adj = Some(ViewOffsetAdjustment::Gesture(gesture));1239 self.view_offset_adj = Some(ViewOffsetAdjustment::Gesture(gesture));
1235 }1240 }
1236 1241
1237- pub fn view_offset_gesture_update(&mut self, delta_x: f64) -> Option<bool> {1242+ pub fn view_offset_gesture_update(
1243+ &mut self,
1244+ delta_x: f64,
1245+ timestamp: Duration,
1246+ ) -> Option<bool> {
1238 let Some(ViewOffsetAdjustment::Gesture(gesture)) = &mut self.view_offset_adj else {1247 let Some(ViewOffsetAdjustment::Gesture(gesture)) = &mut self.view_offset_adj else {
1239 return None;1248 return None;
1240 };1249 };
1241 1250
1242- let mut new_offset = gesture.current_view_offset + delta_x;1251+ gesture.tracker.push(delta_x, timestamp);
1243- gesture.current_view_offset = new_offset;1252+
1253+ let view_offset = gesture.tracker.pos() + gesture.delta_from_tracker;
1254+ gesture.current_view_offset = view_offset;
1244 1255
1245 if self.columns.is_empty() {1256 if self.columns.is_empty() {
1246 return Some(true);1257 return Some(true);
1247 }1258 }
1248 1259
1249- // Switch the next window to be active, if necessary.1260+ // Check if moving too slow to switch focus.
1250- //1261+ if delta_x.abs() < 1. {
1251- // The logic here is similar to PaperWM. The idea is: make the next window (in the1262+ return Some(true);
1252- // direction of the gesture) active when it becomes "more visible" than the current active1263+ }
1253- // window.1264+
1254-1265+ // Switch focus to the furthest visible window towards the gesture direction.
1255- // Make an iterator over column indices into the gesture direction.1266+
1256- let mut idxs_before = (0..=self.active_column_idx).rev();1267+ // Make an iterator over column indices towards the gesture direction.
1257- let mut idxs_after = self.active_column_idx..self.columns.len();1268+ let mut idxs_forward = 0..self.columns.len();
1258- let next_column_idxs = if delta_x < 0. {1269+ let mut idxs_back = idxs_forward.clone().rev();
1259- &mut idxs_before as &mut dyn Iterator<Item = usize>1270+ let idxs = if delta_x < 0. {
1271+ &mut idxs_back as &mut dyn Iterator<Item = usize>
1260 } else {1272 } else {
1261- &mut idxs_after as &mut dyn Iterator<Item = usize>1273+ &mut idxs_forward as &mut dyn Iterator<Item = usize>
1262 };1274 };
1263 1275
1264- let mut last = None;1276+ let active_col_x = self.column_x(self.active_column_idx);
1265- for col_idx in next_column_idxs {1277+ let mut new_col_idx = self.active_column_idx;
1278+ let mut new_col_x = active_col_x;
1279+
1280+ for col_idx in idxs {
1266 let col = &self.columns[col_idx];1281 let col = &self.columns[col_idx];
1267 let col_x = self.column_x(col_idx);1282 let col_x = self.column_x(col_idx);
1268 let col_w = col.width();1283 let col_w = col.width();
@@ -1273,24 +1288,7 @@ impl<W: LayoutElement> Workspace<W> {
1273 self.working_area1288 self.working_area
1274 };1289 };
1275 1290
1276- if let Some((last_col_x, _)) = last {1291+ area_for_col.loc.x += active_col_x + view_offset.round() as i32;
1277- area_for_col.loc.x += last_col_x + new_offset.round() as i32;
1278- } else {
1279- // First iteration of the loop; col_idx == self.active_column_idx.
1280- area_for_col.loc.x += col_x + new_offset.round() as i32;
1281- }
1282-
1283- // Check if the column is fully visible.
1284- if area_for_col.loc.x <= col_x
1285- && col_x + col_w <= area_for_col.loc.x + area_for_col.size.w
1286- {
1287- // Make it the new active column.
1288- if let Some((last_col_x, _)) = last {
1289- new_offset += (last_col_x - col_x) as f64;
1290- self.active_column_idx = col_idx;
1291- }
1292- break;
1293- }
1294 1292
1295 // Check if the column is already past the working area.1293 // Check if the column is already past the working area.
1296 if (delta_x >= 0. && area_for_col.loc.x + area_for_col.size.w <= col_x)1294 if (delta_x >= 0. && area_for_col.loc.x + area_for_col.size.w <= col_x)
@@ -1299,46 +1297,19 @@ impl<W: LayoutElement> Workspace<W> {
1299 break;1297 break;
1300 }1298 }
1301 1299
1302- // Compute the visible width (inside the working area).1300+ new_col_idx = col_idx;
1303- let visible_width = col_w1301+ new_col_x = col_x;
1304- - max(0, area_for_col.loc.x - col_x)
1305- - max(
1306- 0,
1307- (col_x + col_w) - (area_for_col.loc.x + area_for_col.size.w),
1308- );
1309- let visible_ratio = if col_w == 0 {
1310- 1.
1311- } else {
1312- visible_width as f64 / col_w as f64
1313- };
1314-
1315- if let Some((last_col_x, last_ratio)) = last {
1316- // Check if we reached the first visible window.
1317- if area_for_col.loc.x < col_x + col_w
1318- && col_x < area_for_col.loc.x + area_for_col.size.w
1319- {
1320- // If it's more visible than the last one, make it active.
1321- if visible_ratio >= last_ratio {
1322- new_offset += (last_col_x - col_x) as f64;
1323- self.active_column_idx = col_idx;
1324- }
1325-
1326- break;
1327- }
1328-
1329- // Still working through invisible windows.
1330- new_offset += (last_col_x - col_x) as f64;
1331- self.active_column_idx = col_idx;
1332- }
1333-
1334- last = Some((col_x, visible_ratio));
1335 }1302 }
1336 1303
1337 let Some(ViewOffsetAdjustment::Gesture(gesture)) = &mut self.view_offset_adj else {1304 let Some(ViewOffsetAdjustment::Gesture(gesture)) = &mut self.view_offset_adj else {
1338 unreachable!();1305 unreachable!();
1339 };1306 };
1340 1307
1341- gesture.current_view_offset = new_offset;1308+ let delta = (active_col_x - new_col_x) as f64;
1309+ gesture.delta_from_tracker += delta;
1310+ gesture.current_view_offset += delta;
1311+ self.active_column_idx = new_col_idx;
1312+
1342 Some(true)1313 Some(true)
1343 }1314 }
1344 1315
@@ -1354,7 +1325,8 @@ impl<W: LayoutElement> Workspace<W> {
1354 1325
1355 // FIXME: keep track of gesture velocity and use it to compute the final point and1326 // FIXME: keep track of gesture velocity and use it to compute the final point and
1356 // to animate to it.1327 // to animate to it.
1357- let offset = gesture.current_view_offset.round() as i32;1328+ let offset = gesture.tracker.pos() + gesture.delta_from_tracker;
1329+ let offset = offset.round() as i32;
1358 1330
1359 self.view_offset = offset;1331 self.view_offset = offset;
1360 self.view_offset_adj = None;1332 self.view_offset_adj = None;