Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 58 additions & 26 deletions crates/nsh-base/src/rich_editor/view/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ fn rect(b: Bounds<Points>) -> View {
/// The editor.
pub(crate) fn render(rv: Rv) -> impl IntoElement {
let (scroll_rv, layout_rv) = (rv.clone(), rv.clone());
let desktop = is_desktop();
// On a touch screen the selection's handles hang their knobs past the
// text, above the first line and below the last: the root does not
// clip them (the viewport inside it clips the text), as iOS's and
// Android's text views let their handles out of their bounds.
let root = part(
view()
.relative()
.overflow_hidden()
.when(desktop, |v| v.overflow_hidden())
.text_color(move || theme().foreground),
&rv,
Part::Root,
Expand All @@ -107,35 +112,65 @@ pub(crate) fn render(rv: Rv) -> impl IntoElement {
// the root insets both together and the sink stays in the viewport's
// coordinates. Both use `flex_auto` so an editor without a height is as
// tall as its document, and past its `max-height` it scrolls.
let viewport = view()
.relative()
.flex_auto()
.overflow_hidden()
// On a touch screen the root does not clip: the viewport clips to
// its rounded corners instead.
.when(!desktop, |v| v.parent_corners())
.child(
scroll()
.element_ref(&rv.scroll)
.flex_auto()
// The editor's own taps keep its keyboard (Android's scroll
// views dismiss it on a tap otherwise).
.keyboard_taps(KeyboardTaps::Always)
.on_scroll(move |e: &ScrollEvent| {
scroll_rv.scroll_y.set(e.offset.y);
scroll_rv.scrolled.set(e.offset.y);
scroll_rv.viewport.set(e.size);
})
.on_layout(move |b: &Bounds<Points>| {
layout_rv.viewport.set(b.size);
// The viewport moving moves the content on the page
// with no layout of the content's own.
layout_rv.place();
})
.child(content(rv.clone())),
)
.child(sink(rv.clone()));
if desktop {
return root.child(viewport);
}
// On a touch screen, the viewport and the handles' layer over it in a
// box that does not clip (its corners, the root's inside its borders
// and padding, pass on to the viewport).
root.child(
view()
.relative()
.flex_auto()
.overflow_hidden()
.child(
scroll()
.element_ref(&rv.scroll)
.flex_auto()
// The editor's own taps keep its keyboard (Android's scroll
// views dismiss it on a tap otherwise).
.keyboard_taps(KeyboardTaps::Always)
.on_scroll(move |e: &ScrollEvent| {
scroll_rv.scroll_y.set(e.offset.y);
scroll_rv.scrolled.set(e.offset.y);
scroll_rv.viewport.set(e.size);
})
.on_layout(move |b: &Bounds<Points>| {
layout_rv.viewport.set(b.size);
// The viewport moving moves the content on the page
// with no layout of the content's own.
layout_rv.place();
})
.child(content(rv.clone())),
)
.child(sink(rv)),
.parent_corners()
.child(viewport)
.child(handle_layer(rv)),
)
}

/// The layer the touch selection's handles are in: over the viewport, in
/// the root rather than in the scroll view, so neither the scroll view nor
/// the box around it cuts the knobs hanging above the first line and below
/// the last. The handles follow the scrolling themselves
/// ([`super::touch::handles`]).
fn handle_layer(rv: Rv) -> impl IntoElement {
view()
.absolute()
.inset_0()
.pointer_events(PointerEvents::BoxNone)
// The touch selection's handles take touches of their own.
.child(super::touch::handles(rv.clone()))
.child(super::touch::caret_handles(rv))
}

/// The content box: what presses and drags select in.
fn content(rv: Rv) -> impl IntoElement {
let desktop = is_desktop();
Expand Down Expand Up @@ -276,9 +311,6 @@ fn content(rv: Rv) -> impl IntoElement {
.child(gap_cursor(rv.clone()))
.child(drop_cursor(rv.clone()))
.child(caret(rv.clone()))
// The touch selection's handles take touches of their own.
.child(super::touch::handles(rv.clone()))
.child(super::touch::caret_handles(rv.clone()))
.child(content_layer(rv.clone()))
.child(bubble_menu(rv))
}
Expand Down
46 changes: 44 additions & 2 deletions crates/nsh-base/src/rich_editor/view/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,32 @@ pub(crate) fn handle_box(edge: Edge, caret: Bounds<Points>) -> Bounds<Points> {
)
}

/// `handle` (a handle's hit area in the content box, as [`handle_box`]
/// makes it for `edge`) in the viewport, `scroll_y` scrolled down a
/// `viewport` tall: `None` once the middle of the end's line is out of
/// it, so a handle goes with its line as the text scrolls away instead of
/// being drawn over what is around the editor.
pub(crate) fn handle_in_viewport(
edge: Edge,
handle: Bounds<Points>,
scroll_y: Points,
viewport: Size<Points>,
) -> Option<Bounds<Points>> {
let line = handle.size.height - px(KNOB_ROOM);
let line_top = match edge {
Edge::Start => handle.origin.y + px(KNOB_ROOM),
Edge::End => handle.origin.y,
} - scroll_y;
let middle = line_top + line / 2.0;
if middle < px(0.0) || middle > viewport.height {
return None;
}
Some(Bounds::new(
Point::new(handle.origin.x, handle.origin.y - scroll_y),
handle.size,
))
}

/// `selection`'s box grown by the room the handles' knobs hang in, above
/// it and below it: what the edit menu keeps clear of.
pub(crate) fn menu_bounds_around_handles(selection: Bounds<Points>) -> Bounds<Points> {
Expand Down Expand Up @@ -942,7 +968,8 @@ pub(crate) fn caret_handles(rv: Rv) -> impl IntoElement {
}

/// The insertion handle under the caret (Android): a hit area below the
/// caret's line holding the knob (the `selectionHandle` part).
/// caret's line holding the knob (the `selectionHandle` part), placed in
/// the viewport (the editor's handle layer).
fn caret_handle(rv: Rv) -> impl IntoElement {
let at = {
let rv = rv.clone();
Expand All @@ -951,7 +978,9 @@ fn caret_handle(rv: Rv) -> impl IntoElement {
rv.layout_rev.get();
rv.doc_rev.get();
rv.touch.rev.get();
let (scroll_y, viewport) = (rv.scroll_y.get(), rv.viewport.get());
rv.caret_handle_at()
.and_then(|b| handle_in_viewport(Edge::End, b, scroll_y, viewport))
})
};
let height = move || Length::from(at.get().map_or(px(0.0), |b| b.size.height - px(KNOB_ROOM)));
Expand Down Expand Up @@ -979,6 +1008,11 @@ fn caret_handle(rv: Rv) -> impl IntoElement {
.top(move || Length::from(at.get().map_or(px(0.0), |b| b.origin.y)))
.w(px(HANDLE_WIDTH))
.opacity(move || if at.get().is_some() { 1.0_f32 } else { 0.0 })
// Hidden, it takes no touch from the text under it.
.pointer_events(move || match at.get() {
Some(_) => PointerEvents::Auto,
None => PointerEvents::None,
})
.claim_touches(TouchClaim::on_start().ask(move |request: &ClaimRequest| {
if at.get_untracked().is_some() {
request.claim();
Expand Down Expand Up @@ -1028,7 +1062,8 @@ fn stem(paint: View, height: impl Fn() -> Length + Clone + 'static) -> View {
}

/// `edge`'s handle: a hit area around the end, holding the knob (the
/// `selectionHandle` part) and, on iOS, the stem along the end's line.
/// `selectionHandle` part) and, on iOS, the stem along the end's line,
/// placed in the viewport (the editor's handle layer).
fn handle(rv: Rv, edge: Edge) -> impl IntoElement {
let at = {
let rv = rv.clone();
Expand All @@ -1037,7 +1072,9 @@ fn handle(rv: Rv, edge: Edge) -> impl IntoElement {
rv.layout_rev.get();
rv.doc_rev.get();
rv.touch.rev.get();
let (scroll_y, viewport) = (rv.scroll_y.get(), rv.viewport.get());
rv.handle_at(edge)
.and_then(|b| handle_in_viewport(edge, b, scroll_y, viewport))
})
};
let height = move || Length::from(at.get().map_or(px(0.0), |b| b.size.height - px(KNOB_ROOM)));
Expand Down Expand Up @@ -1079,6 +1116,11 @@ fn handle(rv: Rv, edge: Edge) -> impl IntoElement {
.top(move || Length::from(at.get().map_or(px(0.0), |b| b.origin.y)))
.w(px(HANDLE_WIDTH))
.opacity(move || if at.get().is_some() { 1.0_f32 } else { 0.0 })
// Hidden, it takes no touch from the text under it.
.pointer_events(move || match at.get() {
Some(_) => PointerEvents::Auto,
None => PointerEvents::None,
})
.claim_touches(TouchClaim::on_start().ask(move |request: &ClaimRequest| {
if at.get_untracked().is_some() {
request.claim();
Expand Down
Loading