forge
Explore
Log in

Don't you dare go Hollow

layout: Refactor window opening targets

Browse files at this commit

daaee43be387d75c62c7ba63d33b0b8f9ae192c8Ivan MolodetskikhParent: 0d71cb9
niri-visual-tests/src/cases/layout.rsmodified+10 -3
@@ -3,7 +3,7 @@ use std::time::Duration;
3 3
4 use niri::animation::Clock;4 use niri::animation::Clock;
5 use niri::layout::scrolling::ColumnWidth;5 use niri::layout::scrolling::ColumnWidth;
6-use niri::layout::{ActivateWindow, LayoutElement as _, Options};6+use niri::layout::{ActivateWindow, AddWindowTarget, LayoutElement as _, Options};
7 use niri::render_helpers::RenderTarget;7 use niri::render_helpers::RenderTarget;
8 use niri_config::{Color, FloatOrInt, OutputName};8 use niri_config::{Color, FloatOrInt, OutputName};
9 use smithay::backend::renderer::element::RenderElement;9 use smithay::backend::renderer::element::RenderElement;
@@ -170,6 +170,7 @@ impl Layout {
170 170
171 self.layout.add_window(171 self.layout.add_window(
172 window.clone(),172 window.clone(),
173+ AddWindowTarget::Auto,
173 width,174 width,
174 false,175 false,
175 false,176 false,
@@ -194,8 +195,14 @@ impl Layout {
194 );195 );
195 window.communicate();196 window.communicate();
196 197
197- self.layout198+ self.layout.add_window(
198- .add_window_right_of(right_of.id(), window.clone(), width, false, false);199+ window.clone(),
200+ AddWindowTarget::NextTo(right_of.id()),
201+ width,
202+ false,
203+ false,
204+ ActivateWindow::default(),
205+ );
199 self.windows.push(window);206 self.windows.push(window);
200 }207 }
201 208
src/handlers/compositor.rsmodified+22 -42
@@ -19,7 +19,7 @@ use smithay::{delegate_compositor, delegate_shm};
19 19
20 use super::xdg_shell::add_mapped_toplevel_pre_commit_hook;20 use super::xdg_shell::add_mapped_toplevel_pre_commit_hook;
21 use crate::handlers::XDG_ACTIVATION_TOKEN_TIMEOUT;21 use crate::handlers::XDG_ACTIVATION_TOKEN_TIMEOUT;
22-use crate::layout::ActivateWindow;22+use crate::layout::{ActivateWindow, AddWindowTarget};
23 use crate::niri::{ClientState, State};23 use crate::niri::{ClientState, State};
24 use crate::utils::send_scale_transform;24 use crate::utils::send_scale_transform;
25 use crate::utils::transaction::Transaction;25 use crate::utils::transaction::Transaction;
@@ -96,7 +96,7 @@ impl CompositorHandler for State {
96 96
97 let toplevel = window.toplevel().expect("no X11 support");97 let toplevel = window.toplevel().expect("no X11 support");
98 98
99- let (rules, width, is_full_width, output, workspace_name) =99+ let (rules, width, is_full_width, output, workspace_id) =
100 if let InitialConfigureState::Configured {100 if let InitialConfigureState::Configured {
101 rules,101 rules,
102 width,102 width,
@@ -110,10 +110,12 @@ impl CompositorHandler for State {
110 output.filter(|o| self.niri.layout.monitor_for_output(o).is_some());110 output.filter(|o| self.niri.layout.monitor_for_output(o).is_some());
111 111
112 // Check that the workspace still exists.112 // Check that the workspace still exists.
113- let workspace_name = workspace_name113+ let workspace_id = workspace_name
114- .filter(|n| self.niri.layout.find_workspace_by_name(n).is_some());114+ .as_deref()
115+ .and_then(|n| self.niri.layout.find_workspace_by_name(n))
116+ .map(|(_, ws)| ws.id());
115 117
116- (rules, width, is_full_width, output, workspace_name)118+ (rules, width, is_full_width, output, workspace_id)
117 } else {119 } else {
118 error!("window map must happen after initial configure");120 error!("window map must happen after initial configure");
119 (ResolvedWindowRules::empty(), None, false, None, None)121 (ResolvedWindowRules::empty(), None, false, None, None)
@@ -160,46 +162,24 @@ impl CompositorHandler for State {
160 }162 }
161 };163 };
162 164
163- let output = if let Some(p) = parent {165+ let target = if let Some(p) = &parent {
164- // Open dialogs immediately to the right of their parent window.166+ // Open dialogs next to their parent window.
165- //167+ AddWindowTarget::NextTo(p)
166- // FIXME: do we want to use activate here? How do we want things to behave168+ } else if let Some(id) = workspace_id {
167- // exactly?169+ AddWindowTarget::Workspace(id)
168- self.niri.layout.add_window_right_of(
169- &p,
170- mapped,
171- width,
172- is_full_width,
173- is_floating,
174- )
175- } else if let Some(workspace_name) = &workspace_name {
176- self.niri.layout.add_window_to_named_workspace(
177- workspace_name,
178- mapped,
179- width,
180- is_full_width,
181- is_floating,
182- activate,
183- )
184 } else if let Some(output) = &output {170 } else if let Some(output) = &output {
185- self.niri.layout.add_window_on_output(171+ AddWindowTarget::Output(output)
186- output,
187- mapped,
188- width,
189- is_full_width,
190- is_floating,
191- activate,
192- );
193- Some(output)
194 } else {172 } else {
195- self.niri.layout.add_window(173+ AddWindowTarget::Auto
196- mapped,
197- width,
198- is_full_width,
199- is_floating,
200- activate,
201- )
202 };174 };
175+ let output = self.niri.layout.add_window(
176+ mapped,
177+ target,
178+ width,
179+ is_full_width,
180+ is_floating,
181+ activate,
182+ );
203 183
204 if let Some(output) = output.cloned() {184 if let Some(output) = output.cloned() {
205 self.niri.layout.start_open_animation_for_window(&window);185 self.niri.layout.start_open_animation_for_window(&window);
src/handlers/xdg_shell.rsmodified+1 -1
@@ -859,7 +859,7 @@ impl State {
859 });859 });
860 }860 }
861 861
862- width = ws.resolve_default_width(rules.default_width);862+ width = ws.resolve_default_width(rules.default_width, is_floating);
863 863
864 let configure_width = if is_full_width {864 let configure_width = if is_full_width {
865 Some(ColumnWidth::Proportion(1.))865 Some(ColumnWidth::Proportion(1.))
src/layout/floating.rsmodified+1 -4
@@ -428,10 +428,7 @@ impl<W: LayoutElement> FloatingSpace<W> {
428 self.bring_up_descendants_of(idx);428 self.bring_up_descendants_of(idx);
429 }429 }
430 430
431- pub fn add_tile_above(&mut self, above: &W::Id, mut tile: Tile<W>) {431+ pub fn add_tile_above(&mut self, above: &W::Id, mut tile: Tile<W>, activate: bool) {
432- // Activate the new window if above was active.
433- let activate = Some(above) == self.active_window_id.as_ref();
434-
435 let idx = self.idx_of(above).unwrap();432 let idx = self.idx_of(above).unwrap();
436 433
437 let above_pos = self.data[idx].logical_pos;434 let above_pos = self.data[idx].logical_pos;
src/layout/mod.rsmodified+229 -239
@@ -35,6 +35,7 @@ use std::mem;
35 use std::rc::Rc;35 use std::rc::Rc;
36 use std::time::Duration;36 use std::time::Duration;
37 37
38+use monitor::MonitorAddWindowTarget;
38 use niri_config::{39 use niri_config::{
39 CenterFocusedColumn, Config, CornerRadius, FloatOrInt, PresetSize, Struts,40 CenterFocusedColumn, Config, CornerRadius, FloatOrInt, PresetSize, Struts,
40 Workspace as WorkspaceConfig,41 Workspace as WorkspaceConfig,
@@ -48,7 +49,7 @@ use smithay::output::{self, Output};
48 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;49 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
49 use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size, Transform};50 use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size, Transform};
50 use tile::{Tile, TileRenderElement};51 use tile::{Tile, TileRenderElement};
51-use workspace::WorkspaceId;52+use workspace::{WorkspaceAddWindowTarget, WorkspaceId};
52 53
53 pub use self::monitor::MonitorRenderElement;54 pub use self::monitor::MonitorRenderElement;
54 use self::monitor::{Monitor, WorkspaceSwitch};55 use self::monitor::{Monitor, WorkspaceSwitch};
@@ -424,6 +425,20 @@ pub enum ActivateWindow {
424 No,425 No,
425 }426 }
426 427
428+/// Where to put a newly added window.
429+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
430+pub enum AddWindowTarget<'a, W: LayoutElement> {
431+ /// No particular preference.
432+ #[default]
433+ Auto,
434+ /// On this output.
435+ Output(&'a Output),
436+ /// On this workspace.
437+ Workspace(WorkspaceId),
438+ /// Next to this existing window.
439+ NextTo(&'a W::Id),
440+}
441+
427 impl<W: LayoutElement> InteractiveMoveState<W> {442 impl<W: LayoutElement> InteractiveMoveState<W> {
428 fn moving(&self) -> Option<&InteractiveMoveData<W>> {443 fn moving(&self) -> Option<&InteractiveMoveData<W>> {
429 match self {444 match self {
@@ -797,71 +812,6 @@ impl<W: LayoutElement> Layout<W> {
797 }812 }
798 }813 }
799 814
800- /// Adds a new window to the layout on a specific workspace.
801- pub fn add_window_to_named_workspace(
802- &mut self,
803- workspace_name: &str,
804- window: W,
805- width: Option<ColumnWidth>,
806- is_full_width: bool,
807- is_floating: bool,
808- activate: ActivateWindow,
809- ) -> Option<&Output> {
810- let width = self.resolve_default_width(&window, width);
811-
812- match &mut self.monitor_set {
813- MonitorSet::Normal {
814- monitors,
815- active_monitor_idx,
816- ..
817- } => {
818- let (mon_idx, mon, ws_idx) = monitors
819- .iter_mut()
820- .enumerate()
821- .find_map(|(mon_idx, mon)| {
822- mon.find_named_workspace_index(workspace_name)
823- .map(move |ws_idx| (mon_idx, mon, ws_idx))
824- })
825- .unwrap();
826-
827- if activate == ActivateWindow::Yes {
828- *active_monitor_idx = mon_idx;
829- }
830-
831- let activate = activate.map_smart(|| {
832- // Don't steal focus from an active fullscreen window.
833- let ws = &mon.workspaces[ws_idx];
834- if mon_idx == *active_monitor_idx && ws.is_active_fullscreen() {
835- return false;
836- }
837-
838- // Don't activate if on a different workspace.
839- if mon.active_workspace_idx != ws_idx {
840- return false;
841- }
842-
843- true
844- });
845-
846- mon.add_window(ws_idx, window, activate, width, is_full_width, is_floating);
847- Some(&mon.output)
848- }
849- MonitorSet::NoOutputs { workspaces } => {
850- let ws = workspaces
851- .iter_mut()
852- .find(|ws| {
853- ws.name
854- .as_ref()
855- .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name))
856- })
857- .unwrap();
858- let activate = activate.map_smart(|| true);
859- ws.add_window(window, activate, width, is_full_width, is_floating);
860- None
861- }
862- }
863- }
864-
865 pub fn add_column_by_idx(815 pub fn add_column_by_idx(
866 &mut self,816 &mut self,
867 monitor_idx: usize,817 monitor_idx: usize,
@@ -891,12 +841,13 @@ impl<W: LayoutElement> Layout<W> {
891 pub fn add_window(841 pub fn add_window(
892 &mut self,842 &mut self,
893 window: W,843 window: W,
844+ target: AddWindowTarget<W>,
894 width: Option<ColumnWidth>,845 width: Option<ColumnWidth>,
895 is_full_width: bool,846 is_full_width: bool,
896 is_floating: bool,847 is_floating: bool,
897 activate: ActivateWindow,848 activate: ActivateWindow,
898 ) -> Option<&Output> {849 ) -> Option<&Output> {
899- let width = self.resolve_default_width(&window, width);850+ let resolved_width = self.resolve_default_width(&window, width, is_floating);
900 851
901 match &mut self.monitor_set {852 match &mut self.monitor_set {
902 MonitorSet::Normal {853 MonitorSet::Normal {
@@ -904,142 +855,136 @@ impl<W: LayoutElement> Layout<W> {
904 active_monitor_idx,855 active_monitor_idx,
905 ..856 ..
906 } => {857 } => {
907- let mon = &mut monitors[*active_monitor_idx];858+ let (mon_idx, target) = match target {
859+ AddWindowTarget::Auto => (*active_monitor_idx, MonitorAddWindowTarget::Auto),
860+ AddWindowTarget::Output(output) => {
861+ let mon_idx = monitors
862+ .iter()
863+ .position(|mon| mon.output == *output)
864+ .unwrap();
908 865
909- let activate = activate.map_smart(|| {866+ (mon_idx, MonitorAddWindowTarget::Auto)
910- // Don't steal focus from an active fullscreen window.867+ }
911- let ws = &mon.workspaces[mon.active_workspace_idx];868+ AddWindowTarget::Workspace(ws_id) => {
912- !ws.is_active_fullscreen()869+ let mon_idx = monitors
913- });870+ .iter()
871+ .position(|mon| mon.workspaces.iter().any(|ws| ws.id() == ws_id))
872+ .unwrap();
873+
874+ (
875+ mon_idx,
876+ MonitorAddWindowTarget::Workspace {
877+ id: ws_id,
878+ column_idx: None,
879+ },
880+ )
881+ }
882+ AddWindowTarget::NextTo(next_to) => {
883+ if let Some(output) = self
884+ .interactive_move
885+ .as_ref()
886+ .and_then(|move_| {
887+ if let InteractiveMoveState::Moving(move_) = move_ {
888+ Some(move_)
889+ } else {
890+ None
891+ }
892+ })
893+ .filter(|move_| next_to == move_.tile.window().id())
894+ .map(|move_| move_.output.clone())
895+ {
896+ // The next_to window is being interactively moved.
897+ let mon_idx = monitors
898+ .iter()
899+ .position(|mon| mon.output == output)
900+ .unwrap_or(*active_monitor_idx);
901+
902+ (mon_idx, MonitorAddWindowTarget::Auto)
903+ } else {
904+ let mon_idx = monitors
905+ .iter()
906+ .position(|mon| {
907+ mon.workspaces.iter().any(|ws| ws.has_window(next_to))
908+ })
909+ .unwrap();
910+ (mon_idx, MonitorAddWindowTarget::NextTo(next_to))
911+ }
912+ }
913+ };
914+ let mon = &mut monitors[mon_idx];
914 915
915 mon.add_window(916 mon.add_window(
916- mon.active_workspace_idx,
917 window,917 window,
918+ target,
918 activate,919 activate,
919- width,920+ resolved_width,
920 is_full_width,921 is_full_width,
921 is_floating,922 is_floating,
922 );923 );
923- Some(&mon.output)
924- }
925- MonitorSet::NoOutputs { workspaces } => {
926- let ws = if let Some(ws) = workspaces.get_mut(0) {
927- ws
928- } else {
929- workspaces.push(Workspace::new_no_outputs(
930- self.clock.clone(),
931- self.options.clone(),
932- ));
933- &mut workspaces[0]
934- };
935- let activate = activate.map_smart(|| true);
936- ws.add_window(window, activate, width, is_full_width, is_floating);
937- None
938- }
939- }
940- }
941 924
942- /// Adds a new window to the layout immediately to the right of another window.925+ if activate.map_smart(|| false) {
943- ///926+ *active_monitor_idx = mon_idx;
944- /// If that another window was active, activates the new window.
945- ///
946- /// Returns an output that the window was added to, if there were any outputs.
947- pub fn add_window_right_of(
948- &mut self,
949- right_of: &W::Id,
950- window: W,
951- width: Option<ColumnWidth>,
952- is_full_width: bool,
953- is_floating: bool,
954- ) -> Option<&Output> {
955- if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
956- if right_of == move_.tile.window().id() {
957- let output = move_.output.clone();
958- let activate = ActivateWindow::default();
959- if self.monitor_for_output(&output).is_some() {
960- self.add_window_on_output(
961- &output,
962- window,
963- width,
964- is_full_width,
965- is_floating,
966- activate,
967- );
968- return Some(&self.monitor_for_output(&output).unwrap().output);
969- } else {
970- return self.add_window(window, width, is_full_width, is_floating, activate);
971 }927 }
972- }
973- }
974-
975- let width = self.resolve_default_width(&window, width);
976-
977- match &mut self.monitor_set {
978- MonitorSet::Normal { monitors, .. } => {
979- let mon = monitors
980- .iter_mut()
981- .find(|mon| mon.workspaces.iter().any(|ws| ws.has_window(right_of)))
982- .unwrap();
983 928
984- mon.add_window_right_of(right_of, window, width, is_full_width, is_floating);
985 Some(&mon.output)929 Some(&mon.output)
986 }930 }
987 MonitorSet::NoOutputs { workspaces } => {931 MonitorSet::NoOutputs { workspaces } => {
988- let ws = workspaces932+ let (ws_idx, target) = match target {
989- .iter_mut()933+ AddWindowTarget::Auto => {
990- .find(|ws| ws.has_window(right_of))934+ if workspaces.is_empty() {
991- .unwrap();935+ workspaces.push(Workspace::new_no_outputs(
992- ws.add_window_right_of(right_of, window, width, is_full_width, is_floating);936+ self.clock.clone(),
993- None937+ self.options.clone(),
994- }938+ ));
995- }939+ }
996- }
997-
998- /// Adds a new window to the layout on a specific output.
999- pub fn add_window_on_output(
1000- &mut self,
1001- output: &Output,
1002- window: W,
1003- width: Option<ColumnWidth>,
1004- is_full_width: bool,
1005- is_floating: bool,
1006- activate: ActivateWindow,
1007- ) {
1008- let width = self.resolve_default_width(&window, width);
1009 940
1010- let MonitorSet::Normal {941+ (0, WorkspaceAddWindowTarget::Auto)
1011- monitors,942+ }
1012- active_monitor_idx,943+ AddWindowTarget::Output(_) => panic!(),
1013- ..944+ AddWindowTarget::Workspace(ws_id) => {
1014- } = &mut self.monitor_set945+ let ws_idx = workspaces.iter().position(|ws| ws.id() == ws_id).unwrap();
1015- else {946+ (ws_idx, WorkspaceAddWindowTarget::Auto)
1016- panic!()947+ }
1017- };948+ AddWindowTarget::NextTo(next_to) => {
949+ if self
950+ .interactive_move
951+ .as_ref()
952+ .and_then(|move_| {
953+ if let InteractiveMoveState::Moving(move_) = move_ {
954+ Some(move_)
955+ } else {
956+ None
957+ }
958+ })
959+ .filter(|move_| next_to == move_.tile.window().id())
960+ .is_some()
961+ {
962+ // The next_to window is being interactively moved.
963+ (0, WorkspaceAddWindowTarget::Auto)
964+ } else {
965+ let ws_idx = workspaces
966+ .iter()
967+ .position(|ws| ws.has_window(next_to))
968+ .unwrap();
969+ (ws_idx, WorkspaceAddWindowTarget::NextTo(next_to))
970+ }
971+ }
972+ };
973+ let ws = &mut workspaces[ws_idx];
1018 974
1019- let (mon_idx, mon) = monitors975+ let tile = ws.make_tile(window);
1020- .iter_mut()976+ ws.add_tile(
1021- .enumerate()977+ tile,
1022- .find(|(_, mon)| mon.output == *output)978+ target,
1023- .unwrap();979+ activate,
980+ resolved_width,
981+ is_full_width,
982+ is_floating,
983+ );
1024 984
1025- if activate == ActivateWindow::Yes {985+ None
1026- *active_monitor_idx = mon_idx;986+ }
1027 }987 }
1028-
1029- let activate = activate.map_smart(|| {
1030- // Don't steal focus from an active fullscreen window.
1031- let ws = &mon.workspaces[mon.active_workspace_idx];
1032- mon_idx != *active_monitor_idx || !ws.is_active_fullscreen()
1033- });
1034-
1035- mon.add_window(
1036- mon.active_workspace_idx,
1037- window,
1038- activate,
1039- width,
1040- is_full_width,
1041- is_floating,
1042- );
1043 }988 }
1044 989
1045 pub fn remove_window(990 pub fn remove_window(
@@ -2801,12 +2746,18 @@ impl<W: LayoutElement> Layout<W> {
2801 if mon_idx == new_idx && ws_idx == workspace_idx {2746 if mon_idx == new_idx && ws_idx == workspace_idx {
2802 return;2747 return;
2803 }2748 }
2749+ let ws_id = monitors[new_idx].workspaces[workspace_idx].id();
2804 2750
2805 let mon = &mut monitors[mon_idx];2751 let mon = &mut monitors[mon_idx];
2806 let activate = window.map_or(true, |win| {2752 let activate = window.map_or(true, |win| {
2807 mon_idx == *active_monitor_idx2753 mon_idx == *active_monitor_idx
2808 && mon.active_window().map(|win| win.id()) == Some(win)2754 && mon.active_window().map(|win| win.id()) == Some(win)
2809 });2755 });
2756+ let activate = if activate {
2757+ ActivateWindow::Yes
2758+ } else {
2759+ ActivateWindow::No
2760+ };
2810 2761
2811 let ws = &mut mon.workspaces[ws_idx];2762 let ws = &mut mon.workspaces[ws_idx];
2812 let transaction = Transaction::new();2763 let transaction = Transaction::new();
@@ -2821,19 +2772,18 @@ impl<W: LayoutElement> Layout<W> {
2821 removed.tile.stop_move_animations();2772 removed.tile.stop_move_animations();
2822 2773
2823 let mon = &mut monitors[new_idx];2774 let mon = &mut monitors[new_idx];
2824- if removed.is_floating {2775+ mon.add_tile(
2825- mon.add_floating_tile(workspace_idx, removed.tile, activate);2776+ removed.tile,
2826- } else {2777+ MonitorAddWindowTarget::Workspace {
2827- mon.add_tile(2778+ id: ws_id,
2828- workspace_idx,2779+ column_idx: None,
2829- None,2780+ },
2830- removed.tile,2781+ activate,
2831- activate,2782+ removed.width,
2832- removed.width,2783+ removed.is_full_width,
2833- removed.is_full_width,2784+ removed.is_floating,
2834- );2785+ );
2835- }2786+ if activate.map_smart(|| false) {
2836- if activate {
2837 *active_monitor_idx = new_idx;2787 *active_monitor_idx = new_idx;
2838 }2788 }
2839 2789
@@ -3443,13 +3393,17 @@ impl<W: LayoutElement> Layout<W> {
3443 3393
3444 match position {3394 match position {
3445 InsertPosition::NewColumn(column_idx) => {3395 InsertPosition::NewColumn(column_idx) => {
3396+ let ws_id = mon.workspaces[ws_idx].id();
3446 mon.add_tile(3397 mon.add_tile(
3447- ws_idx,
3448- Some(column_idx),
3449 move_.tile,3398 move_.tile,
3450- true,3399+ MonitorAddWindowTarget::Workspace {
3400+ id: ws_id,
3401+ column_idx: Some(column_idx),
3402+ },
3403+ ActivateWindow::Yes,
3451 move_.width,3404 move_.width,
3452 move_.is_full_width,3405 move_.is_full_width,
3406+ false,
3453 );3407 );
3454 }3408 }
3455 InsertPosition::InColumn(column_idx, tile_idx) => {3409 InsertPosition::InColumn(column_idx, tile_idx) => {
@@ -3474,7 +3428,18 @@ impl<W: LayoutElement> Layout<W> {
3474 tile.floating_window_size = Some(size);3428 tile.floating_window_size = Some(size);
3475 }3429 }
3476 3430
3477- mon.add_floating_tile(ws_idx, tile, true);3431+ let ws_id = mon.workspaces[ws_idx].id();
3432+ mon.add_tile(
3433+ tile,
3434+ MonitorAddWindowTarget::Workspace {
3435+ id: ws_id,
3436+ column_idx: None,
3437+ },
3438+ ActivateWindow::Yes,
3439+ move_.width,
3440+ move_.is_full_width,
3441+ true,
3442+ );
3478 }3443 }
3479 }3444 }
3480 3445
@@ -3490,18 +3455,23 @@ impl<W: LayoutElement> Layout<W> {
3490 tile.animate_move_from(window_render_loc - new_window_render_loc);3455 tile.animate_move_from(window_render_loc - new_window_render_loc);
3491 }3456 }
3492 MonitorSet::NoOutputs { workspaces, .. } => {3457 MonitorSet::NoOutputs { workspaces, .. } => {
3493- let ws = if let Some(ws) = workspaces.get_mut(0) {3458+ if workspaces.is_empty() {
3494- ws
3495- } else {
3496 workspaces.push(Workspace::new_no_outputs(3459 workspaces.push(Workspace::new_no_outputs(
3497 self.clock.clone(),3460 self.clock.clone(),
3498 self.options.clone(),3461 self.options.clone(),
3499 ));3462 ));
3500- &mut workspaces[0]3463+ }
3501- };3464+ let ws = &mut workspaces[0];
3502 3465
3503 // No point in trying to use the pointer position without outputs.3466 // No point in trying to use the pointer position without outputs.
3504- ws.add_tile(None, move_.tile, true, move_.width, move_.is_full_width);3467+ ws.add_tile(
3468+ move_.tile,
3469+ WorkspaceAddWindowTarget::Auto,
3470+ ActivateWindow::Yes,
3471+ move_.width,
3472+ move_.is_full_width,
3473+ move_.is_floating,
3474+ );
3505 }3475 }
3506 }3476 }
3507 }3477 }
@@ -3894,8 +3864,19 @@ impl<W: LayoutElement> Layout<W> {
3894 self.windows().any(|(_, win)| win.id() == window)3864 self.windows().any(|(_, win)| win.id() == window)
3895 }3865 }
3896 3866
3897- fn resolve_default_width(&self, window: &W, width: Option<ColumnWidth>) -> ColumnWidth {3867+ fn resolve_default_width(
3868+ &self,
3869+ window: &W,
3870+ width: Option<ColumnWidth>,
3871+ is_floating: bool,
3872+ ) -> ColumnWidth {
3898 let mut width = width.unwrap_or_else(|| ColumnWidth::Fixed(f64::from(window.size().w)));3873 let mut width = width.unwrap_or_else(|| ColumnWidth::Fixed(f64::from(window.size().w)));
3874+ if is_floating {
3875+ return width;
3876+ }
3877+
3878+ // Add border width to account for the issue that the scrolling layout currently doesn't
3879+ // take borders into account for fixed sizes.
3899 if let ColumnWidth::Fixed(w) = &mut width {3880 if let ColumnWidth::Fixed(w) = &mut width {
3900 let rules = window.rules();3881 let rules = window.rules();
3901 let border_config = rules.border.resolve_against(self.options.border);3882 let border_config = rules.border.resolve_against(self.options.border);
@@ -3903,6 +3884,7 @@ impl<W: LayoutElement> Layout<W> {
3903 *w += border_config.width.0 * 2.;3884 *w += border_config.width.0 * 2.;
3904 }3885 }
3905 }3886 }
3887+
3906 width3888 width
3907 }3889 }
3908 }3890 }
@@ -4242,10 +4224,10 @@ mod tests {
4242 AddWindow {4224 AddWindow {
4243 params: TestWindowParams,4225 params: TestWindowParams,
4244 },4226 },
4245- AddWindowRightOf {4227+ AddWindowNextTo {
4246 params: TestWindowParams,4228 params: TestWindowParams,
4247 #[proptest(strategy = "1..=5usize")]4229 #[proptest(strategy = "1..=5usize")]
4248- right_of_id: usize,4230+ next_to_id: usize,
4249 },4231 },
4250 AddWindowToNamedWorkspace {4232 AddWindowToNamedWorkspace {
4251 params: TestWindowParams,4233 params: TestWindowParams,
@@ -4548,25 +4530,26 @@ mod tests {
4548 let win = TestWindow::new(params);4530 let win = TestWindow::new(params);
4549 layout.add_window(4531 layout.add_window(
4550 win,4532 win,
4533+ AddWindowTarget::Auto,
4551 None,4534 None,
4552 false,4535 false,
4553 params.is_floating,4536 params.is_floating,
4554 ActivateWindow::default(),4537 ActivateWindow::default(),
4555 );4538 );
4556 }4539 }
4557- Op::AddWindowRightOf {4540+ Op::AddWindowNextTo {
4558 mut params,4541 mut params,
4559- right_of_id,4542+ next_to_id,
4560 } => {4543 } => {
4561- let mut found_right_of = false;4544+ let mut found_next_to = false;
4562 4545
4563 if let Some(InteractiveMoveState::Moving(move_)) = &layout.interactive_move {4546 if let Some(InteractiveMoveState::Moving(move_)) = &layout.interactive_move {
4564 let win_id = move_.tile.window().0.id;4547 let win_id = move_.tile.window().0.id;
4565 if win_id == params.id {4548 if win_id == params.id {
4566 return;4549 return;
4567 }4550 }
4568- if win_id == right_of_id {4551+ if win_id == next_to_id {
4569- found_right_of = true;4552+ found_next_to = true;
4570 }4553 }
4571 }4554 }
4572 4555
@@ -4579,8 +4562,8 @@ mod tests {
4579 return;4562 return;
4580 }4563 }
4581 4564
4582- if win.0.id == right_of_id {4565+ if win.0.id == next_to_id {
4583- found_right_of = true;4566+ found_next_to = true;
4584 }4567 }
4585 }4568 }
4586 }4569 }
@@ -4593,15 +4576,15 @@ mod tests {
4593 return;4576 return;
4594 }4577 }
4595 4578
4596- if win.0.id == right_of_id {4579+ if win.0.id == next_to_id {
4597- found_right_of = true;4580+ found_next_to = true;
4598 }4581 }
4599 }4582 }
4600 }4583 }
4601 }4584 }
4602 }4585 }
4603 4586
4604- if !found_right_of {4587+ if !found_next_to {
4605 return;4588 return;
4606 }4589 }
4607 4590
@@ -4612,14 +4595,21 @@ mod tests {
4612 }4595 }
4613 4596
4614 let win = TestWindow::new(params);4597 let win = TestWindow::new(params);
4615- layout.add_window_right_of(&right_of_id, win, None, false, params.is_floating);4598+ layout.add_window(
4599+ win,
4600+ AddWindowTarget::NextTo(&next_to_id),
4601+ None,
4602+ false,
4603+ params.is_floating,
4604+ ActivateWindow::default(),
4605+ );
4616 }4606 }
4617 Op::AddWindowToNamedWorkspace {4607 Op::AddWindowToNamedWorkspace {
4618 mut params,4608 mut params,
4619 ws_name,4609 ws_name,
4620 } => {4610 } => {
4621 let ws_name = format!("ws{ws_name}");4611 let ws_name = format!("ws{ws_name}");
4622- let mut found_workspace = false;4612+ let mut ws_id = None;
4623 4613
4624 if let Some(InteractiveMoveState::Moving(move_)) = &layout.interactive_move {4614 if let Some(InteractiveMoveState::Moving(move_)) = &layout.interactive_move {
4625 if move_.tile.window().0.id == params.id {4615 if move_.tile.window().0.id == params.id {
@@ -4642,7 +4632,7 @@ mod tests {
4642 .as_ref()4632 .as_ref()
4643 .map_or(false, |name| name.eq_ignore_ascii_case(&ws_name))4633 .map_or(false, |name| name.eq_ignore_ascii_case(&ws_name))
4644 {4634 {
4645- found_workspace = true;4635+ ws_id = Some(ws.id());
4646 }4636 }
4647 }4637 }
4648 }4638 }
@@ -4660,15 +4650,15 @@ mod tests {
4660 .as_ref()4650 .as_ref()
4661 .map_or(false, |name| name.eq_ignore_ascii_case(&ws_name))4651 .map_or(false, |name| name.eq_ignore_ascii_case(&ws_name))
4662 {4652 {
4663- found_workspace = true;4653+ ws_id = Some(ws.id());
4664 }4654 }
4665 }4655 }
4666 }4656 }
4667 }4657 }
4668 4658
4669- if !found_workspace {4659+ let Some(ws_id) = ws_id else {
4670 return;4660 return;
4671- }4661+ };
4672 4662
4673 if let Some(parent_id) = params.parent_id {4663 if let Some(parent_id) = params.parent_id {
4674 if parent_id_causes_loop(layout, params.id, parent_id) {4664 if parent_id_causes_loop(layout, params.id, parent_id) {
@@ -4677,9 +4667,9 @@ mod tests {
4677 }4667 }
4678 4668
4679 let win = TestWindow::new(params);4669 let win = TestWindow::new(params);
4680- layout.add_window_to_named_workspace(4670+ layout.add_window(
4681- &ws_name,
4682 win,4671 win,
4672+ AddWindowTarget::Workspace(ws_id),
4683 None,4673 None,
4684 false,4674 false,
4685 params.is_floating,4675 params.is_floating,
@@ -5116,9 +5106,9 @@ mod tests {
5116 Op::AddWindow {5106 Op::AddWindow {
5117 params: TestWindowParams::new(1),5107 params: TestWindowParams::new(1),
5118 },5108 },
5119- Op::AddWindowRightOf {5109+ Op::AddWindowNextTo {
5120 params: TestWindowParams::new(2),5110 params: TestWindowParams::new(2),
5121- right_of_id: 1,5111+ next_to_id: 1,
5122 },5112 },
5123 Op::AddWindowToNamedWorkspace {5113 Op::AddWindowToNamedWorkspace {
5124 params: TestWindowParams::new(3),5114 params: TestWindowParams::new(3),
@@ -5265,13 +5255,13 @@ mod tests {
5265 Op::AddWindow {5255 Op::AddWindow {
5266 params: TestWindowParams::new(2),5256 params: TestWindowParams::new(2),
5267 },5257 },
5268- Op::AddWindowRightOf {5258+ Op::AddWindowNextTo {
5269 params: TestWindowParams::new(6),5259 params: TestWindowParams::new(6),
5270- right_of_id: 0,5260+ next_to_id: 0,
5271 },5261 },
5272- Op::AddWindowRightOf {5262+ Op::AddWindowNextTo {
5273 params: TestWindowParams::new(7),5263 params: TestWindowParams::new(7),
5274- right_of_id: 1,5264+ next_to_id: 1,
5275 },5265 },
5276 Op::AddWindowToNamedWorkspace {5266 Op::AddWindowToNamedWorkspace {
5277 params: TestWindowParams::new(5),5267 params: TestWindowParams::new(5),
@@ -5663,9 +5653,9 @@ mod tests {
5663 Op::AddWindow {5653 Op::AddWindow {
5664 params: TestWindowParams::new(2),5654 params: TestWindowParams::new(2),
5665 },5655 },
5666- Op::AddWindowRightOf {5656+ Op::AddWindowNextTo {
5667 params: TestWindowParams::new(3),5657 params: TestWindowParams::new(3),
5668- right_of_id: 1,5658+ next_to_id: 1,
5669 },5659 },
5670 ];5660 ];
5671 5661
@@ -5699,9 +5689,9 @@ mod tests {
5699 Op::AddWindow {5689 Op::AddWindow {
5700 params: TestWindowParams::new(2),5690 params: TestWindowParams::new(2),
5701 },5691 },
5702- Op::AddWindowRightOf {5692+ Op::AddWindowNextTo {
5703 params: TestWindowParams::new(3),5693 params: TestWindowParams::new(3),
5704- right_of_id: 1,5694+ next_to_id: 1,
5705 },5695 },
5706 ];5696 ];
5707 5697
src/layout/monitor.rsmodified+95 -109
@@ -11,8 +11,10 @@ use smithay::utils::{Logical, Point, Rectangle};
11 11
12 use super::scrolling::{Column, ColumnWidth};12 use super::scrolling::{Column, ColumnWidth};
13 use super::tile::Tile;13 use super::tile::Tile;
14-use super::workspace::{OutputId, Workspace, WorkspaceId, WorkspaceRenderElement};14+use super::workspace::{
15-use super::{LayoutElement, Options};15+ OutputId, Workspace, WorkspaceAddWindowTarget, WorkspaceId, WorkspaceRenderElement,
16+};
17+use super::{ActivateWindow, LayoutElement, Options};
16 use crate::animation::{Animation, Clock};18 use crate::animation::{Animation, Clock};
17 use crate::input::swipe_tracker::SwipeTracker;19 use crate::input::swipe_tracker::SwipeTracker;
18 use crate::render_helpers::renderer::NiriRenderer;20 use crate::render_helpers::renderer::NiriRenderer;
@@ -66,6 +68,23 @@ pub struct WorkspaceSwitchGesture {
66 is_touchpad: bool,68 is_touchpad: bool,
67 }69 }
68 70
71+/// Where to put a newly added window.
72+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
73+pub enum MonitorAddWindowTarget<'a, W: LayoutElement> {
74+ /// No particular preference.
75+ #[default]
76+ Auto,
77+ /// On this workspace.
78+ Workspace {
79+ /// Id of the target workspace.
80+ id: WorkspaceId,
81+ /// Override where the window will open as a new column.
82+ column_idx: Option<usize>,
83+ },
84+ /// Next to this existing window.
85+ NextTo(&'a W::Id),
86+}
87+
69 pub type MonitorRenderElement<R> =88 pub type MonitorRenderElement<R> =
70 RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>;89 RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>;
71 90
@@ -220,56 +239,18 @@ impl<W: LayoutElement> Monitor<W> {
220 239
221 pub fn add_window(240 pub fn add_window(
222 &mut self,241 &mut self,
223- mut workspace_idx: usize,
224- window: W,
225- activate: bool,
226- width: ColumnWidth,
227- is_full_width: bool,
228- is_floating: bool,
229- ) {
230- let workspace = &mut self.workspaces[workspace_idx];
231-
232- workspace.add_window(window, activate, width, is_full_width, is_floating);
233-
234- // After adding a new window, workspace becomes this output's own.
235- workspace.original_output = OutputId::new(&self.output);
236-
237- if workspace_idx == self.workspaces.len() - 1 {
238- self.add_workspace_bottom();
239- }
240-
241- if self.options.empty_workspace_above_first && workspace_idx == 0 {
242- self.add_workspace_top();
243- workspace_idx += 1;
244- }
245-
246- if activate {
247- self.activate_workspace(workspace_idx);
248- }
249- }
250-
251- pub fn add_window_right_of(
252- &mut self,
253- right_of: &W::Id,
254 window: W,242 window: W,
243+ target: MonitorAddWindowTarget<W>,
244+ activate: ActivateWindow,
255 width: ColumnWidth,245 width: ColumnWidth,
256 is_full_width: bool,246 is_full_width: bool,
257 is_floating: bool,247 is_floating: bool,
258 ) {248 ) {
259- let workspace_idx = self249+ // Currently, everything a workspace sets on a Tile is the same across all workspaces of a
260- .workspaces250+ // monitor. So we can use any workspace, not necessarily the exact target workspace.
261- .iter_mut()251+ let tile = self.workspaces[0].make_tile(window);
262- .position(|ws| ws.has_window(right_of))
263- .unwrap();
264- let workspace = &mut self.workspaces[workspace_idx];
265 252
266- workspace.add_window_right_of(right_of, window, width, is_full_width, is_floating);253+ self.add_tile(tile, target, activate, width, is_full_width, is_floating);
267-
268- // After adding a new window, workspace becomes this output's own.
269- workspace.original_output = OutputId::new(&self.output);
270-
271- // Since we're adding window right of something, the workspace isn't empty, and therefore
272- // cannot be the last one, so we never need to insert a new empty workspace.
273 }254 }
274 255
275 pub fn add_column(&mut self, mut workspace_idx: usize, column: Column<W>, activate: bool) {256 pub fn add_column(&mut self, mut workspace_idx: usize, column: Column<W>, activate: bool) {
@@ -295,39 +276,39 @@ impl<W: LayoutElement> Monitor<W> {
295 276
296 pub fn add_tile(277 pub fn add_tile(
297 &mut self,278 &mut self,
298- mut workspace_idx: usize,
299- column_idx: Option<usize>,
300 tile: Tile<W>,279 tile: Tile<W>,
301- activate: bool,280+ target: MonitorAddWindowTarget<W>,
281+ activate: ActivateWindow,
302 width: ColumnWidth,282 width: ColumnWidth,
303 is_full_width: bool,283 is_full_width: bool,
284+ is_floating: bool,
304 ) {285 ) {
305- let workspace = &mut self.workspaces[workspace_idx];286+ let (mut workspace_idx, target) = match target {
306-287+ MonitorAddWindowTarget::Auto => {
307- workspace.add_tile(column_idx, tile, activate, width, is_full_width);288+ (self.active_workspace_idx, WorkspaceAddWindowTarget::Auto)
308-289+ }
309- // After adding a new window, workspace becomes this output's own.290+ MonitorAddWindowTarget::Workspace { id, column_idx } => {
310- workspace.original_output = OutputId::new(&self.output);291+ let idx = self.workspaces.iter().position(|ws| ws.id() == id).unwrap();
311-292+ let target = if let Some(column_idx) = column_idx {
312- if workspace_idx == self.workspaces.len() - 1 {293+ WorkspaceAddWindowTarget::NewColumnAt(column_idx)
313- // Insert a new empty workspace.294+ } else {
314- self.add_workspace_bottom();295+ WorkspaceAddWindowTarget::Auto
315- }296+ };
316-297+ (idx, target)
317- if self.options.empty_workspace_above_first && workspace_idx == 0 {298+ }
318- self.add_workspace_top();299+ MonitorAddWindowTarget::NextTo(win_id) => {
319- workspace_idx += 1;300+ let idx = self
320- }301+ .workspaces
321-302+ .iter_mut()
322- if activate {303+ .position(|ws| ws.has_window(win_id))
323- self.activate_workspace(workspace_idx);304+ .unwrap();
324- }305+ (idx, WorkspaceAddWindowTarget::NextTo(win_id))
325- }306+ }
307+ };
326 308
327- pub fn add_floating_tile(&mut self, mut workspace_idx: usize, tile: Tile<W>, activate: bool) {
328 let workspace = &mut self.workspaces[workspace_idx];309 let workspace = &mut self.workspaces[workspace_idx];
329 310
330- workspace.add_floating_tile(tile, activate);311+ workspace.add_tile(tile, target, activate, width, is_full_width, is_floating);
331 312
332 // After adding a new window, workspace becomes this output's own.313 // After adding a new window, workspace becomes this output's own.
333 workspace.original_output = OutputId::new(&self.output);314 workspace.original_output = OutputId::new(&self.output);
@@ -342,7 +323,7 @@ impl<W: LayoutElement> Monitor<W> {
342 workspace_idx += 1;323 workspace_idx += 1;
343 }324 }
344 325
345- if activate {326+ if activate.map_smart(|| false) {
346 self.activate_workspace(workspace_idx);327 self.activate_workspace(workspace_idx);
347 }328 }
348 }329 }
@@ -518,24 +499,24 @@ impl<W: LayoutElement> Monitor<W> {
518 if new_idx == source_workspace_idx {499 if new_idx == source_workspace_idx {
519 return;500 return;
520 }501 }
502+ let new_id = self.workspaces[new_idx].id();
521 503
522 let workspace = &mut self.workspaces[source_workspace_idx];504 let workspace = &mut self.workspaces[source_workspace_idx];
523 let Some(removed) = workspace.remove_active_tile(Transaction::new()) else {505 let Some(removed) = workspace.remove_active_tile(Transaction::new()) else {
524 return;506 return;
525 };507 };
526 508
527- if removed.is_floating {509+ self.add_tile(
528- self.add_floating_tile(new_idx, removed.tile, true);510+ removed.tile,
529- } else {511+ MonitorAddWindowTarget::Workspace {
530- self.add_tile(512+ id: new_id,
531- new_idx,513+ column_idx: None,
532- None,514+ },
533- removed.tile,515+ ActivateWindow::Yes,
534- true,516+ removed.width,
535- removed.width,517+ removed.is_full_width,
536- removed.is_full_width,518+ removed.is_floating,
537- );519+ );
538- }
539 }520 }
540 521
541 pub fn move_to_workspace_down(&mut self) {522 pub fn move_to_workspace_down(&mut self) {
@@ -545,24 +526,24 @@ impl<W: LayoutElement> Monitor<W> {
545 if new_idx == source_workspace_idx {526 if new_idx == source_workspace_idx {
546 return;527 return;
547 }528 }
529+ let new_id = self.workspaces[new_idx].id();
548 530
549 let workspace = &mut self.workspaces[source_workspace_idx];531 let workspace = &mut self.workspaces[source_workspace_idx];
550 let Some(removed) = workspace.remove_active_tile(Transaction::new()) else {532 let Some(removed) = workspace.remove_active_tile(Transaction::new()) else {
551 return;533 return;
552 };534 };
553 535
554- if removed.is_floating {536+ self.add_tile(
555- self.add_floating_tile(new_idx, removed.tile, true);537+ removed.tile,
556- } else {538+ MonitorAddWindowTarget::Workspace {
557- self.add_tile(539+ id: new_id,
558- new_idx,540+ column_idx: None,
559- None,541+ },
560- removed.tile,542+ ActivateWindow::Yes,
561- true,543+ removed.width,
562- removed.width,544+ removed.is_full_width,
563- removed.is_full_width,545+ removed.is_floating,
564- );546+ );
565- }
566 }547 }
567 548
568 pub fn move_to_workspace(&mut self, window: Option<&W::Id>, idx: usize) {549 pub fn move_to_workspace(&mut self, window: Option<&W::Id>, idx: usize) {
@@ -579,10 +560,16 @@ impl<W: LayoutElement> Monitor<W> {
579 if new_idx == source_workspace_idx {560 if new_idx == source_workspace_idx {
580 return;561 return;
581 }562 }
563+ let new_id = self.workspaces[new_idx].id();
582 564
583 let activate = window.map_or(true, |win| {565 let activate = window.map_or(true, |win| {
584 self.active_window().map(|win| win.id()) == Some(win)566 self.active_window().map(|win| win.id()) == Some(win)
585 });567 });
568+ let activate = if activate {
569+ ActivateWindow::Yes
570+ } else {
571+ ActivateWindow::No
572+ };
586 573
587 let workspace = &mut self.workspaces[source_workspace_idx];574 let workspace = &mut self.workspaces[source_workspace_idx];
588 let transaction = Transaction::new();575 let transaction = Transaction::new();
@@ -594,18 +581,17 @@ impl<W: LayoutElement> Monitor<W> {
594 return;581 return;
595 };582 };
596 583
597- if removed.is_floating {584+ self.add_tile(
598- self.add_floating_tile(new_idx, removed.tile, activate);585+ removed.tile,
599- } else {586+ MonitorAddWindowTarget::Workspace {
600- self.add_tile(587+ id: new_id,
601- new_idx,588+ column_idx: None,
602- None,589+ },
603- removed.tile,590+ activate,
604- activate,591+ removed.width,
605- removed.width,592+ removed.is_full_width,
606- removed.is_full_width,593+ removed.is_floating,
607- );594+ );
608- }
609 595
610 if self.workspace_switch.is_none() {596 if self.workspace_switch.is_none() {
611 self.clean_up_workspaces();597 self.clean_up_workspaces();
src/layout/scrolling.rsmodified+1 -3
@@ -783,6 +783,7 @@ impl<W: LayoutElement> ScrollingSpace<W> {
783 &mut self,783 &mut self,
784 right_of: &W::Id,784 right_of: &W::Id,
785 tile: Tile<W>,785 tile: Tile<W>,
786+ activate: bool,
786 width: ColumnWidth,787 width: ColumnWidth,
787 is_full_width: bool,788 is_full_width: bool,
788 ) {789 ) {
@@ -793,9 +794,6 @@ impl<W: LayoutElement> ScrollingSpace<W> {
793 .unwrap();794 .unwrap();
794 let col_idx = right_of_idx + 1;795 let col_idx = right_of_idx + 1;
795 796
796- // Activate the new window if right_of was active.
797- let activate = self.active_column_idx == right_of_idx;
798-
799 self.add_tile(Some(col_idx), tile, activate, width, is_full_width, None);797 self.add_tile(Some(col_idx), tile, activate, width, is_full_width, None);
800 }798 }
801 799
src/layout/workspace.rsmodified+131 -98
@@ -18,7 +18,7 @@ use super::scrolling::{
18 Column, ColumnWidth, InsertHint, InsertPosition, ScrollingSpace, ScrollingSpaceRenderElement,18 Column, ColumnWidth, InsertHint, InsertPosition, ScrollingSpace, ScrollingSpaceRenderElement,
19 };19 };
20 use super::tile::{Tile, TileRenderSnapshot};20 use super::tile::{Tile, TileRenderSnapshot};
21-use super::{InteractiveResizeData, LayoutElement, Options, RemovedTile, SizeFrac};21+use super::{ActivateWindow, InteractiveResizeData, LayoutElement, Options, RemovedTile, SizeFrac};
22 use crate::animation::Clock;22 use crate::animation::Clock;
23 use crate::niri_render_elements;23 use crate::niri_render_elements;
24 use crate::render_helpers::renderer::NiriRenderer;24 use crate::render_helpers::renderer::NiriRenderer;
@@ -157,6 +157,18 @@ enum FloatingActive {
157 Yes,157 Yes,
158 }158 }
159 159
160+/// Where to put a newly added window.
161+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
162+pub enum WorkspaceAddWindowTarget<'a, W: LayoutElement> {
163+ /// No particular preference.
164+ #[default]
165+ Auto,
166+ /// As a new column at this index.
167+ NewColumnAt(usize),
168+ /// Next to this existing window.
169+ NextTo(&'a W::Id),
170+}
171+
160 impl OutputId {172 impl OutputId {
161 pub fn new(output: &Output) -> Self {173 pub fn new(output: &Output) -> Self {
162 let output_name = output.user_data().get::<OutputName>().unwrap();174 let output_name = output.user_data().get::<OutputName>().unwrap();
@@ -488,55 +500,138 @@ impl<W: LayoutElement> Workspace<W> {
488 self.view_size500 self.view_size
489 }501 }
490 502
491- pub fn add_window(503+ pub fn make_tile(&self, window: W) -> Tile<W> {
492- &mut self,504+ Tile::new(
493- window: W,
494- activate: bool,
495- width: ColumnWidth,
496- is_full_width: bool,
497- is_floating: bool,
498- ) {
499- let mut tile = Tile::new(
500 window,505 window,
501 self.view_size,506 self.view_size,
502 self.scale.fractional_scale(),507 self.scale.fractional_scale(),
503 self.clock.clone(),508 self.clock.clone(),
504 self.options.clone(),509 self.options.clone(),
505- );510+ )
506- tile.unfullscreen_to_floating = is_floating;
507-
508- // If the tile is pending fullscreen, open it in the scrolling layout where it can go
509- // fullscreen.
510- if is_floating && !tile.window().is_pending_fullscreen() {
511- self.add_floating_tile(tile, activate);
512- } else {
513- self.add_tile(None, tile, activate, width, is_full_width);
514- }
515 }511 }
516 512
517 pub fn add_tile(513 pub fn add_tile(
518 &mut self,514 &mut self,
519- col_idx: Option<usize>,515+ mut tile: Tile<W>,
520- tile: Tile<W>,516+ target: WorkspaceAddWindowTarget<W>,
521- activate: bool,517+ activate: ActivateWindow,
522 width: ColumnWidth,518 width: ColumnWidth,
523 is_full_width: bool,519 is_full_width: bool,
520+ is_floating: bool,
524 ) {521 ) {
525 self.enter_output_for_window(tile.window());522 self.enter_output_for_window(tile.window());
526- self.scrolling523+ tile.unfullscreen_to_floating = is_floating;
527- .add_tile(col_idx, tile, activate, width, is_full_width, None);
528 524
529- if activate {525+ match target {
530- self.floating_is_active = FloatingActive::No;526+ WorkspaceAddWindowTarget::Auto => {
531- }527+ // Don't steal focus from an active fullscreen window.
532- }528+ let activate = activate.map_smart(|| !self.is_active_fullscreen());
533 529
534- pub fn add_floating_tile(&mut self, tile: Tile<W>, activate: bool) {530+ // If the tile is pending fullscreen, open it in the scrolling layout where it can
535- self.enter_output_for_window(tile.window());531+ // go fullscreen.
536- self.floating.add_tile(tile, activate);532+ if is_floating && !tile.window().is_pending_fullscreen() {
533+ self.floating.add_tile(tile, activate);
537 534
538- if activate || self.scrolling.is_empty() {535+ if activate || self.scrolling.is_empty() {
539- self.floating_is_active = FloatingActive::Yes;536+ self.floating_is_active = FloatingActive::Yes;
537+ }
538+ } else {
539+ self.scrolling
540+ .add_tile(None, tile, activate, width, is_full_width, None);
541+
542+ if activate {
543+ self.floating_is_active = FloatingActive::No;
544+ }
545+ }
546+ }
547+ WorkspaceAddWindowTarget::NewColumnAt(col_idx) => {
548+ let activate = activate.map_smart(|| false);
549+ self.scrolling
550+ .add_tile(Some(col_idx), tile, activate, width, is_full_width, None);
551+
552+ if activate {
553+ self.floating_is_active = FloatingActive::No;
554+ }
555+ }
556+ WorkspaceAddWindowTarget::NextTo(next_to) => {
557+ let activate = activate.map_smart(|| self.active_window().unwrap().id() == next_to);
558+
559+ let floating_has_window = self.floating.has_window(next_to);
560+ if is_floating || floating_has_window {
561+ if floating_has_window {
562+ self.floating.add_tile_above(next_to, tile, activate);
563+ } else {
564+ // FIXME: use static pos
565+ let (next_to_tile, render_pos) = self
566+ .scrolling
567+ .tiles_with_render_positions()
568+ .find(|(tile, _)| tile.window().id() == next_to)
569+ .unwrap();
570+
571+ // Position the new tile in the center above the next_to tile. Think a
572+ // dialog opening on top of a window.
573+ let tile_size = tile.tile_size();
574+ let pos = render_pos
575+ + (next_to_tile.tile_size().to_point() - tile_size.to_point())
576+ .downscale(2.);
577+ let pos = self.floating.clamp_within_working_area(pos, tile_size);
578+ let pos = self.floating.logical_to_size_frac(pos);
579+ tile.floating_pos = Some(pos);
580+
581+ self.floating.add_tile(tile, activate);
582+
583+ if activate {
584+ self.floating_is_active = FloatingActive::Yes;
585+ }
586+ }
587+ } else {
588+ self.scrolling
589+ .add_tile_right_of(next_to, tile, activate, width, is_full_width);
590+ }
591+
592+ // if is_floating && !tile.window().is_pending_fullscreen() {
593+ // if floating_has_window {
594+ // self.floating.add_tile_above(next_to, tile, activate);
595+ // } else {
596+ // // FIXME: use static pos
597+ // let (next_to_tile, render_pos) = self
598+ // .scrolling
599+ // .tiles_with_render_positions()
600+ // .find(|(tile, _)| tile.window().id() == next_to)
601+ // .unwrap();
602+ //
603+ // // Position the new tile in the center above the next_to tile. Think a
604+ // // dialog opening on top of a window.
605+ // let tile_size = tile.tile_size();
606+ // let pos = render_pos
607+ // + (next_to_tile.tile_size().to_point() - tile_size.to_point())
608+ // .downscale(2.);
609+ // let pos = self.floating.clamp_within_working_area(pos, tile_size);
610+ // let pos = self.floating.logical_to_size_frac(pos);
611+ // tile.floating_pos = Some(pos);
612+ //
613+ // self.floating.add_tile(tile, activate);
614+ // }
615+ //
616+ // if activate || self.scrolling.is_empty() {
617+ // self.floating_is_active = FloatingActive::Yes;
618+ // }
619+ // } else if floating_has_window {
620+ // self.scrolling
621+ // .add_tile(None, tile, activate, width, is_full_width, None);
622+ //
623+ // if activate {
624+ // self.floating_is_active = FloatingActive::No;
625+ // }
626+ // } else {
627+ // self.scrolling
628+ // .add_tile_right_of(next_to, tile, activate, width, is_full_width);
629+ //
630+ // if activate {
631+ // self.floating_is_active = FloatingActive::No;
632+ // }
633+ // }
634+ }
540 }635 }
541 }636 }
542 637
@@ -556,70 +651,6 @@ impl<W: LayoutElement> Workspace<W> {
556 }651 }
557 }652 }
558 653
559- pub fn add_window_right_of(
560- &mut self,
561- right_of: &W::Id,
562- window: W,
563- width: ColumnWidth,
564- is_full_width: bool,
565- // TODO: smarter enum, so you can override is_floating = false for floating right_of.
566- is_floating: bool,
567- ) {
568- let mut tile = Tile::new(
569- window,
570- self.view_size,
571- self.scale.fractional_scale(),
572- self.clock.clone(),
573- self.options.clone(),
574- );
575- tile.unfullscreen_to_floating = is_floating;
576- self.add_tile_right_of(right_of, tile, width, is_full_width, is_floating);
577- }
578-
579- pub fn add_tile_right_of(
580- &mut self,
581- right_of: &W::Id,
582- mut tile: Tile<W>,
583- width: ColumnWidth,
584- is_full_width: bool,
585- is_floating: bool,
586- ) {
587- self.enter_output_for_window(tile.window());
588-
589- // TODO: open-fullscreen into scrolling.
590- let floating_has_window = self.floating.has_window(right_of);
591- if is_floating || floating_has_window {
592- if floating_has_window {
593- self.floating.add_tile_above(right_of, tile);
594- } else {
595- let activate = self.scrolling.active_window().unwrap().id() == right_of;
596- // FIXME: use static pos
597- let (right_of_tile, render_pos) = self
598- .scrolling
599- .tiles_with_render_positions()
600- .find(|(tile, _)| tile.window().id() == right_of)
601- .unwrap();
602-
603- // Position the new tile in the center above the right_of tile. Think a dialog
604- // opening on top of a window.
605- let tile_size = tile.tile_size();
606- let pos = render_pos
607- + (right_of_tile.tile_size().to_point() - tile_size.to_point()).downscale(2.);
608- let pos = self.floating.clamp_within_working_area(pos, tile_size);
609- let pos = self.floating.logical_to_size_frac(pos);
610- tile.floating_pos = Some(pos);
611-
612- self.floating.add_tile(tile, activate);
613- if activate {
614- self.floating_is_active = FloatingActive::Yes;
615- }
616- }
617- } else {
618- self.scrolling
619- .add_tile_right_of(right_of, tile, width, is_full_width);
620- }
621- }
622-
623 pub fn add_column(&mut self, column: Column<W>, activate: bool) {654 pub fn add_column(&mut self, column: Column<W>, activate: bool) {
624 for (tile, _) in column.tiles() {655 for (tile, _) in column.tiles() {
625 self.enter_output_for_window(tile.window());656 self.enter_output_for_window(tile.window());
@@ -702,10 +733,12 @@ impl<W: LayoutElement> Workspace<W> {
702 pub fn resolve_default_width(733 pub fn resolve_default_width(
703 &self,734 &self,
704 default_width: Option<Option<ColumnWidth>>,735 default_width: Option<Option<ColumnWidth>>,
736+ is_floating: bool,
705 ) -> Option<ColumnWidth> {737 ) -> Option<ColumnWidth> {
706 match default_width {738 match default_width {
707 Some(Some(width)) => Some(width),739 Some(Some(width)) => Some(width),
708 Some(None) => None,740 Some(None) => None,
741+ None if is_floating => None,
709 None => self.options.default_column_width,742 None => self.options.default_column_width,
710 }743 }
711 }744 }