update iris

This commit is contained in:
2025-12-06 20:48:19 -05:00
parent 09be172e36
commit a1928edb66
10 changed files with 467 additions and 308 deletions

563
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -23,6 +23,8 @@ ron = "0.12.0"
sled = "0.34.7" sled = "0.34.7"
clap = { version = "4.5.53", features = ["derive"] } clap = { version = "4.5.53", features = ["derive"] }
scrypt = "0.11.0" scrypt = "0.11.0"
ed25519-dalek = { version = "3.0.0-pre.2", features = ["rand_core"] }
rand = { version = "0.10.0-rc.5", features = ["chacha"] }
[[bin]] [[bin]]
name = "openworm-client" name = "openworm-client"

2
iris

Submodule iris updated: f6b1143665...62aa02847a

22
src/bin/client/account.rs Normal file
View File

@@ -0,0 +1,22 @@
use ed25519_dalek::SigningKey;
use rand::{
SeedableRng,
rngs::{OsRng, StdRng},
};
pub struct Account {
device_key: SigningKey,
account_key: SigningKey,
}
impl Account {
pub fn new() -> Account {
let mut csprng = StdRng::try_from_rng(&mut OsRng).unwrap();
let device_key = SigningKey::generate(&mut csprng);
let account_key = SigningKey::generate(&mut csprng);
Account {
device_key,
account_key,
}
}
}

View File

@@ -1,7 +1,8 @@
use crate::Client; use crate::Client;
use iris::{prelude::*, winit::UiState};
pub fn debug(client: &mut Client) { pub fn debug(_client: &mut Client, ui: &mut Ui, _state: &UiState) {
client.ui.debug_layers(); ui.debug_layers();
// let mut file = std::fs::OpenOptions::new() // let mut file = std::fs::OpenOptions::new()
// .write(true) // .write(true)
// .create(true) // .create(true)

View File

@@ -16,6 +16,7 @@ use winit::{
window::WindowAttributes, window::WindowAttributes,
}; };
mod account;
mod debug; mod debug;
mod net; mod net;
mod rsc; mod rsc;
@@ -24,7 +25,7 @@ mod ui;
fn main() { fn main() {
install_crypto_provider(); install_crypto_provider();
UiApp::<Client>::run(); DefaultApp::<Client>::run();
} }
pub struct Client { pub struct Client {
@@ -33,7 +34,6 @@ pub struct Client {
state: ClientState, state: ClientState,
main_ui: WidgetId<WidgetPtr>, main_ui: WidgetId<WidgetPtr>,
notif: WidgetId<WidgetPtr>, notif: WidgetId<WidgetPtr>,
ui: DefaultUi,
proxy: Proxy<ClientEvent>, proxy: Proxy<ClientEvent>,
} }
@@ -43,27 +43,23 @@ pub enum ClientEvent {
Err(String), Err(String),
} }
impl DefaultUiState for Client { impl DefaultAppState for Client {
type Event = ClientEvent; type Event = ClientEvent;
fn ui(&mut self) -> &mut DefaultUi {
&mut self.ui
}
fn window_attrs() -> WindowAttributes { fn window_attrs() -> WindowAttributes {
WindowAttributes::default().with_title("OPENWORM") WindowAttributes::default().with_title("OPENWORM")
} }
fn new(mut ui: DefaultUi, proxy: Proxy<Self::Event>) -> Self { fn new(ui: &mut Ui, state: &UiState, proxy: Proxy<Self::Event>) -> Self {
let dir = DataDir::default(); let dir = DataDir::default();
let notif = WidgetPtr::default().add(&mut ui); let notif = WidgetPtr::default().add(ui);
let main_ui = WidgetPtr::default().add(&mut ui); let main_ui = WidgetPtr::default().add(ui);
( (
notif.clone().pad(Padding::top(10)).align(Align::TOP_CENTER), notif.clone().pad(Padding::top(10)).align(Align::TOP_CENTER),
main_ui.clone(), main_ui.clone(),
) )
.stack() .stack()
.set_root(&mut ui); .set_root(ui);
let mut s = Self { let mut s = Self {
data: dir.load(CLIENT_DATA), data: dir.load(CLIENT_DATA),
@@ -71,14 +67,13 @@ impl DefaultUiState for Client {
dir, dir,
main_ui: main_ui.clone(), main_ui: main_ui.clone(),
notif, notif,
ui,
proxy, proxy,
}; };
connect_screen(&mut s).set_ptr(&s.main_ui, &mut s.ui); connect_screen(&mut s, ui, state).set_ptr(&s.main_ui, ui);
s s
} }
fn event(&mut self, event: ClientEvent) { fn event(&mut self, event: ClientEvent, ui: &mut Ui, _state: &UiState) {
match event { match event {
ClientEvent::Connect { send } => { ClientEvent::Connect { send } => {
let ClientState::Connect(connect) = self.state.take() else { let ClientState::Connect(connect) = self.state.take() else {
@@ -91,15 +86,15 @@ impl DefaultUiState for Client {
thread: th, thread: th,
}, },
}); });
login_screen(self).set_ptr(&self.main_ui, &mut self.ui); login_screen(self, ui).set_ptr(&self.main_ui, ui);
} }
ClientEvent::ServerMsg(msg) => match msg { ClientEvent::ServerMsg(msg) => match msg {
ServerMsg::SendMsg(msg) => { ServerMsg::SendMsg(msg) => {
if let ClientState::LoggedIn(state) = &mut self.state if let ClientState::LoggedIn(state) = &mut self.state
&& let Some(msg_area) = &state.channel && let Some(msg_area) = &state.channel
{ {
let msg = msg_widget(&msg.user, &msg.content).add(&mut self.ui); let msg = msg_widget(&msg.user, &msg.content).add(ui);
self.ui[msg_area].children.push(msg.any()); ui[msg_area].children.push(msg.any());
} }
} }
ServerMsg::LoadMsgs(msgs) => { ServerMsg::LoadMsgs(msgs) => {
@@ -108,8 +103,8 @@ impl DefaultUiState for Client {
{ {
for msg in msgs { for msg in msgs {
state.msgs.push(msg.clone()); state.msgs.push(msg.clone());
let msg = msg_widget(&msg.user, &msg.content).add(&mut self.ui); let msg = msg_widget(&msg.user, &msg.content).add(ui);
self.ui[msg_area].children.push(msg.any()); ui[msg_area].children.push(msg.any());
} }
} }
} }
@@ -124,32 +119,32 @@ impl DefaultUiState for Client {
msgs: Vec::new(), msgs: Vec::new(),
username, username,
}); });
main_view(self).set_ptr(&self.main_ui, &mut self.ui); main_view(self, ui).set_ptr(&self.main_ui, ui);
} }
ServerMsg::Error(error) => { ServerMsg::Error(error) => {
let msg = format!("{error:?}"); let msg = format!("{error:?}");
self.ui[&self.notif].inner = Some(werror(&mut self.ui, &msg)); ui[&self.notif].inner = Some(werror(ui, &msg));
} }
}, },
ClientEvent::Err(msg) => { ClientEvent::Err(msg) => {
self.ui[&self.notif].inner = Some(werror(&mut self.ui, &msg)); ui[&self.notif].inner = Some(werror(ui, &msg));
} }
} }
} }
fn exit(&mut self) { fn exit(&mut self, _ui: &mut Ui, _state: &UiState) {
self.state.exit(); self.state.exit();
self.dir.save(CLIENT_DATA, &self.data); self.dir.save(CLIENT_DATA, &self.data);
} }
fn window_event(&mut self, event: WindowEvent) { fn window_event(&mut self, event: WindowEvent, ui: &mut Ui, state: &UiState) {
if let WindowEvent::MouseInput { if let WindowEvent::MouseInput {
state: ElementState::Pressed, state: ElementState::Pressed,
button: MouseButton::Middle, button: MouseButton::Middle,
.. ..
} = event } = event
{ {
debug::debug(self); debug::debug(self, ui, state);
} }
} }
} }

View File

@@ -1,56 +1,34 @@
use iris::winit::{attr::Selector, event::Edited}; use iris::winit::{UiState, event::Edited};
use openworm::net::ClientMsg; use openworm::net::ClientMsg;
use crate::{net::AppHandle, state::ClientState}; use crate::{net::AppHandle, state::ClientState};
use super::*; use super::*;
pub fn field_widget(name: &str, hint_text: &str, ui: &mut Ui) -> WidgetId<TextEdit> { // pub fn start_screen(client: &mut Client) -> WidgetId {
wtext(name) // (wtext("Select Account").text_align(Align::CENTER).size(30),)
.editable(true) // .span(Dir::DOWN)
.size(20) // .gap(10)
.hint(hint(hint_text)) // .pad(15)
.add(ui) // .width(400)
} // .align(Align::CENTER)
// .add(&mut client.ui)
// .any()
// }
pub fn field_box(field: WidgetId<TextEdit>, ui: &mut Ui) -> WidgetId { pub fn connect_screen(client: &mut Client, ui: &mut Ui, state: &UiState) -> WidgetId {
field let Client { data, proxy, .. } = client;
.clone()
.pad(10)
.background(rect(Color::BLACK.brighter(0.1)).radius(15))
.attr::<Selector>(field)
.add(ui)
.any()
}
pub fn submit_button(text: &str, on_submit: impl Fn(&mut Client) + 'static) -> impl WidgetRet {
let color = Color::GREEN;
rect(color)
.radius(15)
.id_on(CursorSense::click(), move |id, client: &mut Client, _| {
client.ui[id].color = color.darker(0.3);
on_submit(client);
})
.height(40)
.foreground(wtext(text).size(20).text_align(Align::CENTER))
.to_any()
}
pub fn connect_screen(client: &mut Client) -> WidgetId {
let Client {
data, ui, proxy, ..
} = client;
let ip = field_widget(&data.ip, "ip", ui); let ip = field_widget(&data.ip, "ip", ui);
let ip_ = ip.clone(); let ip_ = ip.clone();
let handle = AppHandle { let handle = AppHandle {
proxy: proxy.clone(), proxy: proxy.clone(),
window: ui.window.clone(), window: state.window.clone(),
}; };
let submit = submit_button("connect", move |client| { let submit = submit_button("connect", move |client, ui| {
let ClientState::Connect(state) = &mut client.state else { let ClientState::Connect(state) = &mut client.state else {
return; return;
}; };
let ip = client.ui[&ip_].content(); let ip = ui[&ip_].content();
state.handle = Some(connect(handle.clone(), ConnectInfo { ip })); state.handle = Some(connect(handle.clone(), ConnectInfo { ip }));
}); });
( (
@@ -59,8 +37,8 @@ pub fn connect_screen(client: &mut Client) -> WidgetId {
.size(30), .size(30),
field_box( field_box(
// NOTE: should probably do this on submit // NOTE: should probably do this on submit
ip.id_on(Edited, |id, client: &mut Client, _| { ip.on(Edited, |ctx| {
client.data.ip = client.ui[id].content(); ctx.state.data.ip = ctx.ui[ctx.id].content();
}) })
.add(ui), .add(ui),
ui, ui,
@@ -77,28 +55,28 @@ pub fn connect_screen(client: &mut Client) -> WidgetId {
.any() .any()
} }
pub fn login_screen(client: &mut Client) -> WidgetId { pub fn login_screen(client: &mut Client, ui: &mut Ui) -> WidgetId {
let Client { data, ui, .. } = client; let Client { data, .. } = client;
let username = field_widget(&data.username, "username", ui); let username = field_widget(&data.username, "username", ui);
let password = field_widget(&data.password, "password", ui); let password = field_widget(&data.password, "password", ui);
let username_ = username.clone(); let username_ = username.clone();
let password_ = password.clone(); let password_ = password.clone();
let submit = submit_button("login", move |client| { let submit = submit_button("login", move |client, ui| {
let ClientState::Login(state) = &mut client.state else { let ClientState::Login(state) = &mut client.state else {
return; return;
}; };
let username = client.ui[&username_].content(); let username = ui[&username_].content();
let password = client.ui[&password_].content(); let password = ui[&password_].content();
state.handle.send(ClientMsg::Login { username, password }); state.handle.send(ClientMsg::Login { username, password });
}); });
let username_ = username.clone(); let username_ = username.clone();
let password_ = password.clone(); let password_ = password.clone();
let create_button = submit_button("create account", move |client| { let create_button = submit_button("create account", move |client, ui| {
let ClientState::Login(state) = &mut client.state else { let ClientState::Login(state) = &mut client.state else {
return; return;
}; };
let username = client.ui[&username_].content(); let username = ui[&username_].content();
let password = client.ui[&password_].content(); let password = ui[&password_].content();
state state
.handle .handle
.send(ClientMsg::CreateAccount { username, password }); .send(ClientMsg::CreateAccount { username, password });
@@ -107,16 +85,16 @@ pub fn login_screen(client: &mut Client) -> WidgetId {
wtext("login to server").text_align(Align::CENTER).size(30), wtext("login to server").text_align(Align::CENTER).size(30),
field_box( field_box(
username username
.id_on(Edited, |id, client: &mut Client, _| { .on(Edited, |ctx| {
client.data.username = client.ui[id].content(); ctx.state.data.username = ctx.ui[ctx.id].content();
}) })
.add(ui), .add(ui),
ui, ui,
), ),
field_box( field_box(
password password
.id_on(Edited, |id, client: &mut Client, _| { .on(Edited, |ctx| {
client.data.password = client.ui[id].content(); ctx.state.data.password = ctx.ui[ctx.id].content();
}) })
.add(ui), .add(ui),
ui, ui,

View File

@@ -11,11 +11,11 @@ use openworm::net::{ClientMsg, NetClientMsg};
pub const SIZE: u32 = 20; pub const SIZE: u32 = 20;
pub fn main_view(client: &mut Client) -> WidgetId { pub fn main_view(client: &mut Client, ui: &mut Ui) -> WidgetId {
let ClientState::LoggedIn(state) = &mut client.state else { let ClientState::LoggedIn(state) = &mut client.state else {
panic!("we ain't logged in buh"); panic!("we ain't logged in buh");
}; };
let msg_panel = msg_panel(&mut client.ui, state); let msg_panel = msg_panel(ui, state);
let side_bar = rect(Color::BLACK.brighter(0.05)).width(80); let side_bar = rect(Color::BLACK.brighter(0.05)).width(80);
let bg = ( let bg = (
@@ -26,7 +26,7 @@ pub fn main_view(client: &mut Client) -> WidgetId {
(side_bar, msg_panel) (side_bar, msg_panel)
.span(Dir::RIGHT) .span(Dir::RIGHT)
.background(bg) .background(bg)
.add(&mut client.ui) .add(ui)
.any() .any()
} }
@@ -71,17 +71,17 @@ pub fn msg_panel(ui: &mut Ui, state: &mut LoggedIn) -> impl WidgetRet + use<> {
.height(rest(1)), .height(rest(1)),
send_text send_text
.clone() .clone()
.id_on(Submit, move |id, client: &mut Client, _| { .on(Submit, move |ctx| {
let ClientState::LoggedIn(state) = &mut client.state else { let ClientState::LoggedIn(state) = &mut ctx.state.state else {
panic!("we ain't logged in buh"); panic!("we ain't logged in buh");
}; };
let content = client.ui.text(id).take(); let content = ctx.ui.text(ctx.id).take();
let msg = NetClientMsg { let msg = NetClientMsg {
content: content.clone(), content: content.clone(),
}; };
state.network.send(ClientMsg::SendMsg(msg.clone())); state.network.send(ClientMsg::SendMsg(msg.clone()));
let msg = msg_widget(&state.username, &content).add(&mut client.ui); let msg = msg_widget(&state.username, &content).add(ctx.ui);
client.ui[&msg_area].children.push(msg.any()); ctx.ui[&msg_area].children.push(msg.any());
}) })
.pad(15) .pad(15)
.attr::<Selector>(send_text) .attr::<Selector>(send_text)

View File

@@ -1,3 +1,5 @@
use iris::winit::attr::Selector;
use super::*; use super::*;
pub fn werror(ui: &mut Ui, msg: &str) -> WidgetId { pub fn werror(ui: &mut Ui, msg: &str) -> WidgetId {
@@ -12,3 +14,37 @@ pub fn werror(ui: &mut Ui, msg: &str) -> WidgetId {
pub fn hint(msg: impl Into<String>) -> TextBuilder { pub fn hint(msg: impl Into<String>) -> TextBuilder {
wtext(msg).size(20).color(Color::GRAY) wtext(msg).size(20).color(Color::GRAY)
} }
pub fn field_widget(name: &str, hint_text: &str, ui: &mut Ui) -> WidgetId<TextEdit> {
wtext(name)
.editable(true)
.size(20)
.hint(hint(hint_text))
.add(ui)
}
pub fn field_box(field: WidgetId<TextEdit>, ui: &mut Ui) -> WidgetId {
field
.clone()
.pad(10)
.background(rect(Color::BLACK.brighter(0.1)).radius(15))
.attr::<Selector>(field)
.add(ui)
.any()
}
pub fn submit_button(
text: &str,
on_submit: impl Fn(&mut Client, &mut Ui) + 'static,
) -> impl WidgetRet {
let color = Color::GREEN;
rect(color)
.radius(15)
.on(CursorSense::click(), move |ctx| {
ctx.ui[ctx.id].color = color.darker(0.3);
on_submit(ctx.state, ctx.ui);
})
.height(40)
.foreground(wtext(text).size(20).text_align(Align::CENTER))
.to_any()
}

View File

@@ -10,3 +10,5 @@ mod misc;
pub use login::*; pub use login::*;
pub use main::*; pub use main::*;
pub use misc::*; pub use misc::*;
event_ctx!(Client);