Add the chat area

This commit is contained in:
Savanni D'Gerinel 2023-03-22 18:17:35 -04:00
parent 68096ec743
commit b6b85a7add
5 changed files with 121 additions and 49 deletions

View File

@ -1,4 +1,4 @@
pub mod playing_field;
mod types;
pub use types::PlayerCardElement;
pub use types::{ChatElement, PlayerCardElement};

View File

@ -12,3 +12,7 @@ gtk = { version = "0.6", package = "gtk4" }
kifu-core = { path = "../kifu-core" }
tokio = { version = "1.26", features = [ "full" ] }
screenplay = { path = "../../screenplay" }
[[bin]]
name = "kifu-gtk"
path = "src/main.rs"

View File

@ -1,52 +1,18 @@
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use kifu_core::{ui::PlayerCardElement, Color};
use kifu_core::{ui::ChatElement, ui::PlayerCardElement, Color};
use kifu_gtk::ui::{Chat, PlayerCard};
use screenplay::{Screen, Screenplay};
use std::{cell::RefCell, rc::Rc};
#[derive(Default)]
pub struct PlayerCardPrivate {
player_name: gtk::Label,
clock: gtk::Label,
}
#[glib::object_subclass]
impl ObjectSubclass for PlayerCardPrivate {
const NAME: &'static str = "PlayerCard";
type Type = PlayerCard;
type ParentType = gtk::Box;
}
impl ObjectImpl for PlayerCardPrivate {}
impl WidgetImpl for PlayerCardPrivate {}
impl BoxImpl for PlayerCardPrivate {}
glib::wrapper! {
pub struct PlayerCard(ObjectSubclass<PlayerCardPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl PlayerCard {
pub fn new(element: PlayerCardElement) -> PlayerCard {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.imp()
.player_name
.set_text(&format!("{} ({})", element.name, element.rank));
s.imp().clock.set_text(&element.clock);
s.append(&s.imp().player_name);
s.append(&s.imp().clock);
s
}
}
fn main() {
let app = gtk::Application::builder()
.application_id("com.luminescent-dreams.kifu-gtk.screenplay")
.build();
app.connect_activate(|app| {
let screens = vec![Screen {
let screens = vec![
Screen {
title: "PlayerCard".to_owned(),
widget: PlayerCard::new(PlayerCardElement {
color: Color::Black,
@ -56,7 +22,20 @@ fn main() {
})
.upcast::<gtk::Widget>(),
adjustments: vec![],
}];
},
Screen {
title: "ChatArea".to_owned(),
widget: Chat::new(ChatElement {
messages: vec![
"message 1".to_owned(),
"message 2".to_owned(),
"message 3".to_owned(),
],
})
.upcast::<gtk::Widget>(),
adjustments: vec![],
},
];
let screenplay = Screenplay::new(&app, screens).unwrap();
});

1
kifu/kifu-gtk/src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod ui;

View File

@ -0,0 +1,88 @@
use crate::ui;
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use kifu_core::{ui::ChatElement, ui::PlayerCardElement, Color};
use std::{cell::RefCell, rc::Rc};
#[derive(Default)]
pub struct PlayerCardPrivate {
player_name: gtk::Label,
clock: gtk::Label,
}
#[glib::object_subclass]
impl ObjectSubclass for PlayerCardPrivate {
const NAME: &'static str = "PlayerCard";
type Type = PlayerCard;
type ParentType = gtk::Box;
}
impl ObjectImpl for PlayerCardPrivate {}
impl WidgetImpl for PlayerCardPrivate {}
impl BoxImpl for PlayerCardPrivate {}
glib::wrapper! {
pub struct PlayerCard(ObjectSubclass<PlayerCardPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl PlayerCard {
pub fn new(element: PlayerCardElement) -> PlayerCard {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.imp()
.player_name
.set_text(&format!("{} ({})", element.name, element.rank));
s.imp().clock.set_text(&element.clock);
s.append(&s.imp().player_name);
s.append(&s.imp().clock);
s
}
}
#[derive(Default)]
pub struct ChatPrivate {
chat_history: gtk::Box,
entry: gtk::Entry,
}
#[glib::object_subclass]
impl ObjectSubclass for ChatPrivate {
const NAME: &'static str = "Chat";
type Type = Chat;
type ParentType = gtk::Box;
}
impl ObjectImpl for ChatPrivate {
fn constructed(&self) {
self.chat_history
.set_orientation(gtk::Orientation::Vertical);
}
}
impl WidgetImpl for ChatPrivate {}
impl BoxImpl for ChatPrivate {}
glib::wrapper! {
pub struct Chat(ObjectSubclass<ChatPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl Chat {
pub fn new(element: ChatElement) -> Chat {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.append(&s.imp().chat_history);
s.append(&s.imp().entry);
element.messages.into_iter().for_each(|msg| {
s.imp().chat_history.append(
&gtk::Label::builder()
.label(&msg)
.halign(gtk::Align::Start)
.build(),
)
});
s
}
}