use map_or and map_or_else

This commit is contained in:
Emil Ernerfeldt 2021-10-20 16:33:59 +02:00
parent a0cd41755e
commit 40445c450c
8 changed files with 26 additions and 41 deletions

View file

@ -367,8 +367,7 @@ impl<'open> Window<'open> {
}
})
})
.map(|ir| (Some(ir.inner), Some(ir.response)))
.unwrap_or((None, None));
.map_or((None, None), |ir| (Some(ir.inner), Some(ir.response)));
let outer_rect = frame.end(&mut area_content_ui).rect;
paint_resize_corner(&mut area_content_ui, &possible, outer_rect, frame_stroke);

View file

@ -139,8 +139,7 @@ impl InputState {
// `raw.zoom_delta` which is based on the `ctrl-scroll` event which, in turn, may be
// synthesized from an original touch gesture.
self.multi_touch()
.map(|touch| touch.zoom_delta)
.unwrap_or(self.raw.zoom_delta)
.map_or(self.raw.zoom_delta, |touch| touch.zoom_delta)
}
/// 2D non-proportional zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture).
@ -162,9 +161,10 @@ impl InputState {
// the distances of the finger tips. It is therefore potentially more accurate than
// `raw.zoom_delta` which is based on the `ctrl-scroll` event which, in turn, may be
// synthesized from an original touch gesture.
self.multi_touch()
.map(|touch| touch.zoom_delta_2d)
.unwrap_or_else(|| Vec2::splat(self.raw.zoom_delta))
self.multi_touch().map_or_else(
|| Vec2::splat(self.raw.zoom_delta),
|touch| touch.zoom_delta_2d,
)
}
pub fn wants_repaint(&self) -> bool {

View file

@ -658,9 +658,15 @@ impl<'t> TextEdit<'t> {
if ui.memory().has_focus(id) && interactive {
ui.memory().lock_focus(id, lock_focus);
let mut cursorp = state
.cursorp
.map(|cursorp| {
let mut cursorp = state.cursorp.map_or_else(
|| {
if cursor_at_end {
CursorPair::one(galley.end())
} else {
CursorPair::default()
}
},
|cursorp| {
// We only keep the PCursor (paragraph number, and character offset within that paragraph).
// This is so what if we resize the `TextEdit` region, and text wrapping changes,
// we keep the same byte character offset from the beginning of the text,
@ -672,14 +678,8 @@ impl<'t> TextEdit<'t> {
primary: galley.from_pcursor(cursorp.primary.pcursor),
secondary: galley.from_pcursor(cursorp.secondary.pcursor),
}
})
.unwrap_or_else(|| {
if cursor_at_end {
CursorPair::one(galley.end())
} else {
CursorPair::default()
}
});
},
);
// We feed state to the undoer both before and after handling input
// so that the undoer creates automatic saves even when there are no events for a while.

View file

@ -150,8 +150,7 @@ fn remove_leading_indentation(code: &str) -> String {
let start = first_line_indent.min(indent);
let end = code
.find('\n')
.map(|endline| endline + 1)
.unwrap_or_else(|| code.len());
.map_or_else(|| code.len(), |endline| endline + 1);
out += &code[start..end];
code = &code[end..];
}

View file

@ -49,11 +49,7 @@ impl super::View for MultiTouch {
ui.separator();
ui.label("Try touch gestures Pinch/Stretch, Rotation, and Pressure with 2+ fingers.");
let num_touches = ui
.input()
.multi_touch()
.map(|mt| mt.num_touches)
.unwrap_or(0);
let num_touches = ui.input().multi_touch().map_or(0, |mt| mt.num_touches);
ui.label(format!("Current touches: {}", num_touches));
Frame::dark_canvas(ui.style()).show(ui, |ui| {

View file

@ -28,10 +28,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
while !text.is_empty() {
if start_of_line && text.starts_with("```") {
let end = text
.find("\n```")
.map(|i| i + 4)
.unwrap_or_else(|| text.len());
let end = text.find("\n```").map_or_else(|| text.len(), |i| i + 4);
job.append(
&text[..end],
0.0,
@ -52,8 +49,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
style.code = true;
let end = text[1..]
.find(&['`', '\n'][..])
.map(|i| i + 2)
.unwrap_or_else(|| text.len());
.map_or_else(|| text.len(), |i| i + 2);
job.append(&text[..end], 0.0, format_from_style(visuals, &style));
text = &text[end..];
style.code = false;
@ -112,12 +108,10 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
// Swallow everything up to the next special character:
let line_end = text[skip..]
.find('\n')
.map(|i| (skip + i + 1))
.unwrap_or_else(|| text.len());
.map_or_else(|| text.len(), |i| (skip + i + 1));
let end = text[skip..]
.find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '['][..])
.map(|i| (skip + i).max(1)) // make sure we swallow at least one character
.unwrap_or_else(|| text.len());
.map_or_else(|| text.len(), |i| (skip + i).max(1));
if line_end <= end {
job.append(&text[..line_end], 0.0, format_from_style(visuals, &style));

View file

@ -319,8 +319,7 @@ impl<'a> Iterator for Parser<'a> {
let end = self
.s
.find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '[', '\n'][..])
.map(|special| special.max(1)) // make sure we swallow at least one character
.unwrap_or_else(|| self.s.len());
.map_or_else(|| self.s.len(), |special| special.max(1));
let item = Item::Text(self.style, &self.s[..end]);
self.s = &self.s[end..];

View file

@ -416,8 +416,7 @@ impl Highligher {
} else if text.starts_with(|c: char| c.is_ascii_alphanumeric()) {
let end = text[1..]
.find(|c: char| !c.is_ascii_alphanumeric())
.map(|i| i + 1)
.unwrap_or_else(|| text.len());
.map_or_else(|| text.len(), |i| i + 1);
let word = &text[..end];
let tt = if is_keyword(word) {
TokenType::Keyword
@ -429,8 +428,7 @@ impl Highligher {
} else if text.starts_with(|c: char| c.is_ascii_whitespace()) {
let end = text[1..]
.find(|c: char| !c.is_ascii_whitespace())
.map(|i| i + 1)
.unwrap_or_else(|| text.len());
.map_or_else(|| text.len(), |i| i + 1);
job.append(&text[..end], 0.0, theme.formats[TokenType::Whitespace]);
text = &text[end..];
} else {