refactor: swap WaylandIncrInfo from range to start

Since the range end was always encoded in the data length, calculating
the end of the selection is a bit cleaner and removes a potentially
confusing slice operation on a slice.
This commit is contained in:
En-En 2025-10-03 13:12:25 +00:00 committed by Supreeeme
parent cc011f3251
commit cdf405fac5

View file

@ -164,7 +164,7 @@ impl Selection {
pub struct WaylandIncrInfo { pub struct WaylandIncrInfo {
data: Vec<u8>, data: Vec<u8>,
range: std::ops::Range<usize>, start: usize,
property: x::Atom, property: x::Atom,
target_window: x::Window, target_window: x::Window,
target_type: x::Atom, target_type: x::Atom,
@ -180,23 +180,24 @@ pub struct WaylandSelection<T: SelectionType> {
impl<T: SelectionType> WaylandSelection<T> { impl<T: SelectionType> WaylandSelection<T> {
fn check_for_incr(&mut self, connection: &xcb::Connection) -> Option<bool> { fn check_for_incr(&mut self, connection: &xcb::Connection) -> Option<bool> {
let incr_data = self.incr_data.as_mut()?; let incr_data = self.incr_data.as_mut()?;
let range_start = incr_data.range.start; let incr_end = std::cmp::min(
let range_len = std::cmp::min(incr_data.max_req_bytes, incr_data.range.end - range_start); incr_data.max_req_bytes + incr_data.start,
incr_data.data.len(),
);
if let Err(e) = connection.send_and_check_request(&x::ChangeProperty { if let Err(e) = connection.send_and_check_request(&x::ChangeProperty {
mode: x::PropMode::Append, mode: x::PropMode::Append,
window: incr_data.target_window, window: incr_data.target_window,
property: incr_data.property, property: incr_data.property,
r#type: incr_data.target_type, r#type: incr_data.target_type,
data: &incr_data.data[range_start..][..range_len], data: &incr_data.data[incr_data.start..incr_end],
}) { }) {
warn!("failed to write selection data: {e:?}"); warn!("failed to write selection data: {e:?}");
self.incr_data = None; self.incr_data = None;
return Some(true); return Some(true);
} }
incr_data.range.start += range_len; if incr_data.start == incr_end {
if range_len == 0 {
debug!( debug!(
"completed incr for mime {}", "completed incr for mime {}",
get_atom_name(connection, incr_data.target_type) get_atom_name(connection, incr_data.target_type)
@ -207,6 +208,7 @@ impl<T: SelectionType> WaylandSelection<T> {
"received some incr data for {}", "received some incr data for {}",
get_atom_name(connection, incr_data.target_type) get_atom_name(connection, incr_data.target_type)
); );
incr_data.start = incr_end;
} }
Some(true) Some(true)
} }
@ -483,8 +485,8 @@ impl<T: SelectionType> SelectionDataImpl for SelectionData<T> {
get_atom_name(connection, target.atom) get_atom_name(connection, target.atom)
); );
*incr_data = Some(WaylandIncrInfo { *incr_data = Some(WaylandIncrInfo {
range: 0..data.len(),
data, data,
start: 0,
target_window: request.requestor(), target_window: request.requestor(),
property: request.property(), property: request.property(),
target_type: target.atom, target_type: target.atom,