Fixed nearest_char_index for segments without glyphs.

Fixed warnings.
This commit is contained in:
Samuel Guerra 2023-06-15 02:02:56 -03:00
parent f887e24210
commit 8affebfa23
3 changed files with 19 additions and 15 deletions

View File

@ -10,8 +10,8 @@ pub use zero_ui_view_api::{
self, bytes_channel, AnimationsConfig, ApiExtensionId, ApiExtensionName, ApiExtensionNameError, ApiExtensionPayload,
ApiExtensionRecvError, ApiExtensions, ColorScheme, CursorIcon, Event, EventCause, FocusIndicator, FrameRequest, FrameUpdateRequest,
FrameWaitId, HeadlessOpenData, HeadlessRequest, ImageDataFormat, ImageDownscale, ImagePpi, ImageRequest, IpcBytes, IpcBytesReceiver,
IpcBytesSender, LocaleConfig, MsgDialog, MsgDialogIcon, MsgDialogResponse, MonitorInfo, RenderMode, VideoMode, ViewProcessGen,
ViewProcessOffline, WindowId as ApiWindowId, WindowRequest, WindowState, WindowStateAll, MsgDialogButtons,
IpcBytesSender, LocaleConfig, MonitorInfo, MsgDialog, MsgDialogButtons, MsgDialogIcon, MsgDialogResponse, RenderMode, VideoMode,
ViewProcessGen, ViewProcessOffline, WindowId as ApiWindowId, WindowRequest, WindowState, WindowStateAll,
};
use crate::{

View File

@ -2107,16 +2107,20 @@ impl<'a> ShapedSegment<'a> {
}
/// Gets the insert index in the string that is nearest to `x`.
pub fn nearest_char_index(&self, x: Px, full_text: &str) -> Option<usize> {
pub fn nearest_char_index(&self, x: Px, full_text: &str) -> usize {
let x = x.0 as f32;
let (i, (g, advance)) = self
let q = self
.glyphs_with_x_advance()
.flat_map(|(_, g)| g)
.enumerate()
.min_by_key(|(_, (g, _))| {
let key = (g.point.x - x).abs();
(key * 5.0) as i32
})?;
});
let (i, (g, advance)) = match q {
Some(r) => r,
None => return self.text_range().start(), // no glyphs
};
let i = self.glyphs_range().start() + i;
let mut i = self.text_range().start() + self.text.clusters[i] as usize;
@ -2125,7 +2129,7 @@ impl<'a> ShapedSegment<'a> {
i += full_text[i..=i].len();
}
Some(i)
i
}
}

View File

@ -167,14 +167,14 @@ impl Clone for LayoutText {
Self {
fonts: self.fonts.clone(),
shaped_text: self.shaped_text.clone(),
shaped_text_version: self.shaped_text_version.clone(),
shaped_text_version: self.shaped_text_version,
overlines: self.overlines.clone(),
overline_thickness: self.overline_thickness.clone(),
overline_thickness: self.overline_thickness,
strikethroughs: self.strikethroughs.clone(),
strikethrough_thickness: self.strikethrough_thickness.clone(),
strikethrough_thickness: self.strikethrough_thickness,
underlines: self.underlines.clone(),
underline_thickness: self.underline_thickness.clone(),
caret_origin: self.caret_origin.clone(),
underline_thickness: self.underline_thickness,
caret_origin: self.caret_origin,
render_info: Mutex::new(self.render_info.lock().clone()),
}
}
@ -1069,10 +1069,10 @@ pub fn layout_text(child: impl UiNode) -> impl UiNode {
.shaped_text
.nearest_line(pos.y)
.and_then(|l| l.nearest_seg(pos.x))
.and_then(|s| s.nearest_char_index(pos.x, resolved.text.text()));
.map(|s| s.nearest_char_index(pos.x, resolved.text.text()));
// TODO, snap to grapheme start
// FIX click after line break positions at 0.
// should be MOUSE_DOWN, not CLICK?
}
}
if caret_index.is_none() {
@ -1492,7 +1492,7 @@ pub fn render_text() -> impl UiNode {
{
let mut info = t.render_info.lock();
info.transform = frame.transform().clone();
info.transform = *frame.transform();
info.scale_factor = frame.scale_factor();
}
@ -1506,7 +1506,7 @@ pub fn render_text() -> impl UiNode {
{
let t = LayoutText::get();
let mut info = t.render_info.lock();
info.transform = update.transform().clone();
info.transform = *update.transform();
}
if let Some(key) = color_key {