IDC FINALLY OH MY GOD (I think like ctx + resize propagation + some other stuff)
This commit is contained in:
@@ -15,7 +15,7 @@ impl Widget for Image {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn image(image: impl LoadableImage) -> impl WidgetFn<Image> {
|
||||
pub fn image<Ctx>(image: impl LoadableImage) -> impl WidgetFn<Image, Ctx> {
|
||||
let image = image.get_image().expect("Failed to load image");
|
||||
move |ui| Image {
|
||||
handle: ui.add_texture(image),
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub trait Sensable<W, Tag> {
|
||||
fn on(self, sense: Senses, f: impl SenseFn) -> impl WidgetIdFn<W>;
|
||||
pub trait Sensable<W, Ctx, Tag> {
|
||||
fn on(self, sense: Senses, f: impl SenseFn<Ctx>) -> impl WidgetIdFn<W, Ctx>;
|
||||
fn id_on(
|
||||
self,
|
||||
senses: Senses,
|
||||
f: impl FnMut(&WidgetId<W>, &mut Ui, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W>
|
||||
f: impl FnMut(&WidgetId<W>, &mut Ctx, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
where
|
||||
W: Widget;
|
||||
fn edit_on(
|
||||
self,
|
||||
senses: Senses,
|
||||
f: impl FnMut(&mut W, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W>
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
where
|
||||
W: Widget;
|
||||
W: Widget,
|
||||
Ctx: UiCtx;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
|
||||
fn on(self, senses: Senses, f: impl SenseFn) -> impl WidgetIdFn<W::Widget> {
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
|
||||
fn on(self, senses: Senses, f: impl SenseFn<Ctx>) -> impl WidgetIdFn<W::Widget, Ctx> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
ui.add_sensor(
|
||||
@@ -35,24 +36,25 @@ impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
|
||||
fn id_on(
|
||||
self,
|
||||
senses: Senses,
|
||||
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget>
|
||||
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ctx, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
{
|
||||
self.with_id(move |ui, id| {
|
||||
let id2 = id.clone();
|
||||
ui.add(id.on(senses, move |ui, pos| f(&id2, ui, pos)))
|
||||
id.on(senses, move |ctx, pos| f(&id2, ctx, pos)).add(ui)
|
||||
})
|
||||
}
|
||||
fn edit_on(
|
||||
self,
|
||||
senses: Senses,
|
||||
mut f: impl FnMut(&mut W::Widget, SenseCtx) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget>
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
Ctx: UiCtx,
|
||||
{
|
||||
self.id_on(senses, move |id, ui, pos| f(&mut ui[id], pos))
|
||||
self.id_on(senses, move |id, ctx, pos| f(&mut ctx.ui()[id], pos))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ use crate::prelude::*;
|
||||
pub struct Text {
|
||||
pub content: String,
|
||||
pub attrs: TextAttrs,
|
||||
/// inner alignment of text region (within where its drawn)
|
||||
pub align: Align,
|
||||
buf: TextBuffer,
|
||||
cursor: Cursor,
|
||||
@@ -40,17 +41,109 @@ impl Text {
|
||||
size: Vec2::ZERO,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(&mut self, pos: Vec2, size: Vec2) {
|
||||
let pos = pos - self.region().top_left.to_size(size);
|
||||
let Some(cursor) = self.buf.hit(pos.x, pos.y) else {
|
||||
return;
|
||||
};
|
||||
self.cursor = Cursor::Select {
|
||||
line: cursor.line,
|
||||
idx: cursor.index,
|
||||
line: cursor.line as isize,
|
||||
col: cursor.index as isize,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deselect(&mut self) {
|
||||
self.cursor = Cursor::None;
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, text: &str) {
|
||||
let i = self.update_cursor();
|
||||
self.content.insert_str(i, text);
|
||||
|
||||
match &mut self.cursor {
|
||||
Cursor::None => (),
|
||||
Cursor::Select { line, col } => {
|
||||
*col += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backspace(&mut self) {
|
||||
if let Some(i) = self.update_cursor().checked_sub(1) {
|
||||
self.content.remove(i);
|
||||
match &mut self.cursor {
|
||||
Cursor::None => (),
|
||||
Cursor::Select { line, col } => {
|
||||
*col -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete(&mut self) {
|
||||
let i = self.update_cursor();
|
||||
if i != self.content.len() {
|
||||
self.content.remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_cursor(&mut self, dir: Dir) {
|
||||
if let Cursor::Select { line, col } = &mut self.cursor {
|
||||
match dir {
|
||||
Dir::LEFT => *col -= 1,
|
||||
Dir::RIGHT => *col += 1,
|
||||
Dir::UP => *line -= 1,
|
||||
Dir::DOWN => *line += 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_cursor(&mut self) -> usize {
|
||||
match &mut self.cursor {
|
||||
Cursor::None => 0,
|
||||
Cursor::Select { line, col } => {
|
||||
if *col < 0 {
|
||||
*line -= 1;
|
||||
}
|
||||
if *line < 0 {
|
||||
*line = 0;
|
||||
*col = 0;
|
||||
}
|
||||
let mut idx = self.content.len();
|
||||
let mut l = 0;
|
||||
let mut c = 0;
|
||||
let mut cur_len = 0;
|
||||
for (i, ch) in self.content.chars().enumerate() {
|
||||
if ch == '\n' {
|
||||
l += 1;
|
||||
c = 0;
|
||||
} else {
|
||||
if l == *line {
|
||||
cur_len = i as isize + 1;
|
||||
if c == *col {
|
||||
idx = i;
|
||||
}
|
||||
}
|
||||
c += 1;
|
||||
}
|
||||
}
|
||||
if *col < 0 {
|
||||
*col = cur_len;
|
||||
}
|
||||
if *col > cur_len {
|
||||
*col = 0;
|
||||
*line += 1;
|
||||
}
|
||||
if *line > l {
|
||||
*line = l;
|
||||
*col = cur_len;
|
||||
}
|
||||
idx
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
UiRegion::from_size_align(self.size, self.align)
|
||||
}
|
||||
@@ -58,6 +151,7 @@ impl Text {
|
||||
|
||||
impl Widget for Text {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
self.update_cursor();
|
||||
let (handle, offset) =
|
||||
painter.render_text(&mut self.buf, &self.content, &self.attrs, &self.cursor);
|
||||
let dims = handle.size();
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
use super::*;
|
||||
use crate::prelude::*;
|
||||
|
||||
pub trait CoreWidget<W, Tag> {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded>;
|
||||
fn align(self, align: Align) -> impl WidgetFn<Aligned>;
|
||||
fn center(self) -> impl WidgetFn<Aligned>;
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W>;
|
||||
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized>;
|
||||
pub trait CoreWidget<W, Ctx, Tag> {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded, Ctx>;
|
||||
fn align(self, align: Align) -> impl WidgetFn<Aligned, Ctx>;
|
||||
fn center(self) -> impl WidgetFn<Aligned, Ctx>;
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W, Ctx>;
|
||||
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized, Ctx>;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded> {
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> CoreWidget<W::Widget, Ctx, Tag> for W {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded, Ctx> {
|
||||
|ui| Padded {
|
||||
padding: padding.into(),
|
||||
inner: self.add(ui).erase_type(),
|
||||
inner: self.add(ui).any(),
|
||||
}
|
||||
}
|
||||
|
||||
fn align(self, align: Align) -> impl WidgetFn<Aligned> {
|
||||
fn align(self, align: Align) -> impl WidgetFn<Aligned, Ctx> {
|
||||
move |ui| Aligned {
|
||||
inner: self.add(ui).erase_type(),
|
||||
inner: self.add(ui).any(),
|
||||
align,
|
||||
}
|
||||
}
|
||||
|
||||
fn center(self) -> impl WidgetFn<Aligned> {
|
||||
fn center(self) -> impl WidgetFn<Aligned, Ctx> {
|
||||
self.align(Align::Center)
|
||||
}
|
||||
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W::Widget> {
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W::Widget, Ctx> {
|
||||
|ui| {
|
||||
let id = self.add(ui);
|
||||
ui.set_label(&id, label.into());
|
||||
@@ -36,28 +36,28 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
}
|
||||
}
|
||||
|
||||
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized> {
|
||||
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized, Ctx> {
|
||||
move |ui| Sized {
|
||||
inner: self.add(ui).erase_type(),
|
||||
inner: self.add(ui).any(),
|
||||
size: size.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CoreWidgetArr<const LEN: usize, Tag> {
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span>;
|
||||
fn stack(self) -> impl WidgetFn<Stack>;
|
||||
pub trait CoreWidgetArr<const LEN: usize, Ctx, Tag> {
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span, Ctx>;
|
||||
fn stack(self) -> impl WidgetFn<Stack, Ctx>;
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Tag> for Wa {
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span> {
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Ctx, Tag>, Ctx, Tag> CoreWidgetArr<LEN, Ctx, Tag> for Wa {
|
||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span, Ctx> {
|
||||
let lengths = lengths.into_lens();
|
||||
move |ui| Span {
|
||||
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
|
||||
dir,
|
||||
}
|
||||
}
|
||||
fn stack(self) -> impl WidgetFn<Stack> {
|
||||
fn stack(self) -> impl WidgetFn<Stack, Ctx> {
|
||||
move |ui| Stack {
|
||||
children: self.ui(ui).arr.to_vec(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user