preparation
This commit is contained in:
@@ -1,13 +1,20 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub struct Regioned {
|
pub struct Padded {
|
||||||
pub region: UiRegion,
|
pub padding: Padding,
|
||||||
pub inner: WidgetId,
|
pub inner: WidgetId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Widget for Regioned {
|
impl Widget for Padded {
|
||||||
fn draw(&mut self, painter: &mut Painter) {
|
fn draw(&mut self, painter: &mut Painter) {
|
||||||
painter.draw_within(&self.inner, self.region);
|
painter.draw_within(&self.inner, self.padding.region());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||||
|
let mut size = ctx.size(&self.inner);
|
||||||
|
size.x += self.padding.left + self.padding.right;
|
||||||
|
size.y += self.padding.top + self.padding.bottom;
|
||||||
|
size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ impl Widget for Image {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn image(image: impl LoadableImage) -> WidgetFn!(Image) {
|
pub fn image(image: impl LoadableImage) -> impl WidgetFn<Image> {
|
||||||
let image = image.get_image().expect("Failed to load image");
|
let image = image.get_image().expect("Failed to load image");
|
||||||
move |ui| Image {
|
move |ui| Image {
|
||||||
handle: ui.add_texture(image),
|
handle: ui.add_texture(image),
|
||||||
|
|||||||
@@ -1,21 +1,25 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub trait Sensable<W, Tag> {
|
pub trait Sensable<W, Tag> {
|
||||||
fn on(self, sense: Senses, f: impl SenseFn) -> WidgetIdFn!(W);
|
fn on(self, sense: Senses, f: impl SenseFn) -> impl WidgetIdFn<W>;
|
||||||
fn id_on(
|
fn id_on(
|
||||||
self,
|
self,
|
||||||
senses: Senses,
|
senses: Senses,
|
||||||
f: impl FnMut(&WidgetId<W>, &mut Ui) + 'static + Clone,
|
f: impl FnMut(&WidgetId<W>, &mut Ui, SenseCtx) + 'static,
|
||||||
) -> WidgetIdFn!(W)
|
) -> impl WidgetIdFn<W>
|
||||||
where
|
where
|
||||||
W: Widget;
|
W: Widget;
|
||||||
fn edit_on(self, senses: Senses, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFn!(W)
|
fn edit_on(
|
||||||
|
self,
|
||||||
|
senses: Senses,
|
||||||
|
f: impl FnMut(&mut W, SenseCtx) + 'static,
|
||||||
|
) -> impl WidgetIdFn<W>
|
||||||
where
|
where
|
||||||
W: Widget;
|
W: Widget;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
|
impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
|
||||||
fn on(self, senses: Senses, f: impl SenseFn) -> WidgetIdFn!(W::Widget) {
|
fn on(self, senses: Senses, f: impl SenseFn) -> impl WidgetIdFn<W::Widget> {
|
||||||
move |ui| {
|
move |ui| {
|
||||||
let id = self.add(ui);
|
let id = self.add(ui);
|
||||||
ui.add_sensor(
|
ui.add_sensor(
|
||||||
@@ -31,24 +35,24 @@ impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
|
|||||||
fn id_on(
|
fn id_on(
|
||||||
self,
|
self,
|
||||||
senses: Senses,
|
senses: Senses,
|
||||||
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui) + 'static + Clone,
|
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui, SenseCtx) + 'static,
|
||||||
) -> WidgetIdFn!(W::Widget)
|
) -> impl WidgetIdFn<W::Widget>
|
||||||
where
|
where
|
||||||
W::Widget: Widget,
|
W::Widget: Widget,
|
||||||
{
|
{
|
||||||
self.with_id(move |ui, id| {
|
self.with_id(move |ui, id| {
|
||||||
let id2 = id.clone();
|
let id2 = id.clone();
|
||||||
ui.add(id.on(senses, move |ui| f(&id2, ui)))
|
ui.add(id.on(senses, move |ui, pos| f(&id2, ui, pos)))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
fn edit_on(
|
fn edit_on(
|
||||||
self,
|
self,
|
||||||
senses: Senses,
|
senses: Senses,
|
||||||
mut f: impl FnMut(&mut W::Widget) + 'static + Clone,
|
mut f: impl FnMut(&mut W::Widget, SenseCtx) + 'static,
|
||||||
) -> WidgetIdFn!(W::Widget)
|
) -> impl WidgetIdFn<W::Widget>
|
||||||
where
|
where
|
||||||
W::Widget: Widget,
|
W::Widget: Widget,
|
||||||
{
|
{
|
||||||
self.id_on(senses, move |id, ui| f(&mut ui[id]))
|
self.id_on(senses, move |id, ui, pos| f(&mut ui[id], pos))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ use crate::prelude::*;
|
|||||||
pub struct Text {
|
pub struct Text {
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub attrs: TextAttrs,
|
pub attrs: TextAttrs,
|
||||||
|
pub align: Align,
|
||||||
buf: TextBuffer,
|
buf: TextBuffer,
|
||||||
|
cursor: Cursor,
|
||||||
|
size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Text {
|
impl Text {
|
||||||
@@ -32,16 +35,34 @@ impl Text {
|
|||||||
content: content.into(),
|
content: content.into(),
|
||||||
buf: TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height)),
|
buf: TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height)),
|
||||||
attrs,
|
attrs,
|
||||||
|
align: Align::Center,
|
||||||
|
cursor: Cursor::None,
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn region(&self) -> UiRegion {
|
||||||
|
UiRegion::from_size_align(self.size, self.align)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Widget for Text {
|
impl Widget for Text {
|
||||||
fn draw(&mut self, painter: &mut Painter) {
|
fn draw(&mut self, painter: &mut Painter) {
|
||||||
let (handle, offset) = painter.render_text(&mut self.buf, &self.content, &self.attrs);
|
let (handle, offset) =
|
||||||
|
painter.render_text(&mut self.buf, &self.content, &self.attrs, &self.cursor);
|
||||||
let dims = handle.size();
|
let dims = handle.size();
|
||||||
let size = offset.size(&handle);
|
self.size = offset.size(&handle);
|
||||||
let mut region = Align::Center.pos().expand(size);
|
let mut region = self.region();
|
||||||
region.top_left.offset += offset.top_left;
|
region.top_left.offset += offset.top_left;
|
||||||
region.bot_right.offset = region.top_left.offset + dims;
|
region.bot_right.offset = region.top_left.offset + dims;
|
||||||
painter.draw_texture_within(&handle, region);
|
painter.draw_texture_within(&handle, region);
|
||||||
@@ -49,8 +70,7 @@ impl Widget for Text {
|
|||||||
|
|
||||||
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
|
||||||
let (handle, offset) =
|
let (handle, offset) =
|
||||||
ctx.text
|
ctx.draw_text(&mut self.buf, &self.content, &self.attrs, &self.cursor);
|
||||||
.draw(&mut self.buf, &self.content, &self.attrs, ctx.textures);
|
|
||||||
offset.size(&handle)
|
offset.size(&handle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,41 +2,33 @@ use super::*;
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub trait CoreWidget<W, Tag> {
|
pub trait CoreWidget<W, Tag> {
|
||||||
fn pad(self, padding: impl Into<Padding>) -> WidgetFn!(Regioned);
|
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded>;
|
||||||
fn align(self, align: Align) -> WidgetFn!(Aligned);
|
fn align(self, align: Align) -> impl WidgetFn<Aligned>;
|
||||||
fn center(self) -> WidgetFn!(Aligned);
|
fn center(self) -> impl WidgetFn<Aligned>;
|
||||||
fn region(self, region: UiRegion) -> WidgetFn!(Regioned);
|
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W>;
|
||||||
fn label(self, label: impl Into<String>) -> WidgetIdFn!(W);
|
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized>;
|
||||||
fn size(self, size: impl Into<Vec2>) -> WidgetFn!(Sized);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
||||||
fn pad(self, padding: impl Into<Padding>) -> WidgetFn!(Regioned) {
|
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Padded> {
|
||||||
|ui| Regioned {
|
|ui| Padded {
|
||||||
region: padding.into().region(),
|
padding: padding.into(),
|
||||||
inner: self.add(ui).erase_type(),
|
inner: self.add(ui).erase_type(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn align(self, align: Align) -> WidgetFn!(Aligned) {
|
fn align(self, align: Align) -> impl WidgetFn<Aligned> {
|
||||||
move |ui| Aligned {
|
move |ui| Aligned {
|
||||||
inner: self.add(ui).erase_type(),
|
inner: self.add(ui).erase_type(),
|
||||||
align,
|
align,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn center(self) -> WidgetFn!(Aligned) {
|
fn center(self) -> impl WidgetFn<Aligned> {
|
||||||
self.align(Align::Center)
|
self.align(Align::Center)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn region(self, region: UiRegion) -> WidgetFn!(Regioned) {
|
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<W::Widget> {
|
||||||
move |ui| Regioned {
|
|
||||||
region,
|
|
||||||
inner: self.add(ui).erase_type(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn label(self, label: impl Into<String>) -> WidgetIdFn!(W::Widget) {
|
|
||||||
|ui| {
|
|ui| {
|
||||||
let id = self.add(ui);
|
let id = self.add(ui);
|
||||||
ui.set_label(&id, label.into());
|
ui.set_label(&id, label.into());
|
||||||
@@ -44,7 +36,7 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size(self, size: impl Into<Vec2>) -> WidgetFn!(Sized) {
|
fn size(self, size: impl Into<Vec2>) -> impl WidgetFn<Sized> {
|
||||||
move |ui| Sized {
|
move |ui| Sized {
|
||||||
inner: self.add(ui).erase_type(),
|
inner: self.add(ui).erase_type(),
|
||||||
size: size.into(),
|
size: size.into(),
|
||||||
@@ -53,43 +45,25 @@ impl<W: WidgetLike<Tag>, Tag> CoreWidget<W::Widget, Tag> for W {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait CoreWidgetArr<const LEN: usize, Tag> {
|
pub trait CoreWidgetArr<const LEN: usize, Tag> {
|
||||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> WidgetFn!(Span);
|
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span>;
|
||||||
fn stack(self) -> WidgetFn!(Stack);
|
fn stack(self) -> impl WidgetFn<Stack>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Tag> for Wa {
|
impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> CoreWidgetArr<LEN, Tag> for Wa {
|
||||||
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> WidgetFn!(Span) {
|
fn span(self, dir: Dir, lengths: impl IntoSpanLens<LEN>) -> impl WidgetFn<Span> {
|
||||||
let lengths = lengths.into_lens();
|
let lengths = lengths.into_lens();
|
||||||
move |ui| Span {
|
move |ui| Span {
|
||||||
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
|
children: self.ui(ui).arr.into_iter().zip(lengths).collect(),
|
||||||
dir,
|
dir,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn stack(self) -> WidgetFn!(Stack) {
|
fn stack(self) -> impl WidgetFn<Stack> {
|
||||||
move |ui| Stack {
|
move |ui| Stack {
|
||||||
children: self.ui(ui).arr.to_vec(),
|
children: self.ui(ui).arr.to_vec(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub struct SpanBuilder<const LEN: usize, Tag, Wa: WidgetArrLike<LEN, Tag>> {
|
|
||||||
// children: Wa,
|
|
||||||
// dir: Dir,
|
|
||||||
// align: Align,
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// impl WidgetLike<FnTag> for SpanBuilder {
|
|
||||||
// type Widget = Span;
|
|
||||||
//
|
|
||||||
// fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget> {
|
|
||||||
// ui.add_widget(Span {
|
|
||||||
// children: self.children,
|
|
||||||
// dir: self.dir,
|
|
||||||
// align: self.align,
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub trait IntoSpanLens<const LEN: usize> {
|
pub trait IntoSpanLens<const LEN: usize> {
|
||||||
fn into_lens(self) -> [SpanLen; LEN];
|
fn into_lens(self) -> [SpanLen; LEN];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ impl<T: ColorNum> Color<T> {
|
|||||||
self.a = a;
|
self.a = a;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn as_arr(self) -> [T; 4] {
|
||||||
|
[self.r, self.g, self.b, self.a]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ColorNum {
|
pub trait ColorNum {
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
use std::{any::TypeId, marker::PhantomData, sync::mpsc::Sender};
|
use std::{
|
||||||
|
any::TypeId,
|
||||||
|
marker::PhantomData,
|
||||||
|
sync::{
|
||||||
|
Arc,
|
||||||
|
atomic::{AtomicBool, Ordering},
|
||||||
|
mpsc::Sender,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{FnTag, Ui, Widget, WidgetLike, WidgetTag},
|
layout::{FnTag, Ui, Widget, WidgetLike, WidgetTag},
|
||||||
@@ -21,7 +29,7 @@ pub struct WidgetId<W = AnyWidget> {
|
|||||||
pub(super) id: Id,
|
pub(super) id: Id,
|
||||||
counter: RefCounter,
|
counter: RefCounter,
|
||||||
send: Sender<Id>,
|
send: Sender<Id>,
|
||||||
is_static: bool,
|
is_static: Arc<AtomicBool>,
|
||||||
_pd: PhantomData<W>,
|
_pd: PhantomData<W>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +60,7 @@ impl<W> Clone for WidgetId<W> {
|
|||||||
ty: self.ty,
|
ty: self.ty,
|
||||||
counter: self.counter.clone(),
|
counter: self.counter.clone(),
|
||||||
send: self.send.clone(),
|
send: self.send.clone(),
|
||||||
is_static: self.is_static,
|
is_static: self.is_static.clone(),
|
||||||
_pd: PhantomData,
|
_pd: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,7 +73,7 @@ impl<W> WidgetId<W> {
|
|||||||
id,
|
id,
|
||||||
counter: RefCounter::new(),
|
counter: RefCounter::new(),
|
||||||
send,
|
send,
|
||||||
is_static,
|
is_static: Arc::new(is_static.into()),
|
||||||
_pd: PhantomData,
|
_pd: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,11 +96,11 @@ impl<W> WidgetId<W> {
|
|||||||
self.counter.refs()
|
self.counter.refs()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn into_static(self) -> StaticWidgetId<W> {
|
pub fn into_static(self) -> StaticWidgetId<W> {
|
||||||
let s = std::mem::ManuallyDrop::new(self);
|
self.is_static.store(true, Ordering::Release);
|
||||||
StaticWidgetId {
|
StaticWidgetId {
|
||||||
ty: s.ty,
|
ty: self.ty,
|
||||||
id: s.id.copyable(),
|
id: self.id.copyable(),
|
||||||
_pd: PhantomData,
|
_pd: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,7 +118,7 @@ impl WidgetId {
|
|||||||
|
|
||||||
impl<W> Drop for WidgetId<W> {
|
impl<W> Drop for WidgetId<W> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if self.counter.drop() && !self.is_static {
|
if self.counter.drop() && !self.is_static.load(Ordering::Acquire) {
|
||||||
let _ = self.send.send(self.id.duplicate());
|
let _ = self.send.send(self.id.duplicate());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,21 +127,14 @@ impl<W> Drop for WidgetId<W> {
|
|||||||
pub struct IdTag;
|
pub struct IdTag;
|
||||||
pub struct IdFnTag;
|
pub struct IdFnTag;
|
||||||
|
|
||||||
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui) -> WidgetId<W>;
|
pub trait WidgetIdFn<W>: FnOnce(&mut Ui) -> WidgetId<W> {}
|
||||||
macro_rules! WidgetIdFn {
|
impl<W, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetIdFn<W> for F {}
|
||||||
($W:ty) => {
|
|
||||||
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W>
|
|
||||||
};
|
|
||||||
($W:ty, $($use:tt)*) => {
|
|
||||||
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W> + use<$($use)*>
|
|
||||||
};
|
|
||||||
}
|
|
||||||
pub(crate) use WidgetIdFn;
|
|
||||||
|
|
||||||
|
/// TODO: does this ever make sense to use? it allows for invalid ids
|
||||||
pub trait Idable<Tag> {
|
pub trait Idable<Tag> {
|
||||||
type Widget: Widget;
|
type Widget: Widget;
|
||||||
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
|
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
|
||||||
fn id<'a>(self, id: &WidgetId<Self::Widget>) -> WidgetIdFn!(Self::Widget, 'a, Self, Tag)
|
fn id(self, id: &WidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
@@ -143,10 +144,7 @@ pub trait Idable<Tag> {
|
|||||||
id
|
id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn id_static<'a>(
|
fn id_static(self, id: StaticWidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
|
||||||
self,
|
|
||||||
id: StaticWidgetId<Self::Widget>,
|
|
||||||
) -> WidgetIdFn!(Self::Widget, 'a, Self, Tag)
|
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ use std::ops::Range;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{
|
layout::{
|
||||||
Active, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
|
Active, Cursor, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset,
|
||||||
Textures, UiRegion, Vec2, WidgetId, Widgets,
|
TextureHandle, Textures, UiRegion, Vec2, WidgetId, Widgets,
|
||||||
},
|
},
|
||||||
render::{Primitive, PrimitiveHandle, Primitives},
|
render::{Primitive, PrimitiveHandle, Primitives},
|
||||||
util::{HashSet, Id},
|
util::{HashSet, Id},
|
||||||
@@ -251,14 +251,15 @@ impl<'a, 'c> Painter<'a, 'c> {
|
|||||||
buffer: &mut TextBuffer,
|
buffer: &mut TextBuffer,
|
||||||
content: &str,
|
content: &str,
|
||||||
attrs: &TextAttrs,
|
attrs: &TextAttrs,
|
||||||
|
cursor: &Cursor,
|
||||||
) -> (TextureHandle, TextOffset) {
|
) -> (TextureHandle, TextOffset) {
|
||||||
self.ctx
|
self.ctx
|
||||||
.text
|
.text
|
||||||
.draw(buffer, content, attrs, self.ctx.textures)
|
.draw(buffer, content, attrs, cursor, self.ctx.textures)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn region_size(&self) -> Vec2 {
|
pub fn region(&self) -> UiRegion {
|
||||||
self.region.in_size(self.ctx.screen_size)
|
self.region
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
||||||
|
|||||||
@@ -181,8 +181,8 @@ impl UiRegion {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_screen(&self, size: Vec2) -> ScreenRect {
|
pub fn to_screen(&self, size: Vec2) -> ScreenRegion {
|
||||||
ScreenRect {
|
ScreenRegion {
|
||||||
top_left: self.top_left.anchor * size + self.top_left.offset,
|
top_left: self.top_left.anchor * size + self.top_left.offset,
|
||||||
bot_right: self.bot_right.anchor * size + self.bot_right.offset,
|
bot_right: self.bot_right.anchor * size + self.bot_right.offset,
|
||||||
}
|
}
|
||||||
@@ -213,11 +213,11 @@ impl UiRegion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ScreenRect {
|
pub struct ScreenRegion {
|
||||||
top_left: Vec2,
|
pub top_left: Vec2,
|
||||||
bot_right: Vec2,
|
pub bot_right: Vec2,
|
||||||
}
|
}
|
||||||
impl ScreenRect {
|
impl ScreenRegion {
|
||||||
pub fn contains(&self, pos: Vec2) -> bool {
|
pub fn contains(&self, pos: Vec2) -> bool {
|
||||||
pos.x >= self.top_left.x
|
pos.x >= self.top_left.x
|
||||||
&& pos.x <= self.bot_right.x
|
&& pos.x <= self.bot_right.x
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::any::Any;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{Ui, UiRegion, Vec2, WidgetId},
|
layout::{Ui, UiRegion, Vec2, WidgetId},
|
||||||
util::{HashMap, Id, bitflags},
|
util::{HashMap, Id, bitflags},
|
||||||
@@ -42,15 +44,15 @@ pub struct SensorGroup {
|
|||||||
pub cursor: ActivationState,
|
pub cursor: ActivationState,
|
||||||
pub sensors: Vec<Sensor>,
|
pub sensors: Vec<Sensor>,
|
||||||
}
|
}
|
||||||
pub trait SenseFn: FnMut(&mut Ui) + 'static {
|
|
||||||
fn box_clone(&self) -> Box<dyn SenseFn>;
|
pub struct SenseCtx {
|
||||||
}
|
pub cursor: Vec2,
|
||||||
impl<F: FnMut(&mut Ui) + 'static + Clone> SenseFn for F {
|
pub size: Vec2,
|
||||||
fn box_clone(&self) -> Box<dyn SenseFn> {
|
|
||||||
Box::new(self.clone())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait SenseFn: FnMut(&mut Ui, SenseCtx) + 'static {}
|
||||||
|
impl<F: FnMut(&mut Ui, SenseCtx) + 'static> SenseFn for F {}
|
||||||
|
|
||||||
impl Ui {
|
impl Ui {
|
||||||
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor) {
|
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor) {
|
||||||
self.sensor_map
|
self.sensor_map
|
||||||
@@ -72,7 +74,11 @@ impl Ui {
|
|||||||
|
|
||||||
for sensor in &mut group.sensors {
|
for sensor in &mut group.sensors {
|
||||||
if should_run(sensor.senses, group.cursor, group.hover) {
|
if should_run(sensor.senses, group.cursor, group.hover) {
|
||||||
(sensor.f.box_clone())(self);
|
let ctx = SenseCtx {
|
||||||
|
cursor: cursor.pos - region.top_left,
|
||||||
|
size: region.bot_right - region.top_left,
|
||||||
|
};
|
||||||
|
(sensor.f)(self, ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,21 +138,3 @@ impl ActivationState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for SensorGroup {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
hover: self.hover,
|
|
||||||
cursor: self.cursor,
|
|
||||||
sensors: self.sensors.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Clone for Sensor {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self {
|
|
||||||
senses: self.senses,
|
|
||||||
f: self.f.box_clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,6 +28,16 @@ pub struct TextAttrs {
|
|||||||
pub family: Family<'static>,
|
pub family: Family<'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub enum Cursor {
|
||||||
|
#[default]
|
||||||
|
None,
|
||||||
|
Select {
|
||||||
|
line: usize,
|
||||||
|
idx: usize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
pub type TextBuffer = Buffer;
|
pub type TextBuffer = Buffer;
|
||||||
|
|
||||||
impl Default for TextAttrs {
|
impl Default for TextAttrs {
|
||||||
@@ -48,6 +58,7 @@ impl TextData {
|
|||||||
buffer: &mut TextBuffer,
|
buffer: &mut TextBuffer,
|
||||||
content: &str,
|
content: &str,
|
||||||
attrs: &TextAttrs,
|
attrs: &TextAttrs,
|
||||||
|
cursor: &Cursor,
|
||||||
textures: &mut Textures,
|
textures: &mut Textures,
|
||||||
) -> (TextureHandle, TextOffset) {
|
) -> (TextureHandle, TextOffset) {
|
||||||
buffer.set_metrics(
|
buffer.set_metrics(
|
||||||
@@ -67,7 +78,14 @@ impl TextData {
|
|||||||
let mut max_y = 0;
|
let mut max_y = 0;
|
||||||
let c = attrs.color;
|
let c = attrs.color;
|
||||||
let mut max_width = 0.0f32;
|
let mut max_width = 0.0f32;
|
||||||
for run in buffer.layout_runs() {
|
let mut i = 0;
|
||||||
|
let mut cursor_x = 0;
|
||||||
|
for (run_i, run) in buffer.layout_runs().enumerate() {
|
||||||
|
if let Cursor::Select { line, .. } = cursor
|
||||||
|
&& *line == run_i
|
||||||
|
{
|
||||||
|
cursor_x = run.line_w as i32;
|
||||||
|
}
|
||||||
for glyph in run.glyphs.iter() {
|
for glyph in run.glyphs.iter() {
|
||||||
let physical_glyph = glyph.physical((0., 0.), 1.0);
|
let physical_glyph = glyph.physical((0., 0.), 1.0);
|
||||||
|
|
||||||
@@ -76,6 +94,14 @@ impl TextData {
|
|||||||
None => cosmic_text::Color::rgba(c.r, c.g, c.b, c.a),
|
None => cosmic_text::Color::rgba(c.r, c.g, c.b, c.a),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if let Cursor::Select { idx, line } = cursor
|
||||||
|
&& *line == run_i
|
||||||
|
&& *idx == i
|
||||||
|
{
|
||||||
|
cursor_x = physical_glyph.x;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
|
||||||
self.swash_cache.with_pixels(
|
self.swash_cache.with_pixels(
|
||||||
&mut self.font_system,
|
&mut self.font_system,
|
||||||
physical_glyph.cache_key,
|
physical_glyph.cache_key,
|
||||||
@@ -93,6 +119,11 @@ impl TextData {
|
|||||||
}
|
}
|
||||||
max_width = max_width.max(run.line_w);
|
max_width = max_width.max(run.line_w);
|
||||||
}
|
}
|
||||||
|
if let &Cursor::Select { line, .. } = cursor {
|
||||||
|
let y = (attrs.line_height * (line + 1) as f32) as i32 - 1;
|
||||||
|
max_y = max_y.max(y);
|
||||||
|
max_x = max_x.max(cursor_x);
|
||||||
|
}
|
||||||
let width = (max_x - min_x + 1) as u32;
|
let width = (max_x - min_x + 1) as u32;
|
||||||
let height = (max_y - min_y + 1) as u32;
|
let height = (max_y - min_y + 1) as u32;
|
||||||
let mut image = RgbaImage::new(width, height);
|
let mut image = RgbaImage::new(width, height);
|
||||||
@@ -101,6 +132,15 @@ impl TextData {
|
|||||||
let y = (y - min_y) as u32;
|
let y = (y - min_y) as u32;
|
||||||
image.put_pixel(x, y, color);
|
image.put_pixel(x, y, color);
|
||||||
}
|
}
|
||||||
|
if let &Cursor::Select { line, .. } = cursor {
|
||||||
|
for y in 0..attrs.line_height as u32 {
|
||||||
|
// no clue if this is good or bad for non integer values
|
||||||
|
// depends on how the layouting is actually done
|
||||||
|
let x = (cursor_x - min_x) as u32;
|
||||||
|
let y = (y as f32 + attrs.line_height * line as f32 - min_y as f32) as u32;
|
||||||
|
image.put_pixel(x, y, Rgba(c.as_arr()));
|
||||||
|
}
|
||||||
|
}
|
||||||
let offset = TextOffset {
|
let offset = TextOffset {
|
||||||
top_left: Vec2::new(min_x as f32, min_y as f32),
|
top_left: Vec2::new(min_x as f32, min_y as f32),
|
||||||
bot_right: Vec2::new(
|
bot_right: Vec2::new(
|
||||||
|
|||||||
@@ -15,9 +15,8 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct Ui {
|
pub struct Ui {
|
||||||
base: Option<WidgetId>,
|
root: Option<WidgetId>,
|
||||||
widgets: Widgets,
|
widgets: Widgets,
|
||||||
labels: HashMap<Id, String>,
|
|
||||||
updates: Vec<Id>,
|
updates: Vec<Id>,
|
||||||
recv: Receiver<Id>,
|
recv: Receiver<Id>,
|
||||||
pub(super) send: Sender<Id>,
|
pub(super) send: Sender<Id>,
|
||||||
@@ -41,6 +40,7 @@ pub struct Widgets {
|
|||||||
pub struct WidgetData {
|
pub struct WidgetData {
|
||||||
widget: Box<dyn Widget>,
|
widget: Box<dyn Widget>,
|
||||||
borrowed: bool,
|
borrowed: bool,
|
||||||
|
label: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ui {
|
impl Ui {
|
||||||
@@ -58,7 +58,7 @@ impl Ui {
|
|||||||
|
|
||||||
/// useful for debugging
|
/// useful for debugging
|
||||||
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
|
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
|
||||||
self.labels.insert(id.id.duplicate(), label);
|
*self.widgets.label_mut(id).unwrap() = label;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||||
@@ -67,8 +67,6 @@ impl Ui {
|
|||||||
|
|
||||||
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||||
let id = self.id();
|
let id = self.id();
|
||||||
self.labels
|
|
||||||
.insert(id.id.duplicate(), std::any::type_name::<W>().to_string());
|
|
||||||
self.widgets.insert(id.id.duplicate(), w);
|
self.widgets.insert(id.id.duplicate(), w);
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
@@ -77,8 +75,8 @@ impl Ui {
|
|||||||
self.widgets.insert(id.id.duplicate(), w);
|
self.widgets.insert(id.id.duplicate(), w);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Tag>) {
|
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
|
||||||
self.base = Some(w.add(self).erase_type());
|
self.root = Some(w.add(self).erase_type());
|
||||||
self.full_redraw = true;
|
self.full_redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,8 +127,8 @@ impl Ui {
|
|||||||
&mut self.text,
|
&mut self.text,
|
||||||
self.size,
|
self.size,
|
||||||
);
|
);
|
||||||
if let Some(base) = &self.base {
|
if let Some(root) = &self.root {
|
||||||
ctx.draw(&base.id);
|
ctx.draw(&root.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +160,6 @@ impl Ui {
|
|||||||
/// free any resources that don't have references anymore
|
/// free any resources that don't have references anymore
|
||||||
fn free(&mut self) {
|
fn free(&mut self) {
|
||||||
for id in self.recv.try_iter() {
|
for id in self.recv.try_iter() {
|
||||||
self.labels.remove(&id);
|
|
||||||
self.widgets.delete(id);
|
self.widgets.delete(id);
|
||||||
}
|
}
|
||||||
self.textures.free();
|
self.textures.free();
|
||||||
@@ -175,6 +172,10 @@ impl Ui {
|
|||||||
pub fn num_widgets(&self) -> usize {
|
pub fn num_widgets(&self) -> usize {
|
||||||
self.widgets.len()
|
self.widgets.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn active_widgets(&self) -> usize {
|
||||||
|
self.active.widgets.len()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget> Index<&WidgetId<W>> for Ui {
|
impl<W: Widget> Index<&WidgetId<W>> for Ui {
|
||||||
@@ -254,16 +255,25 @@ impl Widgets {
|
|||||||
self.get_dyn_mut(&id.id)?.as_any_mut().downcast_mut()
|
self.get_dyn_mut(&id.id)?.as_any_mut().downcast_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, id: Id, widget: impl Widget) {
|
pub fn insert<W: Widget>(&mut self, id: Id, widget: W) {
|
||||||
self.insert_any(id, Box::new(widget));
|
self.insert_any(id, Box::new(widget), std::any::type_name::<W>().to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>) {
|
pub fn label<W>(&self, id: &WidgetId<W>) -> Option<&String> {
|
||||||
|
self.map.get(&id.id).map(|d| &d.label)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn label_mut<W>(&mut self, id: &WidgetId<W>) -> Option<&mut String> {
|
||||||
|
self.map.get_mut(&id.id).map(|d| &mut d.label)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>, label: String) {
|
||||||
self.map.insert(
|
self.map.insert(
|
||||||
id,
|
id,
|
||||||
WidgetData {
|
WidgetData {
|
||||||
widget,
|
widget,
|
||||||
borrowed: false,
|
borrowed: false,
|
||||||
|
label,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -331,9 +341,8 @@ impl Default for Ui {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let (send, recv) = channel();
|
let (send, recv) = channel();
|
||||||
Self {
|
Self {
|
||||||
base: Default::default(),
|
root: Default::default(),
|
||||||
widgets: Widgets::new(),
|
widgets: Widgets::new(),
|
||||||
labels: Default::default(),
|
|
||||||
updates: Default::default(),
|
updates: Default::default(),
|
||||||
primitives: Default::default(),
|
primitives: Default::default(),
|
||||||
textures: Textures::new(),
|
textures: Textures::new(),
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use crate::layout::{Painter, TextData, Textures, Ui, Vec2, WidgetId, WidgetIdFn, Widgets};
|
use crate::layout::{
|
||||||
|
Cursor, Painter, StaticWidgetId, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
|
||||||
|
Textures, Ui, Vec2, WidgetId, WidgetIdFn, Widgets,
|
||||||
|
};
|
||||||
|
|
||||||
use std::{any::Any, marker::PhantomData};
|
use std::{any::Any, marker::PhantomData};
|
||||||
|
|
||||||
@@ -20,6 +23,16 @@ impl SizeCtx<'_> {
|
|||||||
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
||||||
self.widgets.get_dyn_dynamic(&id.id).get_size(self)
|
self.widgets.get_dyn_dynamic(&id.id).get_size(self)
|
||||||
}
|
}
|
||||||
|
pub fn draw_text(
|
||||||
|
&mut self,
|
||||||
|
buffer: &mut TextBuffer,
|
||||||
|
content: &str,
|
||||||
|
attrs: &TextAttrs,
|
||||||
|
cursor: &Cursor,
|
||||||
|
) -> (TextureHandle, TextOffset) {
|
||||||
|
self.text
|
||||||
|
.draw(buffer, content, attrs, cursor, self.textures)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WidgetTag;
|
pub struct WidgetTag;
|
||||||
@@ -31,7 +44,7 @@ pub trait WidgetLike<Tag> {
|
|||||||
fn with_id<W2>(
|
fn with_id<W2>(
|
||||||
self,
|
self,
|
||||||
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
||||||
) -> WidgetIdFn!(W2)
|
) -> impl WidgetIdFn<W2>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
@@ -40,20 +53,19 @@ pub trait WidgetLike<Tag> {
|
|||||||
f(ui, id)
|
f(ui, id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn add_static(self, ui: &mut Ui) -> StaticWidgetId<Self::Widget>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
self.add(ui).into_static()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub trait WidgetFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
|
|
||||||
|
|
||||||
/// A function that returns a widget given a UI.
|
/// A function that returns a widget given a UI.
|
||||||
/// Useful for defining trait functions on widgets that create a parent widget so that the children
|
/// Useful for defining trait functions on widgets that create a parent widget so that the children
|
||||||
/// don't need to be IDs yet
|
/// don't need to be IDs yet
|
||||||
/// currently a macro for rust analyzer (doesn't support trait aliases atm)
|
pub trait WidgetFn<W: Widget>: FnOnce(&mut Ui) -> W {}
|
||||||
macro_rules! WidgetFn {
|
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
|
||||||
($W:ty) => {
|
|
||||||
impl FnOnce(&mut $crate::layout::Ui) -> $W
|
|
||||||
};
|
|
||||||
}
|
|
||||||
pub(crate) use WidgetFn;
|
|
||||||
|
|
||||||
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
|
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ pub struct Client {
|
|||||||
input: Input,
|
input: Input,
|
||||||
ui: Ui,
|
ui: Ui,
|
||||||
info: WidgetId<Text>,
|
info: WidgetId<Text>,
|
||||||
|
selected: Option<WidgetId<Text>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
@@ -30,58 +31,58 @@ impl Client {
|
|||||||
|
|
||||||
let mut ui = Ui::new();
|
let mut ui = Ui::new();
|
||||||
let rrect = rect(Color::WHITE).radius(20);
|
let rrect = rect(Color::WHITE).radius(20);
|
||||||
let pad_test = ui.add_static(
|
let pad_test = (
|
||||||
|
rrect.color(Color::BLUE),
|
||||||
(
|
(
|
||||||
rrect.color(Color::BLUE),
|
rrect.color(Color::RED).size(100).center(),
|
||||||
(
|
(
|
||||||
rrect.color(Color::RED).size(100).center(),
|
rrect.color(Color::ORANGE),
|
||||||
(
|
rrect.color(Color::LIME).pad(10.0),
|
||||||
rrect.color(Color::ORANGE),
|
|
||||||
rrect.color(Color::LIME).pad(10.0),
|
|
||||||
)
|
|
||||||
.span(Dir::RIGHT, [1, 1]),
|
|
||||||
rrect.color(Color::YELLOW),
|
|
||||||
)
|
)
|
||||||
.span(Dir::RIGHT, [2, 2, 1])
|
.span(Dir::RIGHT, [1, 1]),
|
||||||
.pad(10),
|
rrect.color(Color::YELLOW),
|
||||||
)
|
)
|
||||||
.span(Dir::RIGHT, [1, 3]),
|
.span(Dir::RIGHT, [2, 2, 1])
|
||||||
);
|
.pad(10),
|
||||||
let span_test = ui.add_static(
|
)
|
||||||
(
|
.span(Dir::RIGHT, [1, 3])
|
||||||
rrect.color(Color::GREEN),
|
.add_static(&mut ui);
|
||||||
rrect.color(Color::ORANGE),
|
|
||||||
rrect.color(Color::CYAN),
|
|
||||||
rrect.color(Color::BLUE),
|
|
||||||
rrect.color(Color::MAGENTA),
|
|
||||||
rrect.color(Color::RED),
|
|
||||||
)
|
|
||||||
.span(
|
|
||||||
Dir::LEFT,
|
|
||||||
[
|
|
||||||
fixed(100),
|
|
||||||
ratio(1),
|
|
||||||
ratio(1),
|
|
||||||
relative(0.5),
|
|
||||||
fixed(100),
|
|
||||||
fixed(100),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
let span_add = ui.add_static(Span::empty(Dir::RIGHT));
|
|
||||||
|
|
||||||
let main = ui.add_static(pad_test.pad(10));
|
let span_test = (
|
||||||
|
rrect.color(Color::GREEN),
|
||||||
|
rrect.color(Color::ORANGE),
|
||||||
|
rrect.color(Color::CYAN),
|
||||||
|
rrect.color(Color::BLUE),
|
||||||
|
rrect.color(Color::MAGENTA),
|
||||||
|
rrect.color(Color::RED),
|
||||||
|
)
|
||||||
|
.span(
|
||||||
|
Dir::LEFT,
|
||||||
|
[
|
||||||
|
fixed(100),
|
||||||
|
ratio(1),
|
||||||
|
ratio(1),
|
||||||
|
relative(0.5),
|
||||||
|
fixed(100),
|
||||||
|
fixed(100),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.add_static(&mut ui);
|
||||||
|
|
||||||
|
let span_add = Span::empty(Dir::RIGHT).add_static(&mut ui);
|
||||||
|
|
||||||
|
let main = pad_test.pad(10).add_static(&mut ui);
|
||||||
|
|
||||||
let switch_button = |color, to, label| {
|
let switch_button = |color, to, label| {
|
||||||
let rect = rect(color)
|
let rect = rect(color)
|
||||||
.id_on(PRESS_START, move |id, ui| {
|
.id_on(PRESS_START, move |id, ui, _| {
|
||||||
ui[main].inner.set_static(to);
|
ui[main].inner.set_static(to);
|
||||||
ui[id].color = color.add_rgb(-0.2);
|
ui[id].color = color.add_rgb(-0.2);
|
||||||
})
|
})
|
||||||
.edit_on(HOVER_START | PRESS_END, move |r| {
|
.edit_on(HOVER_START | PRESS_END, move |r, _| {
|
||||||
r.color = color.add_rgb(0.4);
|
r.color = color.add_rgb(0.4);
|
||||||
})
|
})
|
||||||
.edit_on(HOVER_END, move |r| {
|
.edit_on(HOVER_END, move |r, _| {
|
||||||
r.color = color;
|
r.color = color;
|
||||||
});
|
});
|
||||||
(rect, text(label).font_size(30)).stack()
|
(rect, text(label).font_size(30)).stack()
|
||||||
@@ -89,38 +90,52 @@ impl Client {
|
|||||||
|
|
||||||
let btext = |content| text(content).font_size(30);
|
let btext = |content| text(content).font_size(30);
|
||||||
|
|
||||||
let text_test = ui.add_static(
|
let text_test = (
|
||||||
|
btext("this is a").align(Align::Left),
|
||||||
|
btext("teeeeeeeest").align(Align::Right),
|
||||||
|
btext("okkk\nokkkkkk!").align(Align::Left),
|
||||||
|
btext("hmm"),
|
||||||
|
btext("a"),
|
||||||
(
|
(
|
||||||
btext("this is a").align(Align::Left),
|
btext("'").family(Family::Monospace).align(Align::Top),
|
||||||
btext("teeeeeeeest").align(Align::Left),
|
btext("'").family(Family::Monospace),
|
||||||
btext("okkk\nokkkkkk!").align(Align::Left),
|
btext(":gamer mode").family(Family::Monospace),
|
||||||
btext("hmm"),
|
rect(Color::CYAN).size(10).center(),
|
||||||
btext("a"),
|
rect(Color::RED).size(100).center(),
|
||||||
(
|
|
||||||
btext("'").family(Family::Monospace).align(Align::Top),
|
|
||||||
btext("'").family(Family::Monospace),
|
|
||||||
btext(":gamer mode").family(Family::Monospace),
|
|
||||||
rect(Color::CYAN).size(10).center(),
|
|
||||||
rect(Color::RED).size(100).center(),
|
|
||||||
)
|
|
||||||
.span(Dir::RIGHT, sized())
|
|
||||||
.center(),
|
|
||||||
text("pretty cool right?").font_size(50),
|
|
||||||
)
|
)
|
||||||
.span(Dir::DOWN, sized()),
|
.span(Dir::RIGHT, sized())
|
||||||
);
|
.center(),
|
||||||
|
text("pretty cool right?").font_size(50),
|
||||||
|
)
|
||||||
|
.span(Dir::DOWN, sized())
|
||||||
|
.add_static(&mut ui);
|
||||||
|
|
||||||
|
let texts = Span::empty(Dir::DOWN).add(&mut ui);
|
||||||
|
let text_edit_scroll = (
|
||||||
|
texts,
|
||||||
|
text("add").font_size(30).edit_on(PRESS_START, |text, ctx| {
|
||||||
|
text.select(ctx.cursor, ctx.size);
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.span(Dir::DOWN, [ratio(1), fixed(40)])
|
||||||
|
.add_static(&mut ui);
|
||||||
|
|
||||||
let tabs = (
|
let tabs = (
|
||||||
switch_button(Color::RED, pad_test, "pad test"),
|
switch_button(Color::RED, pad_test, "pad"),
|
||||||
switch_button(Color::GREEN, span_test, "span test"),
|
switch_button(Color::GREEN, span_test, "span"),
|
||||||
switch_button(Color::BLUE, span_add, "span add test"),
|
switch_button(Color::BLUE, span_add, "image span"),
|
||||||
switch_button(Color::MAGENTA, text_test, "text test"),
|
switch_button(Color::MAGENTA, text_test, "text layout"),
|
||||||
|
switch_button(
|
||||||
|
Color::YELLOW.mul_rgb(0.5),
|
||||||
|
text_edit_scroll,
|
||||||
|
"text edit scroll",
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.span(Dir::RIGHT, ratio(1));
|
.span(Dir::RIGHT, ratio(1));
|
||||||
|
|
||||||
let add_button = rect(Color::LIME)
|
let add_button = rect(Color::LIME)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
.on(PRESS_START, move |ui| {
|
.on(PRESS_START, move |ui, _| {
|
||||||
let child = ui
|
let child = ui
|
||||||
.add(image(include_bytes!("assets/sungals.png")).center())
|
.add(image(include_bytes!("assets/sungals.png")).center())
|
||||||
.erase_type();
|
.erase_type();
|
||||||
@@ -131,17 +146,15 @@ impl Client {
|
|||||||
|
|
||||||
let del_button = rect(Color::RED)
|
let del_button = rect(Color::RED)
|
||||||
.radius(30)
|
.radius(30)
|
||||||
.on(PRESS_START, move |ui| {
|
.on(PRESS_START, move |ui, _| {
|
||||||
ui[span_add].children.pop();
|
ui[span_add].children.pop();
|
||||||
})
|
})
|
||||||
.size(150)
|
.size(150)
|
||||||
.align(Align::BotLeft);
|
.align(Align::BotLeft);
|
||||||
|
|
||||||
let info = ui.add(text(""));
|
let info = text("").add(&mut ui);
|
||||||
let info_sect = info
|
let info_sect = info.clone().pad(10).align(Align::BotLeft);
|
||||||
.clone()
|
ui.set_root(
|
||||||
.region(Align::TopRight.pos().expand((150, 150)).shifted((-75, 0)));
|
|
||||||
ui.set_base(
|
|
||||||
(
|
(
|
||||||
tabs.label("tabs"),
|
tabs.label("tabs"),
|
||||||
(
|
(
|
||||||
@@ -162,6 +175,7 @@ impl Client {
|
|||||||
input: Input::default(),
|
input: Input::default(),
|
||||||
ui,
|
ui,
|
||||||
info,
|
info,
|
||||||
|
selected: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,8 +198,9 @@ impl Client {
|
|||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
let new = format!(
|
let new = format!(
|
||||||
"widgets: {}\nviews: {}",
|
"widgets: {}\nactive:{}\nviews: {}",
|
||||||
self.ui.num_widgets(),
|
self.ui.num_widgets(),
|
||||||
|
self.ui.active_widgets(),
|
||||||
self.renderer.ui.view_count()
|
self.renderer.ui.view_count()
|
||||||
);
|
);
|
||||||
if new != self.ui[&self.info].content {
|
if new != self.ui[&self.info].content {
|
||||||
|
|||||||
Reference in New Issue
Block a user