remove state generic from a lot of things

This commit is contained in:
2025-12-17 21:37:55 -05:00
parent 7e6369029f
commit 30bc55c78e
45 changed files with 740 additions and 856 deletions

View File

@@ -2,225 +2,230 @@ use cosmic_text::Family;
use std::{cell::RefCell, rc::Rc};
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::WindowAttributes};
iris::state_prelude!(Client);
iris::state_prelude!(DefaultUiRsc<Client>);
fn main() {
App::<Client>::run();
}
#[derive(DefaultUiState)]
pub struct Client {
ui: Ui,
ui_state: UiState,
rsc: DefaultUiRsc<Self>,
info: WidgetRef<Text>,
}
impl HasRsc for Client {
type Rsc = DefaultUiRsc<Self>;
fn rsc(&self) -> &Self::Rsc {
&self.rsc
}
fn rsc_mut(&mut self) -> &mut Self::Rsc {
&mut self.rsc
}
}
impl DefaultAppState for Client {
fn new(event_loop: &ActiveEventLoop, _proxy: Proxy<Self::Event>) -> Self {
let mut ui = Ui::new();
let window = event_loop
.create_window(WindowAttributes::default())
.unwrap();
let mut rsc = DefaultUiRsc::new(window);
let info;
{
let ui = &mut ui;
let rrect = rect(Color::WHITE).radius(20);
let pad_test = (
rrect.color(Color::BLUE),
let rrect = rect(Color::WHITE).radius(20);
let pad_test = (
rrect.color(Color::BLUE),
(
rrect
.color(Color::RED)
.sized((100, 100))
.center()
.width(rest(2)),
(
rrect
.color(Color::RED)
.sized((100, 100))
.center()
.width(rest(2)),
(
rrect.color(Color::ORANGE),
rrect.color(Color::LIME).pad(10.0),
)
.span(Dir::RIGHT)
.width(rest(2)),
rrect.color(Color::YELLOW),
rrect.color(Color::ORANGE),
rrect.color(Color::LIME).pad(10.0),
)
.span(Dir::RIGHT)
.pad(10)
.width(rest(3)),
.width(rest(2)),
rrect.color(Color::YELLOW),
)
.span(Dir::RIGHT)
.handles(ui);
.pad(10)
.width(rest(3)),
)
.span(Dir::RIGHT)
.handles(&mut rsc);
let span_test = (
rrect.color(Color::GREEN).width(100),
rrect.color(Color::ORANGE),
rrect.color(Color::CYAN),
rrect.color(Color::BLUE).width(rel(0.5)),
rrect.color(Color::MAGENTA).width(100),
rrect.color(Color::RED).width(100),
let span_test = (
rrect.color(Color::GREEN).width(100),
rrect.color(Color::ORANGE),
rrect.color(Color::CYAN),
rrect.color(Color::BLUE).width(rel(0.5)),
rrect.color(Color::MAGENTA).width(100),
rrect.color(Color::RED).width(100),
)
.span(Dir::LEFT)
.add(&mut rsc);
let span_add = Span::empty(Dir::RIGHT).handles(&mut rsc);
let add_button = rect(Color::LIME)
.radius(30)
.on(CursorSense::click(), move |mut ctx| {
let child = image(include_bytes!("assets/sungals.png"))
.center()
.add(&mut ctx.state.rsc);
ctx[span_add.r].children.push(child);
})
.sized((150, 150))
.align(Align::BOT_RIGHT);
let del_button = rect(Color::RED)
.radius(30)
.on(CursorSense::click(), move |mut ctx| {
ctx[span_add.r].children.pop();
})
.sized((150, 150))
.align(Align::BOT_LEFT);
let span_add_test = (span_add.h, add_button, del_button).stack().add(&mut rsc);
let btext = |content| wtext(content).size(30);
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("'").family(Family::Monospace).align(Align::TOP),
btext("'").family(Family::Monospace),
btext(":gamer mode").family(Family::Monospace),
rect(Color::CYAN).sized((10, 10)).center(),
rect(Color::RED).sized((100, 100)).center(),
rect(Color::PURPLE).sized((50, 50)).align(Align::TOP),
)
.span(Dir::LEFT)
.add(ui);
.span(Dir::RIGHT)
.center(),
wtext("pretty cool right?").size(50),
)
.span(Dir::DOWN)
.add(&mut rsc);
let span_add = Span::empty(Dir::RIGHT).handles(ui);
let add_button = rect(Color::LIME)
.radius(30)
.on(CursorSense::click(), move |mut ctx| {
let child = ctx.add(image(include_bytes!("assets/sungals.png")).center());
ctx[span_add.r].children.push(child);
})
.sized((150, 150))
.align(Align::BOT_RIGHT);
let del_button = rect(Color::RED)
.radius(30)
.on(CursorSense::click(), move |mut ctx| {
ctx[span_add.r].children.pop();
})
.sized((150, 150))
.align(Align::BOT_LEFT);
let span_add_test = (span_add.h, add_button, del_button).stack().add(ui);
let btext = |content| wtext(content).size(30);
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"),
let texts = Span::empty(Dir::DOWN).gap(10).handles(&mut rsc);
let msg_area = texts.h.scrollable().masked().background(rect(Color::SKY));
let add_text = wtext("add")
.editable(EditMode::MultiLine)
.text_align(Align::LEFT)
.size(30)
.attr::<Selectable>(())
.on(Submit, move |mut ctx| {
let content = ctx.widget.edit(ctx.state.ui()).take();
let text = wtext(content)
.editable(EditMode::MultiLine)
.size(30)
.text_align(Align::LEFT)
.wrap(true)
.attr::<Selectable>(());
let msg_box = text
.background(rect(Color::WHITE.darker(0.5)))
.add(&mut ctx.state.rsc);
ctx[texts.r].children.push(msg_box);
})
.handles(&mut rsc);
let text_edit_scroll = (
msg_area.height(rest(1)),
(
Rect::new(Color::WHITE.darker(0.9)),
(
btext("'").family(Family::Monospace).align(Align::TOP),
btext("'").family(Family::Monospace),
btext(":gamer mode").family(Family::Monospace),
rect(Color::CYAN).sized((10, 10)).center(),
rect(Color::RED).sized((100, 100)).center(),
rect(Color::PURPLE).sized((50, 50)).align(Align::TOP),
add_text.h.width(rest(1)),
Rect::new(Color::GREEN)
.on(CursorSense::click(), move |ctx| {
ctx.state.run_event::<Submit>(add_text.r, &mut ());
})
.sized((40, 40)),
)
.span(Dir::RIGHT)
.center(),
wtext("pretty cool right?").size(50),
.pad(10),
)
.span(Dir::DOWN)
.add(ui);
let texts = Span::empty(Dir::DOWN).gap(10).handles(ui);
let msg_area = texts.h.scrollable().masked().background(rect(Color::SKY));
let add_text = wtext("add")
.editable(EditMode::MultiLine)
.text_align(Align::LEFT)
.size(30)
.attr::<Selectable>(())
.on(Submit, move |mut ctx| {
let content = ctx.widget.edit(ctx.state.ui()).take();
let text = wtext(content)
.editable(EditMode::MultiLine)
.size(30)
.text_align(Align::LEFT)
.wrap(true)
.attr::<Selectable>(());
let msg_box = text
.background(rect(Color::WHITE.darker(0.5)))
.add(&mut ctx);
ctx[texts.r].children.push(msg_box);
})
.handles(ui);
let text_edit_scroll = (
msg_area.height(rest(1)),
(
Rect::new(Color::WHITE.darker(0.9)),
(
add_text.h.width(rest(1)),
Rect::new(Color::GREEN)
.on(CursorSense::click(), move |ctx| {
ctx.state.run_event::<Submit>(add_text.r, &mut ());
})
.sized((40, 40)),
)
.span(Dir::RIGHT)
.pad(10),
)
.stack()
.size(StackSize::Child(1))
.layer_offset(1)
.align(Align::BOT),
)
.span(Dir::DOWN)
.add(ui);
let main = WidgetPtr::new().handles(ui);
let vals = Rc::new(RefCell::new((0, Vec::new())));
let mut switch_button = |color, to: WidgetHandle, label| {
let vec = &mut vals.borrow_mut().1;
let i = vec.len();
if vec.is_empty() {
vec.push(None);
ui[main.r].set(to);
} else {
vec.push(Some(to));
}
let vals = vals.clone();
let rect = rect(color)
.on(CursorSense::click(), move |mut ctx| {
let (prev, vec) = &mut *vals.borrow_mut();
if let Some(h) = vec[i].take() {
vec[*prev] = ctx[main.r].replace(h);
*prev = i;
}
ctx.widget().color = color.darker(0.3);
})
.on(
CursorSense::HoverStart | CursorSense::unclick(),
move |mut ctx| {
ctx.widget().color = color.brighter(0.2);
},
)
.on(CursorSense::HoverEnd, move |mut ctx| {
ctx.widget().color = color;
});
(rect, wtext(label).size(30).text_align(Align::CENTER)).stack()
};
let tabs = (
switch_button(Color::RED, pad_test.h, "pad"),
switch_button(Color::GREEN, span_test, "span"),
switch_button(Color::BLUE, span_add_test, "image span"),
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);
info = wtext("").handles(ui);
let info_sect = info.h.pad(10).align(Align::RIGHT);
((tabs.height(40), main.h.pad(10)).span(Dir::DOWN), info_sect)
.stack()
.set_root(ui);
}
.size(StackSize::Child(1))
.layer_offset(1)
.align(Align::BOT),
)
.span(Dir::DOWN)
.add(&mut rsc);
Self {
ui,
ui_state: UiState::new(window),
info: info.r,
}
let main = WidgetPtr::new().handles(&mut rsc);
let vals = Rc::new(RefCell::new((0, Vec::new())));
let mut switch_button = |color, to: WidgetHandle, label| {
let vec = &mut vals.borrow_mut().1;
let i = vec.len();
if vec.is_empty() {
vec.push(None);
rsc.ui[main.r].set(to);
} else {
vec.push(Some(to));
}
let vals = vals.clone();
let rect = rect(color)
.on(CursorSense::click(), move |mut ctx| {
let (prev, vec) = &mut *vals.borrow_mut();
if let Some(h) = vec[i].take() {
vec[*prev] = ctx[main.r].replace(h);
*prev = i;
}
ctx.widget().color = color.darker(0.3);
})
.on(
CursorSense::HoverStart | CursorSense::unclick(),
move |mut ctx| {
ctx.widget().color = color.brighter(0.2);
},
)
.on(CursorSense::HoverEnd, move |mut ctx| {
ctx.widget().color = color;
});
(rect, wtext(label).size(30).text_align(Align::CENTER)).stack()
};
let tabs = (
switch_button(Color::RED, pad_test.h, "pad"),
switch_button(Color::GREEN, span_test, "span"),
switch_button(Color::BLUE, span_add_test, "image span"),
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);
info = wtext("").handles(&mut rsc);
let info_sect = info.h.pad(10).align(Align::RIGHT);
((tabs.height(40), main.h.pad(10)).span(Dir::DOWN), info_sect)
.stack()
.set_root(&mut rsc);
Self { rsc, info: info.r }
}
fn window_event(&mut self, _: WindowEvent) {
let new = format!(
"widgets: {}\nactive:{}\nviews: {}",
self.ui.num_widgets(),
self.ui.active_widgets(),
self.ui_state.renderer.ui.view_count()
self.ui().num_widgets(),
self.ui().active_widgets(),
self.ui_state().renderer.ui.view_count()
);
if new != *self.ui[self.info].content {
*self.ui[self.info].content = new;
if new != *self.rsc.ui[self.info].content {
*self.rsc.ui[self.info].content = new;
}
if self.ui.needs_redraw() {
self.ui_state.window.request_redraw();
if self.ui().needs_redraw() {
self.ui_state().window.request_redraw();
}
}
}

View File

@@ -4,11 +4,11 @@ use winit::dpi::{LogicalPosition, LogicalSize};
pub struct Selector;
impl<State: HasUiState, W: Widget<State> + 'static> WidgetAttr<State, W> for Selector {
type Input = WidgetRef<State, TextEdit<State>>;
impl<State: HasUiState + HasEvents, W: Widget + 'static> WidgetAttr<State, W> for Selector {
type Input = WidgetRef<TextEdit>;
fn run(ui: &mut Ui<State>, container: WidgetRef<State, W>, id: Self::Input) {
ui.register_event(container, CursorSense::click_or_drag(), move |ctx| {
fn run(state: &mut State, container: WidgetRef<W>, id: Self::Input) {
state.register_event(container, CursorSense::click_or_drag(), move |ctx| {
let region = ctx.state.ui().window_region(&id).unwrap();
let id_pos = region.top_left;
let container_pos = ctx.state.ui().window_region(&container).unwrap().top_left;
@@ -21,11 +21,11 @@ impl<State: HasUiState, W: Widget<State> + 'static> WidgetAttr<State, W> for Sel
pub struct Selectable;
impl<State: HasUiState> WidgetAttr<State, TextEdit<State>> for Selectable {
impl<State: HasUiState + HasEvents> WidgetAttr<State, TextEdit> for Selectable {
type Input = ();
fn run(ui: &mut Ui<State>, id: WidgetRef<State, TextEdit<State>>, _: Self::Input) {
ui.register_event(id, CursorSense::click_or_drag(), move |ctx| {
fn run(state: &mut State, id: WidgetRef<TextEdit>, _: Self::Input) {
state.register_event(id, CursorSense::click_or_drag(), move |ctx| {
select(
ctx.state,
id,
@@ -37,9 +37,9 @@ impl<State: HasUiState> WidgetAttr<State, TextEdit<State>> for Selectable {
}
}
fn select<State: 'static + HasUiState>(
state: &mut State,
id: WidgetRef<State, TextEdit<State>>,
fn select(
state: &mut impl HasUiState,
id: WidgetRef<TextEdit>,
pos: Vec2,
size: Vec2,
dragging: bool,

View File

@@ -66,7 +66,7 @@ impl Input {
}
}
impl<State> UiState<State> {
impl UiState {
pub fn window_size(&self) -> Vec2 {
let size = self.renderer.window().inner_size();
(size.width, size.height).into()

View File

@@ -24,17 +24,57 @@ pub use sense::*;
pub type Proxy<Event> = EventLoopProxy<Event>;
pub struct UiState<State> {
pub struct DefaultUiRsc<State> {
pub ui: Ui,
pub ui_state: UiState,
pub events: EventManager<State>,
}
impl<State> DefaultUiRsc<State> {
pub fn new(window: impl Into<Arc<Window>>) -> Self {
Self {
ui: Ui::new(),
ui_state: UiState::new(window),
events: EventManager::default(),
}
}
}
impl<State: 'static> HasUi for DefaultUiRsc<State> {
fn ui_ref(&self) -> &Ui {
&self.ui
}
fn ui(&mut self) -> &mut Ui {
&mut self.ui
}
}
impl<State: 'static + HasRsc<Rsc = Self>> HasEvents for DefaultUiRsc<State> {
type State = State;
fn events(&mut self) -> &mut EventManager<State> {
&mut self.events
}
}
impl<State: 'static> HasUiState for DefaultUiRsc<State> {
fn ui_state(&mut self) -> &mut UiState {
&mut self.ui_state
}
}
pub struct UiState {
pub renderer: UiRenderer,
pub input: Input,
pub focus: Option<WidgetRef<State, TextEdit<State>>>,
pub focus: Option<WidgetRef<TextEdit>>,
pub clipboard: Clipboard,
pub window: Arc<Window>,
pub ime: usize,
pub last_click: Instant,
}
impl<State> UiState<State> {
impl UiState {
pub fn new(window: impl Into<Arc<Window>>) -> Self {
let window = window.into();
Self {
@@ -50,14 +90,23 @@ impl<State> UiState<State> {
}
pub trait HasUiState: Sized + 'static + HasUi {
fn ui_state(&mut self) -> &mut UiState<Self>;
fn ui_with_ui_state(&mut self) -> (&mut Ui<Self>, &mut UiState<Self>) {
fn ui_state(&mut self) -> &mut UiState;
fn ui_with_ui_state(&mut self) -> (&mut Ui, &mut UiState) {
// as long as you're not doing anything actually unhinged this should always work safely
(unsafe { forget_mut(self.ui()) }, self.ui_state())
}
}
pub trait DefaultAppState: Sized + 'static + HasUi + HasUiState {
impl<R: HasRsc> HasUiState for R
where
R::Rsc: HasUiState,
{
fn ui_state(&mut self) -> &mut UiState {
self.rsc_mut().ui_state()
}
}
pub trait DefaultAppState: Sized + 'static + HasRsc + HasUi + HasUiState {
type Event: 'static = ();
fn new(event_loop: &ActiveEventLoop, proxy: Proxy<Self::Event>) -> Self;
#[allow(unused_variables)]
@@ -80,6 +129,7 @@ impl<State: DefaultAppState> AppState for State {
}
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
let events = unsafe { forget_mut(self.events()) };
let ui_state = self.ui_state();
let input_changed = ui_state.input.event(&event);
let cursor_state = ui_state.cursor_state().clone();
@@ -100,7 +150,7 @@ impl<State: DefaultAppState> AppState for State {
match &event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
ui.update();
ui.update(events);
ui_state.renderer.update(ui);
ui_state.renderer.draw();
}

View File

@@ -18,7 +18,7 @@ pub struct UiRenderer {
}
impl UiRenderer {
pub fn update<State>(&mut self, ui: &mut Ui<State>) {
pub fn update(&mut self, ui: &mut Ui) {
self.ui.update(&self.device, &self.queue, ui);
}

View File

@@ -142,11 +142,10 @@ pub trait SensorUi<State> {
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2);
}
impl<State: 'static + HasUi> SensorUi<State> for State {
impl<State: 'static + HasRsc> SensorUi<State> for State {
fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
let layers = std::mem::take(&mut self.ui().layers);
let mut active =
std::mem::take(&mut self.ui().events.get_type::<CursorSense>().active);
let mut active = std::mem::take(&mut self.events().get_type::<CursorSense>().active);
for layer in layers.indices().rev() {
let mut sensed = false;
for (id, sensor) in active.get_mut(&layer).into_iter().flatten() {
@@ -175,7 +174,7 @@ impl<State: 'static + HasUi> SensorUi<State> for State {
break;
}
}
self.ui().events.get_type::<CursorSense>().active = active;
self.events().get_type::<CursorSense>().active = active;
self.ui().layers = layers;
}
}

View File

@@ -3,15 +3,15 @@ use crate::prelude::*;
pub mod eventable {
use super::*;
widget_trait! {
pub trait Eventable<State: 'static>;
pub trait Eventable<State: HasUi + HasEvents>;
fn on<E: EventLike>(
self,
event: E,
f: impl for<'a> WidgetEventFn<State, <E::Event as Event>::Data<'a>, WL::Widget>,
f: impl for<'a> WidgetEventFn<State::State, <E::Event as Event>::Data<'a>, WL::Widget>,
) -> impl WidgetIdFn<State, WL::Widget> {
move |ui| {
let id = self.handles(ui);
ui.register_event(id.r, event.into_event(), move |ctx| {
move |state| {
let id = self.handles(state);
state.register_event(id.r, event.into_event(), move |ctx| {
f(EventIdCtx {
widget: id.r,
state: ctx.state,

View File

@@ -9,20 +9,17 @@ pub mod event;
pub mod typed;
pub mod widget;
pub use iris_core;
pub use iris_macro;
pub use iris_core as core;
pub use iris_macro as macros;
#[macro_export]
macro_rules! state_prelude {
($vis:vis $state:ty) => {
iris::event_state!($vis $state);
iris::iris_core::core_state!($vis $state);
iris::default_state!($vis $state);
iris::widget_state!($vis $state);
$vis use iris::{
default::*,
iris_core::{len_fns::*, util::Vec2, *},
iris_macro::*,
core::{len_fns::*, util::Vec2, *},
macros::*,
widget::*,
};
};

View File

@@ -1,35 +1,3 @@
#[macro_export]
macro_rules! default_state {
($vis:vis $state:ty) => {
$vis type UiState = $crate::default::UiState<$state>;
};
}
#[macro_export]
macro_rules! widget_state {
($vis:vis $state:ty) => {
$crate::widget_state!(
$vis $state;
Aligned,
LayerOffset,
MaxSize,
Offset,
Scroll,
Sized,
Span,
Stack,
Text,
TextEdit,
Masked,
WidgetPtr,
);
$vis type TextBuilder<O = TextOutput, H: WidgetOption<$state> = ()> = $crate::widget::TextBuilder<$state, O, H>;
};
($vis:vis $state:ty; $($ty:ident,)*) => {
$($vis type $ty = $crate::widget::$ty<$state>;)*
}
}
#[macro_export]
macro_rules! event_state {
($vis:vis $state:ty) => {
@@ -37,24 +5,32 @@ macro_rules! event_state {
use super::*;
#[allow(unused_imports)]
use $crate::prelude::*;
pub trait EventableCtx<WL: WidgetLike<$state, Tag>, Tag> {
fn on<E: EventLike>(
self,
event: E,
f: impl for<'a> WidgetEventFn<$state, <E::Event as Event>::Data<'a>, WL::Widget>,
f: impl for<'a> WidgetEventFn<
<$state as HasEvents>::State,
<E::Event as Event>::Data<'a>,
WL::Widget
>,
) -> impl WidgetIdFn<$state, WL::Widget>;
}
impl<WL: WidgetLike<$state, Tag>, Tag> EventableCtx<WL, Tag> for WL {
fn on<E: EventLike>(
self,
event: E,
f: impl for<'a> WidgetEventFn<Client, <E::Event as Event>::Data<'a>, WL::Widget>,
) -> impl WidgetIdFn<Client, WL::Widget> {
f: impl for<'a> WidgetEventFn<
<$state as HasEvents>::State,
<E::Event as Event>::Data<'a>,
WL::Widget
>,
) -> impl WidgetIdFn<$state, WL::Widget> {
eventable::Eventable::on(self, event, f)
}
}
}
$vis type EventManager = $crate::prelude::EventManager<$state>;
$vis use local_event_trait::*;
};
}

View File

@@ -5,24 +5,24 @@ pub struct Image {
handle: TextureHandle,
}
impl<State: 'static> Widget<State> for Image {
fn draw(&mut self, painter: &mut Painter<State>) {
impl Widget for Image {
fn draw(&mut self, painter: &mut Painter) {
painter.texture(&self.handle);
}
fn desired_width(&mut self, _: &mut SizeCtx<State>) -> Len {
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
Len::abs(self.handle.size().x)
}
fn desired_height(&mut self, _: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
Len::abs(self.handle.size().y)
}
}
pub fn image<State: 'static>(image: impl LoadableImage) -> impl WidgetFn<State, Image> {
pub fn image<State: HasUi>(image: impl LoadableImage) -> impl WidgetFn<State, Image> {
let image = image.get_image().expect("Failed to load image");
move |ui| Image {
handle: ui.add_texture(image),
move |state| Image {
handle: state.ui().add_texture(image),
}
}

View File

@@ -1,20 +1,20 @@
use crate::prelude::*;
pub struct Masked<State> {
pub inner: WidgetHandle<State>,
pub struct Masked {
pub inner: WidgetHandle,
}
impl<State: 'static> Widget<State> for Masked<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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<State>) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
}

View File

@@ -1,12 +1,12 @@
use crate::prelude::*;
pub struct Aligned<State> {
pub inner: WidgetHandle<State>,
pub struct Aligned {
pub inner: WidgetHandle,
pub align: Align,
}
impl<State: 'static> Widget<State> for Aligned<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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)
@@ -25,11 +25,11 @@ impl<State: 'static> Widget<State> for Aligned<State> {
painter.widget_within(&self.inner, region);
}
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
}

View File

@@ -1,23 +1,23 @@
use crate::prelude::*;
pub struct LayerOffset<State> {
pub inner: WidgetHandle<State>,
pub struct LayerOffset {
pub inner: WidgetHandle,
pub offset: usize,
}
impl<State: 'static> Widget<State> for LayerOffset<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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<State>) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
}

View File

@@ -1,13 +1,13 @@
use crate::prelude::*;
pub struct MaxSize<State> {
pub inner: WidgetHandle<State>,
pub struct MaxSize {
pub inner: WidgetHandle,
pub x: Option<Len>,
pub y: Option<Len>,
}
impl<State: 'static> MaxSize<State> {
fn apply_to_outer(&self, ctx: &mut SizeCtx<State>) {
impl MaxSize {
fn apply_to_outer(&self, ctx: &mut SizeCtx) {
if let Some(x) = self.x {
ctx.outer.x.select_len(x.apply_rest());
}
@@ -17,12 +17,12 @@ impl<State: 'static> MaxSize<State> {
}
}
impl<State: 'static> Widget<State> for MaxSize<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
impl Widget for MaxSize {
fn draw(&mut self, painter: &mut Painter) {
painter.widget(&self.inner);
}
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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 {
@@ -34,7 +34,7 @@ impl<State: 'static> Widget<State> for MaxSize<State> {
}
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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 {

View File

@@ -1,21 +1,21 @@
use crate::prelude::*;
pub struct Offset<State> {
pub inner: WidgetHandle<State>,
pub struct Offset {
pub inner: WidgetHandle,
pub amt: UiVec2,
}
impl<State: 'static> Widget<State> for Offset<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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<State>) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
}

View File

@@ -1,16 +1,16 @@
use crate::prelude::*;
pub struct Pad<State> {
pub struct Pad {
pub padding: Padding,
pub inner: WidgetHandle<State>,
pub inner: WidgetHandle,
}
impl<State: 'static> Widget<State> for Pad<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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<State>) -> Len {
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;
@@ -20,7 +20,7 @@ impl<State: 'static> Widget<State> for Pad<State> {
size
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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;

View File

@@ -1,7 +1,7 @@
use crate::prelude::*;
pub struct Scroll<State> {
inner: WidgetHandle<State>,
pub struct Scroll {
inner: WidgetHandle,
axis: Axis,
amt: f32,
snap_end: bool,
@@ -9,8 +9,8 @@ pub struct Scroll<State> {
content_len: f32,
}
impl<State: 'static> Widget<State> for Scroll<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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
@@ -31,17 +31,17 @@ impl<State: 'static> Widget<State> for Scroll<State> {
painter.widget_within(&self.inner, region);
}
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
}
impl<State> Scroll<State> {
pub fn new(inner: WidgetHandle<State>, axis: Axis) -> Self {
impl Scroll {
pub fn new(inner: WidgetHandle, axis: Axis) -> Self {
Self {
inner,
axis,

View File

@@ -1,13 +1,13 @@
use crate::prelude::*;
pub struct Sized<State> {
pub inner: WidgetHandle<State>,
pub struct Sized {
pub inner: WidgetHandle,
pub x: Option<Len>,
pub y: Option<Len>,
}
impl<State: 'static> Sized<State> {
fn apply_to_outer(&self, ctx: &mut SizeCtx<State>) {
impl Sized {
fn apply_to_outer(&self, ctx: &mut SizeCtx) {
if let Some(x) = self.x {
ctx.outer.x.select_len(x.apply_rest());
}
@@ -17,17 +17,17 @@ impl<State: 'static> Sized<State> {
}
}
impl<State: 'static> Widget<State> for Sized<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
impl Widget for Sized {
fn draw(&mut self, painter: &mut Painter) {
painter.widget(&self.inner);
}
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
self.apply_to_outer(ctx);
self.y.unwrap_or_else(|| ctx.height(&self.inner))
}

View File

@@ -1,14 +1,14 @@
use crate::prelude::*;
use std::marker::PhantomData;
pub struct Span<State> {
pub children: Vec<WidgetHandle<State>>,
pub struct Span {
pub children: Vec<WidgetHandle>,
pub dir: Dir,
pub gap: f32,
}
impl<State: 'static> Widget<State> for Span<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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 {
@@ -33,14 +33,14 @@ impl<State: 'static> Widget<State> for Span<State> {
}
}
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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<State>) -> Len {
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),
@@ -48,7 +48,7 @@ impl<State: 'static> Widget<State> for Span<State> {
}
}
impl<State: 'static> Span<State> {
impl Span {
pub fn empty(dir: Dir) -> Self {
Self {
children: Vec::new(),
@@ -62,7 +62,7 @@ impl<State: 'static> Span<State> {
self
}
fn len_sum(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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,
@@ -78,7 +78,7 @@ impl<State: 'static> Span<State> {
})
}
fn desired_len(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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
@@ -87,7 +87,7 @@ impl<State: 'static> Span<State> {
}
}
fn desired_ortho(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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,
@@ -151,14 +151,14 @@ pub struct SpanBuilder<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Ta
_pd: PhantomData<(State, Tag)>,
}
impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut Ui<State>,)>
impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut State,)>
for SpanBuilder<State, LEN, Wa, Tag>
{
type Output = Span<State>;
type Output = Span;
extern "rust-call" fn call_once(self, args: (&mut Ui<State>,)) -> Self::Output {
extern "rust-call" fn call_once(self, args: (&mut State,)) -> Self::Output {
Span {
children: self.children.ui(args.0).arr.into_iter().collect(),
children: self.children.add(args.0).arr.into_iter().collect(),
dir: self.dir,
gap: self.gap,
}
@@ -183,15 +183,15 @@ impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag>
}
}
impl<State> std::ops::Deref for Span<State> {
type Target = Vec<WidgetHandle<State>>;
impl std::ops::Deref for Span {
type Target = Vec<WidgetHandle>;
fn deref(&self) -> &Self::Target {
&self.children
}
}
impl<State> std::ops::DerefMut for Span<State> {
impl std::ops::DerefMut for Span {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.children
}

View File

@@ -2,13 +2,13 @@ use std::marker::PhantomData;
use crate::prelude::*;
pub struct Stack<State> {
pub children: Vec<WidgetHandle<State>>,
pub struct Stack {
pub children: Vec<WidgetHandle>,
pub size: StackSize,
}
impl<State: 'static> Widget<State> for Stack<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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();
@@ -20,14 +20,14 @@ impl<State: 'static> Widget<State> for Stack<State> {
}
}
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
match self.size {
StackSize::Default => Len::default(),
StackSize::Child(i) => ctx.height(&self.children[i]),
@@ -48,14 +48,14 @@ pub struct StackBuilder<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, T
_pd: PhantomData<(State, Tag)>,
}
impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut Ui<State>,)>
impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut State,)>
for StackBuilder<State, LEN, Wa, Tag>
{
type Output = Stack<State>;
type Output = Stack;
extern "rust-call" fn call_once(self, args: (&mut Ui<State>,)) -> Self::Output {
extern "rust-call" fn call_once(self, args: (&mut State,)) -> Self::Output {
Stack {
children: self.children.ui(args.0).arr.into_iter().collect(),
children: self.children.add(args.0).arr.into_iter().collect(),
size: self.size,
}
}

View File

@@ -1,18 +1,18 @@
use crate::prelude::*;
use std::marker::{Sized, Unsize};
pub struct WidgetPtr<State> {
pub inner: Option<WidgetHandle<State>>,
pub struct WidgetPtr {
pub inner: Option<WidgetHandle>,
}
impl<State: 'static> Widget<State> for WidgetPtr<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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<State>) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
if let Some(id) = &self.inner {
ctx.width(id)
} else {
@@ -20,7 +20,7 @@ impl<State: 'static> Widget<State> for WidgetPtr<State> {
}
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
if let Some(id) = &self.inner {
ctx.height(id)
} else {
@@ -29,7 +29,7 @@ impl<State: 'static> Widget<State> for WidgetPtr<State> {
}
}
impl<State> WidgetPtr<State> {
impl WidgetPtr {
pub fn new() -> Self {
Self::default()
}
@@ -38,19 +38,19 @@ impl<State> WidgetPtr<State> {
inner: Default::default(),
}
}
pub fn set<W: ?Sized + Unsize<dyn Widget<State>>>(&mut self, to: WidgetHandle<State, W>) {
pub fn set<W: ?Sized + Unsize<dyn Widget>>(&mut self, to: WidgetHandle<W>) {
self.inner = Some(to)
}
pub fn replace<W: ?Sized + Unsize<dyn Widget<State>>>(
pub fn replace<W: ?Sized + Unsize<dyn Widget>>(
&mut self,
to: WidgetHandle<State, W>,
) -> Option<WidgetHandle<State>> {
to: WidgetHandle<W>,
) -> Option<WidgetHandle> {
self.inner.replace(to)
}
}
impl<State> Default for WidgetPtr<State> {
impl Default for WidgetPtr {
fn default() -> Self {
Self::empty()
}

View File

@@ -27,8 +27,8 @@ impl Rect {
}
}
impl<State: 'static> Widget<State> for Rect {
fn draw(&mut self, painter: &mut Painter<State>) {
impl Widget for Rect {
fn draw(&mut self, painter: &mut Painter) {
painter.primitive(RectPrimitive {
color: self.color,
radius: self.radius,
@@ -37,11 +37,11 @@ impl<State: 'static> Widget<State> for Rect {
});
}
fn desired_width(&mut self, _: &mut SizeCtx<State>) -> Len {
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
Len::rest(1)
}
fn desired_height(&mut self, _: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
Len::rest(1)
}
}

View File

@@ -51,7 +51,7 @@ impl<State, O, H: WidgetOption<State>> TextBuilder<State, O, H> {
}
}
impl<State: 'static, O> TextBuilder<State, O> {
impl<State: HasUi, O> TextBuilder<State, O> {
pub fn hint<W: WidgetLike<State, Tag>, Tag>(
self,
hint: W,
@@ -59,7 +59,7 @@ impl<State: 'static, O> TextBuilder<State, O> {
TextBuilder {
content: self.content,
attrs: self.attrs,
hint: move |ui: &mut Ui<State>| Some(hint.add(ui).any()),
hint: move |ui: &mut State| Some(hint.add(ui).any()),
output: self.output,
state: PhantomData,
}
@@ -69,25 +69,25 @@ impl<State: 'static, O> TextBuilder<State, O> {
pub trait TextBuilderOutput<State>: Sized {
type Output;
fn run<H: WidgetOption<State>>(
ui: &mut Ui<State>,
state: &mut State,
builder: TextBuilder<State, Self, H>,
) -> Self::Output;
}
pub struct TextOutput;
impl<State: 'static> TextBuilderOutput<State> for TextOutput {
type Output = Text<State>;
impl<State: HasUi> TextBuilderOutput<State> for TextOutput {
type Output = Text;
fn run<H: WidgetOption<State>>(
ui: &mut Ui<State>,
state: &mut State,
builder: TextBuilder<State, 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.text.font_system;
let hint = builder.hint.get(state);
let font_system = &mut state.ui().text.font_system;
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
let mut text = Text {
content: builder.content.into(),
@@ -102,11 +102,12 @@ impl<State: 'static> TextBuilderOutput<State> for TextOutput {
pub struct TextEditOutput {
mode: EditMode,
}
impl<State: 'static> TextBuilderOutput<State> for TextEditOutput {
type Output = TextEdit<State>;
impl<State: HasUi> TextBuilderOutput<State> for TextEditOutput {
type Output = TextEdit;
fn run<H: WidgetOption<State>>(
ui: &mut Ui<State>,
state: &mut State,
builder: TextBuilder<State, Self, H>,
) -> Self::Output {
let buf = TextBuffer::new_empty(Metrics::new(
@@ -114,10 +115,10 @@ impl<State: 'static> TextBuilderOutput<State> for TextEditOutput {
builder.attrs.line_height,
));
let mut text = TextEdit::new(
TextView::new(buf, builder.attrs, builder.hint.get(ui)),
TextView::new(buf, builder.attrs, builder.hint.get(state)),
builder.output.mode,
);
let font_system = &mut ui.text.font_system;
let font_system = &mut state.ui().text.font_system;
text.buf
.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
builder.attrs.apply(font_system, &mut text.buf, None);
@@ -125,12 +126,12 @@ impl<State: 'static> TextBuilderOutput<State> for TextEditOutput {
}
}
impl<State, O: TextBuilderOutput<State>, H: WidgetOption<State>> FnOnce<(&mut Ui<State>,)>
impl<State, O: TextBuilderOutput<State>, H: WidgetOption<State>> FnOnce<(&mut State,)>
for TextBuilder<State, O, H>
{
type Output = O::Output;
extern "rust-call" fn call_once(self, args: (&mut Ui<State>,)) -> Self::Output {
extern "rust-call" fn call_once(self, args: (&mut State,)) -> Self::Output {
O::run(args.0, self)
}
}

View File

@@ -7,8 +7,8 @@ use winit::{
keyboard::{Key, NamedKey},
};
pub struct TextEdit<State> {
view: TextView<State>,
pub struct TextEdit {
view: TextView,
selection: TextSelection,
history: Vec<(String, TextSelection)>,
double_hit: Option<Cursor>,
@@ -21,8 +21,8 @@ pub enum EditMode {
MultiLine,
}
impl<State: 'static> TextEdit<State> {
pub fn new(view: TextView<State>, mode: EditMode) -> Self {
impl TextEdit {
pub fn new(view: TextView, mode: EditMode) -> Self {
Self {
view,
selection: Default::default(),
@@ -49,8 +49,8 @@ impl<State: 'static> TextEdit<State> {
}
}
impl<State: 'static> Widget<State> for TextEdit<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
impl Widget for TextEdit {
fn draw(&mut self, painter: &mut Painter) {
let base = painter.layer;
painter.child_layer();
self.view.draw(painter);
@@ -92,11 +92,11 @@ impl<State: 'static> Widget<State> for TextEdit<State> {
}
}
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
self.view.desired_width(ctx)
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
self.view.desired_height(ctx)
}
}
@@ -180,12 +180,12 @@ fn cursor_pos(cursor: Cursor, buf: &TextBuffer) -> Option<Vec2> {
prev
}
pub struct TextEditCtx<'a, State> {
pub text: &'a mut TextEdit<State>,
pub struct TextEditCtx<'a> {
pub text: &'a mut TextEdit,
pub font_system: &'a mut FontSystem,
}
impl<'a, State: 'static> TextEditCtx<'a, State> {
impl<'a> TextEditCtx<'a> {
pub fn take(&mut self) -> String {
let text = self
.text
@@ -602,26 +602,26 @@ impl TextInputResult {
}
}
impl<State> Deref for TextEdit<State> {
type Target = TextView<State>;
impl Deref for TextEdit {
type Target = TextView;
fn deref(&self) -> &Self::Target {
&self.view
}
}
impl<State> DerefMut for TextEdit<State> {
impl DerefMut for TextEdit {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.view
}
}
pub trait TextEditable<State> {
fn edit<'a>(&self, ui: &'a mut Ui<State>) -> TextEditCtx<'a, State>;
pub trait TextEditable {
fn edit<'a>(&self, ui: &'a mut Ui) -> TextEditCtx<'a>;
}
impl<State: 'static, I: IdLike<State, Widget = TextEdit<State>>> TextEditable<State> for I {
fn edit<'a>(&self, ui: &'a mut Ui<State>) -> TextEditCtx<'a, State> {
impl<I: IdLike<Widget = TextEdit>> TextEditable for I {
fn edit<'a>(&self, ui: &'a mut Ui) -> TextEditCtx<'a> {
TextEditCtx {
text: ui.widgets.get_mut(self).unwrap(),
font_system: &mut ui.text.font_system,

View File

@@ -11,22 +11,22 @@ use std::ops::{Deref, DerefMut};
pub const SHAPING: Shaping = Shaping::Advanced;
pub struct Text<State> {
pub struct Text {
pub content: MutDetect<String>,
view: TextView<State>,
view: TextView,
}
pub struct TextView<State> {
pub struct TextView {
pub attrs: MutDetect<TextAttrs>,
pub buf: MutDetect<TextBuffer>,
// cache
tex: Option<RenderedText>,
width: Option<f32>,
pub hint: Option<WidgetHandle<State>>,
pub hint: Option<WidgetHandle>,
}
impl<State: 'static> TextView<State> {
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetHandle<State>>) -> Self {
impl TextView {
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetHandle>) -> Self {
Self {
attrs: attrs.into(),
buf: buf.into(),
@@ -54,7 +54,7 @@ impl<State: 'static> TextView<State> {
region
}
fn render(&mut self, ctx: &mut SizeCtx<State>) -> RenderedText {
fn render(&mut self, ctx: &mut SizeCtx) -> RenderedText {
let width = if self.attrs.wrap {
Some(ctx.px_size().x)
} else {
@@ -80,7 +80,7 @@ impl<State: 'static> TextView<State> {
pub fn tex(&self) -> Option<&RenderedText> {
self.tex.as_ref()
}
pub fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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()
@@ -90,7 +90,7 @@ impl<State: 'static> TextView<State> {
Len::abs(self.render(ctx).size.x)
}
}
pub fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
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()
@@ -100,7 +100,7 @@ impl<State: 'static> TextView<State> {
Len::abs(self.render(ctx).size.y)
}
}
pub fn draw(&mut self, painter: &mut Painter<State>) -> UiRegion {
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
@@ -124,7 +124,7 @@ impl<State: 'static> TextView<State> {
}
}
impl<State: 'static> Text<State> {
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));
@@ -133,7 +133,7 @@ impl<State: 'static> Text<State> {
view: TextView::new(buf, attrs, None),
}
}
fn update_buf(&mut self, ctx: &mut SizeCtx<State>) {
fn update_buf(&mut self, ctx: &mut SizeCtx) {
if self.content.changed {
self.content.changed = false;
self.view.buf.set_text(
@@ -147,18 +147,18 @@ impl<State: 'static> Text<State> {
}
}
impl<State: 'static> Widget<State> for Text<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
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<State>) -> Len {
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<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
self.update_buf(ctx);
self.view.desired_height(ctx)
}
@@ -174,7 +174,7 @@ pub fn edit_line(line: &mut BufferLine, text: String) {
line.set_text(text, line.ending(), line.attrs_list().clone());
}
impl<State> Deref for Text<State> {
impl Deref for Text {
type Target = TextAttrs;
fn deref(&self) -> &Self::Target {
@@ -182,13 +182,13 @@ impl<State> Deref for Text<State> {
}
}
impl<State> DerefMut for Text<State> {
impl DerefMut for Text {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.view
}
}
impl<State> Deref for TextView<State> {
impl Deref for TextView {
type Target = TextAttrs;
fn deref(&self) -> &Self::Target {
@@ -196,7 +196,7 @@ impl<State> Deref for TextView<State> {
}
}
impl<State> DerefMut for TextView<State> {
impl DerefMut for TextView {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.attrs
}

View File

@@ -3,132 +3,132 @@ use crate::prelude::*;
// these methods should "not require any context" (require unit) because they're in core
widget_trait! {
pub trait CoreWidget<State: HasUi + 'static>;
pub trait CoreWidget<State: HasUi>;
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<State, Pad<State>> {
|ui| Pad {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<State, Pad> {
|state| Pad {
padding: padding.into(),
inner: self.add(ui),
inner: self.add(state),
}
}
fn align(self, align: impl Into<Align>) -> impl WidgetFn<State, Aligned<State>> {
move |ui| Aligned {
inner: self.add(ui),
fn align(self, align: impl Into<Align>) -> impl WidgetFn<State, Aligned> {
move |state| Aligned {
inner: self.add(state),
align: align.into(),
}
}
fn center(self) -> impl WidgetFn<State, Aligned<State>> {
fn center(self) -> impl WidgetFn<State, Aligned> {
self.align(Align::CENTER)
}
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<State, WL::Widget> {
|ui| {
let id = self.add(ui);
ui.set_label(&id, label.into());
|state| {
let id = self.add(state);
state.ui().set_label(&id, label.into());
id
}
}
fn sized(self, size: impl Into<Size>) -> impl WidgetFn<State, Sized<State>> {
fn sized(self, size: impl Into<Size>) -> impl WidgetFn<State, Sized> {
let size = size.into();
move |ui| Sized {
inner: self.add(ui),
move |state| Sized {
inner: self.add(state),
x: Some(size.x),
y: Some(size.y),
}
}
fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize<State>> {
fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize> {
let len = len.into();
move |ui| MaxSize {
inner: self.add(ui),
move |state| MaxSize {
inner: self.add(state),
x: Some(len),
y: None,
}
}
fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize<State>> {
fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize> {
let len = len.into();
move |ui| MaxSize {
inner: self.add(ui),
move |state| MaxSize {
inner: self.add(state),
x: None,
y: Some(len),
}
}
fn width(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized<State>> {
fn width(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized> {
let len = len.into();
move |ui| Sized {
inner: self.add(ui),
move |state| Sized {
inner: self.add(state),
x: Some(len),
y: None,
}
}
fn height(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized<State>> {
fn height(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized> {
let len = len.into();
move |ui| Sized {
inner: self.add(ui),
move |state| Sized {
inner: self.add(state),
x: None,
y: Some(len),
}
}
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<State, Offset<State>> {
move |ui| Offset {
inner: self.add(ui),
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<State, Offset> {
move |state| Offset {
inner: self.add(state),
amt: amt.into(),
}
}
fn scrollable(self) -> impl WidgetIdFn<State, Scroll<State>> {
fn scrollable(self) -> impl WidgetIdFn<State, Scroll> where State: HasEvents {
use eventable::*;
move |ui| {
Scroll::new(self.add(ui), Axis::Y)
move |state| {
Scroll::new(self.add(state), Axis::Y)
.on(CursorSense::Scroll, |mut ctx| {
let delta = ctx.data.scroll_delta.y * 50.0;
ctx.widget().scroll(delta);
})
.add(ui)
.add(state)
}
}
fn masked(self) -> impl WidgetFn<State, Masked<State>> {
move |ui| Masked {
inner: self.add(ui),
fn masked(self) -> impl WidgetFn<State, Masked> {
move |state| Masked {
inner: self.add(state),
}
}
fn background<T>(self, w: impl WidgetLike<State, T>) -> impl WidgetFn<State, Stack<State>> {
move |ui| Stack {
children: vec![w.add(ui), self.add(ui)],
fn background<T>(self, w: impl WidgetLike<State, T>) -> impl WidgetFn<State, Stack> {
move |state| Stack {
children: vec![w.add(state), self.add(state)],
size: StackSize::Child(1),
}
}
fn foreground<T>(self, w: impl WidgetLike<State, T>) -> impl WidgetFn<State, Stack<State>> {
move |ui| Stack {
children: vec![self.add(ui), w.add(ui)],
fn foreground<T>(self, w: impl WidgetLike<State, T>) -> impl WidgetFn<State, Stack> {
move |state| Stack {
children: vec![self.add(state), w.add(state)],
size: StackSize::Child(0),
}
}
fn layer_offset(self, offset: usize) -> impl WidgetFn<State, LayerOffset<State>> {
move |ui| LayerOffset {
inner: self.add(ui),
fn layer_offset(self, offset: usize) -> impl WidgetFn<State, LayerOffset> {
move |state| LayerOffset {
inner: self.add(state),
offset,
}
}
fn to_any(self) -> impl WidgetIdFn<State> {
|ui| self.add(ui)
|state| self.add(state)
}
fn set_ptr(self, ptr: WidgetRef<State, WidgetPtr<State>>, ui: &mut Ui<State>) {
let id = self.add(ui);
ui[ptr].inner = Some(id);
fn set_ptr(self, ptr: WidgetRef<WidgetPtr>, state: &mut State) {
let id = self.add(state);
state.ui()[ptr].inner = Some(id);
}
}