refactor project structure (start of redoing atomic branch without atomics)
This commit is contained in:
0
src/widget/event.rs
Normal file
0
src/widget/event.rs
Normal file
55
src/widget/image.rs
Normal file
55
src/widget/image.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use crate::prelude::*;
|
||||
use image::DynamicImage;
|
||||
|
||||
pub struct Image {
|
||||
handle: TextureHandle,
|
||||
}
|
||||
|
||||
impl Widget for Image {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.texture(&self.handle);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
|
||||
Len::abs(self.handle.size().x)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
|
||||
Len::abs(self.handle.size().y)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn image(image: impl LoadableImage) -> impl WidgetFn<Image> {
|
||||
let image = image.get_image().expect("Failed to load image");
|
||||
move |ui| Image {
|
||||
handle: ui.add_texture(image),
|
||||
}
|
||||
}
|
||||
|
||||
pub trait LoadableImage {
|
||||
fn get_image(self) -> Result<DynamicImage, String>;
|
||||
}
|
||||
|
||||
impl LoadableImage for &str {
|
||||
fn get_image(self) -> Result<DynamicImage, String> {
|
||||
image::open(self).map_err(|e| format!("{e:?}"))
|
||||
}
|
||||
}
|
||||
|
||||
impl LoadableImage for String {
|
||||
fn get_image(self) -> Result<DynamicImage, String> {
|
||||
image::open(self).map_err(|e| format!("{e:?}"))
|
||||
}
|
||||
}
|
||||
|
||||
impl<const LEN: usize> LoadableImage for &[u8; LEN] {
|
||||
fn get_image(self) -> Result<DynamicImage, String> {
|
||||
image::load_from_memory(self).map_err(|e| format!("{e:?}"))
|
||||
}
|
||||
}
|
||||
|
||||
impl LoadableImage for DynamicImage {
|
||||
fn get_image(self) -> Result<DynamicImage, String> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
20
src/widget/mask.rs
Normal file
20
src/widget/mask.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Masked {
|
||||
pub inner: WidgetId,
|
||||
}
|
||||
|
||||
impl Widget for Masked {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.set_mask(painter.region());
|
||||
painter.widget(&self.inner);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.width(&self.inner)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.height(&self.inner)
|
||||
}
|
||||
}
|
||||
17
src/widget/mod.rs
Normal file
17
src/widget/mod.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
mod image;
|
||||
mod mask;
|
||||
mod position;
|
||||
mod ptr;
|
||||
mod rect;
|
||||
mod sense;
|
||||
mod text;
|
||||
mod trait_fns;
|
||||
|
||||
pub use image::*;
|
||||
pub use mask::*;
|
||||
pub use position::*;
|
||||
pub use ptr::*;
|
||||
pub use rect::*;
|
||||
pub use sense::*;
|
||||
pub use text::*;
|
||||
pub use trait_fns::*;
|
||||
35
src/widget/position/align.rs
Normal file
35
src/widget/position/align.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Aligned {
|
||||
pub inner: WidgetId,
|
||||
pub align: Align,
|
||||
}
|
||||
|
||||
impl Widget for Aligned {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let region = match self.align.tuple() {
|
||||
(Some(x), Some(y)) => painter
|
||||
.size(&self.inner)
|
||||
.to_uivec2()
|
||||
.align(RegionAlign { x, y }),
|
||||
(Some(x), None) => {
|
||||
let x = painter.size_ctx().width(&self.inner).apply_rest().align(x);
|
||||
UiRegion::new(x, UiSpan::FULL)
|
||||
}
|
||||
(None, Some(y)) => {
|
||||
let y = painter.size_ctx().height(&self.inner).apply_rest().align(y);
|
||||
UiRegion::new(UiSpan::FULL, y)
|
||||
}
|
||||
(None, None) => UiRegion::FULL,
|
||||
};
|
||||
painter.widget_within(&self.inner, region);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.width(&self.inner)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.height(&self.inner)
|
||||
}
|
||||
}
|
||||
23
src/widget/position/layer.rs
Normal file
23
src/widget/position/layer.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct LayerOffset {
|
||||
pub inner: WidgetId,
|
||||
pub offset: usize,
|
||||
}
|
||||
|
||||
impl Widget for LayerOffset {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
for _ in 0..self.offset {
|
||||
painter.next_layer();
|
||||
}
|
||||
painter.widget(&self.inner);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.width(&self.inner)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.height(&self.inner)
|
||||
}
|
||||
}
|
||||
48
src/widget/position/max_size.rs
Normal file
48
src/widget/position/max_size.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct MaxSize {
|
||||
pub inner: WidgetId,
|
||||
pub x: Option<Len>,
|
||||
pub y: Option<Len>,
|
||||
}
|
||||
|
||||
impl MaxSize {
|
||||
fn apply_to_outer(&self, ctx: &mut SizeCtx) {
|
||||
if let Some(x) = self.x {
|
||||
ctx.outer.x.select_len(x.apply_rest());
|
||||
}
|
||||
if let Some(y) = self.y {
|
||||
ctx.outer.y.select_len(y.apply_rest());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for MaxSize {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.widget(&self.inner);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
self.apply_to_outer(ctx);
|
||||
let width = ctx.width(&self.inner);
|
||||
if let Some(x) = self.x {
|
||||
let width_px = width.apply_rest().to_abs(ctx.output_size().x);
|
||||
let x_px = x.apply_rest().to_abs(ctx.output_size().x);
|
||||
if width_px > x_px { x } else { width }
|
||||
} else {
|
||||
width
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
self.apply_to_outer(ctx);
|
||||
let height = ctx.height(&self.inner);
|
||||
if let Some(y) = self.y {
|
||||
let height_px = height.apply_rest().to_abs(ctx.output_size().y);
|
||||
let y_px = y.apply_rest().to_abs(ctx.output_size().y);
|
||||
if height_px > y_px { y } else { height }
|
||||
} else {
|
||||
height
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/widget/position/mod.rs
Normal file
19
src/widget/position/mod.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
mod align;
|
||||
mod layer;
|
||||
mod max_size;
|
||||
mod offset;
|
||||
mod pad;
|
||||
mod scroll;
|
||||
mod sized;
|
||||
mod span;
|
||||
mod stack;
|
||||
|
||||
pub use align::*;
|
||||
pub use layer::*;
|
||||
pub use max_size::*;
|
||||
pub use offset::*;
|
||||
pub use pad::*;
|
||||
pub use scroll::*;
|
||||
pub use sized::*;
|
||||
pub use span::*;
|
||||
pub use stack::*;
|
||||
21
src/widget/position/offset.rs
Normal file
21
src/widget/position/offset.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Offset {
|
||||
pub inner: WidgetId,
|
||||
pub amt: UiVec2,
|
||||
}
|
||||
|
||||
impl Widget for Offset {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let region = UiRegion::FULL.offset(self.amt);
|
||||
painter.widget_within(&self.inner, region);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.width(&self.inner)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.height(&self.inner)
|
||||
}
|
||||
}
|
||||
134
src/widget/position/pad.rs
Normal file
134
src/widget/position/pad.rs
Normal file
@@ -0,0 +1,134 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Pad {
|
||||
pub padding: Padding,
|
||||
pub inner: WidgetId,
|
||||
}
|
||||
|
||||
impl Widget for Pad {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.widget_within(&self.inner, self.padding.region());
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
let width = self.padding.left + self.padding.right;
|
||||
let height = self.padding.top + self.padding.bottom;
|
||||
ctx.outer.x.abs -= width;
|
||||
ctx.outer.y.abs -= height;
|
||||
let mut size = ctx.width(&self.inner);
|
||||
size.abs += width;
|
||||
size
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
let width = self.padding.left + self.padding.right;
|
||||
let height = self.padding.top + self.padding.bottom;
|
||||
ctx.outer.x.abs -= width;
|
||||
ctx.outer.y.abs -= height;
|
||||
let mut size = ctx.height(&self.inner);
|
||||
size.abs += height;
|
||||
size
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Padding {
|
||||
pub left: f32,
|
||||
pub right: f32,
|
||||
pub top: f32,
|
||||
pub bottom: f32,
|
||||
}
|
||||
|
||||
impl Padding {
|
||||
pub const ZERO: Self = Self {
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
top: 0.0,
|
||||
bottom: 0.0,
|
||||
};
|
||||
|
||||
pub fn uniform(amt: impl UiNum) -> Self {
|
||||
let amt = amt.to_f32();
|
||||
Self {
|
||||
left: amt,
|
||||
right: amt,
|
||||
top: amt,
|
||||
bottom: amt,
|
||||
}
|
||||
}
|
||||
pub fn region(&self) -> UiRegion {
|
||||
let mut region = UiRegion::FULL;
|
||||
region.x.start.abs += self.left;
|
||||
region.y.start.abs += self.top;
|
||||
region.x.end.abs -= self.right;
|
||||
region.y.end.abs -= self.bottom;
|
||||
region
|
||||
}
|
||||
pub fn x(amt: impl UiNum) -> Self {
|
||||
let amt = amt.to_f32();
|
||||
Self {
|
||||
left: amt,
|
||||
right: amt,
|
||||
top: 0.0,
|
||||
bottom: 0.0,
|
||||
}
|
||||
}
|
||||
pub fn y(amt: impl UiNum) -> Self {
|
||||
let amt = amt.to_f32();
|
||||
Self {
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
top: amt,
|
||||
bottom: amt,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn top(amt: impl UiNum) -> Self {
|
||||
let mut s = Self::ZERO;
|
||||
s.top = amt.to_f32();
|
||||
s
|
||||
}
|
||||
|
||||
pub fn bottom(amt: impl UiNum) -> Self {
|
||||
let mut s = Self::ZERO;
|
||||
s.bottom = amt.to_f32();
|
||||
s
|
||||
}
|
||||
|
||||
pub fn left(amt: impl UiNum) -> Self {
|
||||
let mut s = Self::ZERO;
|
||||
s.left = amt.to_f32();
|
||||
s
|
||||
}
|
||||
|
||||
pub fn right(amt: impl UiNum) -> Self {
|
||||
let mut s = Self::ZERO;
|
||||
s.right = amt.to_f32();
|
||||
s
|
||||
}
|
||||
|
||||
pub fn with_top(mut self, amt: impl UiNum) -> Self {
|
||||
self.top = amt.to_f32();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_bottom(mut self, amt: impl UiNum) -> Self {
|
||||
self.bottom = amt.to_f32();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_left(mut self, amt: impl UiNum) -> Self {
|
||||
self.left = amt.to_f32();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_right(mut self, amt: impl UiNum) -> Self {
|
||||
self.right = amt.to_f32();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: UiNum> From<T> for Padding {
|
||||
fn from(amt: T) -> Self {
|
||||
Self::uniform(amt.to_f32())
|
||||
}
|
||||
}
|
||||
66
src/widget/position/scroll.rs
Normal file
66
src/widget/position/scroll.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Scroll {
|
||||
inner: WidgetId,
|
||||
axis: Axis,
|
||||
amt: f32,
|
||||
snap_end: bool,
|
||||
container_len: f32,
|
||||
content_len: f32,
|
||||
}
|
||||
|
||||
impl Widget for Scroll {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let output_len = painter.output_size().axis(self.axis);
|
||||
let container_len = painter.region().axis(self.axis).len();
|
||||
let content_len = painter
|
||||
.len_axis(&self.inner, self.axis)
|
||||
.apply_rest()
|
||||
.within_len(container_len)
|
||||
.to_abs(output_len);
|
||||
self.container_len = container_len.to_abs(output_len);
|
||||
self.content_len = content_len;
|
||||
|
||||
if self.snap_end {
|
||||
self.amt = self.content_len - self.container_len;
|
||||
}
|
||||
self.update_amt();
|
||||
|
||||
let mut region = UiRegion::FULL.offset(Vec2::from_axis(self.axis, -self.amt, 0.0));
|
||||
region.axis_mut(self.axis).end = region.axis(self.axis).start.offset(self.content_len);
|
||||
painter.widget_within(&self.inner, region);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.width(&self.inner)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
ctx.height(&self.inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl Scroll {
|
||||
pub fn new(inner: WidgetId, axis: Axis) -> Self {
|
||||
Self {
|
||||
inner,
|
||||
axis,
|
||||
amt: 0.0,
|
||||
snap_end: true,
|
||||
container_len: 0.0,
|
||||
content_len: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_amt(&mut self) {
|
||||
self.amt = self.amt.max(0.0);
|
||||
let len = (self.content_len - self.container_len).max(0.0);
|
||||
self.amt = self.amt.min(len);
|
||||
self.snap_end = self.amt == len;
|
||||
}
|
||||
|
||||
pub fn scroll(&mut self, amt: f32) {
|
||||
self.amt -= amt;
|
||||
self.update_amt();
|
||||
}
|
||||
}
|
||||
34
src/widget/position/sized.rs
Normal file
34
src/widget/position/sized.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Sized {
|
||||
pub inner: WidgetId,
|
||||
pub x: Option<Len>,
|
||||
pub y: Option<Len>,
|
||||
}
|
||||
|
||||
impl Sized {
|
||||
fn apply_to_outer(&self, ctx: &mut SizeCtx) {
|
||||
if let Some(x) = self.x {
|
||||
ctx.outer.x.select_len(x.apply_rest());
|
||||
}
|
||||
if let Some(y) = self.y {
|
||||
ctx.outer.y.select_len(y.apply_rest());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Sized {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.widget(&self.inner);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
self.apply_to_outer(ctx);
|
||||
self.x.unwrap_or_else(|| ctx.width(&self.inner))
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
self.apply_to_outer(ctx);
|
||||
self.y.unwrap_or_else(|| ctx.height(&self.inner))
|
||||
}
|
||||
}
|
||||
196
src/widget/position/span.rs
Normal file
196
src/widget/position/span.rs
Normal file
@@ -0,0 +1,196 @@
|
||||
use crate::prelude::*;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct Span {
|
||||
pub children: Vec<WidgetId>,
|
||||
pub dir: Dir,
|
||||
pub gap: f32,
|
||||
}
|
||||
|
||||
impl Widget for Span {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let total = self.len_sum(&mut painter.size_ctx());
|
||||
let mut start = UiScalar::rel_min();
|
||||
for child in &self.children {
|
||||
let mut span = UiSpan::FULL;
|
||||
span.start = start;
|
||||
let len = painter.len_axis(child, self.dir.axis);
|
||||
if len.rest > 0.0 {
|
||||
let offset = UiScalar::new(total.rel, total.abs);
|
||||
let rel_end = UiScalar::rel(len.rest / total.rest);
|
||||
let end = (UiScalar::rel_max() + start) - offset;
|
||||
start = rel_end.within(&start.to(end));
|
||||
}
|
||||
start.abs += len.abs;
|
||||
start.rel += len.rel;
|
||||
span.end = start;
|
||||
let mut child_region = UiRegion::from_axis(self.dir.axis, span, UiSpan::FULL);
|
||||
if self.dir.sign == Sign::Neg {
|
||||
child_region.flip(self.dir.axis);
|
||||
}
|
||||
painter.widget_within(child, child_region);
|
||||
start.abs += self.gap;
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
match self.dir.axis {
|
||||
Axis::X => self.desired_len(ctx),
|
||||
Axis::Y => self.desired_ortho(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
match self.dir.axis {
|
||||
Axis::X => self.desired_ortho(ctx),
|
||||
Axis::Y => self.desired_len(ctx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Span {
|
||||
pub fn empty(dir: Dir) -> Self {
|
||||
Self {
|
||||
children: Vec::new(),
|
||||
dir,
|
||||
gap: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gap(mut self, gap: impl UiNum) -> Self {
|
||||
self.gap = gap.to_f32();
|
||||
self
|
||||
}
|
||||
|
||||
fn len_sum(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
let gap = self.gap * self.children.len().saturating_sub(1) as f32;
|
||||
self.children.iter().fold(Len::abs(gap), |mut s, id| {
|
||||
// it's tempting to subtract the abs & rel from the ctx outer,
|
||||
// but that would create inconsistent sizing if you put
|
||||
// a rest first vs last & only speed up in one direction.
|
||||
// I think this is only solvable by restricting how you can
|
||||
// compute size, bc currently you need child to define parent's
|
||||
// sectioning and you need parent's sectioning to define child.
|
||||
// Fortunately, that doesn't matter in most cases
|
||||
let len = ctx.len_axis(id, self.dir.axis);
|
||||
s += len;
|
||||
s
|
||||
})
|
||||
}
|
||||
|
||||
fn desired_len(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
let len = self.len_sum(ctx);
|
||||
if len.rest == 0.0 && len.rel == 0.0 {
|
||||
len
|
||||
} else {
|
||||
Len::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_ortho(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
// this is a weird hack to get text wrapping to work properly when in a downward span
|
||||
// the correct solution here is to add a function to widget that lets them
|
||||
// request that ctx.outer has an axis "resolved" before checking the other,
|
||||
// and panicking or warning if two request opposite axis (unsolvable in that case)
|
||||
let outer = ctx.outer.axis(self.dir.axis);
|
||||
if self.dir.axis == Axis::X {
|
||||
// so....... this literally copies draw so that the lengths are correctly set in the
|
||||
// context, which makes this slow and not cool
|
||||
let total = self.len_sum(ctx);
|
||||
let mut start = UiScalar::rel_min();
|
||||
let mut ortho_len = Len::ZERO;
|
||||
for child in &self.children {
|
||||
let mut span = UiSpan::FULL;
|
||||
span.start = start;
|
||||
let len = ctx.len_axis(child, self.dir.axis);
|
||||
if len.rest > 0.0 {
|
||||
let offset = UiScalar::new(total.rel, total.abs);
|
||||
let rel_end = UiScalar::rel(len.rest / total.rest);
|
||||
let end = (UiScalar::rel_max() + start) - offset;
|
||||
start = rel_end.within(&start.to(end));
|
||||
}
|
||||
start.abs += len.abs;
|
||||
start.rel += len.rel;
|
||||
span.end = start;
|
||||
|
||||
let scalar = span.len();
|
||||
*ctx.outer.axis_mut(self.dir.axis) = outer.select_len(scalar);
|
||||
let ortho = ctx.len_axis(child, !self.dir.axis);
|
||||
// TODO: rel shouldn't do this, but no easy way before actually calculating pixels
|
||||
if ortho.rel > 0.0 || ortho.rest > 0.0 {
|
||||
ortho_len.rest = 1.0;
|
||||
ortho_len.abs = 0.0;
|
||||
break;
|
||||
}
|
||||
ortho_len.abs = ortho_len.abs.max(ortho.abs);
|
||||
start.abs += self.gap;
|
||||
}
|
||||
ortho_len
|
||||
} else {
|
||||
let mut ortho_len = Len::ZERO;
|
||||
let ortho = !self.dir.axis;
|
||||
for child in &self.children {
|
||||
let len = ctx.len_axis(child, ortho);
|
||||
// TODO: rel shouldn't do this, but no easy way before actually calculating pixels
|
||||
if len.rel > 0.0 || len.rest > 0.0 {
|
||||
ortho_len.rest = 1.0;
|
||||
ortho_len.abs = 0.0;
|
||||
break;
|
||||
}
|
||||
ortho_len.abs = ortho_len.abs.max(len.abs);
|
||||
}
|
||||
ortho_len
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SpanBuilder<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> {
|
||||
pub children: Wa,
|
||||
pub dir: Dir,
|
||||
pub gap: f32,
|
||||
_pd: PhantomData<Tag>,
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> FnOnce<(&mut Ui,)>
|
||||
for SpanBuilder<LEN, Wa, Tag>
|
||||
{
|
||||
type Output = Span;
|
||||
|
||||
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
|
||||
Span {
|
||||
children: self.children.ui(args.0).arr.to_vec(),
|
||||
dir: self.dir,
|
||||
gap: self.gap,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> SpanBuilder<LEN, Wa, Tag> {
|
||||
pub fn new(children: Wa, dir: Dir) -> Self {
|
||||
Self {
|
||||
children,
|
||||
dir,
|
||||
gap: 0.0,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gap(mut self, gap: impl UiNum) -> Self {
|
||||
self.gap = gap.to_f32();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for Span {
|
||||
type Target = Vec<WidgetId>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.children
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DerefMut for Span {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.children
|
||||
}
|
||||
}
|
||||
77
src/widget/position/stack.rs
Normal file
77
src/widget/position/stack.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Stack {
|
||||
pub children: Vec<WidgetId>,
|
||||
pub size: StackSize,
|
||||
}
|
||||
|
||||
impl Widget for Stack {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let mut iter = self.children.iter();
|
||||
if let Some(child) = iter.next() {
|
||||
painter.child_layer();
|
||||
painter.widget(child);
|
||||
}
|
||||
for child in iter {
|
||||
painter.next_layer();
|
||||
painter.widget(child);
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
match self.size {
|
||||
StackSize::Default => Len::default(),
|
||||
StackSize::Child(i) => ctx.width(&self.children[i]),
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
match self.size {
|
||||
StackSize::Default => Len::default(),
|
||||
StackSize::Child(i) => ctx.height(&self.children[i]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub enum StackSize {
|
||||
#[default]
|
||||
Default,
|
||||
Child(usize),
|
||||
}
|
||||
|
||||
pub struct StackBuilder<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> {
|
||||
pub children: Wa,
|
||||
pub size: StackSize,
|
||||
_pd: PhantomData<Tag>,
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> FnOnce<(&mut Ui,)>
|
||||
for StackBuilder<LEN, Wa, Tag>
|
||||
{
|
||||
type Output = Stack;
|
||||
|
||||
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
|
||||
Stack {
|
||||
children: self.children.ui(args.0).arr.to_vec(),
|
||||
size: self.size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> StackBuilder<LEN, Wa, Tag> {
|
||||
pub fn new(children: Wa) -> Self {
|
||||
Self {
|
||||
children,
|
||||
size: StackSize::default(),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size(mut self, size: StackSize) -> Self {
|
||||
self.size = size;
|
||||
self
|
||||
}
|
||||
}
|
||||
30
src/widget/ptr.rs
Normal file
30
src/widget/ptr.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WidgetPtr {
|
||||
pub inner: Option<WidgetId>,
|
||||
}
|
||||
|
||||
impl Widget for WidgetPtr {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
if let Some(id) = &self.inner {
|
||||
painter.widget(id);
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
if let Some(id) = &self.inner {
|
||||
ctx.width(id)
|
||||
} else {
|
||||
Len::ZERO
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
if let Some(id) = &self.inner {
|
||||
ctx.height(id)
|
||||
} else {
|
||||
Len::ZERO
|
||||
}
|
||||
}
|
||||
}
|
||||
51
src/widget/rect.rs
Normal file
51
src/widget/rect.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Rect {
|
||||
pub color: UiColor,
|
||||
pub radius: f32,
|
||||
pub thickness: f32,
|
||||
pub inner_radius: f32,
|
||||
}
|
||||
|
||||
impl Rect {
|
||||
pub fn new(color: UiColor) -> Self {
|
||||
Self {
|
||||
color,
|
||||
radius: 0.0,
|
||||
inner_radius: 0.0,
|
||||
thickness: 0.0,
|
||||
}
|
||||
}
|
||||
pub fn color(mut self, color: UiColor) -> Self {
|
||||
self.color = color;
|
||||
self
|
||||
}
|
||||
pub fn radius(mut self, radius: impl UiNum) -> Self {
|
||||
self.radius = radius.to_f32();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Rect {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
painter.primitive(RectPrimitive {
|
||||
color: self.color,
|
||||
radius: self.radius,
|
||||
thickness: self.thickness,
|
||||
inner_radius: self.inner_radius,
|
||||
});
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
|
||||
Len::rest(1)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
|
||||
Len::rest(1)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rect(color: UiColor) -> Rect {
|
||||
Rect::new(color)
|
||||
}
|
||||
403
src/widget/sense.rs
Normal file
403
src/widget/sense.rs
Normal file
@@ -0,0 +1,403 @@
|
||||
use crate::prelude::*;
|
||||
|
||||
use std::{
|
||||
ops::{BitOr, Deref, DerefMut},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
layout::{UiModule, UiRegion},
|
||||
util::{HashMap, Id, Vec2},
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub enum Button {
|
||||
Left,
|
||||
Right,
|
||||
Middle,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
pub enum CursorSense {
|
||||
PressStart(Button),
|
||||
Pressing(Button),
|
||||
PressEnd(Button),
|
||||
HoverStart,
|
||||
Hovering,
|
||||
HoverEnd,
|
||||
Scroll,
|
||||
}
|
||||
|
||||
pub struct CursorSenses(Vec<CursorSense>);
|
||||
|
||||
impl CursorSense {
|
||||
pub fn click() -> Self {
|
||||
Self::PressStart(Button::Left)
|
||||
}
|
||||
pub fn click_or_drag() -> CursorSenses {
|
||||
Self::click() | Self::Pressing(Button::Left)
|
||||
}
|
||||
pub fn unclick() -> Self {
|
||||
Self::PressEnd(Button::Left)
|
||||
}
|
||||
pub fn is_dragging(&self) -> bool {
|
||||
matches!(self, CursorSense::Pressing(Button::Left))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct CursorState {
|
||||
pub pos: Vec2,
|
||||
pub exists: bool,
|
||||
pub buttons: CursorButtons,
|
||||
pub scroll_delta: Vec2,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct CursorButtons {
|
||||
pub left: ActivationState,
|
||||
pub middle: ActivationState,
|
||||
pub right: ActivationState,
|
||||
}
|
||||
|
||||
impl CursorButtons {
|
||||
pub fn select(&self, button: &Button) -> &ActivationState {
|
||||
match button {
|
||||
Button::Left => &self.left,
|
||||
Button::Right => &self.right,
|
||||
Button::Middle => &self.middle,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn end_frame(&mut self) {
|
||||
self.left.end_frame();
|
||||
self.middle.end_frame();
|
||||
self.right.end_frame();
|
||||
}
|
||||
}
|
||||
|
||||
impl CursorState {
|
||||
pub fn end_frame(&mut self) {
|
||||
self.buttons.end_frame();
|
||||
self.scroll_delta = Vec2::ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq)]
|
||||
pub enum ActivationState {
|
||||
Start,
|
||||
On,
|
||||
End,
|
||||
#[default]
|
||||
Off,
|
||||
}
|
||||
|
||||
/// this and other similar stuff has a generic
|
||||
/// because I kind of want to make CursorModule generic
|
||||
/// or basically have some way to have custom senses
|
||||
/// that depend on active widget positions
|
||||
/// but I'm not sure how or if worth it
|
||||
pub struct Sensor<Ctx, Data> {
|
||||
pub senses: CursorSenses,
|
||||
pub f: Rc<dyn EventFn<Ctx, Data>>,
|
||||
}
|
||||
|
||||
pub type SensorMap<Ctx, Data> = HashMap<Id, SensorGroup<Ctx, Data>>;
|
||||
pub type SenseShape = UiRegion;
|
||||
pub struct SensorGroup<Ctx, Data> {
|
||||
pub hover: ActivationState,
|
||||
pub sensors: Vec<Sensor<Ctx, Data>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CursorData {
|
||||
pub cursor: Vec2,
|
||||
pub size: Vec2,
|
||||
pub scroll_delta: Vec2,
|
||||
/// the (first) sense that triggered this event
|
||||
/// the senses are checked in order
|
||||
pub sense: CursorSense,
|
||||
}
|
||||
|
||||
pub struct CursorModule<Ctx> {
|
||||
map: SensorMap<Ctx, CursorData>,
|
||||
active: HashMap<usize, HashMap<Id, SenseShape>>,
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> UiModule for CursorModule<Ctx> {
|
||||
fn on_draw(&mut self, inst: &WidgetInstance) {
|
||||
if self.map.contains_key(&inst.id) {
|
||||
self.active
|
||||
.entry(inst.layer)
|
||||
.or_default()
|
||||
.insert(inst.id, inst.region);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_undraw(&mut self, inst: &WidgetInstance) {
|
||||
if let Some(layer) = self.active.get_mut(&inst.layer) {
|
||||
layer.remove(&inst.id);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_remove(&mut self, id: &Id) {
|
||||
self.map.remove(id);
|
||||
for layer in self.active.values_mut() {
|
||||
layer.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_move(&mut self, inst: &WidgetInstance) {
|
||||
if let Some(map) = self.active.get_mut(&inst.layer)
|
||||
&& let Some(region) = map.get_mut(&inst.id)
|
||||
{
|
||||
*region = inst.region;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> CursorModule<Ctx> {
|
||||
pub fn merge(&mut self, other: Self) {
|
||||
for (id, group) in other.map {
|
||||
for sensor in group.sensors {
|
||||
self.map.entry(id).or_default().sensors.push(sensor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SensorUi {
|
||||
fn run_sensors<Ctx: 'static>(&mut self, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2);
|
||||
}
|
||||
|
||||
impl SensorUi for Ui {
|
||||
fn run_sensors<Ctx: 'static>(
|
||||
&mut self,
|
||||
ctx: &mut Ctx,
|
||||
cursor: &CursorState,
|
||||
window_size: Vec2,
|
||||
) {
|
||||
CursorModule::<Ctx>::run(self, ctx, cursor, window_size);
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> CursorModule<Ctx> {
|
||||
pub fn run(ui: &mut Ui, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
||||
let layers = std::mem::take(&mut ui.data.layers);
|
||||
let mut module = std::mem::take(ui.data.modules.get_mut::<Self>());
|
||||
|
||||
for i in layers.indices().rev() {
|
||||
let Some(list) = module.active.get_mut(&i) else {
|
||||
continue;
|
||||
};
|
||||
let mut sensed = false;
|
||||
for (id, shape) in list.iter() {
|
||||
let group = module.map.get_mut(id).unwrap();
|
||||
let region = shape.to_px(window_size);
|
||||
let in_shape = cursor.exists && region.contains(cursor.pos);
|
||||
group.hover.update(in_shape);
|
||||
if group.hover == ActivationState::Off {
|
||||
continue;
|
||||
}
|
||||
sensed = true;
|
||||
|
||||
for sensor in &mut group.sensors {
|
||||
if let Some(sense) = should_run(&sensor.senses, cursor, group.hover) {
|
||||
let data = CursorData {
|
||||
cursor: cursor.pos - region.top_left,
|
||||
size: region.bot_right - region.top_left,
|
||||
scroll_delta: cursor.scroll_delta,
|
||||
sense,
|
||||
};
|
||||
(sensor.f)(EventCtx {
|
||||
ui,
|
||||
state: ctx,
|
||||
data,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if sensed {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let ui_mod = ui.data.modules.get_mut::<Self>();
|
||||
std::mem::swap(ui_mod, &mut module);
|
||||
ui_mod.merge(module);
|
||||
ui.data.layers = layers;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn should_run(
|
||||
senses: &CursorSenses,
|
||||
cursor: &CursorState,
|
||||
hover: ActivationState,
|
||||
) -> Option<CursorSense> {
|
||||
for sense in senses.iter() {
|
||||
if match sense {
|
||||
CursorSense::PressStart(button) => cursor.buttons.select(button).is_start(),
|
||||
CursorSense::Pressing(button) => cursor.buttons.select(button).is_on(),
|
||||
CursorSense::PressEnd(button) => cursor.buttons.select(button).is_end(),
|
||||
CursorSense::HoverStart => hover.is_start(),
|
||||
CursorSense::Hovering => hover.is_on(),
|
||||
CursorSense::HoverEnd => hover.is_end(),
|
||||
CursorSense::Scroll => cursor.scroll_delta != Vec2::ZERO,
|
||||
} {
|
||||
return Some(*sense);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
impl ActivationState {
|
||||
pub fn is_start(&self) -> bool {
|
||||
*self == Self::Start
|
||||
}
|
||||
pub fn is_on(&self) -> bool {
|
||||
*self == Self::Start || *self == Self::On
|
||||
}
|
||||
pub fn is_end(&self) -> bool {
|
||||
*self == Self::End
|
||||
}
|
||||
pub fn is_off(&self) -> bool {
|
||||
*self == Self::End || *self == Self::Off
|
||||
}
|
||||
pub fn update(&mut self, on: bool) {
|
||||
*self = match *self {
|
||||
Self::Start => match on {
|
||||
true => Self::On,
|
||||
false => Self::End,
|
||||
},
|
||||
Self::On => match on {
|
||||
true => Self::On,
|
||||
false => Self::End,
|
||||
},
|
||||
Self::End => match on {
|
||||
true => Self::Start,
|
||||
false => Self::Off,
|
||||
},
|
||||
Self::Off => match on {
|
||||
true => Self::Start,
|
||||
false => Self::Off,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn end_frame(&mut self) {
|
||||
match self {
|
||||
Self::Start => *self = Self::On,
|
||||
Self::End => *self = Self::Off,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Event for CursorSenses {
|
||||
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
||||
type Data = CursorData;
|
||||
}
|
||||
|
||||
impl Event for CursorSense {
|
||||
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
||||
type Data = CursorData;
|
||||
}
|
||||
|
||||
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static>
|
||||
EventModule<E, Ctx> for CursorModule<Ctx>
|
||||
{
|
||||
fn register(&mut self, id: Id, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
|
||||
// TODO: does not add to active if currently active
|
||||
self.map.entry(id).or_default().sensors.push(Sensor {
|
||||
senses: senses.into(),
|
||||
f: Rc::new(f),
|
||||
});
|
||||
}
|
||||
|
||||
fn run<'a>(
|
||||
&self,
|
||||
id: &Id,
|
||||
event: E,
|
||||
) -> Option<impl Fn(EventCtx<Ctx, <E as Event>::Data>) + use<'a, E, Ctx>> {
|
||||
let senses = event.into();
|
||||
if let Some(group) = self.map.get(id) {
|
||||
let fs: Vec<_> = group
|
||||
.sensors
|
||||
.iter()
|
||||
.filter_map(|sensor| {
|
||||
if sensor.senses.iter().any(|s| senses.contains(s)) {
|
||||
Some(sensor.f.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Some(move |ctx: EventCtx<Ctx, CursorData>| {
|
||||
for f in &fs {
|
||||
f(EventCtx {
|
||||
state: ctx.state,
|
||||
ui: ctx.ui,
|
||||
data: ctx.data.clone(),
|
||||
});
|
||||
}
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx, Data> Default for SensorGroup<Ctx, Data> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hover: Default::default(),
|
||||
sensors: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for CursorSenses {
|
||||
type Target = Vec<CursorSense>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for CursorSenses {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CursorSense> for CursorSenses {
|
||||
fn from(val: CursorSense) -> Self {
|
||||
CursorSenses(vec![val])
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr for CursorSense {
|
||||
type Output = CursorSenses;
|
||||
|
||||
fn bitor(self, rhs: Self) -> Self::Output {
|
||||
CursorSenses(vec![self, rhs])
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr<CursorSense> for CursorSenses {
|
||||
type Output = Self;
|
||||
|
||||
fn bitor(mut self, rhs: CursorSense) -> Self::Output {
|
||||
self.0.push(rhs);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> Default for CursorModule<Ctx> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
map: Default::default(),
|
||||
active: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
128
src/widget/text/build.rs
Normal file
128
src/widget/text/build.rs
Normal file
@@ -0,0 +1,128 @@
|
||||
use crate::prelude::*;
|
||||
use cosmic_text::{Attrs, Family, Metrics};
|
||||
use std::marker::Sized;
|
||||
|
||||
pub struct TextBuilder<O = TextOutput, H: WidgetOption = ()> {
|
||||
pub content: String,
|
||||
pub attrs: TextAttrs,
|
||||
pub hint: H,
|
||||
pub output: O,
|
||||
}
|
||||
|
||||
impl<O, H: WidgetOption> TextBuilder<O, H> {
|
||||
pub fn size(mut self, size: impl UiNum) -> Self {
|
||||
self.attrs.font_size = size.to_f32();
|
||||
self.attrs.line_height = self.attrs.font_size * LINE_HEIGHT_MULT;
|
||||
self
|
||||
}
|
||||
pub fn color(mut self, color: UiColor) -> Self {
|
||||
self.attrs.color = color;
|
||||
self
|
||||
}
|
||||
pub fn family(mut self, family: Family<'static>) -> Self {
|
||||
self.attrs.family = family;
|
||||
self
|
||||
}
|
||||
pub fn line_height(mut self, height: f32) -> Self {
|
||||
self.attrs.line_height = height;
|
||||
self
|
||||
}
|
||||
pub fn text_align(mut self, align: impl Into<RegionAlign>) -> Self {
|
||||
self.attrs.align = align.into();
|
||||
self
|
||||
}
|
||||
pub fn center_text(mut self) -> Self {
|
||||
self.attrs.align = Align::CENTER;
|
||||
self
|
||||
}
|
||||
pub fn wrap(mut self, wrap: bool) -> Self {
|
||||
self.attrs.wrap = wrap;
|
||||
self
|
||||
}
|
||||
pub fn editable(self, single_line: bool) -> TextBuilder<TextEditOutput, H> {
|
||||
TextBuilder {
|
||||
content: self.content,
|
||||
attrs: self.attrs,
|
||||
hint: self.hint,
|
||||
output: TextEditOutput { single_line },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<O> TextBuilder<O> {
|
||||
pub fn hint<W: WidgetLike<Tag>, Tag>(self, hint: W) -> TextBuilder<O, impl WidgetOption> {
|
||||
TextBuilder {
|
||||
content: self.content,
|
||||
attrs: self.attrs,
|
||||
hint: move |ui: &mut Ui| Some(hint.add(ui).any()),
|
||||
output: self.output,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TextBuilderOutput: Sized {
|
||||
type Output;
|
||||
fn run<H: WidgetOption>(ui: &mut Ui, builder: TextBuilder<Self, H>) -> Self::Output;
|
||||
}
|
||||
|
||||
pub struct TextOutput;
|
||||
impl TextBuilderOutput for TextOutput {
|
||||
type Output = Text;
|
||||
|
||||
fn run<H: WidgetOption>(ui: &mut Ui, builder: TextBuilder<Self, H>) -> Self::Output {
|
||||
let mut buf = TextBuffer::new_empty(Metrics::new(
|
||||
builder.attrs.font_size,
|
||||
builder.attrs.line_height,
|
||||
));
|
||||
let hint = builder.hint.get(ui);
|
||||
let font_system = &mut ui.data.text.font_system;
|
||||
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
||||
let mut text = Text {
|
||||
content: builder.content.into(),
|
||||
view: TextView::new(buf, builder.attrs, hint),
|
||||
};
|
||||
text.content.changed = false;
|
||||
builder.attrs.apply(font_system, &mut text.view.buf, None);
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TextEditOutput {
|
||||
single_line: bool,
|
||||
}
|
||||
impl TextBuilderOutput for TextEditOutput {
|
||||
type Output = TextEdit;
|
||||
|
||||
fn run<H: WidgetOption>(ui: &mut Ui, builder: TextBuilder<Self, H>) -> Self::Output {
|
||||
let buf = TextBuffer::new_empty(Metrics::new(
|
||||
builder.attrs.font_size,
|
||||
builder.attrs.line_height,
|
||||
));
|
||||
let mut text = TextEdit::new(
|
||||
TextView::new(buf, builder.attrs, builder.hint.get(ui)),
|
||||
builder.output.single_line,
|
||||
);
|
||||
let font_system = &mut ui.data.text.font_system;
|
||||
text.buf
|
||||
.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
||||
builder.attrs.apply(font_system, &mut text.buf, None);
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
impl<O: TextBuilderOutput, H: WidgetOption> FnOnce<(&mut Ui,)> for TextBuilder<O, H> {
|
||||
type Output = O::Output;
|
||||
|
||||
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
|
||||
O::run(args.0, self)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wtext(content: impl Into<String>) -> TextBuilder {
|
||||
TextBuilder {
|
||||
content: content.into(),
|
||||
attrs: TextAttrs::default(),
|
||||
hint: (),
|
||||
output: TextOutput,
|
||||
}
|
||||
}
|
||||
625
src/widget/text/edit.rs
Normal file
625
src/widget/text/edit.rs
Normal file
@@ -0,0 +1,625 @@
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use crate::prelude::*;
|
||||
use cosmic_text::{Affinity, Attrs, Cursor, FontSystem, LayoutRun, Motion};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use winit::{
|
||||
event::KeyEvent,
|
||||
keyboard::{Key, NamedKey},
|
||||
};
|
||||
|
||||
pub struct TextEdit {
|
||||
view: TextView,
|
||||
selection: TextSelection,
|
||||
history: Vec<(String, TextSelection)>,
|
||||
double_hit: Option<Cursor>,
|
||||
pub single_line: bool,
|
||||
}
|
||||
|
||||
impl TextEdit {
|
||||
pub fn new(view: TextView, single_line: bool) -> Self {
|
||||
Self {
|
||||
view,
|
||||
selection: Default::default(),
|
||||
history: Default::default(),
|
||||
double_hit: None,
|
||||
single_line,
|
||||
}
|
||||
}
|
||||
pub fn select_content(&self, start: Cursor, end: Cursor) -> String {
|
||||
let (start, end) = sort_cursors(start, end);
|
||||
let mut iter = self.buf.lines.iter().skip(start.line);
|
||||
let first = iter.next().unwrap();
|
||||
if start.line == end.line {
|
||||
first.text()[start.index..end.index].to_string()
|
||||
} else {
|
||||
let mut str = first.text()[start.index..].to_string();
|
||||
for _ in (start.line + 1)..end.line {
|
||||
str = str + "\n" + iter.next().unwrap().text();
|
||||
}
|
||||
let last = iter.next().unwrap();
|
||||
str = str + "\n" + &last.text()[..end.index];
|
||||
str
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for TextEdit {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let base = painter.layer;
|
||||
painter.child_layer();
|
||||
self.view.draw(painter);
|
||||
painter.layer = base;
|
||||
let region = self.region();
|
||||
|
||||
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) {
|
||||
painter.primitive_within(
|
||||
RectPrimitive::color(Color::WHITE),
|
||||
size.align(Align::TOP_LEFT).offset(offset).within(®ion),
|
||||
);
|
||||
}
|
||||
}
|
||||
TextSelection::Span { start, end } => {
|
||||
let (start, end) = sort_cursors(start, end);
|
||||
for (l, x, width) in iter_layout_lines(start, end, &self.buf) {
|
||||
let top_left = vec2(x, self.attrs.line_height * l as f32);
|
||||
painter.primitive_within(
|
||||
RectPrimitive::color(Color::SKY),
|
||||
size.with_x(width)
|
||||
.align(Align::TOP_LEFT)
|
||||
.offset(top_left)
|
||||
.within(®ion),
|
||||
);
|
||||
}
|
||||
if let Some(end_offset) = cursor_pos(end, &self.buf) {
|
||||
painter.primitive_within(
|
||||
RectPrimitive::color(Color::WHITE),
|
||||
size.align(Align::TOP_LEFT)
|
||||
.offset(end_offset)
|
||||
.within(®ion),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
self.view.desired_width(ctx)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
self.view.desired_height(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
/// provides top left + width
|
||||
fn iter_layout_lines(
|
||||
start: Cursor,
|
||||
end: Cursor,
|
||||
buf: &TextBuffer,
|
||||
) -> impl Iterator<Item = (usize, f32, f32)> {
|
||||
gen move {
|
||||
let mut iter = buf.layout_runs().enumerate();
|
||||
for (i, line) in iter.by_ref() {
|
||||
if line.line_i == start.line
|
||||
&& let Some(start_x) = index_x(&line, start.index)
|
||||
{
|
||||
if start.line == end.line
|
||||
&& let Some(end_x) = index_x(&line, end.index)
|
||||
{
|
||||
yield (i, start_x, end_x - start_x);
|
||||
return;
|
||||
}
|
||||
yield (i, start_x, line.line_w - start_x);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i, line) in iter {
|
||||
if line.line_i > end.line {
|
||||
return;
|
||||
}
|
||||
if line.line_i == end.line
|
||||
&& let Some(end_x) = index_x(&line, end.index)
|
||||
{
|
||||
yield (i, 0.0, end_x);
|
||||
return;
|
||||
}
|
||||
yield (i, 0.0, line.line_w);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// copied & modified from fn found in Editor in cosmic_text
|
||||
/// returns x pos of a (non layout) index within an layout run
|
||||
fn index_x(run: &LayoutRun, index: usize) -> Option<f32> {
|
||||
for glyph in run.glyphs.iter() {
|
||||
if index == glyph.start {
|
||||
return Some(glyph.x);
|
||||
} else if index > glyph.start && index < glyph.end {
|
||||
// Guess x offset based on characters
|
||||
let mut before = 0;
|
||||
let mut total = 0;
|
||||
|
||||
let cluster = &run.text[glyph.start..glyph.end];
|
||||
for (i, _) in cluster.grapheme_indices(true) {
|
||||
if glyph.start + i < index {
|
||||
before += 1;
|
||||
}
|
||||
total += 1;
|
||||
}
|
||||
|
||||
let offset = glyph.w * (before as f32) / (total as f32);
|
||||
return Some(glyph.x + offset);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// returns top of line segment where cursor should visually select
|
||||
fn cursor_pos(cursor: Cursor, buf: &TextBuffer) -> Option<Vec2> {
|
||||
let mut prev = None;
|
||||
for run in buf
|
||||
.layout_runs()
|
||||
.skip_while(|r| r.line_i < cursor.line)
|
||||
.take_while(|r| r.line_i == cursor.line)
|
||||
{
|
||||
prev = Some(vec2(run.line_w, run.line_top));
|
||||
if let Some(pos) = index_x(&run, cursor.index) {
|
||||
return Some(vec2(pos, run.line_top));
|
||||
}
|
||||
}
|
||||
prev
|
||||
}
|
||||
|
||||
pub struct TextEditCtx<'a> {
|
||||
pub text: &'a mut TextEdit,
|
||||
pub font_system: &'a mut FontSystem,
|
||||
}
|
||||
|
||||
impl<'a> TextEditCtx<'a> {
|
||||
pub fn take(&mut self) -> String {
|
||||
let text = self
|
||||
.text
|
||||
.buf
|
||||
.lines
|
||||
.drain(..)
|
||||
.map(|l| l.into_text())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
self.text
|
||||
.buf
|
||||
.set_text(self.font_system, "", &Attrs::new(), SHAPING, None);
|
||||
self.text.selection.clear();
|
||||
text
|
||||
}
|
||||
|
||||
pub fn set(&mut self, text: &str) {
|
||||
let text = self.string(text);
|
||||
self.text
|
||||
.buf
|
||||
.set_text(self.font_system, &text, &Attrs::new(), SHAPING, None);
|
||||
self.text.selection.clear();
|
||||
}
|
||||
|
||||
pub fn motion(&mut self, motion: Motion, select: bool) {
|
||||
if let TextSelection::Pos(cursor) = self.text.selection
|
||||
&& let Some(new) = self.buf_motion(cursor, motion)
|
||||
{
|
||||
if select {
|
||||
self.text.selection = TextSelection::Span {
|
||||
start: cursor,
|
||||
end: new,
|
||||
};
|
||||
} else {
|
||||
self.text.selection = TextSelection::Pos(new);
|
||||
}
|
||||
} else if let TextSelection::Span { start, end } = self.text.selection {
|
||||
if select {
|
||||
if let Some(cursor) = self.buf_motion(end, motion) {
|
||||
self.text.selection = TextSelection::Span { start, end: cursor };
|
||||
}
|
||||
} else {
|
||||
let (start, end) = sort_cursors(start, end);
|
||||
let sel = &mut self.text.selection;
|
||||
match motion {
|
||||
Motion::Left | Motion::LeftWord => *sel = TextSelection::Pos(start),
|
||||
Motion::Right | Motion::RightWord => *sel = TextSelection::Pos(end),
|
||||
_ => {
|
||||
if let Some(cursor) = self.buf_motion(end, motion) {
|
||||
self.text.selection = TextSelection::Pos(cursor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn replace(&mut self, len: usize, text: &str) {
|
||||
let text = self.string(text);
|
||||
for _ in 0..len {
|
||||
self.delete(false);
|
||||
}
|
||||
self.insert_inner(&text, false);
|
||||
}
|
||||
|
||||
fn string(&self, text: &str) -> String {
|
||||
if self.text.single_line {
|
||||
text.replace('\n', "")
|
||||
} else {
|
||||
text.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, text: &str) {
|
||||
let text = self.string(text);
|
||||
let mut lines = text.split('\n');
|
||||
let Some(first) = lines.next() else {
|
||||
return;
|
||||
};
|
||||
self.insert_inner(first, true);
|
||||
for line in lines {
|
||||
self.newline();
|
||||
self.insert_inner(line, true);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_span(&mut self) -> bool {
|
||||
if let TextSelection::Span { start, end } = self.text.selection {
|
||||
self.delete_between(start, end);
|
||||
let (start, _) = sort_cursors(start, end);
|
||||
self.text.selection = TextSelection::Pos(start);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_between(&mut self, start: Cursor, end: Cursor) {
|
||||
let lines = &mut self.text.view.buf.lines;
|
||||
let (start, end) = sort_cursors(start, end);
|
||||
if start.line == end.line {
|
||||
let line = &mut lines[start.line];
|
||||
let text = line.text();
|
||||
let text = text[..start.index].to_string() + &text[end.index..];
|
||||
edit_line(line, text);
|
||||
} else {
|
||||
// start
|
||||
let start_text = lines[start.line].text()[..start.index].to_string();
|
||||
let end_text = &lines[end.line].text()[end.index..];
|
||||
let text = start_text + end_text;
|
||||
edit_line(&mut lines[start.line], text);
|
||||
}
|
||||
// between
|
||||
let range = (start.line + 1)..=end.line;
|
||||
if !range.is_empty() {
|
||||
lines.splice(range, None);
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_inner(&mut self, text: &str, mov: bool) {
|
||||
self.clear_span();
|
||||
if let TextSelection::Pos(cursor) = &mut self.text.selection {
|
||||
let line = &mut self.text.view.buf.lines[cursor.line];
|
||||
let mut line_text = line.text().to_string();
|
||||
line_text.insert_str(cursor.index, text);
|
||||
edit_line(line, line_text);
|
||||
if mov {
|
||||
for _ in 0..text.chars().count() {
|
||||
self.motion(Motion::Right, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn newline(&mut self) {
|
||||
if self.text.single_line {
|
||||
return;
|
||||
}
|
||||
self.clear_span();
|
||||
if let TextSelection::Pos(cursor) = &mut self.text.selection {
|
||||
let lines = &mut self.text.view.buf.lines;
|
||||
let line = &mut lines[cursor.line];
|
||||
let new = line.split_off(cursor.index);
|
||||
cursor.line += 1;
|
||||
lines.insert(cursor.line, new);
|
||||
cursor.index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backspace(&mut self, word: bool) {
|
||||
if !self.clear_span()
|
||||
&& let TextSelection::Pos(cursor) = &mut self.text.selection
|
||||
&& (cursor.index != 0 || cursor.line != 0)
|
||||
{
|
||||
self.motion(if word { Motion::LeftWord } else { Motion::Left }, false);
|
||||
self.delete(word);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, word: bool) {
|
||||
if !self.clear_span()
|
||||
&& let TextSelection::Pos(cursor) = &mut self.text.selection
|
||||
{
|
||||
if word {
|
||||
let start = *cursor;
|
||||
if let Some(end) = self.buf_motion(start, Motion::RightWord) {
|
||||
self.delete_between(start, end);
|
||||
}
|
||||
} else {
|
||||
let lines = &mut self.text.view.buf.lines;
|
||||
let line = &mut lines[cursor.line];
|
||||
if cursor.index == line.text().len() {
|
||||
if cursor.line == lines.len() - 1 {
|
||||
return;
|
||||
}
|
||||
let add = lines.remove(cursor.line + 1).into_text();
|
||||
let line = &mut lines[cursor.line];
|
||||
let mut cur = line.text().to_string();
|
||||
cur.push_str(&add);
|
||||
edit_line(line, cur);
|
||||
} else {
|
||||
let mut text = line.text().to_string();
|
||||
text.remove(cursor.index);
|
||||
edit_line(line, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn buf_motion(&mut self, cursor: Cursor, motion: Motion) -> Option<Cursor> {
|
||||
self.text
|
||||
.buf
|
||||
.cursor_motion(self.font_system, cursor, None, motion)
|
||||
.map(|r| r.0)
|
||||
}
|
||||
|
||||
pub fn select_word_at(&mut self, cursor: Cursor) {
|
||||
if let (Some(start), Some(end)) = (
|
||||
self.buf_motion(cursor, Motion::LeftWord),
|
||||
self.buf_motion(cursor, Motion::RightWord),
|
||||
) {
|
||||
self.text.selection = TextSelection::Span { start, end };
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select_line_at(&mut self, cursor: Cursor) {
|
||||
let end = self.text.buf.lines[cursor.line].text().len();
|
||||
self.text.selection = TextSelection::Span {
|
||||
start: Cursor::new(cursor.line, 0),
|
||||
end: Cursor::new(cursor.line, end),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(&mut self, pos: Vec2, size: Vec2, drag: bool, recent: bool) {
|
||||
let pos = pos - self.text.region().top_left().to_abs(size);
|
||||
let hit = self.text.buf.hit(pos.x, pos.y);
|
||||
let sel = &mut self.text.selection;
|
||||
match sel {
|
||||
TextSelection::None => {
|
||||
if !drag && let Some(hit) = hit {
|
||||
*sel = TextSelection::Pos(hit)
|
||||
}
|
||||
}
|
||||
TextSelection::Pos(pos) => match (hit, drag) {
|
||||
(None, false) => *sel = TextSelection::None,
|
||||
(None, true) => (),
|
||||
(Some(hit), false) => {
|
||||
if recent && hit == *pos {
|
||||
self.text.double_hit = Some(hit);
|
||||
return self.select_word_at(hit);
|
||||
} else {
|
||||
*pos = hit
|
||||
}
|
||||
}
|
||||
(Some(end), true) => *sel = TextSelection::Span { start: *pos, end },
|
||||
},
|
||||
TextSelection::Span { start, end } => match (hit, drag) {
|
||||
(None, false) => *sel = TextSelection::None,
|
||||
(None, true) => *sel = TextSelection::Pos(*start),
|
||||
(Some(hit), false) => {
|
||||
if recent
|
||||
&& let Some(double) = self.text.double_hit
|
||||
&& double == hit
|
||||
{
|
||||
return self.select_line_at(hit);
|
||||
} else {
|
||||
*sel = TextSelection::Pos(hit)
|
||||
}
|
||||
}
|
||||
(Some(hit), true) => *end = hit,
|
||||
},
|
||||
}
|
||||
if let TextSelection::Span { start, end } = sel
|
||||
&& start == end
|
||||
{
|
||||
*sel = TextSelection::Pos(*start);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deselect(&mut self) {
|
||||
self.text.selection = TextSelection::None;
|
||||
}
|
||||
|
||||
pub fn apply_event(&mut self, event: &KeyEvent, modifiers: &Modifiers) -> TextInputResult {
|
||||
let old = (self.text.content(), self.text.selection);
|
||||
let mut undo = false;
|
||||
let res = self.apply_event_inner(event, modifiers, &mut undo);
|
||||
if undo && let Some((old, selection)) = self.text.history.pop() {
|
||||
self.set(&old);
|
||||
self.text.selection = selection;
|
||||
} else if self.text.content() != old.0 {
|
||||
self.text.history.push(old);
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn apply_event_inner(
|
||||
&mut self,
|
||||
event: &KeyEvent,
|
||||
modifiers: &Modifiers,
|
||||
undo: &mut bool,
|
||||
) -> TextInputResult {
|
||||
match &event.logical_key {
|
||||
Key::Named(named) => match named {
|
||||
NamedKey::Backspace => self.backspace(modifiers.control),
|
||||
NamedKey::Delete => self.delete(modifiers.control),
|
||||
NamedKey::Space => self.insert(" "),
|
||||
NamedKey::Enter => {
|
||||
if modifiers.shift {
|
||||
self.newline();
|
||||
} else {
|
||||
return TextInputResult::Submit;
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowRight => {
|
||||
if modifiers.control {
|
||||
self.motion(Motion::RightWord, modifiers.shift)
|
||||
} else {
|
||||
self.motion(Motion::Right, modifiers.shift)
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowLeft => {
|
||||
if modifiers.control {
|
||||
self.motion(Motion::LeftWord, modifiers.shift)
|
||||
} else {
|
||||
self.motion(Motion::Left, modifiers.shift)
|
||||
}
|
||||
}
|
||||
NamedKey::ArrowUp => self.motion(Motion::Up, modifiers.shift),
|
||||
NamedKey::ArrowDown => self.motion(Motion::Down, modifiers.shift),
|
||||
NamedKey::Escape => {
|
||||
self.deselect();
|
||||
return TextInputResult::Unfocus;
|
||||
}
|
||||
_ => return TextInputResult::Unused,
|
||||
},
|
||||
Key::Character(text) => {
|
||||
if modifiers.control {
|
||||
match text.as_str() {
|
||||
"v" => return TextInputResult::Paste,
|
||||
"c" => {
|
||||
if let TextSelection::Span { start, end } = self.text.selection {
|
||||
let content = self.text.select_content(start, end);
|
||||
return TextInputResult::Copy(content);
|
||||
}
|
||||
}
|
||||
"x" => {
|
||||
if let TextSelection::Span { start, end } = self.text.selection {
|
||||
let content = self.text.select_content(start, end);
|
||||
self.clear_span();
|
||||
return TextInputResult::Copy(content);
|
||||
}
|
||||
}
|
||||
"a" => {
|
||||
if !self.text.buf.lines[0].text().is_empty()
|
||||
|| self.text.buf.lines.len() > 1
|
||||
{
|
||||
let lines = &self.text.buf.lines;
|
||||
let last_line = lines.len() - 1;
|
||||
let last_idx = lines[last_line].text().len();
|
||||
self.text.selection = TextSelection::Span {
|
||||
start: Cursor::new(0, 0),
|
||||
end: Cursor::new(last_line, last_idx),
|
||||
};
|
||||
}
|
||||
}
|
||||
"z" => {
|
||||
*undo = true;
|
||||
}
|
||||
_ => self.insert(text),
|
||||
}
|
||||
} else {
|
||||
self.insert(text);
|
||||
}
|
||||
}
|
||||
_ => return TextInputResult::Unused,
|
||||
}
|
||||
TextInputResult::Used
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Modifiers {
|
||||
pub shift: bool,
|
||||
pub control: bool,
|
||||
}
|
||||
|
||||
impl Modifiers {
|
||||
pub fn clear(&mut self) {
|
||||
self.shift = false;
|
||||
self.control = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub enum TextInputResult {
|
||||
Used,
|
||||
Unused,
|
||||
Unfocus,
|
||||
Submit,
|
||||
Copy(String),
|
||||
Paste,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub enum TextSelection {
|
||||
#[default]
|
||||
None,
|
||||
Pos(Cursor),
|
||||
Span {
|
||||
start: Cursor,
|
||||
end: Cursor,
|
||||
},
|
||||
}
|
||||
|
||||
impl TextSelection {
|
||||
pub fn clear(&mut self) {
|
||||
match self {
|
||||
TextSelection::None => (),
|
||||
TextSelection::Pos(cursor) => {
|
||||
cursor.line = 0;
|
||||
cursor.index = 0;
|
||||
cursor.affinity = Affinity::default();
|
||||
}
|
||||
TextSelection::Span { start: _, end: _ } => {
|
||||
*self = TextSelection::None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TextInputResult {
|
||||
pub fn unfocus(&self) -> bool {
|
||||
matches!(self, TextInputResult::Unfocus)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for TextEdit {
|
||||
type Target = TextView;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.view
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for TextEdit {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.view
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TextEditable {
|
||||
fn edit<'a>(&self, ui: &'a mut Ui) -> TextEditCtx<'a>;
|
||||
}
|
||||
|
||||
impl<I: IdLike<Widget = TextEdit>> TextEditable for I {
|
||||
fn edit<'a>(&self, ui: &'a mut Ui) -> TextEditCtx<'a> {
|
||||
TextEditCtx {
|
||||
text: ui.data.widgets.get_mut(self).unwrap(),
|
||||
font_system: &mut ui.data.text.font_system,
|
||||
}
|
||||
}
|
||||
}
|
||||
202
src/widget/text/mod.rs
Normal file
202
src/widget/text/mod.rs
Normal file
@@ -0,0 +1,202 @@
|
||||
mod build;
|
||||
mod edit;
|
||||
|
||||
pub use build::*;
|
||||
pub use edit::*;
|
||||
|
||||
use crate::{prelude::*, util::MutDetect};
|
||||
use cosmic_text::{Attrs, BufferLine, Cursor, Metrics, Shaping};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
pub const SHAPING: Shaping = Shaping::Advanced;
|
||||
|
||||
pub struct Text {
|
||||
pub content: MutDetect<String>,
|
||||
view: TextView,
|
||||
}
|
||||
|
||||
pub struct TextView {
|
||||
pub attrs: MutDetect<TextAttrs>,
|
||||
pub buf: MutDetect<TextBuffer>,
|
||||
// cache
|
||||
tex: Option<RenderedText>,
|
||||
width: Option<f32>,
|
||||
pub hint: Option<WidgetId>,
|
||||
}
|
||||
|
||||
impl TextView {
|
||||
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetId>) -> Self {
|
||||
Self {
|
||||
attrs: attrs.into(),
|
||||
buf: buf.into(),
|
||||
tex: None,
|
||||
width: None,
|
||||
hint,
|
||||
}
|
||||
}
|
||||
|
||||
/// region where the text should be draw
|
||||
/// does not include extra height or width from weird unicode
|
||||
pub fn region(&self) -> UiRegion {
|
||||
self.tex()
|
||||
.map(|t| t.size)
|
||||
.unwrap_or(Vec2::ZERO)
|
||||
.align(self.align)
|
||||
}
|
||||
|
||||
fn tex_region(&self, tex: &RenderedText) -> UiRegion {
|
||||
let region = tex.size.align(self.align);
|
||||
let dims = tex.handle.size();
|
||||
let mut region = region.offset(tex.top_left_offset);
|
||||
region.x.end = region.x.start + UiScalar::abs(dims.x);
|
||||
region.y.end = region.y.start + UiScalar::abs(dims.y);
|
||||
region
|
||||
}
|
||||
|
||||
fn render(&mut self, ctx: &mut SizeCtx) -> RenderedText {
|
||||
let width = if self.attrs.wrap {
|
||||
Some(ctx.px_size().x)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if width == self.width
|
||||
&& let Some(tex) = &self.tex
|
||||
&& !self.attrs.changed
|
||||
&& !self.buf.changed
|
||||
{
|
||||
return tex.clone();
|
||||
}
|
||||
self.width = width;
|
||||
let font_system = &mut ctx.text.font_system;
|
||||
self.attrs.apply(font_system, &mut self.buf, width);
|
||||
self.buf.shape_until_scroll(font_system, false);
|
||||
let tex = ctx.draw_text(&mut self.buf, &self.attrs);
|
||||
self.tex = Some(tex.clone());
|
||||
self.attrs.changed = false;
|
||||
self.buf.changed = false;
|
||||
tex
|
||||
}
|
||||
pub fn tex(&self) -> Option<&RenderedText> {
|
||||
self.tex.as_ref()
|
||||
}
|
||||
pub fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
if let Some(hint) = &self.hint
|
||||
&& let [line] = &self.buf.lines[..]
|
||||
&& line.text().is_empty()
|
||||
{
|
||||
ctx.width(hint)
|
||||
} else {
|
||||
Len::abs(self.render(ctx).size.x)
|
||||
}
|
||||
}
|
||||
pub fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
if let Some(hint) = &self.hint
|
||||
&& let [line] = &self.buf.lines[..]
|
||||
&& line.text().is_empty()
|
||||
{
|
||||
ctx.height(hint)
|
||||
} else {
|
||||
Len::abs(self.render(ctx).size.y)
|
||||
}
|
||||
}
|
||||
pub fn draw(&mut self, painter: &mut Painter) -> UiRegion {
|
||||
let tex = self.render(&mut painter.size_ctx());
|
||||
let region = self.tex_region(&tex);
|
||||
if let Some(hint) = &self.hint
|
||||
&& let [line] = &self.buf.lines[..]
|
||||
&& line.text().is_empty()
|
||||
{
|
||||
painter.widget(hint);
|
||||
} else {
|
||||
painter.texture_within(&tex.handle, region);
|
||||
}
|
||||
region
|
||||
}
|
||||
|
||||
pub fn content(&self) -> String {
|
||||
self.buf
|
||||
.lines
|
||||
.iter()
|
||||
.map(|l| l.text())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
}
|
||||
|
||||
impl Text {
|
||||
pub fn new(content: impl Into<String>) -> Self {
|
||||
let attrs = TextAttrs::default();
|
||||
let buf = TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height));
|
||||
Self {
|
||||
content: content.into().into(),
|
||||
view: TextView::new(buf, attrs, None),
|
||||
}
|
||||
}
|
||||
fn update_buf(&mut self, ctx: &mut SizeCtx) {
|
||||
if self.content.changed {
|
||||
self.content.changed = false;
|
||||
self.view.buf.set_text(
|
||||
&mut ctx.text.font_system,
|
||||
&self.content,
|
||||
&Attrs::new().family(self.view.attrs.family),
|
||||
SHAPING,
|
||||
None,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Text {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
self.update_buf(&mut painter.size_ctx());
|
||||
self.view.draw(painter);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
self.update_buf(ctx);
|
||||
self.view.desired_width(ctx)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
self.update_buf(ctx);
|
||||
self.view.desired_height(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sort_cursors(a: Cursor, b: Cursor) -> (Cursor, Cursor) {
|
||||
let start = a.min(b);
|
||||
let end = a.max(b);
|
||||
(start, end)
|
||||
}
|
||||
|
||||
pub fn edit_line(line: &mut BufferLine, text: String) {
|
||||
line.set_text(text, line.ending(), line.attrs_list().clone());
|
||||
}
|
||||
|
||||
impl Deref for Text {
|
||||
type Target = TextAttrs;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.view
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for Text {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.view
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for TextView {
|
||||
type Target = TextAttrs;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.attrs
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for TextView {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.attrs
|
||||
}
|
||||
}
|
||||
160
src/widget/trait_fns.rs
Normal file
160
src/widget/trait_fns.rs
Normal file
@@ -0,0 +1,160 @@
|
||||
use super::*;
|
||||
use crate::prelude::*;
|
||||
|
||||
// these methods should "not require any context" (require unit) because they're in core
|
||||
event_ctx!(());
|
||||
|
||||
pub trait CoreWidget<W, Tag> {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Pad>;
|
||||
fn align(self, align: impl Into<Align>) -> impl WidgetFn<Aligned>;
|
||||
fn center(self) -> impl WidgetFn<Aligned>;
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W>;
|
||||
fn sized(self, size: impl Into<Size>) -> impl WidgetFn<Sized>;
|
||||
fn width(self, len: impl Into<Len>) -> impl WidgetFn<Sized>;
|
||||
fn height(self, len: impl Into<Len>) -> impl WidgetFn<Sized>;
|
||||
fn max_width(self, width: impl Into<Len>) -> impl WidgetFn<MaxSize>;
|
||||
fn max_height(self, height: impl Into<Len>) -> impl WidgetFn<MaxSize>;
|
||||
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Offset>;
|
||||
fn scroll(self) -> impl WidgetIdFn<Scroll>;
|
||||
fn masked(self) -> impl WidgetFn<Masked>;
|
||||
fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack>;
|
||||
fn foreground<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack>;
|
||||
fn layer_offset(self, offset: usize) -> impl WidgetFn<LayerOffset>;
|
||||
fn to_any(self) -> impl WidgetRet;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Pad> {
|
||||
|ui| Pad {
|
||||
padding: padding.into(),
|
||||
inner: self.add(ui).any(),
|
||||
}
|
||||
}
|
||||
|
||||
fn align(self, align: impl Into<Align>) -> impl WidgetFn<Aligned> {
|
||||
move |ui| Aligned {
|
||||
inner: self.add(ui).any(),
|
||||
align: align.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn center(self) -> impl WidgetFn<Aligned> {
|
||||
self.align(Align::CENTER)
|
||||
}
|
||||
|
||||
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W::Widget> {
|
||||
|ui| {
|
||||
let id = self.add(ui);
|
||||
ui.set_label(&id, label.into());
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
fn sized(self, size: impl Into<Size>) -> impl WidgetFn<Sized> {
|
||||
let size = size.into();
|
||||
move |ui| Sized {
|
||||
inner: self.add(ui).any(),
|
||||
x: Some(size.x),
|
||||
y: Some(size.y),
|
||||
}
|
||||
}
|
||||
|
||||
fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<MaxSize> {
|
||||
let len = len.into();
|
||||
move |ui| MaxSize {
|
||||
inner: self.add(ui).any(),
|
||||
x: Some(len),
|
||||
y: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<MaxSize> {
|
||||
let len = len.into();
|
||||
move |ui| MaxSize {
|
||||
inner: self.add(ui).any(),
|
||||
x: None,
|
||||
y: Some(len),
|
||||
}
|
||||
}
|
||||
|
||||
fn width(self, len: impl Into<Len>) -> impl WidgetFn<Sized> {
|
||||
let len = len.into();
|
||||
move |ui| Sized {
|
||||
inner: self.add(ui).any(),
|
||||
x: Some(len),
|
||||
y: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn height(self, len: impl Into<Len>) -> impl WidgetFn<Sized> {
|
||||
let len = len.into();
|
||||
move |ui| Sized {
|
||||
inner: self.add(ui).any(),
|
||||
x: None,
|
||||
y: Some(len),
|
||||
}
|
||||
}
|
||||
|
||||
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Offset> {
|
||||
move |ui| Offset {
|
||||
inner: self.add(ui).any(),
|
||||
amt: amt.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn scroll(self) -> impl WidgetIdFn<Scroll> {
|
||||
move |ui| {
|
||||
Scroll::new(self.add(ui).any(), Axis::Y)
|
||||
.on(CursorSense::Scroll, |ctx| {
|
||||
let s = &mut ctx.ui[ctx.id];
|
||||
s.scroll(ctx.data.scroll_delta.y * 50.0);
|
||||
})
|
||||
.add(ui)
|
||||
}
|
||||
}
|
||||
|
||||
fn masked(self) -> impl WidgetFn<Masked> {
|
||||
move |ui| Masked {
|
||||
inner: self.add(ui).any(),
|
||||
}
|
||||
}
|
||||
|
||||
fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack> {
|
||||
move |ui| Stack {
|
||||
children: vec![w.add(ui).any(), self.add(ui).any()],
|
||||
size: StackSize::Child(1),
|
||||
}
|
||||
}
|
||||
|
||||
fn foreground<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack> {
|
||||
move |ui| Stack {
|
||||
children: vec![self.add(ui).any(), w.add(ui).any()],
|
||||
size: StackSize::Child(0),
|
||||
}
|
||||
}
|
||||
|
||||
fn layer_offset(self, offset: usize) -> impl WidgetFn<LayerOffset> {
|
||||
move |ui| LayerOffset {
|
||||
inner: self.add(ui).any(),
|
||||
offset,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_any(self) -> impl WidgetRet {
|
||||
|ui| self.add(ui).any()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CoreWidgetArr<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> {
|
||||
fn span(self, dir: Dir) -> SpanBuilder<LEN, Wa, Tag>;
|
||||
fn stack(self) -> StackBuilder<LEN, Wa, Tag>;
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Wa, Tag> for Wa {
|
||||
fn span(self, dir: Dir) -> SpanBuilder<LEN, Wa, Tag> {
|
||||
SpanBuilder::new(self, dir)
|
||||
}
|
||||
fn stack(self) -> StackBuilder<LEN, Wa, Tag> {
|
||||
StackBuilder::new(self)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user