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"
clap = { version = "4.5.53", features = ["derive"] }
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]]
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 iris::{prelude::*, winit::UiState};
pub fn debug(client: &mut Client) {
client.ui.debug_layers();
pub fn debug(_client: &mut Client, ui: &mut Ui, _state: &UiState) {
ui.debug_layers();
// let mut file = std::fs::OpenOptions::new()
// .write(true)
// .create(true)

View File

@@ -16,6 +16,7 @@ use winit::{
window::WindowAttributes,
};
mod account;
mod debug;
mod net;
mod rsc;
@@ -24,7 +25,7 @@ mod ui;
fn main() {
install_crypto_provider();
UiApp::<Client>::run();
DefaultApp::<Client>::run();
}
pub struct Client {
@@ -33,7 +34,6 @@ pub struct Client {
state: ClientState,
main_ui: WidgetId<WidgetPtr>,
notif: WidgetId<WidgetPtr>,
ui: DefaultUi,
proxy: Proxy<ClientEvent>,
}
@@ -43,27 +43,23 @@ pub enum ClientEvent {
Err(String),
}
impl DefaultUiState for Client {
impl DefaultAppState for Client {
type Event = ClientEvent;
fn ui(&mut self) -> &mut DefaultUi {
&mut self.ui
}
fn window_attrs() -> WindowAttributes {
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 notif = WidgetPtr::default().add(&mut ui);
let main_ui = WidgetPtr::default().add(&mut ui);
let notif = WidgetPtr::default().add(ui);
let main_ui = WidgetPtr::default().add(ui);
(
notif.clone().pad(Padding::top(10)).align(Align::TOP_CENTER),
main_ui.clone(),
)
.stack()
.set_root(&mut ui);
.set_root(ui);
let mut s = Self {
data: dir.load(CLIENT_DATA),
@@ -71,14 +67,13 @@ impl DefaultUiState for Client {
dir,
main_ui: main_ui.clone(),
notif,
ui,
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
}
fn event(&mut self, event: ClientEvent) {
fn event(&mut self, event: ClientEvent, ui: &mut Ui, _state: &UiState) {
match event {
ClientEvent::Connect { send } => {
let ClientState::Connect(connect) = self.state.take() else {
@@ -91,15 +86,15 @@ impl DefaultUiState for Client {
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 {
ServerMsg::SendMsg(msg) => {
if let ClientState::LoggedIn(state) = &mut self.state
&& let Some(msg_area) = &state.channel
{
let msg = msg_widget(&msg.user, &msg.content).add(&mut self.ui);
self.ui[msg_area].children.push(msg.any());
let msg = msg_widget(&msg.user, &msg.content).add(ui);
ui[msg_area].children.push(msg.any());
}
}
ServerMsg::LoadMsgs(msgs) => {
@@ -108,8 +103,8 @@ impl DefaultUiState for Client {
{
for msg in msgs {
state.msgs.push(msg.clone());
let msg = msg_widget(&msg.user, &msg.content).add(&mut self.ui);
self.ui[msg_area].children.push(msg.any());
let msg = msg_widget(&msg.user, &msg.content).add(ui);
ui[msg_area].children.push(msg.any());
}
}
}
@@ -124,32 +119,32 @@ impl DefaultUiState for Client {
msgs: Vec::new(),
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) => {
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) => {
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.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 {
state: ElementState::Pressed,
button: MouseButton::Middle,
..
} = 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 crate::{net::AppHandle, state::ClientState};
use super::*;
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 start_screen(client: &mut Client) -> WidgetId {
// (wtext("Select Account").text_align(Align::CENTER).size(30),)
// .span(Dir::DOWN)
// .gap(10)
// .pad(15)
// .width(400)
// .align(Align::CENTER)
// .add(&mut client.ui)
// .any()
// }
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) + '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;
pub fn connect_screen(client: &mut Client, ui: &mut Ui, state: &UiState) -> WidgetId {
let Client { data, proxy, .. } = client;
let ip = field_widget(&data.ip, "ip", ui);
let ip_ = ip.clone();
let handle = AppHandle {
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 {
return;
};
let ip = client.ui[&ip_].content();
let ip = ui[&ip_].content();
state.handle = Some(connect(handle.clone(), ConnectInfo { ip }));
});
(
@@ -59,8 +37,8 @@ pub fn connect_screen(client: &mut Client) -> WidgetId {
.size(30),
field_box(
// NOTE: should probably do this on submit
ip.id_on(Edited, |id, client: &mut Client, _| {
client.data.ip = client.ui[id].content();
ip.on(Edited, |ctx| {
ctx.state.data.ip = ctx.ui[ctx.id].content();
})
.add(ui),
ui,
@@ -77,28 +55,28 @@ pub fn connect_screen(client: &mut Client) -> WidgetId {
.any()
}
pub fn login_screen(client: &mut Client) -> WidgetId {
let Client { data, ui, .. } = client;
pub fn login_screen(client: &mut Client, ui: &mut Ui) -> WidgetId {
let Client { data, .. } = client;
let username = field_widget(&data.username, "username", ui);
let password = field_widget(&data.password, "password", ui);
let username_ = username.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 {
return;
};
let username = client.ui[&username_].content();
let password = client.ui[&password_].content();
let username = ui[&username_].content();
let password = ui[&password_].content();
state.handle.send(ClientMsg::Login { username, password });
});
let username_ = username.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 {
return;
};
let username = client.ui[&username_].content();
let password = client.ui[&password_].content();
let username = ui[&username_].content();
let password = ui[&password_].content();
state
.handle
.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),
field_box(
username
.id_on(Edited, |id, client: &mut Client, _| {
client.data.username = client.ui[id].content();
.on(Edited, |ctx| {
ctx.state.data.username = ctx.ui[ctx.id].content();
})
.add(ui),
ui,
),
field_box(
password
.id_on(Edited, |id, client: &mut Client, _| {
client.data.password = client.ui[id].content();
.on(Edited, |ctx| {
ctx.state.data.password = ctx.ui[ctx.id].content();
})
.add(ui),
ui,

View File

@@ -11,11 +11,11 @@ use openworm::net::{ClientMsg, NetClientMsg};
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 {
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 bg = (
@@ -26,7 +26,7 @@ pub fn main_view(client: &mut Client) -> WidgetId {
(side_bar, msg_panel)
.span(Dir::RIGHT)
.background(bg)
.add(&mut client.ui)
.add(ui)
.any()
}
@@ -71,17 +71,17 @@ pub fn msg_panel(ui: &mut Ui, state: &mut LoggedIn) -> impl WidgetRet + use<> {
.height(rest(1)),
send_text
.clone()
.id_on(Submit, move |id, client: &mut Client, _| {
let ClientState::LoggedIn(state) = &mut client.state else {
.on(Submit, move |ctx| {
let ClientState::LoggedIn(state) = &mut ctx.state.state else {
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 {
content: content.clone(),
};
state.network.send(ClientMsg::SendMsg(msg.clone()));
let msg = msg_widget(&state.username, &content).add(&mut client.ui);
client.ui[&msg_area].children.push(msg.any());
let msg = msg_widget(&state.username, &content).add(ctx.ui);
ctx.ui[&msg_area].children.push(msg.any());
})
.pad(15)
.attr::<Selector>(send_text)

View File

@@ -1,3 +1,5 @@
use iris::winit::attr::Selector;
use super::*;
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 {
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 main::*;
pub use misc::*;
event_ctx!(Client);