1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-03 15:20:05 -04:00

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.