Make focus ring configurable
Browse files at this commita62e1cbef6028cb9de945252fbd1b1e9723e4bdeParent: 906ee364 changed files +185 -26
- resources/default-config.kdl +16 -0
- src/config.rs +71 -0
- src/layout.rs +96 -25
- src/niri.rs +2 -1
resources/default-config.kdlmodified+16 -0
| @@ -40,6 +40,22 @@ input { | |||
| 40 | // which may be more convenient to use. | 40 | // which may be more convenient to use. |
| 41 | // spawn-at-startup "alacritty" "-e" "fish" | 41 | // spawn-at-startup "alacritty" "-e" "fish" |
| 42 | 42 | ||
| 43 | +// You can change how the focus ring looks. | ||
| 44 | +// Remember to uncommend the node by removing "/-"! | ||
| 45 | +focus-ring { | ||
| 46 | + // Uncomment this line to disable the focus ring. | ||
| 47 | + // off | ||
| 48 | + | ||
| 49 | + // How many logical pixels the ring extends out from the windows. | ||
| 50 | + width 4 | ||
| 51 | + | ||
| 52 | + // Color of the ring on the active monitor: red, green, blue, alpha. | ||
| 53 | + active-color 0.5 0.8 1.0 1.0 | ||
| 54 | + | ||
| 55 | + // Color of the ring on inactive monitors: red, green, blue, alpha. | ||
| 56 | + inactive-color 0.3 0.3 0.3 1.0 | ||
| 57 | +} | ||
| 58 | + | ||
| 43 | binds { | 59 | binds { |
| 44 | // Keys consist of modifiers separated by + signs, followed by an XKB key name | 60 | // Keys consist of modifiers separated by + signs, followed by an XKB key name |
| 45 | // in the end. To find an XKB name for a particular key, you may use a program | 61 | // in the end. To find an XKB name for a particular key, you may use a program |
src/config.rsmodified+71 -0
| @@ -17,6 +17,8 @@ pub struct Config { | |||
| 17 | #[knuffel(children(name = "spawn-at-startup"))] | 17 | #[knuffel(children(name = "spawn-at-startup"))] |
| 18 | pub spawn_at_startup: Vec<SpawnAtStartup>, | 18 | pub spawn_at_startup: Vec<SpawnAtStartup>, |
| 19 | #[knuffel(child, default)] | 19 | #[knuffel(child, default)] |
| 20 | + pub focus_ring: FocusRing, | ||
| 21 | + #[knuffel(child, default)] | ||
| 20 | pub binds: Binds, | 22 | pub binds: Binds, |
| 21 | #[knuffel(child, default)] | 23 | #[knuffel(child, default)] |
| 22 | pub debug: DebugConfig, | 24 | pub debug: DebugConfig, |
| @@ -90,6 +92,53 @@ pub struct SpawnAtStartup { | |||
| 90 | pub command: Vec<String>, | 92 | pub command: Vec<String>, |
| 91 | } | 93 | } |
| 92 | 94 | ||
| 95 | +#[derive(knuffel::Decode, Debug, Clone, PartialEq)] | ||
| 96 | +pub struct FocusRing { | ||
| 97 | + #[knuffel(child)] | ||
| 98 | + pub off: bool, | ||
| 99 | + #[knuffel(child, unwrap(argument), default = 4)] | ||
| 100 | + pub width: u16, | ||
| 101 | + #[knuffel(child, default = Color::new(0.5, 0.8, 1.0, 1.0))] | ||
| 102 | + pub active_color: Color, | ||
| 103 | + #[knuffel(child, default = Color::new(0.3, 0.3, 0.3, 1.0))] | ||
| 104 | + pub inactive_color: Color, | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +impl Default for FocusRing { | ||
| 108 | + fn default() -> Self { | ||
| 109 | + Self { | ||
| 110 | + off: false, | ||
| 111 | + width: 4, | ||
| 112 | + active_color: Color::new(0.5, 0.8, 1.0, 1.0), | ||
| 113 | + inactive_color: Color::new(0.3, 0.3, 0.3, 1.0), | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | +} | ||
| 117 | + | ||
| 118 | +#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)] | ||
| 119 | +pub struct Color { | ||
| 120 | + #[knuffel(argument)] | ||
| 121 | + pub r: f32, | ||
| 122 | + #[knuffel(argument)] | ||
| 123 | + pub g: f32, | ||
| 124 | + #[knuffel(argument)] | ||
| 125 | + pub b: f32, | ||
| 126 | + #[knuffel(argument)] | ||
| 127 | + pub a: f32, | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +impl Color { | ||
| 131 | + pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self { | ||
| 132 | + Self { r, g, b, a } | ||
| 133 | + } | ||
| 134 | +} | ||
| 135 | + | ||
| 136 | +impl From<Color> for [f32; 4] { | ||
| 137 | + fn from(c: Color) -> Self { | ||
| 138 | + [c.r, c.g, c.b, c.a] | ||
| 139 | + } | ||
| 140 | +} | ||
| 141 | + | ||
| 93 | #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] | 142 | #[derive(knuffel::Decode, Debug, Default, PartialEq, Eq)] |
| 94 | pub struct Binds(#[knuffel(children)] pub Vec<Bind>); | 143 | pub struct Binds(#[knuffel(children)] pub Vec<Bind>); |
| 95 | 144 | ||
| @@ -295,6 +344,12 @@ mod tests { | |||
| 295 | 344 | ||
| 296 | spawn-at-startup "alacritty" "-e" "fish" | 345 | spawn-at-startup "alacritty" "-e" "fish" |
| 297 | 346 | ||
| 347 | + focus-ring { | ||
| 348 | + width 5 | ||
| 349 | + active-color 0.0 0.25 0.5 1.0 | ||
| 350 | + inactive-color 1.0 0.5 0.25 0.0 | ||
| 351 | + } | ||
| 352 | + | ||
| 298 | binds { | 353 | binds { |
| 299 | Mod+T { spawn "alacritty"; } | 354 | Mod+T { spawn "alacritty"; } |
| 300 | Mod+Q { close-window; } | 355 | Mod+Q { close-window; } |
| @@ -332,6 +387,22 @@ mod tests { | |||
| 332 | spawn_at_startup: vec![SpawnAtStartup { | 387 | spawn_at_startup: vec![SpawnAtStartup { |
| 333 | command: vec!["alacritty".to_owned(), "-e".to_owned(), "fish".to_owned()], | 388 | command: vec!["alacritty".to_owned(), "-e".to_owned(), "fish".to_owned()], |
| 334 | }], | 389 | }], |
| 390 | + focus_ring: FocusRing { | ||
| 391 | + off: false, | ||
| 392 | + width: 5, | ||
| 393 | + active_color: Color { | ||
| 394 | + r: 0., | ||
| 395 | + g: 0.25, | ||
| 396 | + b: 0.5, | ||
| 397 | + a: 1., | ||
| 398 | + }, | ||
| 399 | + inactive_color: Color { | ||
| 400 | + r: 1., | ||
| 401 | + g: 0.5, | ||
| 402 | + b: 0.25, | ||
| 403 | + a: 0., | ||
| 404 | + }, | ||
| 405 | + }, | ||
| 335 | binds: Binds(vec![ | 406 | binds: Binds(vec![ |
| 336 | Bind { | 407 | Bind { |
| 337 | key: Key { | 408 | key: Key { |
src/layout.rsmodified+96 -25
| @@ -53,11 +53,9 @@ use smithay::wayland::dmabuf::DmabufFeedback; | |||
| 53 | use smithay::wayland::shell::xdg::SurfaceCachedState; | 53 | use smithay::wayland::shell::xdg::SurfaceCachedState; |
| 54 | 54 | ||
| 55 | use crate::animation::Animation; | 55 | use crate::animation::Animation; |
| 56 | +use crate::config::{Color, Config}; | ||
| 56 | 57 | ||
| 57 | const PADDING: i32 = 16; | 58 | const PADDING: i32 = 16; |
| 58 | -const FOCUS_RING_PADDING: i32 = 4; | ||
| 59 | -const FOCUS_RING_ACTIVE_COLOR: [f32; 4] = [0.5, 0.8, 1.0, 1.0]; | ||
| 60 | -const FOCUS_RING_INACTIVE_COLOR: [f32; 4] = [0.3, 0.3, 0.3, 1.0]; | ||
| 61 | const WIDTH_PROPORTIONS: [ColumnWidth; 3] = [ | 59 | const WIDTH_PROPORTIONS: [ColumnWidth; 3] = [ |
| 62 | ColumnWidth::Proportion(1. / 3.), | 60 | ColumnWidth::Proportion(1. / 3.), |
| 63 | ColumnWidth::Proportion(0.5), | 61 | ColumnWidth::Proportion(0.5), |
| @@ -151,8 +149,8 @@ pub struct Workspace<W: LayoutElement> { | |||
| 151 | /// Index of the currently active column, if any. | 149 | /// Index of the currently active column, if any. |
| 152 | active_column_idx: usize, | 150 | active_column_idx: usize, |
| 153 | 151 | ||
| 154 | - /// The solid color buffer for the focus ring. | 152 | + /// Focus ring buffer and parameters. |
| 155 | - focus_ring: SolidColorBuffer, | 153 | + focus_ring: FocusRing, |
| 156 | 154 | ||
| 157 | /// Offset of the view computed from the active column. | 155 | /// Offset of the view computed from the active column. |
| 158 | view_offset: i32, | 156 | view_offset: i32, |
| @@ -171,6 +169,15 @@ pub struct Workspace<W: LayoutElement> { | |||
| 171 | activate_prev_column_on_removal: bool, | 169 | activate_prev_column_on_removal: bool, |
| 172 | } | 170 | } |
| 173 | 171 | ||
| 172 | +#[derive(Debug)] | ||
| 173 | +struct FocusRing { | ||
| 174 | + buffer: SolidColorBuffer, | ||
| 175 | + is_off: bool, | ||
| 176 | + width: i32, | ||
| 177 | + active_color: Color, | ||
| 178 | + inactive_color: Color, | ||
| 179 | +} | ||
| 180 | + | ||
| 174 | /// Width of a column. | 181 | /// Width of a column. |
| 175 | #[derive(Debug, Clone, Copy)] | 182 | #[derive(Debug, Clone, Copy)] |
| 176 | enum ColumnWidth { | 183 | enum ColumnWidth { |
| @@ -267,6 +274,52 @@ impl LayoutElement for Window { | |||
| 267 | } | 274 | } |
| 268 | } | 275 | } |
| 269 | 276 | ||
| 277 | +impl FocusRing { | ||
| 278 | + fn resize(&mut self, size: Size<i32, Logical>) { | ||
| 279 | + let size = size + Size::from((self.width * 2, self.width * 2)); | ||
| 280 | + self.buffer.resize(size); | ||
| 281 | + } | ||
| 282 | + | ||
| 283 | + fn set_active(&mut self, is_active: bool) { | ||
| 284 | + self.buffer.set_color(if is_active { | ||
| 285 | + self.active_color.into() | ||
| 286 | + } else { | ||
| 287 | + self.inactive_color.into() | ||
| 288 | + }); | ||
| 289 | + } | ||
| 290 | + | ||
| 291 | + fn render( | ||
| 292 | + &self, | ||
| 293 | + loc: Point<i32, Logical>, | ||
| 294 | + scale: Scale<f64>, | ||
| 295 | + ) -> Option<SolidColorRenderElement> { | ||
| 296 | + if self.is_off { | ||
| 297 | + return None; | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + let offset = Point::from((self.width, self.width)); | ||
| 301 | + Some(SolidColorRenderElement::from_buffer( | ||
| 302 | + &self.buffer, | ||
| 303 | + (loc - offset).to_physical_precise_round(scale), | ||
| 304 | + scale, | ||
| 305 | + 1., | ||
| 306 | + Kind::Unspecified, | ||
| 307 | + )) | ||
| 308 | + } | ||
| 309 | +} | ||
| 310 | + | ||
| 311 | +impl Default for FocusRing { | ||
| 312 | + fn default() -> Self { | ||
| 313 | + Self { | ||
| 314 | + buffer: SolidColorBuffer::new((0, 0), [0., 0., 0., 0.]), | ||
| 315 | + is_off: true, | ||
| 316 | + width: 0, | ||
| 317 | + active_color: Color::default(), | ||
| 318 | + inactive_color: Color::default(), | ||
| 319 | + } | ||
| 320 | + } | ||
| 321 | +} | ||
| 322 | + | ||
| 270 | impl ColumnWidth { | 323 | impl ColumnWidth { |
| 271 | fn resolve(self, view_width: i32) -> i32 { | 324 | fn resolve(self, view_width: i32) -> i32 { |
| 272 | match self { | 325 | match self { |
| @@ -873,6 +926,21 @@ impl<W: LayoutElement> MonitorSet<W> { | |||
| 873 | } | 926 | } |
| 874 | } | 927 | } |
| 875 | 928 | ||
| 929 | + pub fn update_config(&mut self, config: &Config) { | ||
| 930 | + match self { | ||
| 931 | + MonitorSet::Normal { monitors, .. } => { | ||
| 932 | + for mon in monitors { | ||
| 933 | + mon.update_config(config); | ||
| 934 | + } | ||
| 935 | + } | ||
| 936 | + MonitorSet::NoOutputs(workspaces) => { | ||
| 937 | + for ws in workspaces { | ||
| 938 | + ws.update_config(config); | ||
| 939 | + } | ||
| 940 | + } | ||
| 941 | + } | ||
| 942 | + } | ||
| 943 | + | ||
| 876 | pub fn toggle_width(&mut self) { | 944 | pub fn toggle_width(&mut self) { |
| 877 | let Some(monitor) = self.active_monitor() else { | 945 | let Some(monitor) = self.active_monitor() else { |
| 878 | return; | 946 | return; |
| @@ -1235,6 +1303,12 @@ impl<W: LayoutElement> Monitor<W> { | |||
| 1235 | } | 1303 | } |
| 1236 | } | 1304 | } |
| 1237 | 1305 | ||
| 1306 | + pub fn update_config(&mut self, config: &Config) { | ||
| 1307 | + for ws in &mut self.workspaces { | ||
| 1308 | + ws.update_config(config); | ||
| 1309 | + } | ||
| 1310 | + } | ||
| 1311 | + | ||
| 1238 | fn toggle_width(&mut self) { | 1312 | fn toggle_width(&mut self) { |
| 1239 | self.active_workspace().toggle_width(); | 1313 | self.active_workspace().toggle_width(); |
| 1240 | } | 1314 | } |
| @@ -1319,7 +1393,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1319 | output: Some(output), | 1393 | output: Some(output), |
| 1320 | columns: vec![], | 1394 | columns: vec![], |
| 1321 | active_column_idx: 0, | 1395 | active_column_idx: 0, |
| 1322 | - focus_ring: SolidColorBuffer::new((0, 0), FOCUS_RING_ACTIVE_COLOR), | 1396 | + focus_ring: FocusRing::default(), |
| 1323 | view_offset: 0, | 1397 | view_offset: 0, |
| 1324 | view_offset_anim: None, | 1398 | view_offset_anim: None, |
| 1325 | activate_prev_column_on_removal: false, | 1399 | activate_prev_column_on_removal: false, |
| @@ -1333,7 +1407,7 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1333 | view_size: Size::from((1280, 720)), | 1407 | view_size: Size::from((1280, 720)), |
| 1334 | columns: vec![], | 1408 | columns: vec![], |
| 1335 | active_column_idx: 0, | 1409 | active_column_idx: 0, |
| 1336 | - focus_ring: SolidColorBuffer::new((0, 0), FOCUS_RING_ACTIVE_COLOR), | 1410 | + focus_ring: FocusRing::default(), |
| 1337 | view_offset: 0, | 1411 | view_offset: 0, |
| 1338 | view_offset_anim: None, | 1412 | view_offset_anim: None, |
| 1339 | activate_prev_column_on_removal: false, | 1413 | activate_prev_column_on_removal: false, |
| @@ -1357,17 +1431,20 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1357 | let col = &self.columns[self.active_column_idx]; | 1431 | let col = &self.columns[self.active_column_idx]; |
| 1358 | let active_win = &col.windows[col.active_window_idx]; | 1432 | let active_win = &col.windows[col.active_window_idx]; |
| 1359 | let geom = active_win.geometry(); | 1433 | let geom = active_win.geometry(); |
| 1360 | - self.focus_ring | 1434 | + self.focus_ring.resize(geom.size); |
| 1361 | - .resize(geom.size + Size::from((FOCUS_RING_PADDING * 2, FOCUS_RING_PADDING * 2))); | 1435 | + self.focus_ring.set_active(is_active); |
| 1362 | - | ||
| 1363 | - self.focus_ring.set_color(if is_active { | ||
| 1364 | - FOCUS_RING_ACTIVE_COLOR | ||
| 1365 | - } else { | ||
| 1366 | - FOCUS_RING_INACTIVE_COLOR | ||
| 1367 | - }); | ||
| 1368 | } | 1436 | } |
| 1369 | } | 1437 | } |
| 1370 | 1438 | ||
| 1439 | + pub fn update_config(&mut self, config: &Config) { | ||
| 1440 | + let c = &config.focus_ring; | ||
| 1441 | + self.focus_ring.is_off = c.off; | ||
| 1442 | + self.focus_ring.width = c.width.into(); | ||
| 1443 | + self.focus_ring.active_color = c.active_color; | ||
| 1444 | + self.focus_ring.inactive_color = c.inactive_color; | ||
| 1445 | + // The focus ring buffer will be updated in a subsequent update_animations call. | ||
| 1446 | + } | ||
| 1447 | + | ||
| 1371 | fn windows(&self) -> impl Iterator<Item = &W> + '_ { | 1448 | fn windows(&self) -> impl Iterator<Item = &W> + '_ { |
| 1372 | self.columns.iter().flat_map(|col| col.windows.iter()) | 1449 | self.columns.iter().flat_map(|col| col.windows.iter()) |
| 1373 | } | 1450 | } |
| @@ -1917,16 +1994,10 @@ impl Workspace<Window> { | |||
| 1917 | )); | 1994 | )); |
| 1918 | 1995 | ||
| 1919 | // Draw the focus ring. | 1996 | // Draw the focus ring. |
| 1920 | - rv.push( | 1997 | + rv.extend( |
| 1921 | - SolidColorRenderElement::from_buffer( | 1998 | + self.focus_ring |
| 1922 | - &self.focus_ring, | 1999 | + .render(win_pos + geom.loc, output_scale) |
| 1923 | - (win_pos + geom.loc - Point::from((FOCUS_RING_PADDING, FOCUS_RING_PADDING))) | 2000 | + .map(Into::into), |
| 1924 | - .to_physical_precise_round(output_scale), | ||
| 1925 | - output_scale, | ||
| 1926 | - 1., | ||
| 1927 | - Kind::Unspecified, | ||
| 1928 | - ) | ||
| 1929 | - .into(), | ||
| 1930 | ); | 2001 | ); |
| 1931 | 2002 | ||
| 1932 | let mut x = PADDING; | 2003 | let mut x = PADDING; |
src/niri.rsmodified+2 -1
| @@ -959,7 +959,8 @@ impl Niri { | |||
| 959 | assert!(state.queued_redraw.take().is_some()); | 959 | assert!(state.queued_redraw.take().is_some()); |
| 960 | assert!(!state.waiting_for_vblank); | 960 | assert!(!state.waiting_for_vblank); |
| 961 | 961 | ||
| 962 | - // Advance the animations. | 962 | + // Update from the config and advance the animations. |
| 963 | + self.monitor_set.update_config(&self.config.borrow()); | ||
| 963 | self.monitor_set.advance_animations(presentation_time); | 964 | self.monitor_set.advance_animations(presentation_time); |
| 964 | 965 | ||
| 965 | // Render the elements. | 966 | // Render the elements. |