refactor for server

This commit is contained in:
2025-11-11 14:34:42 -05:00
parent 0ff6756713
commit d72a070a73
10 changed files with 17 additions and 5 deletions

164
src/client/mod.rs Normal file
View File

@@ -0,0 +1,164 @@
use std::sync::Arc;
pub use app::App;
use arboard::Clipboard;
use input::Input;
use render::Renderer;
use ui::prelude::*;
use winit::{event::WindowEvent, event_loop::ActiveEventLoop, window::Window};
mod app;
mod input;
mod render;
pub struct Client {
renderer: Renderer,
input: Input,
ui: Ui,
focus: Option<WidgetId<TextEdit>>,
clipboard: Clipboard,
}
#[derive(Eq, PartialEq, Hash, Clone)]
struct Submit;
impl DefaultEvent for Submit {
type Data = ();
}
impl Client {
pub fn new(window: Arc<Window>) -> Self {
let renderer = Renderer::new(window);
let mut ui = Ui::new();
let send_text = text("some stuff idk")
.editable()
.size(20)
.text_align(Align::Left)
.add(&mut ui);
let msg_area = Span::empty(Dir::DOWN).spacing(15).add(&mut ui);
let msg_panel = (
rect(Color::BLACK.brighter(0.1)),
(
msg_area
.clone()
.align(Align::BotLeft)
.scroll()
.pad(Padding::x(15)),
(
rect(Color::BLACK.brighter(0.05)).radius(15),
send_text
.clone()
.id_on(Submit, move |id, client: &mut Client, _| {
let content = client.ui.text(id).take();
let content = text(content)
.editable()
.size(20)
.text_align(Align::Left)
.wrap(true)
.id_on(CursorSense::click(), |id, client: &mut Client, ctx| {
client.ui.text(id).select(ctx.cursor, ctx.size);
client.focus = Some(id.clone());
});
let header = text("some user").size(20);
let msg = (
image(include_bytes!("./assets/sungals.png"))
.sized(70)
.align(Align::TopLeft),
().sized(10),
(header.align(Align::TopLeft), content.align(Align::TopLeft))
.span(Dir::DOWN, [fixed(30), sized()]),
)
.span(Dir::RIGHT, [sized(), sized(), ratio(1)])
.add(&mut client.ui);
client.ui[&msg_area].children.push((msg.any(), sized()));
})
.pad(15)
.on(CursorSense::click(), move |client: &mut Client, data| {
client.ui.text(&send_text).select(data.cursor, data.size);
client.focus = Some(send_text.clone());
}),
)
.stack()
.pad(15),
)
.span(Dir::DOWN, [ratio(1), fixed(80)]),
)
.stack();
(rect(Color::BLACK.brighter(0.05)), msg_panel)
.span(Dir::RIGHT, [fixed(80), ratio(1)])
.set_root(&mut ui);
Self {
renderer,
input: Input::default(),
ui,
focus: None,
clipboard: Clipboard::new().unwrap(),
}
}
pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
let input_changed = self.input.event(&event);
let cursor_state = self.cursor_state().clone();
if let Some(focus) = &self.focus
&& cursor_state.buttons.left.is_start()
{
self.ui.text(focus).deselect();
self.focus = None;
}
if input_changed {
let window_size = self.window_size();
self.run_sensors(&cursor_state, window_size);
self.ui.run_sensors(&cursor_state, window_size);
}
match event {
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
self.ui.update();
self.renderer.update(&mut self.ui);
self.renderer.draw()
}
WindowEvent::Resized(size) => {
self.ui.resize((size.width, size.height));
self.renderer.resize(&size)
}
WindowEvent::KeyboardInput { event, .. } => {
if let Some(sel) = &self.focus
&& event.state.is_pressed()
{
let mut text = self.ui.text(sel);
match text.apply_event(&event, &self.input.modifiers) {
TextInputResult::Unfocus => {
self.focus = None;
}
TextInputResult::Submit => {
self.run_event(&sel.clone(), Submit, ());
}
TextInputResult::Paste => {
if let Ok(t) = self.clipboard.get_text() {
text.insert(&t);
}
}
TextInputResult::Unused | TextInputResult::Used => (),
}
}
}
_ => (),
}
if self.ui.needs_redraw() {
self.renderer.window().request_redraw();
}
self.input.end_frame();
}
}
impl UiCtx for Client {
fn ui(&mut self) -> &mut Ui {
&mut self.ui
}
}