rename z offset to layer offset

This commit is contained in:
2025-11-22 18:45:01 -05:00
parent 9deba3d9d7
commit d757e805e8
3 changed files with 22 additions and 14 deletions

View File

@@ -39,11 +39,11 @@ impl Widget for TextEdit {
let region = self.view.draw(painter);
painter.layer = base;
match &self.selection {
let size = vec2(1, self.attrs.line_height);
match self.selection {
TextSelection::None => (),
TextSelection::Pos(cursor) => {
if let Some(offset) = cursor_pos(cursor, &self.buf) {
let size = vec2(1, self.attrs.line_height);
painter.primitive_within(
RectPrimitive::color(Color::WHITE),
size.align(Align::TOP_LEFT).offset(offset).within(&region),
@@ -51,19 +51,17 @@ impl Widget for TextEdit {
}
}
TextSelection::Span { start, end } => {
let (start, end) = sort_cursors(*start, *end);
let line_height = self.attrs.line_height;
for (top_left, width) in iter_layout_lines(&self.buf, &start, &end) {
let (start, end) = sort_cursors(start, end);
for (top_left, width) in iter_layout_lines(start, end, &self.buf) {
painter.primitive_within(
RectPrimitive::color(Color::SKY),
vec2(width, line_height)
size.with_x(width)
.align(Align::TOP_LEFT)
.offset(top_left)
.within(&region),
);
}
if let Some(end_offset) = cursor_pos(&end, &self.buf) {
let size = vec2(1, self.attrs.line_height);
if let Some(end_offset) = cursor_pos(end, &self.buf) {
painter.primitive_within(
RectPrimitive::color(Color::WHITE),
size.align(Align::TOP_LEFT)
@@ -86,11 +84,11 @@ impl Widget for TextEdit {
/// provides top left + width
fn iter_layout_lines(
start: Cursor,
end: Cursor,
buf: &TextBuffer,
start: &Cursor,
end: &Cursor,
) -> impl Iterator<Item = (Vec2, f32)> {
gen {
gen move {
let mut iter = buf.layout_runs();
for line in iter.by_ref() {
if line.line_i == start.line
@@ -145,7 +143,7 @@ fn index_x(run: &LayoutRun, index: usize) -> Option<f32> {
}
/// returns top of line segment where cursor should visually select
fn cursor_pos(cursor: &Cursor, buf: &TextBuffer) -> Option<Vec2> {
fn cursor_pos(cursor: Cursor, buf: &TextBuffer) -> Option<Vec2> {
let mut prev = None;
for run in buf
.layout_runs()

View File

@@ -15,7 +15,7 @@ pub trait CoreWidget<W, Tag> {
fn scroll(self) -> impl WidgetIdFn<Scroll>;
fn masked(self) -> impl WidgetFn<Masked>;
fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack>;
fn z_offset(self, offset: usize) -> impl WidgetFn<Stack>;
fn layer_offset(self, offset: usize) -> impl WidgetFn<Stack>;
}
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
@@ -121,7 +121,7 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
}
}
fn z_offset(self, offset: usize) -> impl WidgetFn<Stack> {
fn layer_offset(self, offset: usize) -> impl WidgetFn<Stack> {
move |ui| Stack {
children: vec![self.add(ui).any()],
size: StackSize::Child(0),

View File

@@ -56,6 +56,16 @@ impl Vec2 {
pub const fn tuple(&self) -> (f32, f32) {
(self.x, self.y)
}
pub const fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
}
pub const fn with_y(mut self, y: f32) -> Self {
self.y = y;
self
}
}
impl<T: const UiNum + Copy> const From<T> for Vec2 {