strong & weak widgets
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use cosmic_text::Family;
|
||||
use iris::prelude::*;
|
||||
use len_fns::*;
|
||||
@@ -8,7 +10,7 @@ fn main() {
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
info: WidgetHandle<Text>,
|
||||
info: WidgetView<Text>,
|
||||
}
|
||||
|
||||
event_ctx!(Client);
|
||||
@@ -37,7 +39,8 @@ impl DefaultAppState for Client {
|
||||
.width(rest(3)),
|
||||
)
|
||||
.span(Dir::RIGHT)
|
||||
.add(ui);
|
||||
.add(ui)
|
||||
.handles();
|
||||
|
||||
let span_test = (
|
||||
rrect.color(Color::GREEN).width(100),
|
||||
@@ -50,8 +53,7 @@ impl DefaultAppState for Client {
|
||||
.span(Dir::LEFT)
|
||||
.add(ui);
|
||||
|
||||
let span_add = Span::empty(Dir::RIGHT).add(ui);
|
||||
let span_add_ = span_add.clone();
|
||||
let span_add = Span::empty(Dir::RIGHT).add(ui).handles();
|
||||
|
||||
let add_button = rect(Color::LIME)
|
||||
.radius(30)
|
||||
@@ -60,21 +62,20 @@ impl DefaultAppState for Client {
|
||||
.ui
|
||||
.add(image(include_bytes!("assets/sungals.png")).center())
|
||||
.any();
|
||||
ctx.ui[&span_add_].children.push(child);
|
||||
ctx.ui[span_add.1].children.push(child);
|
||||
})
|
||||
.sized((150, 150))
|
||||
.align(Align::BOT_RIGHT);
|
||||
|
||||
let span_add_ = span_add.clone();
|
||||
let del_button = rect(Color::RED)
|
||||
.radius(30)
|
||||
.on(CursorSense::click(), move |ctx| {
|
||||
ctx.ui[&span_add_].children.pop();
|
||||
ctx.ui[span_add.1].children.pop();
|
||||
})
|
||||
.sized((150, 150))
|
||||
.align(Align::BOT_LEFT);
|
||||
|
||||
let span_add_test = (span_add, add_button, del_button).stack().add(ui);
|
||||
let span_add_test = (span_add.0, add_button, del_button).stack().add(ui);
|
||||
|
||||
let btext = |content| wtext(content).size(30);
|
||||
|
||||
@@ -99,8 +100,8 @@ impl DefaultAppState for Client {
|
||||
.span(Dir::DOWN)
|
||||
.add(ui);
|
||||
|
||||
let texts = Span::empty(Dir::DOWN).gap(10).add(ui);
|
||||
let msg_area = texts.clone().scroll().masked().background(rect(Color::SKY));
|
||||
let texts = Span::empty(Dir::DOWN).gap(10).add(ui).handles();
|
||||
let msg_area = texts.0.scroll().masked().background(rect(Color::SKY));
|
||||
let add_text = wtext("add")
|
||||
.editable(false)
|
||||
.text_align(Align::LEFT)
|
||||
@@ -115,18 +116,19 @@ impl DefaultAppState for Client {
|
||||
.wrap(true)
|
||||
.attr::<Selectable>(());
|
||||
let msg_box = text.background(rect(Color::WHITE.darker(0.5))).add(ctx.ui);
|
||||
ctx.ui[&texts].children.push(msg_box.any());
|
||||
ctx.ui[texts.1].children.push(msg_box.any());
|
||||
})
|
||||
.add(ui);
|
||||
.add(ui)
|
||||
.handles();
|
||||
let text_edit_scroll = (
|
||||
msg_area.height(rest(1)),
|
||||
(
|
||||
Rect::new(Color::WHITE.darker(0.9)),
|
||||
(
|
||||
add_text.clone().width(rest(1)),
|
||||
add_text.0.width(rest(1)),
|
||||
Rect::new(Color::GREEN)
|
||||
.on(CursorSense::click(), move |ctx| {
|
||||
ctx.ui.run_event(ctx.state, &add_text, Submit, ());
|
||||
ctx.ui.run_event(ctx.state, add_text.1, Submit, ());
|
||||
})
|
||||
.sized((40, 40)),
|
||||
)
|
||||
@@ -141,13 +143,26 @@ impl DefaultAppState for Client {
|
||||
.span(Dir::DOWN)
|
||||
.add(ui);
|
||||
|
||||
let main = pad_test.clone().pad(10).add(ui);
|
||||
let main = WidgetPtr::new().add(ui).handles();
|
||||
|
||||
let switch_button = |color, to: WidgetHandle, label| {
|
||||
let main_ = main.clone();
|
||||
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.1].set(to);
|
||||
} else {
|
||||
vec.push(Some(to));
|
||||
}
|
||||
let vals = vals.clone();
|
||||
let rect = rect(color)
|
||||
.on(CursorSense::click(), move |ctx| {
|
||||
ctx.ui[&main_.clone()].inner = to.clone();
|
||||
let (prev, vec) = &mut *vals.borrow_mut();
|
||||
if let Some(h) = vec[i].take() {
|
||||
vec[*prev] = ctx.ui[main.1].replace(h);
|
||||
*prev = i;
|
||||
}
|
||||
ctx.ui[ctx.id].color = color.darker(0.3);
|
||||
})
|
||||
.on(
|
||||
@@ -163,26 +178,26 @@ impl DefaultAppState for Client {
|
||||
};
|
||||
|
||||
let tabs = (
|
||||
switch_button(Color::RED, pad_test.any(), "pad"),
|
||||
switch_button(Color::GREEN, span_test.any(), "span"),
|
||||
switch_button(Color::BLUE, span_add_test.any(), "image span"),
|
||||
switch_button(Color::MAGENTA, text_test.any(), "text layout"),
|
||||
switch_button(Color::RED, pad_test.0, "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.any(),
|
||||
text_edit_scroll,
|
||||
"text edit scroll",
|
||||
),
|
||||
)
|
||||
.span(Dir::RIGHT);
|
||||
|
||||
let info = wtext("").add(ui);
|
||||
let info_sect = info.clone().pad(10).align(Align::RIGHT);
|
||||
let info = wtext("").add(ui).handles();
|
||||
let info_sect = info.0.pad(10).align(Align::RIGHT);
|
||||
|
||||
((tabs.height(40), main).span(Dir::DOWN), info_sect)
|
||||
((tabs.height(40), main.0.pad(10)).span(Dir::DOWN), info_sect)
|
||||
.stack()
|
||||
.set_root(ui);
|
||||
|
||||
Self { info }
|
||||
Self { info: info.1 }
|
||||
}
|
||||
|
||||
fn window_event(&mut self, _: WindowEvent, ui: &mut Ui, state: &UiState) {
|
||||
@@ -192,8 +207,8 @@ impl DefaultAppState for Client {
|
||||
ui.active_widgets(),
|
||||
state.renderer.ui.view_count()
|
||||
);
|
||||
if new != *ui[&self.info].content {
|
||||
*ui[&self.info].content = new;
|
||||
if new != *ui[self.info].content {
|
||||
*ui[self.info].content = new;
|
||||
}
|
||||
if ui.needs_redraw() {
|
||||
state.window.request_redraw();
|
||||
|
||||
@@ -5,23 +5,18 @@ use winit::dpi::{LogicalPosition, LogicalSize};
|
||||
pub struct Selector;
|
||||
|
||||
impl<W: 'static + Widget> WidgetAttr<W> for Selector {
|
||||
type Input = WidgetHandle<TextEdit>;
|
||||
type Input = WidgetView<TextEdit>;
|
||||
|
||||
fn run(ui: &mut Ui, container: &WidgetHandle<W>, id: Self::Input) {
|
||||
let container = container.clone();
|
||||
ui.register_event(
|
||||
&container.clone(),
|
||||
CursorSense::click_or_drag(),
|
||||
move |mut ctx| {
|
||||
let ui = ctx.ui;
|
||||
let region = ui.window_region(&id).unwrap();
|
||||
let id_pos = region.top_left;
|
||||
let container_pos = ui.window_region(&container).unwrap().top_left;
|
||||
ctx.data.cursor += container_pos - id_pos;
|
||||
ctx.data.size = region.size();
|
||||
select(ui, id.clone(), ctx.state, ctx.data);
|
||||
},
|
||||
);
|
||||
fn run(ui: &mut Ui, container: WidgetView<W>, id: Self::Input) {
|
||||
ui.register_event(&container, CursorSense::click_or_drag(), move |mut ctx| {
|
||||
let ui = ctx.ui;
|
||||
let region = ui.window_region(&id).unwrap();
|
||||
let id_pos = region.top_left;
|
||||
let container_pos = ui.window_region(&container).unwrap().top_left;
|
||||
ctx.data.cursor += container_pos - id_pos;
|
||||
ctx.data.size = region.size();
|
||||
select(ui, id, ctx.state, ctx.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,15 +25,14 @@ pub struct Selectable;
|
||||
impl WidgetAttr<TextEdit> for Selectable {
|
||||
type Input = ();
|
||||
|
||||
fn run(ui: &mut Ui, id: &WidgetHandle<TextEdit>, _: Self::Input) {
|
||||
let id = id.clone();
|
||||
ui.register_event(&id.clone(), CursorSense::click_or_drag(), move |ctx| {
|
||||
select(ctx.ui, id.clone(), ctx.state, ctx.data);
|
||||
fn run(ui: &mut Ui, id: WidgetView<TextEdit>, _: Self::Input) {
|
||||
ui.register_event(&id, CursorSense::click_or_drag(), move |ctx| {
|
||||
select(ctx.ui, id, ctx.state, ctx.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn select(ui: &mut Ui, id: WidgetHandle<TextEdit>, state: &mut UiState, data: CursorData) {
|
||||
fn select(ui: &mut Ui, id: WidgetView<TextEdit>, state: &mut UiState, data: CursorData) {
|
||||
let now = Instant::now();
|
||||
let recent = (now - state.last_click) < Duration::from_millis(300);
|
||||
state.last_click = now;
|
||||
|
||||
@@ -30,7 +30,7 @@ pub struct DefaultState<AppState> {
|
||||
pub struct UiState {
|
||||
pub renderer: UiRenderer,
|
||||
pub input: Input,
|
||||
pub focus: Option<WidgetHandle<TextEdit>>,
|
||||
pub focus: Option<WidgetView<TextEdit>>,
|
||||
pub clipboard: Clipboard,
|
||||
pub window: Arc<Window>,
|
||||
pub ime: usize,
|
||||
@@ -91,7 +91,7 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
|
||||
let input_changed = ui_state.input.event(&event);
|
||||
let cursor_state = ui_state.cursor_state().clone();
|
||||
let old = ui_state.focus.clone();
|
||||
let old = ui_state.focus;
|
||||
if cursor_state.buttons.left.is_start() {
|
||||
ui_state.focus = None;
|
||||
}
|
||||
@@ -121,10 +121,9 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
||||
ui_state.renderer.resize(size)
|
||||
}
|
||||
WindowEvent::KeyboardInput { event, .. } => {
|
||||
if let Some(sel) = &ui_state.focus
|
||||
if let Some(sel) = ui_state.focus
|
||||
&& event.state.is_pressed()
|
||||
{
|
||||
let sel = &sel.clone();
|
||||
let mut text = sel.edit(ui);
|
||||
match text.apply_event(event, &ui_state.input.modifiers) {
|
||||
TextInputResult::Unfocus => {
|
||||
|
||||
12
src/event.rs
12
src/event.rs
@@ -9,20 +9,20 @@ macro_rules! event_ctx {
|
||||
#[allow(unused_imports)]
|
||||
use $crate::prelude::*;
|
||||
|
||||
pub trait EventableCtx<W, Tag, Ctx: 'static> {
|
||||
pub trait EventableCtx<WL: WidgetLike<Tag>, Tag, Ctx: 'static> {
|
||||
fn on<E: Event>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl WidgetEventFn<Ctx, E::Data, W>,
|
||||
) -> impl WidgetIdFn<W> + EventableCtx<W, IdFnTag, Ctx>;
|
||||
f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>,
|
||||
) -> impl WidgetIdFn<WL::Widget>;
|
||||
}
|
||||
|
||||
impl<WL: WidgetLike<Tag>, Tag> EventableCtx<WL::Widget, Tag, $ty> for WL {
|
||||
impl<WL: WidgetLike<Tag>, Tag> EventableCtx<WL, Tag, $ty> for WL {
|
||||
fn on<E: Event>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl WidgetEventFn<$ty, E::Data, WL::Widget>,
|
||||
) -> impl WidgetIdFn<WL::Widget> + EventableCtx<WL::Widget, IdFnTag, $ty> {
|
||||
) -> impl WidgetIdFn<WL::Widget> {
|
||||
eventable::Eventable::on(self, event, f)
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ pub mod eventable {
|
||||
let id_ = id.weak();
|
||||
ui.register_event(&id, event, move |ctx| {
|
||||
f(EventIdCtx {
|
||||
id: &id_,
|
||||
id: id_,
|
||||
state: ctx.state,
|
||||
data: ctx.data,
|
||||
ui: ctx.ui,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#![feature(fn_traits)]
|
||||
#![feature(gen_blocks)]
|
||||
#![feature(associated_type_defaults)]
|
||||
#![feature(unsize)]
|
||||
|
||||
mod default;
|
||||
mod event;
|
||||
|
||||
@@ -158,7 +158,7 @@ impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> FnOnce<(&mut Ui,)>
|
||||
|
||||
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
|
||||
Span {
|
||||
children: self.children.ui(args.0).arr.to_vec(),
|
||||
children: self.children.ui(args.0).arr.into_iter().collect(),
|
||||
dir: self.dir,
|
||||
gap: self.gap,
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ impl<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> FnOnce<(&mut Ui,)>
|
||||
|
||||
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
|
||||
Stack {
|
||||
children: self.children.ui(args.0).arr.to_vec(),
|
||||
children: self.children.ui(args.0).arr.into_iter().collect(),
|
||||
size: self.size,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::prelude::*;
|
||||
use std::marker::{Sized, Unsize};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WidgetPtr {
|
||||
@@ -28,3 +29,19 @@ impl Widget for WidgetPtr {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetPtr {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
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>>(
|
||||
&mut self,
|
||||
to: WidgetHandle<W>,
|
||||
) -> Option<WidgetHandle> {
|
||||
self.inner.replace(to)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ widget_trait! {
|
||||
}
|
||||
}
|
||||
|
||||
fn to_any(self) -> impl WidgetRet {
|
||||
fn to_any(self) -> impl WidgetIdFn {
|
||||
|ui| self.add(ui).any()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user