Skip to content

Commit

Permalink
Add SwitchPresetWindowHeight by id
Browse files Browse the repository at this point in the history
  • Loading branch information
YaLTeR committed Sep 12, 2024
1 parent 7bd79d9 commit 93d356b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
9 changes: 8 additions & 1 deletion niri-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,8 @@ pub enum Action {
ResetWindowHeightById(u64),
SwitchPresetColumnWidth,
SwitchPresetWindowHeight,
#[knuffel(skip)]
SwitchPresetWindowHeightById(u64),
MaximizeColumn,
SetColumnWidth(#[knuffel(argument, str)] SizeChange),
SwitchLayout(#[knuffel(argument, str)] LayoutSwitchTarget),
Expand Down Expand Up @@ -1270,7 +1272,12 @@ impl From<niri_ipc::Action> for Action {
niri_ipc::Action::ResetWindowHeight { id: None } => Self::ResetWindowHeight,
niri_ipc::Action::ResetWindowHeight { id: Some(id) } => Self::ResetWindowHeightById(id),
niri_ipc::Action::SwitchPresetColumnWidth {} => Self::SwitchPresetColumnWidth,
niri_ipc::Action::SwitchPresetWindowHeight {} => Self::SwitchPresetWindowHeight,
niri_ipc::Action::SwitchPresetWindowHeight { id: None } => {
Self::SwitchPresetWindowHeight
}
niri_ipc::Action::SwitchPresetWindowHeight { id: Some(id) } => {
Self::SwitchPresetWindowHeightById(id)
}
niri_ipc::Action::MaximizeColumn {} => Self::MaximizeColumn,
niri_ipc::Action::SetColumnWidth { change } => Self::SetColumnWidth(change),
niri_ipc::Action::SwitchLayout { layout } => Self::SwitchLayout(layout),
Expand Down
8 changes: 7 additions & 1 deletion niri-ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,13 @@ pub enum Action {
/// Switch between preset column widths.
SwitchPresetColumnWidth {},
/// Switch between preset window heights.
SwitchPresetWindowHeight {},
SwitchPresetWindowHeight {
/// Id of the window whose height to switch.
///
/// If `None`, uses the focused window.
#[cfg_attr(feature = "clap", arg(long))]
id: Option<u64>,
},
/// Toggle the maximized state of the focused column.
MaximizeColumn {},
/// Change the width of the focused column.
Expand Down
9 changes: 8 additions & 1 deletion src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,14 @@ impl State {
self.niri.layout.toggle_width();
}
Action::SwitchPresetWindowHeight => {
self.niri.layout.toggle_window_height();
self.niri.layout.toggle_window_height(None);
}
Action::SwitchPresetWindowHeightById(id) => {
let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
let window = window.map(|(_, m)| m.window.clone());
if let Some(window) = window {
self.niri.layout.toggle_window_height(Some(&window));
}
}
Action::CenterColumn => {
self.niri.layout.center_column();
Expand Down
30 changes: 23 additions & 7 deletions src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1951,11 +1951,21 @@ impl<W: LayoutElement> Layout<W> {
monitor.toggle_width();
}

pub fn toggle_window_height(&mut self) {
let Some(monitor) = self.active_monitor() else {
pub fn toggle_window_height(&mut self, window: Option<&W::Id>) {
let workspace = if let Some(window) = window {
Some(
self.workspaces_mut()
.find(|ws| ws.has_window(window))
.unwrap(),
)
} else {
self.active_workspace_mut()
};

let Some(workspace) = workspace else {
return;
};
monitor.toggle_window_height();
workspace.toggle_window_height(window);
}

pub fn toggle_full_width(&mut self) {
Expand Down Expand Up @@ -3048,7 +3058,10 @@ mod tests {
},
MoveColumnToOutput(#[proptest(strategy = "1..=5u8")] u8),
SwitchPresetColumnWidth,
SwitchPresetWindowHeight,
SwitchPresetWindowHeight {
#[proptest(strategy = "proptest::option::of(1..=5usize)")]
id: Option<usize>,
},
MaximizeColumn,
SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),
SetWindowHeight {
Expand Down Expand Up @@ -3475,7 +3488,10 @@ mod tests {
Op::MoveWorkspaceDown => layout.move_workspace_down(),
Op::MoveWorkspaceUp => layout.move_workspace_up(),
Op::SwitchPresetColumnWidth => layout.toggle_width(),
Op::SwitchPresetWindowHeight => layout.toggle_window_height(),
Op::SwitchPresetWindowHeight { id } => {
let id = id.filter(|id| layout.has_window(id));
layout.toggle_window_height(id.as_ref());
}
Op::MaximizeColumn => layout.toggle_full_width(),
Op::SetColumnWidth(change) => layout.set_column_width(change),
Op::SetWindowHeight { id, change } => {
Expand Down Expand Up @@ -4458,8 +4474,8 @@ mod tests {
min_max_size: Default::default(),
},
Op::ConsumeOrExpelWindowLeft,
Op::SwitchPresetWindowHeight,
Op::SwitchPresetWindowHeight,
Op::SwitchPresetWindowHeight { id: None },
Op::SwitchPresetWindowHeight { id: None },
];
for op in ops {
op.apply(&mut layout);
Expand Down
4 changes: 0 additions & 4 deletions src/layout/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,6 @@ impl<W: LayoutElement> Monitor<W> {
self.active_workspace().toggle_width();
}

pub fn toggle_window_height(&mut self) {
self.active_workspace().toggle_window_height();
}

pub fn toggle_full_width(&mut self) {
self.active_workspace().toggle_full_width();
}
Expand Down
19 changes: 16 additions & 3 deletions src/layout/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2373,13 +2373,26 @@ impl<W: LayoutElement> Workspace<W> {
cancel_resize_for_column(&mut self.interactive_resize, col);
}

pub fn toggle_window_height(&mut self) {
pub fn toggle_window_height(&mut self, window: Option<&W::Id>) {
if self.columns.is_empty() {
return;
}

let col = &mut self.columns[self.active_column_idx];
col.toggle_window_height(None, true);
let (col, tile_idx) = if let Some(window) = window {
self.columns
.iter_mut()
.find_map(|col| {
col.tiles
.iter()
.position(|tile| tile.window().id() == window)
.map(|tile_idx| (col, Some(tile_idx)))
})
.unwrap()
} else {
(&mut self.columns[self.active_column_idx], None)
};

col.toggle_window_height(tile_idx, true);

cancel_resize_for_column(&mut self.interactive_resize, col);
}
Expand Down

0 comments on commit 93d356b

Please sign in to comment.