IDC FINALLY OH MY GOD (I think like ctx + resize propagation + some other stuff)

This commit is contained in:
2025-09-11 00:59:26 -04:00
parent 709a2d0e17
commit 242c3b992e
15 changed files with 476 additions and 281 deletions

View File

@@ -5,7 +5,12 @@ use cosmic_text::Family;
use render::Renderer;
use senses::*;
use ui::prelude::*;
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
use winit::{
event::WindowEvent,
event_loop::ActiveEventLoop,
keyboard::{Key, NamedKey},
window::Window,
};
use crate::testing::input::Input;
@@ -20,7 +25,7 @@ pub fn main() {
pub struct Client {
renderer: Renderer,
input: Input,
ui: Ui,
ui: Ui<Client>,
info: WidgetId<Text>,
selected: Option<WidgetId<Text>>,
}
@@ -71,22 +76,31 @@ impl Client {
let span_add = Span::empty(Dir::RIGHT).add_static(&mut ui);
let main = pad_test.pad(10).add_static(&mut ui);
let add_button = rect(Color::LIME)
.radius(30)
.on(PRESS_START, move |ctx: &mut Client, _| {
let child = ctx
.ui
.add(image(include_bytes!("assets/sungals.png")).center())
.any();
ctx.ui[span_add].children.push((child, sized()));
})
.size(150)
.align(Align::BotRight);
let switch_button = |color, to, label| {
let rect = rect(color)
.id_on(PRESS_START, move |id, ui, _| {
ui[main].inner.set_static(to);
ui[id].color = color.add_rgb(-0.2);
})
.edit_on(HOVER_START | PRESS_END, move |r, _| {
r.color = color.add_rgb(0.4);
})
.edit_on(HOVER_END, move |r, _| {
r.color = color;
});
(rect, text(label).font_size(30)).stack()
};
let del_button = rect(Color::RED)
.radius(30)
.on(PRESS_START, move |ctx: &mut Client, _| {
ctx.ui[span_add].children.pop();
})
.size(150)
.align(Align::BotLeft);
let span_add_test = (span_add, add_button, del_button)
.stack()
.add_static(&mut ui);
let main = pad_test.pad(10).add_static(&mut ui);
let btext = |content| text(content).font_size(30);
@@ -113,56 +127,51 @@ impl Client {
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);
}),
text("add")
.font_size(30)
.id_on(PRESS_START, |id, client: &mut Client, ctx| {
client.ui[id].select(ctx.cursor, ctx.size);
client.selected = Some(id.clone());
})
.pad(30),
)
.span(Dir::DOWN, [ratio(1), fixed(40)])
.span(Dir::DOWN, [ratio(1), sized()])
.add_static(&mut ui);
let switch_button = |color, to, label| {
let rect = rect(color)
.id_on(PRESS_START, move |id, ctx: &mut Client, _| {
ctx.ui[main].inner.set_static(to);
ctx.ui[id].color = color.add_rgb(-0.2);
})
.edit_on(HOVER_START | PRESS_END, move |r, _| {
r.color = color.add_rgb(0.4);
})
.edit_on(HOVER_END, move |r, _| {
r.color = color;
});
(rect, text(label).font_size(30)).stack()
};
let tabs = (
switch_button(Color::RED, pad_test, "pad"),
switch_button(Color::GREEN, span_test, "span"),
switch_button(Color::BLUE, span_add, "image span"),
switch_button(Color::MAGENTA, text_test, "text layout"),
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::YELLOW.mul_rgb(0.5),
text_edit_scroll,
text_edit_scroll.any(),
"text edit scroll",
),
)
.span(Dir::RIGHT, ratio(1));
let add_button = rect(Color::LIME)
.radius(30)
.on(PRESS_START, move |ui, _| {
let child = ui
.add(image(include_bytes!("assets/sungals.png")).center())
.erase_type();
ui[span_add].children.push((child, sized()));
})
.size(150)
.align(Align::BotRight);
let del_button = rect(Color::RED)
.radius(30)
.on(PRESS_START, move |ui, _| {
ui[span_add].children.pop();
})
.size(150)
.align(Align::BotLeft);
let info = text("").add(&mut ui);
let info_sect = info.clone().pad(10).align(Align::BotLeft);
ui.set_root(
(
tabs.label("tabs"),
(
main,
add_button.label("add button"),
del_button.label("del button"),
info_sect.label("info sect"),
)
(main, info_sect.label("info sect"))
.stack()
.label("main stack"),
)
@@ -183,7 +192,7 @@ impl Client {
self.input.event(&event);
let cursor_state = self.cursor_state();
let window_size = self.window_size();
self.ui.run_sensors(&cursor_state, window_size);
run_sensors(self, &cursor_state, window_size);
match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
@@ -195,6 +204,34 @@ impl Client {
self.ui.resize((size.width, size.height));
self.renderer.resize(&size)
}
WindowEvent::KeyboardInput { event, .. } => {
if let Some(sel) = &self.selected
&& event.state.is_pressed()
{
let w = &mut self.ui[sel];
match &event.logical_key {
Key::Named(named) => match named {
NamedKey::Backspace => w.backspace(),
NamedKey::Delete => w.delete(),
NamedKey::Space => w.insert(" "),
NamedKey::Enter => w.insert("\n"),
NamedKey::ArrowRight => w.move_cursor(Dir::RIGHT),
NamedKey::ArrowLeft => w.move_cursor(Dir::LEFT),
NamedKey::ArrowUp => w.move_cursor(Dir::UP),
NamedKey::ArrowDown => w.move_cursor(Dir::DOWN),
NamedKey::Escape => {
w.deselect();
self.selected = None;
}
_ => (),
},
Key::Character(text) => {
w.insert(text);
}
_ => (),
}
}
}
_ => (),
}
let new = format!(
@@ -211,3 +248,9 @@ impl Client {
}
}
}
impl UiCtx for Client {
fn ui(&mut self) -> &mut Ui<Self> {
&mut self.ui
}
}