forge
Explore
Log in

Don't you dare go Hollow

Add per-workspace layout config

Browse files at this commit

d5f4e79e4c35d4f5a7a4a64e85e72fb1545ef2dbIvan MolodetskikhParent: d015c7e
Per-workspace background-color doesn't work yet.
niri-config/src/lib.rsmodified+4 -1
@@ -38,7 +38,7 @@ pub use crate::output::{Output, OutputName, Outputs, Position, Vrr};
3838 pub use crate::utils::FloatOrInt;
3939 use crate::utils::MergeWith as _;
4040 pub use crate::window_rule::{FloatingPosition, RelativeTo, WindowRule};
41-pub use crate::workspace::Workspace;
41+pub use crate::workspace::{Workspace, WorkspaceLayoutPart};
4242
4343 #[derive(knuffel::Decode, Debug, PartialEq)]
4444 pub struct Config {
@@ -1795,18 +1795,21 @@ mod tests {
17951795 open_on_output: Some(
17961796 "eDP-1",
17971797 ),
1798+ layout: None,
17981799 },
17991800 Workspace {
18001801 name: WorkspaceName(
18011802 "workspace-2",
18021803 ),
18031804 open_on_output: None,
1805+ layout: None,
18041806 },
18051807 Workspace {
18061808 name: WorkspaceName(
18071809 "workspace-3",
18081810 ),
18091811 open_on_output: None,
1812+ layout: None,
18101813 },
18111814 ],
18121815 }
niri-config/src/workspace.rsmodified+35 -1
@@ -1,16 +1,50 @@
11 use knuffel::errors::DecodeError;
22
3-#[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)]
3+use crate::LayoutPart;
4+
5+#[derive(knuffel::Decode, Debug, Clone, PartialEq)]
46 pub struct Workspace {
57 #[knuffel(argument)]
68 pub name: WorkspaceName,
79 #[knuffel(child, unwrap(argument))]
810 pub open_on_output: Option<String>,
11+ #[knuffel(child)]
12+ pub layout: Option<WorkspaceLayoutPart>,
913 }
1014
1115 #[derive(Debug, Clone, PartialEq, Eq)]
1216 pub struct WorkspaceName(pub String);
1317
18+#[derive(Debug, Clone, PartialEq)]
19+pub struct WorkspaceLayoutPart(pub LayoutPart);
20+
21+impl<S: knuffel::traits::ErrorSpan> knuffel::Decode<S> for WorkspaceLayoutPart {
22+ fn decode_node(
23+ node: &knuffel::ast::SpannedNode<S>,
24+ ctx: &mut knuffel::decode::Context<S>,
25+ ) -> Result<Self, DecodeError<S>> {
26+ for child in node.children() {
27+ let name = &**child.node_name;
28+
29+ // Check for disallowed properties.
30+ //
31+ // - empty-workspace-above-first is a monitor-level concept.
32+ // - insert-hint customization could make sense for workspaces, however currently it is
33+ // also handled at the monitor level (since insert hints in-between workspaces are a
34+ // monitor-level concept), so for now this config option would do nothing.
35+ if matches!(name, "empty-workspace-above-first" | "insert-hint") {
36+ ctx.emit_error(DecodeError::unexpected(
37+ child,
38+ "node",
39+ format!("node `{name}` is not allowed inside `workspace.layout`"),
40+ ));
41+ }
42+ }
43+
44+ LayoutPart::decode_node(node, ctx).map(Self)
45+ }
46+}
47+
1448 impl<S: knuffel::traits::ErrorSpan> knuffel::DecodeScalar<S> for WorkspaceName {
1549 fn type_check(
1650 type_name: &Option<knuffel::span::Spanned<knuffel::ast::TypeName, S>>,
src/layout/mod.rsmodified+63 -19
@@ -380,6 +380,12 @@ struct InteractiveMoveData<W: LayoutElement> {
380380 ///
381381 /// This helps the pointer remain inside the window as it resizes.
382382 pub(self) pointer_ratio_within_window: (f64, f64),
383+ /// Config overrides for the workspace where the window is currently located.
384+ ///
385+ /// To avoid sudden window changes when starting an interactive move, it will remember the
386+ /// config overrides for the workspace where the move originated from. As soon as the window
387+ /// moves over some different workspace though, this override will reset.
388+ pub(self) workspace_config: Option<(WorkspaceId, niri_config::LayoutPart)>,
383389 }
384390
385391 #[derive(Debug)]
@@ -576,6 +582,13 @@ impl Options {
576582 }
577583 }
578584
585+ fn with_merged_layout(mut self, part: Option<&niri_config::LayoutPart>) -> Self {
586+ if let Some(part) = part {
587+ self.layout.merge_with(part);
588+ }
589+ self
590+ }
591+
579592 fn adjusted_for_scale(mut self, scale: f64) -> Self {
580593 let round = |logical: f64| round_logical_in_physical_max1(scale, logical);
581594
@@ -2296,7 +2309,9 @@ impl<W: LayoutElement> Layout<W> {
22962309 move_.tile.verify_invariants();
22972310
22982311 let scale = move_.output.current_scale().fractional_scale();
2299- let options = Options::clone(&self.options).adjusted_for_scale(scale);
2312+ let options = Options::clone(&self.options)
2313+ .with_merged_layout(move_.workspace_config.as_ref().map(|(_, c)| c))
2314+ .adjusted_for_scale(scale);
23002315 assert_eq!(
23012316 &*move_.tile.options, &options,
23022317 "interactive moved tile options must be \
@@ -2830,6 +2845,14 @@ impl<W: LayoutElement> Layout<W> {
28302845 }
28312846
28322847 pub fn update_config(&mut self, config: &Config) {
2848+ // Update workspace-specific config for all named workspaces.
2849+ for ws in self.workspaces_mut() {
2850+ let Some(name) = ws.name() else { continue };
2851+ if let Some(config) = config.workspaces.iter().find(|w| &w.name.0 == name) {
2852+ ws.update_layout_config(config.layout.clone().map(|x| x.0));
2853+ }
2854+ }
2855+
28332856 self.update_options(Options::from_config(config));
28342857 }
28352858
@@ -2839,11 +2862,10 @@ impl<W: LayoutElement> Layout<W> {
28392862 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
28402863 let view_size = output_size(&move_.output);
28412864 let scale = move_.output.current_scale().fractional_scale();
2842- move_.tile.update_config(
2843- view_size,
2844- scale,
2845- Rc::new(Options::clone(&options).adjusted_for_scale(scale)),
2846- );
2865+ let options = Options::clone(&options)
2866+ .with_merged_layout(move_.workspace_config.as_ref().map(|(_, c)| c))
2867+ .adjusted_for_scale(scale);
2868+ move_.tile.update_config(view_size, scale, Rc::new(options));
28472869 }
28482870
28492871 match &mut self.monitor_set {
@@ -3737,15 +3759,17 @@ impl<W: LayoutElement> Layout<W> {
37373759 }
37383760 .band(sq_dist / INTERACTIVE_MOVE_START_THRESHOLD);
37393761
3740- let (is_floating, tile) = self
3762+ let (is_floating, tile, workspace_config) = self
37413763 .workspaces_mut()
37423764 .find(|ws| ws.has_window(&window_id))
37433765 .map(|ws| {
3766+ let workspace_config = ws.layout_config().cloned().map(|c| (ws.id(), c));
37443767 (
37453768 ws.is_floating(&window_id),
37463769 ws.tiles_mut()
37473770 .find(|tile| *tile.window().id() == window_id)
37483771 .unwrap(),
3772+ workspace_config,
37493773 )
37503774 })
37513775 .unwrap();
@@ -3807,11 +3831,10 @@ impl<W: LayoutElement> Layout<W> {
38073831
38083832 let view_size = output_size(&output);
38093833 let scale = output.current_scale().fractional_scale();
3810- tile.update_config(
3811- view_size,
3812- scale,
3813- Rc::new(Options::clone(&self.options).adjusted_for_scale(scale)),
3814- );
3834+ let options = Options::clone(&self.options)
3835+ .with_merged_layout(workspace_config.as_ref().map(|(_, c)| c))
3836+ .adjusted_for_scale(scale);
3837+ tile.update_config(view_size, scale, Rc::new(options));
38153838
38163839 // Unfullscreen.
38173840 let floating_size = tile.floating_window_size;
@@ -3864,6 +3887,7 @@ impl<W: LayoutElement> Layout<W> {
38643887 is_full_width,
38653888 is_floating,
38663889 pointer_ratio_within_window,
3890+ workspace_config,
38673891 };
38683892
38693893 if let Some((tile_pos, zoom)) = tile_pos {
@@ -3880,6 +3904,23 @@ impl<W: LayoutElement> Layout<W> {
38803904 return false;
38813905 }
38823906
3907+ let mut ws_id = None;
3908+ if let Some(mon) = self.monitor_for_output(&output) {
3909+ let (insert_ws, _) = mon.insert_position(move_.pointer_pos_within_output);
3910+ if let InsertWorkspace::Existing(id) = insert_ws {
3911+ ws_id = Some(id);
3912+ }
3913+ }
3914+
3915+ // If moved over a different workspace, reset the config override.
3916+ let mut update_config = false;
3917+ if let Some((id, _)) = &move_.workspace_config {
3918+ if Some(*id) != ws_id {
3919+ move_.workspace_config = None;
3920+ update_config = true;
3921+ }
3922+ }
3923+
38833924 if output != move_.output {
38843925 move_.tile.window().output_leave(&move_.output);
38853926 move_.tile.window().output_enter(&output);
@@ -3887,15 +3928,18 @@ impl<W: LayoutElement> Layout<W> {
38873928 output.current_scale(),
38883929 output.current_transform(),
38893930 );
3890- let view_size = output_size(&output);
3891- let scale = output.current_scale().fractional_scale();
3892- move_.tile.update_config(
3893- view_size,
3894- scale,
3895- Rc::new(Options::clone(&self.options).adjusted_for_scale(scale)),
3896- );
38973931 move_.output = output.clone();
38983932 self.focus_output(&output);
3933+ update_config = true;
3934+ }
3935+
3936+ if update_config {
3937+ let view_size = output_size(&output);
3938+ let scale = output.current_scale().fractional_scale();
3939+ let options = Options::clone(&self.options)
3940+ .with_merged_layout(move_.workspace_config.as_ref().map(|(_, c)| c))
3941+ .adjusted_for_scale(scale);
3942+ move_.tile.update_config(view_size, scale, Rc::new(options));
38993943 }
39003944
39013945 move_.pointer_pos_within_output = pointer_pos_within_output;
src/layout/tests.rsmodified+89 -0
@@ -414,11 +414,19 @@ enum Op {
414414 ws_name: usize,
415415 #[proptest(strategy = "prop::option::of(1..=5usize)")]
416416 output_name: Option<usize>,
417+ #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")]
418+ layout_config: Option<Box<niri_config::LayoutPart>>,
417419 },
418420 UnnameWorkspace {
419421 #[proptest(strategy = "1..=5usize")]
420422 ws_name: usize,
421423 },
424+ UpdateWorkspaceLayoutConfig {
425+ #[proptest(strategy = "1..=5usize")]
426+ ws_name: usize,
427+ #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")]
428+ layout_config: Option<Box<niri_config::LayoutPart>>,
429+ },
422430 AddWindow {
423431 params: TestWindowParams,
424432 },
@@ -817,15 +825,31 @@ impl Op {
817825 Op::AddNamedWorkspace {
818826 ws_name,
819827 output_name,
828+ layout_config,
820829 } => {
821830 layout.ensure_named_workspace(&WorkspaceConfig {
822831 name: WorkspaceName(format!("ws{ws_name}")),
823832 open_on_output: output_name.map(|name| format!("output{name}")),
833+ layout: layout_config.map(|x| niri_config::WorkspaceLayoutPart(*x)),
824834 });
825835 }
826836 Op::UnnameWorkspace { ws_name } => {
827837 layout.unname_workspace(&format!("ws{ws_name}"));
828838 }
839+ Op::UpdateWorkspaceLayoutConfig {
840+ ws_name,
841+ layout_config,
842+ } => {
843+ let ws_name = format!("ws{ws_name}");
844+ let Some(ws) = layout
845+ .workspaces_mut()
846+ .find(|ws| ws.name() == Some(&ws_name))
847+ else {
848+ return;
849+ };
850+
851+ ws.update_layout_config(layout_config.map(|x| *x));
852+ }
829853 Op::SetWorkspaceName {
830854 new_ws_name,
831855 ws_name,
@@ -1607,6 +1631,7 @@ fn operations_dont_panic() {
16071631 Op::AddNamedWorkspace {
16081632 ws_name: 1,
16091633 output_name: Some(1),
1634+ layout_config: None,
16101635 },
16111636 Op::UnnameWorkspace { ws_name: 1 },
16121637 Op::AddWindow {
@@ -1754,6 +1779,7 @@ fn operations_from_starting_state_dont_panic() {
17541779 Op::AddNamedWorkspace {
17551780 ws_name: 1,
17561781 output_name: Some(1),
1782+ layout_config: None,
17571783 },
17581784 Op::UnnameWorkspace { ws_name: 1 },
17591785 Op::AddWindow {
@@ -2289,10 +2315,12 @@ fn removing_all_outputs_preserves_empty_named_workspaces() {
22892315 Op::AddNamedWorkspace {
22902316 ws_name: 1,
22912317 output_name: None,
2318+ layout_config: None,
22922319 },
22932320 Op::AddNamedWorkspace {
22942321 ws_name: 2,
22952322 output_name: None,
2323+ layout_config: None,
22962324 },
22972325 Op::RemoveOutput(1),
22982326 ];
@@ -2655,6 +2683,7 @@ fn named_workspace_to_output() {
26552683 Op::AddNamedWorkspace {
26562684 ws_name: 1,
26572685 output_name: None,
2686+ layout_config: None,
26582687 },
26592688 Op::AddOutput(1),
26602689 Op::MoveWorkspaceToOutput(1),
@@ -2670,6 +2699,7 @@ fn named_workspace_to_output_ewaf() {
26702699 Op::AddNamedWorkspace {
26712700 ws_name: 1,
26722701 output_name: Some(2),
2702+ layout_config: None,
26732703 },
26742704 Op::AddOutput(1),
26752705 Op::AddOutput(2),
@@ -2897,6 +2927,65 @@ fn interactive_move_toggle_floating_ends_dnd_gesture() {
28972927 check_ops(ops);
28982928 }
28992929
2930+#[test]
2931+fn interactive_move_from_workspace_with_layout_config() {
2932+ let ops = [
2933+ Op::AddNamedWorkspace {
2934+ ws_name: 1,
2935+ output_name: Some(2),
2936+ layout_config: Some(Box::new(niri_config::LayoutPart {
2937+ border: Some(niri_config::BorderRule {
2938+ on: true,
2939+ ..Default::default()
2940+ }),
2941+ ..Default::default()
2942+ })),
2943+ },
2944+ Op::AddOutput(1),
2945+ Op::AddWindow {
2946+ params: TestWindowParams::new(2),
2947+ },
2948+ Op::InteractiveMoveBegin {
2949+ window: 2,
2950+ output_idx: 1,
2951+ px: 0.0,
2952+ py: 0.0,
2953+ },
2954+ Op::InteractiveMoveUpdate {
2955+ window: 2,
2956+ dx: 0.0,
2957+ dy: 3586.692842955048,
2958+ output_idx: 1,
2959+ px: 0.0,
2960+ py: 0.0,
2961+ },
2962+ // Now remove and add the output. It will have the same workspace.
2963+ Op::RemoveOutput(1),
2964+ Op::AddOutput(1),
2965+ Op::InteractiveMoveUpdate {
2966+ window: 2,
2967+ dx: 0.0,
2968+ dy: 0.0,
2969+ output_idx: 1,
2970+ px: 0.0,
2971+ py: 0.0,
2972+ },
2973+ // Now move onto a different workspace.
2974+ Op::FocusWorkspaceDown,
2975+ Op::CompleteAnimations,
2976+ Op::InteractiveMoveUpdate {
2977+ window: 2,
2978+ dx: 0.0,
2979+ dy: 0.0,
2980+ output_idx: 1,
2981+ px: 0.0,
2982+ py: 0.0,
2983+ },
2984+ ];
2985+
2986+ check_ops(ops);
2987+}
2988+
29002989 #[test]
29012990 fn set_width_fixed_negative() {
29022991 let ops = [
src/layout/workspace.rsmodified+42 -8
@@ -99,6 +99,9 @@ pub struct Workspace<W: LayoutElement> {
9999 /// Optional name of this workspace.
100100 pub(super) name: Option<String>,
101101
102+ /// Layout config overrides for this workspace.
103+ layout_config: Option<niri_config::LayoutPart>,
104+
102105 /// Unique ID of this workspace.
103106 id: WorkspaceId,
104107 }
@@ -202,7 +205,7 @@ impl<W: LayoutElement> Workspace<W> {
202205
203206 pub fn new_with_config(
204207 output: Output,
205- config: Option<WorkspaceConfig>,
208+ mut config: Option<WorkspaceConfig>,
206209 clock: Clock,
207210 base_options: Rc<Options>,
208211 ) -> Self {
@@ -212,9 +215,14 @@ impl<W: LayoutElement> Workspace<W> {
212215 .map(OutputId)
213216 .unwrap_or(OutputId::new(&output));
214217
218+ let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0));
219+
215220 let scale = output.current_scale();
216- let options =
217- Rc::new(Options::clone(&base_options).adjusted_for_scale(scale.fractional_scale()));
221+ let options = Rc::new(
222+ Options::clone(&base_options)
223+ .with_merged_layout(layout_config.as_ref())
224+ .adjusted_for_scale(scale.fractional_scale()),
225+ );
218226
219227 let view_size = output_size(&output);
220228 let working_area = compute_working_area(&output);
@@ -253,12 +261,13 @@ impl<W: LayoutElement> Workspace<W> {
253261 base_options,
254262 options,
255263 name: config.map(|c| c.name.0),
264+ layout_config,
256265 id: WorkspaceId::next(),
257266 }
258267 }
259268
260269 pub fn new_with_config_no_outputs(
261- config: Option<WorkspaceConfig>,
270+ mut config: Option<WorkspaceConfig>,
262271 clock: Clock,
263272 base_options: Rc<Options>,
264273 ) -> Self {
@@ -269,9 +278,14 @@ impl<W: LayoutElement> Workspace<W> {
269278 .unwrap_or_default(),
270279 );
271280
281+ let layout_config = config.as_mut().and_then(|c| c.layout.take().map(|x| x.0));
282+
272283 let scale = smithay::output::Scale::Integer(1);
273- let options =
274- Rc::new(Options::clone(&base_options).adjusted_for_scale(scale.fractional_scale()));
284+ let options = Rc::new(
285+ Options::clone(&base_options)
286+ .with_merged_layout(layout_config.as_ref())
287+ .adjusted_for_scale(scale.fractional_scale()),
288+ );
275289
276290 let view_size = Size::from((1280., 720.));
277291 let working_area = Rectangle::from_size(Size::from((1280., 720.)));
@@ -310,6 +324,7 @@ impl<W: LayoutElement> Workspace<W> {
310324 base_options,
311325 options,
312326 name: config.map(|c| c.name.0),
327+ layout_config,
313328 id: WorkspaceId::next(),
314329 }
315330 }
@@ -370,7 +385,11 @@ impl<W: LayoutElement> Workspace<W> {
370385
371386 pub fn update_config(&mut self, base_options: Rc<Options>) {
372387 let scale = self.scale.fractional_scale();
373- let options = Rc::new(Options::clone(&base_options).adjusted_for_scale(scale));
388+ let options = Rc::new(
389+ Options::clone(&base_options)
390+ .with_merged_layout(self.layout_config.as_ref())
391+ .adjusted_for_scale(scale),
392+ );
374393
375394 self.scrolling.update_config(
376395 self.view_size,
@@ -394,6 +413,15 @@ impl<W: LayoutElement> Workspace<W> {
394413 self.options = options;
395414 }
396415
416+ pub fn update_layout_config(&mut self, layout_config: Option<niri_config::LayoutPart>) {
417+ if self.layout_config == layout_config {
418+ return;
419+ }
420+
421+ self.layout_config = layout_config;
422+ self.update_config(self.base_options.clone());
423+ }
424+
397425 pub fn update_shaders(&mut self) {
398426 self.scrolling.update_shaders();
399427 self.floating.update_shaders();
@@ -1770,6 +1798,10 @@ impl<W: LayoutElement> Workspace<W> {
17701798 self.working_area
17711799 }
17721800
1801+ pub fn layout_config(&self) -> Option<&niri_config::LayoutPart> {
1802+ self.layout_config.as_ref()
1803+ }
1804+
17731805 #[cfg(test)]
17741806 pub fn scrolling(&self) -> &ScrollingSpace<W> {
17751807 &self.scrolling
@@ -1788,7 +1820,9 @@ impl<W: LayoutElement> Workspace<W> {
17881820 assert!(scale > 0.);
17891821 assert!(scale.is_finite());
17901822
1791- let options = Options::clone(&self.base_options).adjusted_for_scale(scale);
1823+ let options = Options::clone(&self.base_options)
1824+ .with_merged_layout(self.layout_config.as_ref())
1825+ .adjusted_for_scale(scale);
17921826 assert_eq!(
17931827 &*self.options, &options,
17941828 "options must be base options adjusted for scale"