forge
Explore
Log in

Don't you dare go Hollow

Add client crate with GUI

Browse files at this commit

edef9f3ba9744ab2a2b49751c96811827bd93665Ahmet Kaan GümüşParent: c197361
  • feat
    • add client crate with GUI
Show full message
✨ feat(client): add client crate with GUI
crates/client/Cargo.tomladded+11 -0
@@ -0,0 +1,11 @@
1+[package]
2+name = "client"
3+version = "0.1.0"
4+edition = "2024"
5+
6+[dependencies]
7+common = { version = "0.1.0", path = "../common", features = ["client"] }
8+gpui = "0.2.2"
9+gpui-component = "0.5.1"
10+gpui-component-assets = "0.5.1"
11+tokio = {version = "1.50.0", features = ["full"]}
crates/client/src/gui.rsadded+69 -0
@@ -0,0 +1,69 @@
1+use gpui::{IntoElement, ParentElement, Render, Styled, div, rgb};
2+use gpui_component::Root;
3+
4+use crate::gui::{chat::Chat, login::Login};
5+mod chat;
6+mod login;
7+
8+#[derive(Debug, PartialEq, Eq)]
9+enum Route {
10+ Login,
11+ Chat,
12+}
13+
14+pub struct Nefes {
15+ current_route: Route,
16+ login: Login,
17+ chat: Chat,
18+}
19+
20+impl Nefes {
21+ pub fn new(window: &mut gpui::Window, cx: &mut gpui::Context<Self>) -> Self {
22+ Self {
23+ current_route: Route::Login,
24+ login: Login::new(window, cx),
25+ chat: Chat::new(window, cx),
26+ }
27+ }
28+
29+ fn change_page(
30+ &mut self,
31+ new_route: Route,
32+ window: &mut gpui::Window,
33+ cx: &mut gpui::Context<Self>,
34+ ) {
35+ self.current_route = new_route;
36+
37+ match self.current_route {
38+ Route::Login => self.login.focus(window, cx),
39+ Route::Chat => self.chat.focus(window, cx),
40+ }
41+ }
42+}
43+
44+impl Render for Nefes {
45+ fn render(
46+ &mut self,
47+ window: &mut gpui::Window,
48+ cx: &mut gpui::Context<Self>,
49+ ) -> impl gpui::IntoElement {
50+ div()
51+ .size_full()
52+ .relative()
53+ .child(
54+ div()
55+ .flex()
56+ .flex_col()
57+ .justify_center()
58+ .items_center()
59+ .size_full()
60+ .bg(rgb(0x1e1e1e))
61+ .text_color(rgb(0xffffff))
62+ .child(match self.current_route {
63+ Route::Login => self.login_page(cx).into_any_element(),
64+ Route::Chat => self.chat_page().into_any_element(),
65+ }),
66+ )
67+ .child(Root::read(window, cx).notification.clone())
68+ }
69+}
crates/client/src/gui/chat.rsadded+141 -0
@@ -0,0 +1,141 @@
1+use common::{Message, User};
2+use gpui::{
3+ AppContext, Context, Entity, FontWeight, InteractiveElement, IntoElement, ParentElement,
4+ StatefulInteractiveElement, Styled, Window, div, rgb,
5+};
6+use gpui_component::{
7+ WindowExt,
8+ input::{Input, InputEvent, InputState},
9+ notification::Notification,
10+};
11+
12+use crate::gui::Nefes;
13+
14+pub struct Chat {
15+ users: Vec<User>,
16+ chat_messages: Vec<Message>,
17+ message_input: Entity<InputState>,
18+}
19+
20+impl Chat {
21+ pub fn new(window: &mut Window, cx: &mut Context<Nefes>) -> Self {
22+ let message_input = cx.new(|cx| {
23+ let mut input_state = InputState::new(window, cx);
24+ input_state.set_placeholder("", window, cx);
25+
26+ input_state
27+ });
28+
29+ cx.subscribe_in(&message_input, window, |this, input, event, window, cx| {
30+ if matches!(event, InputEvent::PressEnter { .. }) {
31+ match Message::new(this.login.get_user().unwrap(), input.read(cx).text()) {
32+ Ok(message) => {
33+ this.chat.chat_messages.push(message);
34+ input.update(cx, |input_state, cx| {
35+ input_state.set_value("", window, cx);
36+ });
37+ }
38+ Err(error_value) => {
39+ window.push_notification(Notification::error(error_value.to_string()), cx)
40+ }
41+ }
42+ }
43+ })
44+ .detach();
45+
46+ let person_1 = User::new("Ahmet").unwrap();
47+ let person_2 = User::new("Kaan").unwrap();
48+ let message_1 = Message::new(person_1, "Hello, how are you?").unwrap();
49+ let message_2 = Message::new(person_2, "I'm doing well. How are you?").unwrap();
50+ let person_1 = User::new("Ahmet").unwrap();
51+ let message_3 = Message::new(person_1, "I'm good too. Thanks for asking!").unwrap();
52+ let person_2 = User::new("Kaan").unwrap();
53+ let message_4 = Message::new(person_2, "A".repeat(1024)).unwrap();
54+ let person_1 = User::new("Ahmet").unwrap();
55+ let person_2 = User::new("Kaan").unwrap();
56+ Self {
57+ users: vec![person_1, person_2],
58+ chat_messages: vec![message_1, message_2, message_3, message_4],
59+ message_input,
60+ }
61+ }
62+
63+ pub fn focus(&self, window: &mut Window, cx: &mut Context<'_, Nefes>) {
64+ self.message_input.update(cx, |input, cx| {
65+ input.focus(window, cx);
66+ });
67+ }
68+}
69+
70+impl Nefes {
71+ pub fn chat_page(&self) -> impl IntoElement {
72+ div()
73+ .flex()
74+ .flex_row()
75+ .size_full()
76+ .child(
77+ div()
78+ .flex()
79+ .flex_col()
80+ .min_w_0()
81+ .child(
82+ div()
83+ .id("chat_messages")
84+ .flex()
85+ .flex_col()
86+ .flex_1()
87+ .min_w_0()
88+ .overflow_y_scroll()
89+ .p_2()
90+ .gap_1()
91+ .children(self.chat.chat_messages.iter().map(render_message)),
92+ )
93+ .child(
94+ div()
95+ .p_2()
96+ .border_t_1()
97+ .border_color(rgb(0x2f3136))
98+ .child(Input::new(&self.chat.message_input)),
99+ ),
100+ )
101+ .child(
102+ div()
103+ .id("user_list")
104+ .h_full()
105+ .flex()
106+ .flex_col()
107+ .w_1_5()
108+ .border_l_1()
109+ .border_color(rgb(0x2f3136))
110+ .bg(rgb(0x2b2d31))
111+ .p_2()
112+ .gap_1()
113+ .child(
114+ div()
115+ .text_color(rgb(0x949ba4))
116+ .font_weight(FontWeight::SEMIBOLD)
117+ .child(format!("Online - {}", self.chat.users.len())),
118+ )
119+ .children(
120+ self.chat
121+ .users
122+ .iter()
123+ .map(|user| div().p_1().child(user.get_username().clone())),
124+ ),
125+ )
126+ }
127+}
128+
129+fn render_message(message: &Message) -> impl IntoElement {
130+ div()
131+ .flex()
132+ .flex_col()
133+ .p_1()
134+ .child(
135+ div()
136+ .text_color(rgb(0x7289da))
137+ .font_weight(FontWeight::BOLD)
138+ .child(message.get_sender().get_username().clone()),
139+ )
140+ .child(div().pl_2().child(message.get_message().clone()))
141+}
crates/client/src/gui/login.rsadded+88 -0
@@ -0,0 +1,88 @@
1+use common::User;
2+use gpui::{AppContext, Context, Entity, IntoElement, ParentElement, Styled, Window, div};
3+use gpui_component::{
4+ WindowExt,
5+ button::Button,
6+ input::{Input, InputEvent, InputState},
7+ notification::Notification,
8+};
9+
10+use crate::gui::{Nefes, Route};
11+
12+pub struct Login {
13+ username_input: Entity<InputState>,
14+ user: Option<User>,
15+}
16+
17+impl Login {
18+ pub fn new(window: &mut Window, cx: &mut Context<Nefes>) -> Self {
19+ let username_input = cx.new(|cx| {
20+ let mut input_state = InputState::new(window, cx);
21+ input_state.set_placeholder("Username...", window, cx);
22+ input_state.focus(window, cx);
23+
24+ input_state
25+ });
26+
27+ cx.subscribe_in(&username_input, window, |this, input, event, window, cx| {
28+ if matches!(event, InputEvent::PressEnter { .. }) {
29+ login_action(this, input, window, cx);
30+ }
31+ })
32+ .detach();
33+
34+ Self {
35+ username_input,
36+ user: None,
37+ }
38+ }
39+
40+ pub fn get_user(&self) -> Option<User> {
41+ self.user.clone()
42+ }
43+
44+ pub fn focus(&self, window: &mut Window, cx: &mut Context<'_, Nefes>) {
45+ self.username_input.update(cx, |input, cx| {
46+ input.focus(window, cx);
47+ });
48+ }
49+}
50+
51+fn login_action(
52+ this: &mut Nefes,
53+ input: &Entity<InputState>,
54+ window: &mut Window,
55+ cx: &mut Context<'_, Nefes>,
56+) {
57+ let username = input.read(cx).text().to_string();
58+ match User::new(username) {
59+ Ok(user) => {
60+ this.login.user = Some(user);
61+ this.change_page(Route::Chat, window, cx);
62+ }
63+ Err(error_value) => {
64+ window.push_notification(Notification::error(error_value.to_string()), cx)
65+ }
66+ }
67+}
68+
69+impl Nefes {
70+ pub fn login_page(&mut self, cx: &mut Context<Self>) -> impl IntoElement {
71+ let entity = cx.entity();
72+ let username_input = self.login.username_input.clone();
73+ let login_button = Button::new("login_button")
74+ .on_click(move |_, window, cx| {
75+ entity.update(cx, |this, cx| {
76+ login_action(this, &username_input, window, cx);
77+ })
78+ })
79+ .label("Login");
80+
81+ div()
82+ .w_1_5()
83+ .child("Username")
84+ .child(Input::new(&self.login.username_input))
85+ .child(login_button)
86+ .into_element()
87+ }
88+}
crates/client/src/lib.rsadded+1 -0
@@ -0,0 +1 @@
1+pub mod gui;
crates/client/src/main.rsadded+35 -0
@@ -0,0 +1,35 @@
1+use client::gui::Nefes;
2+use gpui::{
3+ App, AppContext, Application, Bounds, TitlebarOptions, WindowBounds, WindowOptions, px, size,
4+};
5+use gpui_component::Root;
6+
7+fn main() {
8+ println!("Hello, world!");
9+
10+ Application::new().run(|cx: &mut App| {
11+ gpui_component::init(cx);
12+
13+ let window_bounds = Bounds::centered(None, size(px(800.0), px(600.0)), cx);
14+
15+ let titlebar = TitlebarOptions {
16+ title: Some("Nefes".into()),
17+ ..Default::default()
18+ };
19+
20+ cx.open_window(
21+ WindowOptions {
22+ window_bounds: Some(WindowBounds::Windowed(window_bounds)),
23+ titlebar: Some(titlebar),
24+ ..Default::default()
25+ },
26+ |window, cx| {
27+ let view = cx.new(|cx| Nefes::new(window, cx));
28+ cx.new(|cx| Root::new(view, window, cx))
29+ },
30+ )
31+ .unwrap();
32+
33+ cx.activate(true);
34+ });
35+}