From edef9f3ba9744ab2a2b49751c96811827bd93665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 8 Apr 2026 17:36:04 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(client):=20add=20client=20crat?= =?UTF-8?q?e=20with=20GUI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/Cargo.toml | 11 +++ crates/client/src/gui.rs | 69 ++++++++++++++++ crates/client/src/gui/chat.rs | 141 +++++++++++++++++++++++++++++++++ crates/client/src/gui/login.rs | 88 ++++++++++++++++++++ crates/client/src/lib.rs | 1 + crates/client/src/main.rs | 35 ++++++++ 6 files changed, 345 insertions(+) create mode 100644 crates/client/Cargo.toml create mode 100644 crates/client/src/gui.rs create mode 100644 crates/client/src/gui/chat.rs create mode 100644 crates/client/src/gui/login.rs create mode 100644 crates/client/src/lib.rs create mode 100644 crates/client/src/main.rs diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml new file mode 100644 index 0000000..5f87327 --- /dev/null +++ b/crates/client/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "client" +version = "0.1.0" +edition = "2024" + +[dependencies] +common = { version = "0.1.0", path = "../common", features = ["client"] } +gpui = "0.2.2" +gpui-component = "0.5.1" +gpui-component-assets = "0.5.1" +tokio = {version = "1.50.0", features = ["full"]} diff --git a/crates/client/src/gui.rs b/crates/client/src/gui.rs new file mode 100644 index 0000000..1423818 --- /dev/null +++ b/crates/client/src/gui.rs @@ -0,0 +1,69 @@ +use gpui::{IntoElement, ParentElement, Render, Styled, div, rgb}; +use gpui_component::Root; + +use crate::gui::{chat::Chat, login::Login}; +mod chat; +mod login; + +#[derive(Debug, PartialEq, Eq)] +enum Route { + Login, + Chat, +} + +pub struct Nefes { + current_route: Route, + login: Login, + chat: Chat, +} + +impl Nefes { + pub fn new(window: &mut gpui::Window, cx: &mut gpui::Context) -> Self { + Self { + current_route: Route::Login, + login: Login::new(window, cx), + chat: Chat::new(window, cx), + } + } + + fn change_page( + &mut self, + new_route: Route, + window: &mut gpui::Window, + cx: &mut gpui::Context, + ) { + self.current_route = new_route; + + match self.current_route { + Route::Login => self.login.focus(window, cx), + Route::Chat => self.chat.focus(window, cx), + } + } +} + +impl Render for Nefes { + fn render( + &mut self, + window: &mut gpui::Window, + cx: &mut gpui::Context, + ) -> impl gpui::IntoElement { + div() + .size_full() + .relative() + .child( + div() + .flex() + .flex_col() + .justify_center() + .items_center() + .size_full() + .bg(rgb(0x1e1e1e)) + .text_color(rgb(0xffffff)) + .child(match self.current_route { + Route::Login => self.login_page(cx).into_any_element(), + Route::Chat => self.chat_page().into_any_element(), + }), + ) + .child(Root::read(window, cx).notification.clone()) + } +} diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs new file mode 100644 index 0000000..744667d --- /dev/null +++ b/crates/client/src/gui/chat.rs @@ -0,0 +1,141 @@ +use common::{Message, User}; +use gpui::{ + AppContext, Context, Entity, FontWeight, InteractiveElement, IntoElement, ParentElement, + StatefulInteractiveElement, Styled, Window, div, rgb, +}; +use gpui_component::{ + WindowExt, + input::{Input, InputEvent, InputState}, + notification::Notification, +}; + +use crate::gui::Nefes; + +pub struct Chat { + users: Vec, + chat_messages: Vec, + message_input: Entity, +} + +impl Chat { + pub fn new(window: &mut Window, cx: &mut Context) -> Self { + let message_input = cx.new(|cx| { + let mut input_state = InputState::new(window, cx); + input_state.set_placeholder("", window, cx); + + input_state + }); + + cx.subscribe_in(&message_input, window, |this, input, event, window, cx| { + if matches!(event, InputEvent::PressEnter { .. }) { + match Message::new(this.login.get_user().unwrap(), input.read(cx).text()) { + Ok(message) => { + this.chat.chat_messages.push(message); + input.update(cx, |input_state, cx| { + input_state.set_value("", window, cx); + }); + } + Err(error_value) => { + window.push_notification(Notification::error(error_value.to_string()), cx) + } + } + } + }) + .detach(); + + let person_1 = User::new("Ahmet").unwrap(); + let person_2 = User::new("Kaan").unwrap(); + let message_1 = Message::new(person_1, "Hello, how are you?").unwrap(); + let message_2 = Message::new(person_2, "I'm doing well. How are you?").unwrap(); + let person_1 = User::new("Ahmet").unwrap(); + let message_3 = Message::new(person_1, "I'm good too. Thanks for asking!").unwrap(); + let person_2 = User::new("Kaan").unwrap(); + let message_4 = Message::new(person_2, "A".repeat(1024)).unwrap(); + let person_1 = User::new("Ahmet").unwrap(); + let person_2 = User::new("Kaan").unwrap(); + Self { + users: vec![person_1, person_2], + chat_messages: vec![message_1, message_2, message_3, message_4], + message_input, + } + } + + pub fn focus(&self, window: &mut Window, cx: &mut Context<'_, Nefes>) { + self.message_input.update(cx, |input, cx| { + input.focus(window, cx); + }); + } +} + +impl Nefes { + pub fn chat_page(&self) -> impl IntoElement { + div() + .flex() + .flex_row() + .size_full() + .child( + div() + .flex() + .flex_col() + .min_w_0() + .child( + div() + .id("chat_messages") + .flex() + .flex_col() + .flex_1() + .min_w_0() + .overflow_y_scroll() + .p_2() + .gap_1() + .children(self.chat.chat_messages.iter().map(render_message)), + ) + .child( + div() + .p_2() + .border_t_1() + .border_color(rgb(0x2f3136)) + .child(Input::new(&self.chat.message_input)), + ), + ) + .child( + div() + .id("user_list") + .h_full() + .flex() + .flex_col() + .w_1_5() + .border_l_1() + .border_color(rgb(0x2f3136)) + .bg(rgb(0x2b2d31)) + .p_2() + .gap_1() + .child( + div() + .text_color(rgb(0x949ba4)) + .font_weight(FontWeight::SEMIBOLD) + .child(format!("Online - {}", self.chat.users.len())), + ) + .children( + self.chat + .users + .iter() + .map(|user| div().p_1().child(user.get_username().clone())), + ), + ) + } +} + +fn render_message(message: &Message) -> impl IntoElement { + div() + .flex() + .flex_col() + .p_1() + .child( + div() + .text_color(rgb(0x7289da)) + .font_weight(FontWeight::BOLD) + .child(message.get_sender().get_username().clone()), + ) + .child(div().pl_2().child(message.get_message().clone())) +} diff --git a/crates/client/src/gui/login.rs b/crates/client/src/gui/login.rs new file mode 100644 index 0000000..e53d6fe --- /dev/null +++ b/crates/client/src/gui/login.rs @@ -0,0 +1,88 @@ +use common::User; +use gpui::{AppContext, Context, Entity, IntoElement, ParentElement, Styled, Window, div}; +use gpui_component::{ + WindowExt, + button::Button, + input::{Input, InputEvent, InputState}, + notification::Notification, +}; + +use crate::gui::{Nefes, Route}; + +pub struct Login { + username_input: Entity, + user: Option, +} + +impl Login { + pub fn new(window: &mut Window, cx: &mut Context) -> Self { + let username_input = cx.new(|cx| { + let mut input_state = InputState::new(window, cx); + input_state.set_placeholder("Username...", window, cx); + input_state.focus(window, cx); + + input_state + }); + + cx.subscribe_in(&username_input, window, |this, input, event, window, cx| { + if matches!(event, InputEvent::PressEnter { .. }) { + login_action(this, input, window, cx); + } + }) + .detach(); + + Self { + username_input, + user: None, + } + } + + pub fn get_user(&self) -> Option { + self.user.clone() + } + + pub fn focus(&self, window: &mut Window, cx: &mut Context<'_, Nefes>) { + self.username_input.update(cx, |input, cx| { + input.focus(window, cx); + }); + } +} + +fn login_action( + this: &mut Nefes, + input: &Entity, + window: &mut Window, + cx: &mut Context<'_, Nefes>, +) { + let username = input.read(cx).text().to_string(); + match User::new(username) { + Ok(user) => { + this.login.user = Some(user); + this.change_page(Route::Chat, window, cx); + } + Err(error_value) => { + window.push_notification(Notification::error(error_value.to_string()), cx) + } + } +} + +impl Nefes { + pub fn login_page(&mut self, cx: &mut Context) -> impl IntoElement { + let entity = cx.entity(); + let username_input = self.login.username_input.clone(); + let login_button = Button::new("login_button") + .on_click(move |_, window, cx| { + entity.update(cx, |this, cx| { + login_action(this, &username_input, window, cx); + }) + }) + .label("Login"); + + div() + .w_1_5() + .child("Username") + .child(Input::new(&self.login.username_input)) + .child(login_button) + .into_element() + } +} diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs new file mode 100644 index 0000000..14c39ab --- /dev/null +++ b/crates/client/src/lib.rs @@ -0,0 +1 @@ +pub mod gui; diff --git a/crates/client/src/main.rs b/crates/client/src/main.rs new file mode 100644 index 0000000..e6a6e90 --- /dev/null +++ b/crates/client/src/main.rs @@ -0,0 +1,35 @@ +use client::gui::Nefes; +use gpui::{ + App, AppContext, Application, Bounds, TitlebarOptions, WindowBounds, WindowOptions, px, size, +}; +use gpui_component::Root; + +fn main() { + println!("Hello, world!"); + + Application::new().run(|cx: &mut App| { + gpui_component::init(cx); + + let window_bounds = Bounds::centered(None, size(px(800.0), px(600.0)), cx); + + let titlebar = TitlebarOptions { + title: Some("Nefes".into()), + ..Default::default() + }; + + cx.open_window( + WindowOptions { + window_bounds: Some(WindowBounds::Windowed(window_bounds)), + titlebar: Some(titlebar), + ..Default::default() + }, + |window, cx| { + let view = cx.new(|cx| Nefes::new(window, cx)); + cx.new(|cx| Root::new(view, window, cx)) + }, + ) + .unwrap(); + + cx.activate(true); + }); +}