Skip to content

Commit

Permalink
Make access_position_from_offset return an Option so it doesn't have …
Browse files Browse the repository at this point in the history
…to panic in release builds
  • Loading branch information
mwcampbell committed Oct 16, 2024
1 parent 0f80d2e commit d0107d2
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions masonry/src/text/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ impl<T: Selectable> TextWithSelection<T> {
self.layout.draw(scene, point);
}

fn access_position_from_offset(&self, offset: usize, affinity: Affinity) -> TextPosition {
fn access_position_from_offset(
&self,
offset: usize,
affinity: Affinity,
) -> Option<TextPosition> {
let text = self.text().as_ref();
debug_assert!(offset <= text.len(), "offset out of range");

Expand Down Expand Up @@ -300,21 +304,26 @@ impl<T: Selectable> TextWithSelection<T> {
let mut length_sum = 0_usize;
for (character_index, length) in character_lengths.iter().copied().enumerate() {
if run_offset == length_sum {
return TextPosition {
return Some(TextPosition {
node: id,
character_index,
};
});
}
length_sum += length as usize;
}
return TextPosition {
return Some(TextPosition {
node: id,
character_index: character_lengths.len(),
};
});
}
}

panic!("offset not within the range of any run");
debug_panic!(
"offset {} not within the range of any run; text length: {}",
offset,
text.len()
);
None
}

pub fn accessibility(&mut self, update: &mut TreeUpdate, parent_node: &mut NodeBuilder) {
Expand All @@ -324,10 +333,15 @@ impl<T: Selectable> TextWithSelection<T> {
} else {
Affinity::Downstream
};
let anchor = self.access_position_from_offset(self.selection.anchor, anchor_affinity);
let focus =
self.access_position_from_offset(self.selection.active, self.selection.active_affinity);
parent_node.set_text_selection(TextSelection { anchor, focus });
if let Some(anchor) =
self.access_position_from_offset(self.selection.anchor, anchor_affinity)
{
if let Some(focus) = self
.access_position_from_offset(self.selection.active, self.selection.active_affinity)
{
parent_node.set_text_selection(TextSelection { anchor, focus });
}
}
parent_node.add_action(accesskit::Action::SetTextSelection);
}

Expand Down

0 comments on commit d0107d2

Please sign in to comment.