forge
Explore
Log in

Don't you dare go Hollow

layout: Refactor view offset, padding, work area handling

Browse files at this commit

624b3296e927b7b71b2b2f92006cc242a9c06205Ivan MolodetskikhParent: 23b59b4
Fixes broken padding on fullscreen windows, especially with struts on
the sides present.

It is now view offset's job to include padding and struts.
src/layout.rsmodified+140 -168
@@ -30,6 +30,7 @@
30 //! making the primary output their original output.30 //! making the primary output their original output.
31 31
32 use std::cmp::{max, min};32 use std::cmp::{max, min};
33+use std::iter::zip;
33 use std::mem;34 use std::mem;
34 use std::rc::Rc;35 use std::rc::Rc;
35 use std::time::Duration;36 use std::time::Duration;
@@ -150,6 +151,10 @@ pub struct Workspace<W: LayoutElement> {
150 focus_ring: FocusRing,151 focus_ring: FocusRing,
151 152
152 /// Offset of the view computed from the active column.153 /// Offset of the view computed from the active column.
154+ ///
155+ /// Any left padding, including left padding from work area left exclusive zone, is handled
156+ /// with this view offset (rather than added as a constant elsewhere in the code). This allows
157+ /// for natural handling of fullscreen windows, which must ignore work area padding.
153 view_offset: i32,158 view_offset: i32,
154 159
155 /// Animation of the view offset, if one is currently ongoing.160 /// Animation of the view offset, if one is currently ongoing.
@@ -1612,12 +1617,12 @@ impl<W: LayoutElement> Workspace<W> {
1612 });1617 });
1613 }1618 }
1614 1619
1615- fn activate_column(&mut self, idx: usize) {1620+ fn compute_new_view_offset_for_column(&self, current_x: i32, idx: usize) -> i32 {
1616- if self.active_column_idx == idx {1621+ if self.columns[idx].is_fullscreen {
1617- return;1622+ return 0;
1618 }1623 }
1619 1624
1620- let current_x = self.view_pos();1625+ let new_col_x = self.column_x(idx);
1621 1626
1622 let final_x = if let Some(anim) = &self.view_offset_anim {1627 let final_x = if let Some(anim) = &self.view_offset_anim {
1623 current_x - self.view_offset + anim.to().round() as i321628 current_x - self.view_offset + anim.to().round() as i32
@@ -1625,26 +1630,58 @@ impl<W: LayoutElement> Workspace<W> {
1625 current_x1630 current_x
1626 };1631 };
1627 1632
1628- self.active_column_idx = idx;1633+ let new_offset = compute_new_view_offset(
1629-1634+ final_x + self.working_area.loc.x,
1630- // A different column was activated; reset the flag.
1631- self.activate_prev_column_on_removal = false;
1632-
1633- let new_x = self.column_x(idx) - PADDING;
1634- let new_view_offset = compute_new_view_offset(
1635- final_x,
1636 self.working_area.size.w,1635 self.working_area.size.w,
1637- new_x,1636+ new_col_x,
1638- self.columns[idx].size().w,1637+ self.columns[idx].width(),
1639 );1638 );
1640 1639
1641- let from_view_offset = current_x - new_x;1640+ // Non-fullscreen windows are always offset at least by the working area position.
1641+ new_offset - self.working_area.loc.x
1642+ }
1643+
1644+ fn animate_view_offset_to_column(&mut self, current_x: i32, idx: usize) {
1645+ let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx);
1646+
1647+ let new_col_x = self.column_x(idx);
1648+ let from_view_offset = current_x - new_col_x;
1649+ self.view_offset = from_view_offset;
1650+
1651+ // If we're already animating towards that, don't restart it.
1652+ if let Some(anim) = &self.view_offset_anim {
1653+ if anim.value().round() as i32 == self.view_offset
1654+ && anim.to().round() as i32 == new_view_offset
1655+ {
1656+ return;
1657+ }
1658+ }
1659+
1660+ // If our view offset is already this, we don't need to do anything.
1661+ if self.view_offset == new_view_offset {
1662+ self.view_offset_anim = None;
1663+ return;
1664+ }
1665+
1642 self.view_offset_anim = Some(Animation::new(1666 self.view_offset_anim = Some(Animation::new(
1643- from_view_offset as f64,1667+ self.view_offset as f64,
1644 new_view_offset as f64,1668 new_view_offset as f64,
1645 Duration::from_millis(250),1669 Duration::from_millis(250),
1646 ));1670 ));
1647- self.view_offset = from_view_offset;1671+ }
1672+
1673+ fn activate_column(&mut self, idx: usize) {
1674+ if self.active_column_idx == idx {
1675+ return;
1676+ }
1677+
1678+ let current_x = self.view_pos();
1679+ self.animate_view_offset_to_column(current_x, idx);
1680+
1681+ self.active_column_idx = idx;
1682+
1683+ // A different column was activated; reset the flag.
1684+ self.activate_prev_column_on_removal = false;
1648 }1685 }
1649 1686
1650 fn has_windows(&self) -> bool {1687 fn has_windows(&self) -> bool {
@@ -1661,10 +1698,10 @@ impl<W: LayoutElement> Workspace<W> {
1661 1698
1662 /// Computes the X position of the windows in the given column, in logical coordinates.1699 /// Computes the X position of the windows in the given column, in logical coordinates.
1663 fn column_x(&self, column_idx: usize) -> i32 {1700 fn column_x(&self, column_idx: usize) -> i32 {
1664- let mut x = PADDING;1701+ let mut x = 0;
1665 1702
1666 for column in self.columns.iter().take(column_idx) {1703 for column in self.columns.iter().take(column_idx) {
1667- x += column.size().w + PADDING;1704+ x += column.width() + PADDING;
1668 }1705 }
1669 1706
1670 x1707 x
@@ -1673,6 +1710,8 @@ impl<W: LayoutElement> Workspace<W> {
1673 fn add_window(&mut self, window: W, activate: bool) {1710 fn add_window(&mut self, window: W, activate: bool) {
1674 self.enter_output_for_window(&window);1711 self.enter_output_for_window(&window);
1675 1712
1713+ let was_empty = self.columns.is_empty();
1714+
1676 let idx = if self.columns.is_empty() {1715 let idx = if self.columns.is_empty() {
1677 01716 0
1678 } else {1717 } else {
@@ -1688,6 +1727,15 @@ impl<W: LayoutElement> Workspace<W> {
1688 self.columns.insert(idx, column);1727 self.columns.insert(idx, column);
1689 1728
1690 if activate {1729 if activate {
1730+ // If this is the first window on an empty workspace, skip the animation from whatever
1731+ // view_offset was left over.
1732+ if was_empty {
1733+ // Try to make the code produce a left-aligned offset, even in presence of left
1734+ // exclusive zones.
1735+ self.view_offset = self.compute_new_view_offset_for_column(self.column_x(0), 0);
1736+ self.view_offset_anim = None;
1737+ }
1738+
1691 self.activate_column(idx);1739 self.activate_column(idx);
1692 self.activate_prev_column_on_removal = true;1740 self.activate_prev_column_on_removal = true;
1693 }1741 }
@@ -1752,36 +1800,7 @@ impl<W: LayoutElement> Workspace<W> {
1752 if idx == self.active_column_idx {1800 if idx == self.active_column_idx {
1753 // We might need to move the view to ensure the resized window is still visible.1801 // We might need to move the view to ensure the resized window is still visible.
1754 let current_x = self.view_pos();1802 let current_x = self.view_pos();
1755-1803+ self.animate_view_offset_to_column(current_x, idx);
1756- let final_x = if let Some(anim) = &self.view_offset_anim {
1757- current_x - self.view_offset + anim.to().round() as i32
1758- } else {
1759- current_x
1760- };
1761-
1762- let new_x = self.column_x(idx) - PADDING;
1763-
1764- let new_view_offset = compute_new_view_offset(
1765- final_x,
1766- self.working_area.size.w,
1767- new_x,
1768- self.columns[idx].size().w,
1769- );
1770-
1771- let cur_view_offset = self
1772- .view_offset_anim
1773- .as_ref()
1774- .map(|a| a.to().round() as i32)
1775- .unwrap_or(self.view_offset);
1776- if cur_view_offset != new_view_offset {
1777- let from_view_offset = current_x - new_x;
1778- self.view_offset_anim = Some(Animation::new(
1779- from_view_offset as f64,
1780- new_view_offset as f64,
1781- Duration::from_millis(250),
1782- ));
1783- self.view_offset = from_view_offset;
1784- }
1785 }1804 }
1786 }1805 }
1787 1806
@@ -1849,14 +1868,8 @@ impl<W: LayoutElement> Workspace<W> {
1849 1868
1850 self.columns.swap(self.active_column_idx, new_idx);1869 self.columns.swap(self.active_column_idx, new_idx);
1851 1870
1852- let new_x = self.column_x(self.active_column_idx) - PADDING;1871+ self.view_offset =
1853- let new_view_offset = compute_new_view_offset(1872+ self.compute_new_view_offset_for_column(current_x, self.active_column_idx);
1854- current_x,
1855- self.working_area.size.w,
1856- new_x,
1857- self.columns[self.active_column_idx].size().w,
1858- );
1859- self.view_offset = new_view_offset;
1860 1873
1861 self.activate_column(new_idx);1874 self.activate_column(new_idx);
1862 }1875 }
@@ -1875,14 +1888,8 @@ impl<W: LayoutElement> Workspace<W> {
1875 1888
1876 self.columns.swap(self.active_column_idx, new_idx);1889 self.columns.swap(self.active_column_idx, new_idx);
1877 1890
1878- let new_x = self.column_x(self.active_column_idx) - PADDING;1891+ self.view_offset =
1879- let new_view_offset = compute_new_view_offset(1892+ self.compute_new_view_offset_for_column(current_x, self.active_column_idx);
1880- current_x,
1881- self.working_area.size.w,
1882- new_x,
1883- self.columns[self.active_column_idx].size().w,
1884- );
1885- self.view_offset = new_view_offset;
1886 1893
1887 self.activate_column(new_idx);1894 self.activate_column(new_idx);
1888 }1895 }
@@ -1939,70 +1946,44 @@ impl<W: LayoutElement> Workspace<W> {
1939 }1946 }
1940 1947
1941 fn view_pos(&self) -> i32 {1948 fn view_pos(&self) -> i32 {
1942- self.column_x(self.active_column_idx) + self.view_offset - PADDING1949+ self.column_x(self.active_column_idx) + self.view_offset
1943 }1950 }
1944 1951
1945- fn window_under(1952+ fn window_under(&self, pos: Point<f64, Logical>) -> Option<(&W, Point<i32, Logical>)> {
1946- &self,
1947- pos_within_output: Point<f64, Logical>,
1948- ) -> Option<(&W, Point<i32, Logical>)> {
1949 if self.columns.is_empty() {1953 if self.columns.is_empty() {
1950 return None;1954 return None;
1951 }1955 }
1952 1956
1953 let view_pos = self.view_pos();1957 let view_pos = self.view_pos();
1954 1958
1955- let mut pos = pos_within_output;
1956- pos.x += view_pos as f64;
1957-
1958 // Prefer the active window since it's drawn on top.1959 // Prefer the active window since it's drawn on top.
1959 let col = &self.columns[self.active_column_idx];1960 let col = &self.columns[self.active_column_idx];
1960 let active_win = &col.windows[col.active_window_idx];1961 let active_win = &col.windows[col.active_window_idx];
1961 let geom = active_win.geometry();1962 let geom = active_win.geometry();
1962- let mut win_pos = Point::from((1963+ let buf_pos = Point::from((
1963- self.column_x(self.active_column_idx),1964+ self.column_x(self.active_column_idx) - view_pos,
1964 col.window_y(col.active_window_idx),1965 col.window_y(col.active_window_idx),
1965 )) - geom.loc;1966 )) - geom.loc;
1966- if col.is_fullscreen {1967+ if active_win.is_in_input_region(&(pos - buf_pos.to_f64())) {
1967- // FIXME: fullscreen windows are missing left padding1968+ return Some((active_win, buf_pos));
1968- win_pos.x -= PADDING;
1969- } else {
1970- win_pos += self.working_area.loc;
1971- }
1972- if active_win.is_in_input_region(&(pos - win_pos.to_f64())) {
1973- let mut win_pos_within_output = win_pos;
1974- win_pos_within_output.x -= view_pos;
1975- return Some((active_win, win_pos_within_output));
1976 }1969 }
1977 1970
1978- let mut x = PADDING;1971+ let mut x = -view_pos;
1979 for col in &self.columns {1972 for col in &self.columns {
1980- let mut y = PADDING;1973+ for (win, y) in zip(&col.windows, col.window_ys()) {
1974+ if win == active_win {
1975+ // Already handled it above.
1976+ continue;
1977+ }
1981 1978
1982- for win in &col.windows {
1983 let geom = win.geometry();1979 let geom = win.geometry();
1984-1980+ let buf_pos = Point::from((x, y)) - geom.loc;
1985- if win != active_win {1981+ if win.is_in_input_region(&(pos - buf_pos.to_f64())) {
1986- // x, y point at the top-left of the window geometry.1982+ return Some((win, buf_pos));
1987- let mut win_pos = Point::from((x, y)) - geom.loc;
1988- if col.is_fullscreen {
1989- // FIXME: fullscreen windows are missing left padding
1990- win_pos.x -= PADDING;
1991- win_pos.y -= PADDING;
1992- } else {
1993- win_pos += self.working_area.loc;
1994- }
1995- if win.is_in_input_region(&(pos - win_pos.to_f64())) {
1996- let mut win_pos_within_output = win_pos;
1997- win_pos_within_output.x -= view_pos;
1998- return Some((win, win_pos_within_output));
1999- }
2000 }1983 }
2001-
2002- y += geom.size.h + PADDING;
2003 }1984 }
2004 1985
2005- x += col.size().w + PADDING;1986+ x += col.width() + PADDING;
2006 }1987 }
2007 1988
2008 None1989 None
@@ -2119,22 +2100,17 @@ impl Workspace<Window> {
2119 // Draw the active window on top.2100 // Draw the active window on top.
2120 let col = &self.columns[self.active_column_idx];2101 let col = &self.columns[self.active_column_idx];
2121 let active_win = &col.windows[col.active_window_idx];2102 let active_win = &col.windows[col.active_window_idx];
2122- let geom = active_win.geometry();2103+ let win_pos = Point::from((
2123- let mut win_pos = Point::from((
2124 self.column_x(self.active_column_idx) - view_pos,2104 self.column_x(self.active_column_idx) - view_pos,
2125 col.window_y(col.active_window_idx),2105 col.window_y(col.active_window_idx),
2126- )) - geom.loc;2106+ ));
2127- if col.is_fullscreen {
2128- // FIXME: fullscreen windows are missing left padding
2129- win_pos.x -= PADDING;
2130- } else {
2131- win_pos += self.working_area.loc;
2132- }
2133 2107
2134 // Draw the window itself.2108 // Draw the window itself.
2109+ let geom = active_win.geometry();
2110+ let buf_pos = win_pos - geom.loc;
2135 rv.extend(active_win.render_elements(2111 rv.extend(active_win.render_elements(
2136 renderer,2112 renderer,
2137- win_pos.to_physical_precise_round(output_scale),2113+ buf_pos.to_physical_precise_round(output_scale),
2138 output_scale,2114 output_scale,
2139 1.,2115 1.,
2140 ));2116 ));
@@ -2142,39 +2118,29 @@ impl Workspace<Window> {
2142 // Draw the focus ring.2118 // Draw the focus ring.
2143 rv.extend(2119 rv.extend(
2144 self.focus_ring2120 self.focus_ring
2145- .render(win_pos + geom.loc, output_scale)2121+ .render(win_pos, output_scale)
2146 .map(Into::into),2122 .map(Into::into),
2147 );2123 );
2148 2124
2149- let mut x = PADDING;2125+ let mut x = -view_pos;
2150 for col in &self.columns {2126 for col in &self.columns {
2151- let mut y = PADDING;2127+ for (win, y) in zip(&col.windows, col.window_ys()) {
2152-2128+ if win == active_win {
2153- for win in &col.windows {2129+ // Already handled it above.
2154- let geom = win.geometry();2130+ continue;
2155-
2156- if win != active_win {
2157- let mut win_pos = Point::from((x - view_pos, y)) - geom.loc;
2158- if col.is_fullscreen {
2159- // FIXME: fullscreen windows are missing left padding
2160- win_pos.x -= PADDING;
2161- win_pos.y -= PADDING;
2162- } else {
2163- win_pos += self.working_area.loc;
2164- }
2165-
2166- rv.extend(win.render_elements(
2167- renderer,
2168- win_pos.to_physical_precise_round(output_scale),
2169- output_scale,
2170- 1.,
2171- ));
2172 }2131 }
2173 2132
2174- y += geom.size.h + PADDING;2133+ let geom = win.geometry();
2134+ let buf_pos = Point::from((x, y)) - geom.loc;
2135+ rv.extend(win.render_elements(
2136+ renderer,
2137+ buf_pos.to_physical_precise_round(output_scale),
2138+ output_scale,
2139+ 1.,
2140+ ));
2175 }2141 }
2176 2142
2177- x += col.size().w + PADDING;2143+ x += col.width() + PADDING;
2178 }2144 }
2179 2145
2180 rv2146 rv
@@ -2292,17 +2258,12 @@ impl<W: LayoutElement> Column<W> {
2292 }2258 }
2293 }2259 }
2294 2260
2295- /// Computes the size of the column including top and bottom padding.2261+ fn width(&self) -> i32 {
2296- fn size(&self) -> Size<i32, Logical> {2262+ self.windows
2297- let mut total = Size::from((0, PADDING));2263+ .iter()
2298-2264+ .map(|win| win.geometry().size.w)
2299- for window in &self.windows {2265+ .max()
2300- let size = window.geometry().size;2266+ .unwrap()
2301- total.w = max(total.w, size.w);
2302- total.h += size.h + PADDING;
2303- }
2304-
2305- total
2306 }2267 }
2307 2268
2308 fn focus_up(&mut self) {2269 fn focus_up(&mut self) {
@@ -2347,7 +2308,7 @@ impl<W: LayoutElement> Column<W> {
2347 let idx = match self.width {2308 let idx = match self.width {
2348 ColumnWidth::Preset(idx) => (idx + 1) % self.options.preset_widths.len(),2309 ColumnWidth::Preset(idx) => (idx + 1) % self.options.preset_widths.len(),
2349 _ => {2310 _ => {
2350- let current = self.size().w;2311+ let current = self.width();
2351 self.options2312 self.options
2352 .preset_widths2313 .preset_widths
2353 .iter()2314 .iter()
@@ -2416,17 +2377,21 @@ impl<W: LayoutElement> Column<W> {
2416 }2377 }
2417 2378
2418 fn window_y(&self, window_idx: usize) -> i32 {2379 fn window_y(&self, window_idx: usize) -> i32 {
2419- if self.is_fullscreen {2380+ self.window_ys().nth(window_idx).unwrap()
2420- return 0;2381+ }
2421- }
2422 2382
2423- let mut y = PADDING;2383+ fn window_ys(&self) -> impl Iterator<Item = i32> + '_ {
2384+ let mut y = 0;
2424 2385
2425- for win in self.windows.iter().take(window_idx) {2386+ if !self.is_fullscreen {
2426- y += win.geometry().size.h + PADDING;2387+ y = self.working_area.loc.y + PADDING;
2427 }2388 }
2428 2389
2429- y2390+ self.windows.iter().map(move |win| {
2391+ let pos = y;
2392+ y += win.geometry().size.h + PADDING;
2393+ pos
2394+ })
2430 }2395 }
2431 }2396 }
2432 2397
@@ -2440,24 +2405,31 @@ pub fn output_size(output: &Output) -> Size<i32, Logical> {
2440 .to_logical(output_scale)2405 .to_logical(output_scale)
2441 }2406 }
2442 2407
2443-fn compute_new_view_offset(cur_x: i32, view_width: i32, new_x: i32, new_col_width: i32) -> i32 {2408+fn compute_new_view_offset(cur_x: i32, view_width: i32, new_col_x: i32, new_col_width: i32) -> i32 {
2444 // If the column is wider than the view, always left-align it.2409 // If the column is wider than the view, always left-align it.
2445- if new_col_width + PADDING * 2 >= view_width {2410+ if view_width <= new_col_width {
2446 return 0;2411 return 0;
2447 }2412 }
2448 2413
2414+ // Compute the padding in case it needs to be smaller due to large window width.
2415+ let padding = ((view_width - new_col_width) / 2).clamp(0, PADDING);
2416+
2417+ // Compute the desired new X with padding.
2418+ let new_x = new_col_x - padding;
2419+ let new_right_x = new_col_x + new_col_width + padding;
2420+
2449 // If the column is already fully visible, leave the view as is.2421 // If the column is already fully visible, leave the view as is.
2450- if new_x >= cur_x && new_x + new_col_width + PADDING * 2 <= cur_x + view_width {2422+ if cur_x <= new_x && new_right_x <= cur_x + view_width {
2451- return -(new_x - cur_x);2423+ return -(new_col_x - cur_x);
2452 }2424 }
2453 2425
2454 // Otherwise, prefer the aligment that results in less motion from the current position.2426 // Otherwise, prefer the aligment that results in less motion from the current position.
2455 let dist_to_left = cur_x.abs_diff(new_x);2427 let dist_to_left = cur_x.abs_diff(new_x);
2456- let dist_to_right = (cur_x + view_width).abs_diff(new_x + new_col_width + PADDING * 2);2428+ let dist_to_right = (cur_x + view_width).abs_diff(new_right_x);
2457 if dist_to_left <= dist_to_right {2429 if dist_to_left <= dist_to_right {
2458- 02430+ -padding
2459 } else {2431 } else {
2460- -(view_width - new_col_width - PADDING * 2)2432+ -(view_width - padding - new_col_width)
2461 }2433 }
2462 }2434 }
2463 2435