Skip to content

Commit

Permalink
Add actions to focus/move to next/previous monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
julianschuler authored and YaLTeR committed Jan 2, 2025
1 parent 4e33f45 commit de51d18
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
18 changes: 18 additions & 0 deletions niri-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,14 +1265,20 @@ pub enum Action {
FocusMonitorRight,
FocusMonitorDown,
FocusMonitorUp,
FocusMonitorPrevious,
FocusMonitorNext,
MoveWindowToMonitorLeft,
MoveWindowToMonitorRight,
MoveWindowToMonitorDown,
MoveWindowToMonitorUp,
MoveWindowToMonitorPrevious,
MoveWindowToMonitorNext,
MoveColumnToMonitorLeft,
MoveColumnToMonitorRight,
MoveColumnToMonitorDown,
MoveColumnToMonitorUp,
MoveColumnToMonitorPrevious,
MoveColumnToMonitorNext,
SetWindowWidth(#[knuffel(argument, str)] SizeChange),
#[knuffel(skip)]
SetWindowWidthById {
Expand Down Expand Up @@ -1303,6 +1309,8 @@ pub enum Action {
MoveWorkspaceToMonitorRight,
MoveWorkspaceToMonitorDown,
MoveWorkspaceToMonitorUp,
MoveWorkspaceToMonitorPrevious,
MoveWorkspaceToMonitorNext,
ToggleWindowFloating,
#[knuffel(skip)]
ToggleWindowFloatingById(u64),
Expand Down Expand Up @@ -1422,14 +1430,20 @@ impl From<niri_ipc::Action> for Action {
niri_ipc::Action::FocusMonitorRight {} => Self::FocusMonitorRight,
niri_ipc::Action::FocusMonitorDown {} => Self::FocusMonitorDown,
niri_ipc::Action::FocusMonitorUp {} => Self::FocusMonitorUp,
niri_ipc::Action::FocusMonitorPrevious {} => Self::FocusMonitorPrevious,
niri_ipc::Action::FocusMonitorNext {} => Self::FocusMonitorNext,
niri_ipc::Action::MoveWindowToMonitorLeft {} => Self::MoveWindowToMonitorLeft,
niri_ipc::Action::MoveWindowToMonitorRight {} => Self::MoveWindowToMonitorRight,
niri_ipc::Action::MoveWindowToMonitorDown {} => Self::MoveWindowToMonitorDown,
niri_ipc::Action::MoveWindowToMonitorUp {} => Self::MoveWindowToMonitorUp,
niri_ipc::Action::MoveWindowToMonitorPrevious {} => Self::MoveWindowToMonitorPrevious,
niri_ipc::Action::MoveWindowToMonitorNext {} => Self::MoveWindowToMonitorNext,
niri_ipc::Action::MoveColumnToMonitorLeft {} => Self::MoveColumnToMonitorLeft,
niri_ipc::Action::MoveColumnToMonitorRight {} => Self::MoveColumnToMonitorRight,
niri_ipc::Action::MoveColumnToMonitorDown {} => Self::MoveColumnToMonitorDown,
niri_ipc::Action::MoveColumnToMonitorUp {} => Self::MoveColumnToMonitorUp,
niri_ipc::Action::MoveColumnToMonitorPrevious {} => Self::MoveColumnToMonitorPrevious,
niri_ipc::Action::MoveColumnToMonitorNext {} => Self::MoveColumnToMonitorNext,
niri_ipc::Action::SetWindowWidth { id: None, change } => Self::SetWindowWidth(change),
niri_ipc::Action::SetWindowWidth {
id: Some(id),
Expand Down Expand Up @@ -1461,6 +1475,10 @@ impl From<niri_ipc::Action> for Action {
niri_ipc::Action::MoveWorkspaceToMonitorRight {} => Self::MoveWorkspaceToMonitorRight,
niri_ipc::Action::MoveWorkspaceToMonitorDown {} => Self::MoveWorkspaceToMonitorDown,
niri_ipc::Action::MoveWorkspaceToMonitorUp {} => Self::MoveWorkspaceToMonitorUp,
niri_ipc::Action::MoveWorkspaceToMonitorPrevious {} => {
Self::MoveWorkspaceToMonitorPrevious
}
niri_ipc::Action::MoveWorkspaceToMonitorNext {} => Self::MoveWorkspaceToMonitorNext,
niri_ipc::Action::ToggleDebugTint {} => Self::ToggleDebugTint,
niri_ipc::Action::DebugToggleOpaqueRegions {} => Self::DebugToggleOpaqueRegions,
niri_ipc::Action::DebugToggleDamage {} => Self::DebugToggleDamage,
Expand Down
16 changes: 16 additions & 0 deletions niri-ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ pub enum Action {
FocusMonitorDown {},
/// Focus the monitor above.
FocusMonitorUp {},
/// Focus the previous monitor.
FocusMonitorPrevious {},
/// Focus the next monitor.
FocusMonitorNext {},
/// Move the focused window to the monitor to the left.
MoveWindowToMonitorLeft {},
/// Move the focused window to the monitor to the right.
Expand All @@ -366,6 +370,10 @@ pub enum Action {
MoveWindowToMonitorDown {},
/// Move the focused window to the monitor above.
MoveWindowToMonitorUp {},
/// Move the focused window to the previous monitor.
MoveWindowToMonitorPrevious {},
/// Move the focused window to the next monitor.
MoveWindowToMonitorNext {},
/// Move the focused column to the monitor to the left.
MoveColumnToMonitorLeft {},
/// Move the focused column to the monitor to the right.
Expand All @@ -374,6 +382,10 @@ pub enum Action {
MoveColumnToMonitorDown {},
/// Move the focused column to the monitor above.
MoveColumnToMonitorUp {},
/// Move the focused column to the previous monitor.
MoveColumnToMonitorPrevious {},
/// Move the focused column to the next monitor.
MoveColumnToMonitorNext {},
/// Change the width of a window.
#[cfg_attr(
feature = "clap",
Expand Down Expand Up @@ -460,6 +472,10 @@ pub enum Action {
MoveWorkspaceToMonitorDown {},
/// Move the focused workspace to the monitor above.
MoveWorkspaceToMonitorUp {},
/// Move the focused workspace to the previous monitor.
MoveWorkspaceToMonitorPrevious {},
/// Move the focused workspace to the next monitor.
MoveWorkspaceToMonitorNext {},
/// Toggle a debug tint on windows.
ToggleDebugTint {},
/// Toggle visualization of render element opaque regions.
Expand Down
68 changes: 68 additions & 0 deletions src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,22 @@ impl State {
}
}
}
Action::FocusMonitorPrevious => {
if let Some(output) = self.niri.output_previous() {
self.niri.layout.focus_output(&output);
if !self.maybe_warp_cursor_to_focus_centered() {
self.move_cursor_to_output(&output);
}
}
}
Action::FocusMonitorNext => {
if let Some(output) = self.niri.output_next() {
self.niri.layout.focus_output(&output);
if !self.maybe_warp_cursor_to_focus_centered() {
self.move_cursor_to_output(&output);
}
}
}
Action::MoveWindowToMonitorLeft => {
if let Some(output) = self.niri.output_left() {
self.niri.layout.move_to_output(None, &output, None);
Expand Down Expand Up @@ -1222,6 +1238,24 @@ impl State {
}
}
}
Action::MoveWindowToMonitorPrevious => {
if let Some(output) = self.niri.output_previous() {
self.niri.layout.move_to_output(None, &output, None);
self.niri.layout.focus_output(&output);
if !self.maybe_warp_cursor_to_focus_centered() {
self.move_cursor_to_output(&output);
}
}
}
Action::MoveWindowToMonitorNext => {
if let Some(output) = self.niri.output_next() {
self.niri.layout.move_to_output(None, &output, None);
self.niri.layout.focus_output(&output);
if !self.maybe_warp_cursor_to_focus_centered() {
self.move_cursor_to_output(&output);
}
}
}
Action::MoveColumnToMonitorLeft => {
if let Some(output) = self.niri.output_left() {
self.niri.layout.move_column_to_output(&output);
Expand Down Expand Up @@ -1258,6 +1292,24 @@ impl State {
}
}
}
Action::MoveColumnToMonitorPrevious => {
if let Some(output) = self.niri.output_previous() {
self.niri.layout.move_column_to_output(&output);
self.niri.layout.focus_output(&output);
if !self.maybe_warp_cursor_to_focus_centered() {
self.move_cursor_to_output(&output);
}
}
}
Action::MoveColumnToMonitorNext => {
if let Some(output) = self.niri.output_next() {
self.niri.layout.move_column_to_output(&output);
self.niri.layout.focus_output(&output);
if !self.maybe_warp_cursor_to_focus_centered() {
self.move_cursor_to_output(&output);
}
}
}
Action::SetColumnWidth(change) => {
self.niri.layout.set_column_width(change);
}
Expand Down Expand Up @@ -1328,6 +1380,22 @@ impl State {
}
}
}
Action::MoveWorkspaceToMonitorPrevious => {
if let Some(output) = self.niri.output_previous() {
self.niri.layout.move_workspace_to_output(&output);
if !self.maybe_warp_cursor_to_focus_centered() {
self.move_cursor_to_output(&output);
}
}
}
Action::MoveWorkspaceToMonitorNext => {
if let Some(output) = self.niri.output_next() {
self.niri.layout.move_workspace_to_output(&output);
if !self.maybe_warp_cursor_to_focus_centered() {
self.move_cursor_to_output(&output);
}
}
}
Action::ToggleWindowFloating => {
self.niri.layout.toggle_window_floating(None);
// FIXME: granular
Expand Down
34 changes: 34 additions & 0 deletions src/niri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ pub struct Niri {
// according to their global position.
pub global_space: Space<Window>,

/// Mapped outputs, sorted by their name and position.
pub sorted_outputs: Vec<Output>,

// Windows which don't have a buffer attached yet.
pub unmapped_windows: HashMap<WlSurface, Unmapped>,

Expand Down Expand Up @@ -1959,6 +1962,7 @@ impl Niri {

layout,
global_space: Space::default(),
sorted_outputs: Vec::default(),
output_state: HashMap::new(),
unmapped_windows: HashMap::new(),
unmapped_layer_surfaces: HashSet::new(),
Expand Down Expand Up @@ -2148,6 +2152,11 @@ impl Niri {
outputs.iter().map(|d| &d.name.connector)
);

self.sorted_outputs = outputs
.iter()
.map(|Data { output, .. }| output.clone())
.collect();

for data in outputs.into_iter() {
let Data {
output,
Expand Down Expand Up @@ -2658,6 +2667,31 @@ impl Niri {
.cloned()
}

pub fn output_previous(&self) -> Option<Output> {
let active = self.layout.active_output()?;

self.sorted_outputs
.iter()
.rev()
.skip_while(|&output| output != active)
.nth(1)
.or(self.sorted_outputs.last())
.filter(|&output| output != active)
.cloned()
}

pub fn output_next(&self) -> Option<Output> {
let active = self.layout.active_output()?;

self.sorted_outputs
.iter()
.skip_while(|&output| output != active)
.nth(1)
.or(self.sorted_outputs.first())
.filter(|&output| output != active)
.cloned()
}

pub fn find_output_and_workspace_index(
&self,
workspace_reference: WorkspaceReference,
Expand Down

0 comments on commit de51d18

Please sign in to comment.