diff --git a/iris b/iris index 1102dc7..c118bb4 160000 --- a/iris +++ b/iris @@ -1 +1 @@ -Subproject commit 1102dc7338f1b4ec5ad6362da33ab3c52fcdff98 +Subproject commit c118bb446b83660347a56486dfa196a1be6a3a5d diff --git a/src/bin/client/ui/color.rs b/src/bin/client/ui/color.rs index 3f2ac30..142e20c 100644 --- a/src/bin/client/ui/color.rs +++ b/src/bin/client/ui/color.rs @@ -2,4 +2,5 @@ use super::*; pub const MODAL_BG: UiColor = UiColor::BLACK.brighter(0.05); pub const GREEN: UiColor = UiColor::rgb(0, 150, 0); +pub const RED: UiColor = UiColor::rgb(255, 100, 100); pub const DARK: UiColor = UiColor::BLACK.brighter(0.02); diff --git a/src/bin/client/ui/friends.rs b/src/bin/client/ui/friends.rs index 07931c3..7f222e4 100644 --- a/src/bin/client/ui/friends.rs +++ b/src/bin/client/ui/friends.rs @@ -1,4 +1,9 @@ -use openworm::net::{AddFriend, AddFriendResp, GenerateToken, RequestFriends, ServerPerms}; +use openworm::net::{ + AddFriend, AddFriendResp, AnswerFriendRequest, FriendRequestAction, GenerateToken, + RequestFriends, ServerPerms, UserId, +}; + +use crate::net::NetHandle; use super::*; @@ -99,13 +104,34 @@ fn friends_list(rsc: &mut Rsc, session: &Session) -> WeakWidget { let mut all = Span::empty(Dir::DOWN); if !resp.incoming.is_empty() { all.push(section_label("incoming").add_strong(rsc)); - all.push(user_list(&resp.incoming, &session, rsc).add_strong(rsc)) + all.push( + resp.incoming + .rsc_map(|id, rsc| { + ( + user_rect(id, &session, rsc), + accept_req(id, &con, rsc), + deny_req(id, &con, rsc), + ) + .span(Dir::RIGHT) + .add(rsc) + }) + .span(Dir::DOWN) + .add_strong(rsc), + ) } all.push(section_label("friends").add_strong(rsc)); - all.push(user_list(&resp.current, &session, rsc).add_strong(rsc)); + all.push( + user_list(&resp.current, &session) + .span(Dir::DOWN) + .add_strong(rsc), + ); if !resp.outgoing.is_empty() { all.push(section_label("outgoing").add_strong(rsc)); - all.push(user_list(&resp.outgoing, &session, rsc).add_strong(rsc)) + all.push( + user_list(&resp.outgoing, &session) + .span(Dir::DOWN) + .add_strong(rsc), + ) } all.set_ptr(ptr, rsc); }); @@ -113,3 +139,33 @@ fn friends_list(rsc: &mut Rsc, session: &Session) -> WeakWidget { }); ptr } + +pub fn accept_req(id: UserId, con: &NetHandle, rsc: &mut Rsc) -> WeakWidget { + let button = Button::submit("󰸞", rsc); + let con = con.clone(); + rsc.events.register(button, Submit, move |_, rsc| { + let con = con.clone(); + rsc.tasks.spawn(async move |_| { + con.send(AnswerFriendRequest { + id, + action: FriendRequestAction::Accept, + }); + }); + }); + button.width(60).add(rsc) +} + +pub fn deny_req(id: UserId, con: &NetHandle, rsc: &mut Rsc) -> WeakWidget { + let button = Button::new("X", color::RED, rsc); + let con = con.clone(); + rsc.events.register(button, Submit, move |_, rsc| { + let con = con.clone(); + rsc.tasks.spawn(async move |_| { + con.send(AnswerFriendRequest { + id, + action: FriendRequestAction::Deny, + }); + }); + }); + button.width(60).add(rsc) +} diff --git a/src/bin/client/ui/misc.rs b/src/bin/client/ui/misc.rs index a5f110e..1264d74 100644 --- a/src/bin/client/ui/misc.rs +++ b/src/bin/client/ui/misc.rs @@ -152,22 +152,6 @@ pub fn section_label(text: impl Into) -> TextBuilder { wtext(text).size(30) } -pub fn user_list<'a>( - ids: impl IntoIterator, - session: &Session, - rsc: &mut Rsc, -) -> Span { - let mut span = Span::empty(Dir::DOWN); - for id in ids { - let thing = (session.username(*id, rsc),) - .span(Dir::RIGHT) - .gap(30) - .pad(15); - span.push(thing.add_strong(rsc)); - } - span -} - impl Client { pub fn error(&self, text: &str, rsc: &mut Rsc) { rsc[self.notif].inner = Some(werror(text, rsc)); diff --git a/src/bin/client/ui/user.rs b/src/bin/client/ui/user.rs index 5312419..de790df 100644 --- a/src/bin/client/ui/user.rs +++ b/src/bin/client/ui/user.rs @@ -47,3 +47,18 @@ impl Session { wid } } + +pub fn user_list<'a>( + ids: impl IntoIterator, + session: &Session, +) -> impl IntoIterator WeakWidget> { + ids.rsc_map(|id, rsc: &mut Rsc| user_rect(*id, session, rsc)) +} + +pub fn user_rect(id: UserId, session: &Session, rsc: &mut Rsc) -> WeakWidget { + (session.username(id, rsc),) + .span(Dir::RIGHT) + .gap(30) + .pad(15) + .add(rsc) as WeakWidget +} diff --git a/src/bin/server/handle.rs b/src/bin/server/handle.rs index e85b987..b737c35 100644 --- a/src/bin/server/handle.rs +++ b/src/bin/server/handle.rs @@ -164,6 +164,10 @@ impl ClientHandler { if user.friends.outgoing.contains(&other_id) { reply!(AddFriendResp::AlreadySent); } + // TODO: just accept in this case? + if user.friends.incoming.contains(&other_id) { + reply!(AddFriendResp::AlreadySent); + } if other_id == user_id { reply!(AddFriendResp::CannotAddSelf); }