switch to WidgetRef

This commit is contained in:
2025-12-07 15:18:39 -05:00
parent f9376eb6d4
commit 089ed08c16
6 changed files with 32 additions and 32 deletions

View File

@@ -32,8 +32,8 @@ pub struct Client {
dir: DataDir,
data: ClientData,
state: ClientState,
main_ui: WidgetId<WidgetPtr>,
notif: WidgetId<WidgetPtr>,
main_ui: WidgetRef<WidgetPtr>,
notif: WidgetRef<WidgetPtr>,
proxy: Proxy<ClientEvent>,
}
@@ -50,7 +50,7 @@ impl DefaultAppState for Client {
WindowAttributes::default().with_title("OPENWORM")
}
fn new(ui: &mut Ui, state: &UiState, 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(ui);
let main_ui = WidgetPtr::default().add(ui);
@@ -94,7 +94,7 @@ impl DefaultAppState for Client {
&& let Some(msg_area) = &state.channel
{
let msg = msg_widget(&msg.user, &msg.content).add(ui);
ui[msg_area].children.push(msg.any());
msg_area.get_mut().children.push(msg.any());
}
}
ServerMsg::LoadMsgs(msgs) => {
@@ -104,7 +104,7 @@ impl DefaultAppState for Client {
for msg in msgs {
state.msgs.push(msg.clone());
let msg = msg_widget(&msg.user, &msg.content).add(ui);
ui[msg_area].children.push(msg.any());
msg_area.get_mut().children.push(msg.any());
}
}
}
@@ -123,11 +123,11 @@ impl DefaultAppState for Client {
}
ServerMsg::Error(error) => {
let msg = format!("{error:?}");
ui[&self.notif].inner = Some(werror(ui, &msg));
self.notif.get_mut().inner = Some(werror(ui, &msg));
}
},
ClientEvent::Err(msg) => {
ui[&self.notif].inner = Some(werror(ui, &msg));
self.notif.get_mut().inner = Some(werror(ui, &msg));
}
}
}

View File

@@ -16,7 +16,7 @@ pub struct Login {
pub struct LoggedIn {
pub network: NetHandle,
pub msgs: Vec<NetServerMsg>,
pub channel: Option<WidgetId<Span>>,
pub channel: Option<WidgetRef<Span>>,
pub username: String,
}

View File

@@ -5,7 +5,7 @@ use crate::{net::AppHandle, state::ClientState};
use super::*;
pub fn start_screen(client: &mut Client, ui: &mut Ui) -> WidgetId {
pub fn start_screen(client: &mut Client, ui: &mut Ui) -> WidgetRef {
let mut accounts = Span::empty(Dir::DOWN);
accounts.push(
@@ -38,7 +38,7 @@ pub fn start_screen(client: &mut Client, ui: &mut Ui) -> WidgetId {
.any()
}
pub fn connect_screen(client: &mut Client, ui: &mut Ui, state: &UiState) -> WidgetId {
pub fn connect_screen(client: &mut Client, ui: &mut Ui, state: &UiState) -> WidgetRef {
let Client { data, proxy, .. } = client;
let ip = field_widget(&data.ip, "ip", ui);
let ip_ = ip.clone();
@@ -46,11 +46,11 @@ pub fn connect_screen(client: &mut Client, ui: &mut Ui, state: &UiState) -> Widg
proxy: proxy.clone(),
window: state.window.clone(),
};
let submit = submit_button("connect", move |client, ui| {
let submit = submit_button("connect", move |client, _ui| {
let ClientState::Connect(state) = &mut client.state else {
return;
};
let ip = ui[&ip_].content();
let ip = ip_.get().content();
state.handle = Some(connect(handle.clone(), ConnectInfo { ip }));
});
(
@@ -60,7 +60,7 @@ pub fn connect_screen(client: &mut Client, ui: &mut Ui, state: &UiState) -> Widg
field_box(
// NOTE: should probably do this on submit
ip.on(Edited, |ctx| {
ctx.state.data.ip = ctx.ui[ctx.id].content();
ctx.state.data.ip = ctx.widget.get().content();
})
.add(ui),
ui,
@@ -77,28 +77,28 @@ pub fn connect_screen(client: &mut Client, ui: &mut Ui, state: &UiState) -> Widg
.any()
}
pub fn login_screen(client: &mut Client, ui: &mut Ui) -> WidgetId {
pub fn login_screen(client: &mut Client, ui: &mut Ui) -> WidgetRef {
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, ui| {
let submit = submit_button("login", move |client, _ui| {
let ClientState::Login(state) = &mut client.state else {
return;
};
let username = ui[&username_].content();
let password = ui[&password_].content();
let username = username_.get().content();
let password = password_.get().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, ui| {
let create_button = submit_button("create account", move |client, _ui| {
let ClientState::Login(state) = &mut client.state else {
return;
};
let username = ui[&username_].content();
let password = ui[&password_].content();
let username = username_.get().content();
let password = password_.get().content();
state
.handle
.send(ClientMsg::CreateAccount { username, password });
@@ -108,7 +108,7 @@ pub fn login_screen(client: &mut Client, ui: &mut Ui) -> WidgetId {
field_box(
username
.on(Edited, |ctx| {
ctx.state.data.username = ctx.ui[ctx.id].content();
ctx.state.data.username = ctx.widget.get().content();
})
.add(ui),
ui,
@@ -116,7 +116,7 @@ pub fn login_screen(client: &mut Client, ui: &mut Ui) -> WidgetId {
field_box(
password
.on(Edited, |ctx| {
ctx.state.data.password = ctx.ui[ctx.id].content();
ctx.state.data.password = ctx.widget.get().content();
})
.add(ui),
ui,

View File

@@ -11,7 +11,7 @@ use openworm::net::{ClientMsg, NetClientMsg};
pub const SIZE: u32 = 20;
pub fn main_view(client: &mut Client, ui: &mut Ui) -> WidgetId {
pub fn main_view(client: &mut Client, ui: &mut Ui) -> WidgetRef {
let ClientState::LoggedIn(state) = &mut client.state else {
panic!("we ain't logged in buh");
};
@@ -75,13 +75,13 @@ pub fn msg_panel(ui: &mut Ui, state: &mut LoggedIn) -> impl WidgetRet + use<> {
let ClientState::LoggedIn(state) = &mut ctx.state.state else {
panic!("we ain't logged in buh");
};
let content = ctx.ui.text(ctx.id).take();
let content = ctx.widget.get_mut().take();
let msg = NetClientMsg {
content: content.clone(),
};
state.network.send(ClientMsg::SendMsg(msg.clone()));
let msg = msg_widget(&state.username, &content).add(ctx.ui);
ctx.ui[&msg_area].children.push(msg.any());
msg_area.get_mut().children.push(msg.any());
})
.pad(15)
.attr::<Selector>(send_text)

View File

@@ -2,7 +2,7 @@ use iris::winit::attr::Selector;
use super::*;
pub fn werror(ui: &mut Ui, msg: &str) -> WidgetId {
pub fn werror(ui: &mut Ui, msg: &str) -> WidgetRef {
wtext(msg)
.size(20)
.pad(10)
@@ -15,7 +15,7 @@ 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> {
pub fn field_widget(name: &str, hint_text: &str, ui: &mut Ui) -> WidgetRef<TextEdit> {
wtext(name)
.editable(true)
.size(20)
@@ -23,7 +23,7 @@ pub fn field_widget(name: &str, hint_text: &str, ui: &mut Ui) -> WidgetId<TextEd
.add(ui)
}
pub fn field_box(field: WidgetId<TextEdit>, ui: &mut Ui) -> WidgetId {
pub fn field_box(field: WidgetRef<TextEdit>, ui: &mut Ui) -> WidgetRef {
field
.clone()
.pad(10)
@@ -41,17 +41,17 @@ pub fn submit_button(
rect(color)
.radius(15)
.on(CursorSense::click(), move |ctx| {
ctx.ui[ctx.id].color = color.darker(0.2);
ctx.widget.get_mut().color = color.darker(0.2);
on_submit(ctx.state, ctx.ui);
})
.on(
CursorSense::HoverStart | CursorSense::unclick(),
move |ctx| {
ctx.ui[ctx.id].color = color.brighter(0.1);
ctx.widget.get_mut().color = color.brighter(0.1);
},
)
.on(CursorSense::HoverEnd, move |ctx| {
ctx.ui[ctx.id].color = color;
ctx.widget.get_mut().color = color;
})
.height(60)
.foreground(wtext(text).size(25).text_align(Align::CENTER))