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))) .map_or((None, None), |ir| (Some(ir.inner), Some(ir.response)));
.unwrap_or((None, None));
let outer_rect = frame.end(&mut area_content_ui).rect; let outer_rect = frame.end(&mut area_content_ui).rect;
paint_resize_corner(&mut area_content_ui, &possible, outer_rect, frame_stroke); 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 // `raw.zoom_delta` which is based on the `ctrl-scroll` event which, in turn, may be
// synthesized from an original touch gesture. // synthesized from an original touch gesture.
self.multi_touch() self.multi_touch()
.map(|touch| touch.zoom_delta) .map_or(self.raw.zoom_delta, |touch| touch.zoom_delta)
.unwrap_or(self.raw.zoom_delta)
} }
/// 2D non-proportional zoom scale factor this frame (e.g. from ctrl-scroll or pinch gesture). /// 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 // 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 // `raw.zoom_delta` which is based on the `ctrl-scroll` event which, in turn, may be
// synthesized from an original touch gesture. // synthesized from an original touch gesture.
self.multi_touch() self.multi_touch().map_or_else(
.map(|touch| touch.zoom_delta_2d) || Vec2::splat(self.raw.zoom_delta),
.unwrap_or_else(|| Vec2::splat(self.raw.zoom_delta)) |touch| touch.zoom_delta_2d,
)
} }
pub fn wants_repaint(&self) -> bool { pub fn wants_repaint(&self) -> bool {

View file

@ -658,9 +658,15 @@ impl<'t> TextEdit<'t> {
if ui.memory().has_focus(id) && interactive { if ui.memory().has_focus(id) && interactive {
ui.memory().lock_focus(id, lock_focus); ui.memory().lock_focus(id, lock_focus);
let mut cursorp = state let mut cursorp = state.cursorp.map_or_else(
.cursorp || {
.map(|cursorp| { 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). // 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, // 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, // 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), primary: galley.from_pcursor(cursorp.primary.pcursor),
secondary: galley.from_pcursor(cursorp.secondary.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 // 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. // 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 start = first_line_indent.min(indent);
let end = code let end = code
.find('\n') .find('\n')
.map(|endline| endline + 1) .map_or_else(|| code.len(), |endline| endline + 1);
.unwrap_or_else(|| code.len());
out += &code[start..end]; out += &code[start..end];
code = &code[end..]; code = &code[end..];
} }

View file

@ -49,11 +49,7 @@ impl super::View for MultiTouch {
ui.separator(); ui.separator();
ui.label("Try touch gestures Pinch/Stretch, Rotation, and Pressure with 2+ fingers."); ui.label("Try touch gestures Pinch/Stretch, Rotation, and Pressure with 2+ fingers.");
let num_touches = ui let num_touches = ui.input().multi_touch().map_or(0, |mt| mt.num_touches);
.input()
.multi_touch()
.map(|mt| mt.num_touches)
.unwrap_or(0);
ui.label(format!("Current touches: {}", num_touches)); ui.label(format!("Current touches: {}", num_touches));
Frame::dark_canvas(ui.style()).show(ui, |ui| { 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() { while !text.is_empty() {
if start_of_line && text.starts_with("```") { if start_of_line && text.starts_with("```") {
let end = text let end = text.find("\n```").map_or_else(|| text.len(), |i| i + 4);
.find("\n```")
.map(|i| i + 4)
.unwrap_or_else(|| text.len());
job.append( job.append(
&text[..end], &text[..end],
0.0, 0.0,
@ -52,8 +49,7 @@ pub fn highlight_easymark(visuals: &egui::Visuals, mut text: &str) -> egui::text
style.code = true; style.code = true;
let end = text[1..] let end = text[1..]
.find(&['`', '\n'][..]) .find(&['`', '\n'][..])
.map(|i| i + 2) .map_or_else(|| text.len(), |i| i + 2);
.unwrap_or_else(|| text.len());
job.append(&text[..end], 0.0, format_from_style(visuals, &style)); job.append(&text[..end], 0.0, format_from_style(visuals, &style));
text = &text[end..]; text = &text[end..];
style.code = false; 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: // Swallow everything up to the next special character:
let line_end = text[skip..] let line_end = text[skip..]
.find('\n') .find('\n')
.map(|i| (skip + i + 1)) .map_or_else(|| text.len(), |i| (skip + i + 1));
.unwrap_or_else(|| text.len());
let end = text[skip..] let end = text[skip..]
.find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '['][..]) .find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '['][..])
.map(|i| (skip + i).max(1)) // make sure we swallow at least one character .map_or_else(|| text.len(), |i| (skip + i).max(1));
.unwrap_or_else(|| text.len());
if line_end <= end { if line_end <= end {
job.append(&text[..line_end], 0.0, format_from_style(visuals, &style)); 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 let end = self
.s .s
.find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '[', '\n'][..]) .find(&['*', '`', '~', '_', '/', '$', '^', '\\', '<', '[', '\n'][..])
.map(|special| special.max(1)) // make sure we swallow at least one character .map_or_else(|| self.s.len(), |special| special.max(1));
.unwrap_or_else(|| self.s.len());
let item = Item::Text(self.style, &self.s[..end]); let item = Item::Text(self.style, &self.s[..end]);
self.s = &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()) { } else if text.starts_with(|c: char| c.is_ascii_alphanumeric()) {
let end = text[1..] let end = text[1..]
.find(|c: char| !c.is_ascii_alphanumeric()) .find(|c: char| !c.is_ascii_alphanumeric())
.map(|i| i + 1) .map_or_else(|| text.len(), |i| i + 1);
.unwrap_or_else(|| text.len());
let word = &text[..end]; let word = &text[..end];
let tt = if is_keyword(word) { let tt = if is_keyword(word) {
TokenType::Keyword TokenType::Keyword
@ -429,8 +428,7 @@ impl Highligher {
} else if text.starts_with(|c: char| c.is_ascii_whitespace()) { } else if text.starts_with(|c: char| c.is_ascii_whitespace()) {
let end = text[1..] let end = text[1..]
.find(|c: char| !c.is_ascii_whitespace()) .find(|c: char| !c.is_ascii_whitespace())
.map(|i| i + 1) .map_or_else(|| text.len(), |i| i + 1);
.unwrap_or_else(|| text.len());
job.append(&text[..end], 0.0, theme.formats[TokenType::Whitespace]); job.append(&text[..end], 0.0, theme.formats[TokenType::Whitespace]);
text = &text[end..]; text = &text[end..];
} else { } else {