From 6f92b3296a9fc67dbce5a88708c2ed2dfabe4927 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Mon, 12 Jan 2026 18:25:31 +0300 Subject: [PATCH] Store output name in CastTarget Will be useful in the next commit to avoid fetching it every time. --- src/input/mod.rs | 3 +-- src/niri.rs | 25 ++++++++++++++++++++++--- src/screencasting/mod.rs | 17 ++++++++--------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/input/mod.rs b/src/input/mod.rs index a2fbea22..aed2e9c5 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -2235,8 +2235,7 @@ impl State { Some(name) => self.niri.output_by_name_match(&name), }; if let Some(output) = output { - let output = output.downgrade(); - self.set_dynamic_cast_target(CastTarget::Output(output)); + self.set_dynamic_cast_target(CastTarget::output(output)); } } Action::ClearDynamicCastTarget => { diff --git a/src/niri.rs b/src/niri.rs index dcffd3bf..ed029cfa 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -571,8 +571,27 @@ pub enum CenterCoords { pub enum CastTarget { // Dynamic cast before selecting anything. Nothing, - Output(WeakOutput), - Window { id: u64 }, + Output { + output: WeakOutput, + /// Cached name of the output. + name: String, + }, + Window { + id: u64, + }, +} + +impl CastTarget { + pub fn output(output: &Output) -> Self { + Self::Output { + output: output.downgrade(), + name: output.name(), + } + } + + pub fn matches_output(&self, weak: &WeakOutput) -> bool { + matches!(self, CastTarget::Output { output, .. } if output == weak) + } } /// Pending update to a window's focus timestamp. @@ -2797,7 +2816,7 @@ impl Niri { RedrawState::WaitingForEstimatedVBlankAndQueued(token) => self.event_loop.remove(token), } - self.stop_casts_for_target(CastTarget::Output(output.downgrade())); + self.stop_casts_for_target(CastTarget::output(output)); self.remove_screencopy_output(output); diff --git a/src/screencasting/mod.rs b/src/screencasting/mod.rs index adadbc72..a5b4bcf2 100644 --- a/src/screencasting/mod.rs +++ b/src/screencasting/mod.rs @@ -156,8 +156,8 @@ impl State { }); return; } - CastTarget::Output(weak) => { - if let Some(output) = weak.upgrade() { + CastTarget::Output { output, .. } => { + if let Some(output) = output.upgrade() { self.niri.queue_redraw(&output); } return; @@ -260,7 +260,7 @@ impl State { // Leave refresh as is when clearing. Chances are, the next refresh will match it, // then we'll avoid reconfiguring. CastTarget::Nothing => (), - CastTarget::Output(output) => { + CastTarget::Output { output, .. } => { if let Some(output) = output.upgrade() { refresh = Some(output.current_mode().unwrap().refresh as u32); } @@ -316,8 +316,8 @@ impl State { // We don't stop dynamic casts on missing output/window. let (size, refresh) = match target { CastTarget::Nothing => panic!("dynamic cast starting target must not be Nothing"), - CastTarget::Output(weak) => { - let Some(output) = weak.upgrade() else { + CastTarget::Output { output, .. } => { + let Some(output) = output.upgrade() else { return; }; cast_params_for_output(&output) @@ -404,7 +404,7 @@ impl State { }; let (size, refresh) = cast_params_for_output(output); - (CastTarget::Output(output.downgrade()), size, refresh, false) + (CastTarget::output(output), size, refresh, false) } StreamTargetId::Window { id } if id == self.niri.casting.dynamic_cast_id_for_portal.get() => @@ -538,8 +538,7 @@ impl Niri { ) { let _span = tracy_client::span!("Niri::render_for_screen_cast"); - let target = CastTarget::Output(output.downgrade()); - + let weak = output.downgrade(); let size = output.current_mode().unwrap().size; let transform = output.current_transform(); let size = transform.transform_size(size); @@ -558,7 +557,7 @@ impl Niri { continue; } - if cast.target != target { + if !cast.target.matches_output(&weak) { continue; }