|
|
|
@ -1,21 +1,18 @@
|
|
|
|
|
use crate::chat::Chat; |
|
|
|
|
use crate::degeon_worker::Degeon; |
|
|
|
|
use crate::gui_events::GuiEvent; |
|
|
|
|
use crate::message::{DegMessage, DegMessageContent, Profile, ProtocolMsg}; |
|
|
|
|
use crate::message::{DegMessage, DegMessageContent, ProtocolMsg}; |
|
|
|
|
use crate::styles::style; |
|
|
|
|
use core::default::Default; |
|
|
|
|
use futures::Stream; |
|
|
|
|
use iced::window::Mode; |
|
|
|
|
use iced::{ |
|
|
|
|
button, Align, Application, Button, Column, Element, HorizontalAlignment, Length, Row, Text, |
|
|
|
|
TextInput, VerticalAlignment, |
|
|
|
|
button, Align, Application, Button, Color, Column, Element, HorizontalAlignment, Length, Row, |
|
|
|
|
Settings, Text, TextInput, VerticalAlignment, |
|
|
|
|
}; |
|
|
|
|
use ironforce::res::{IFError, IFResult}; |
|
|
|
|
use ironforce::{IronForce, Keys, Message, MessageType, PublicKey}; |
|
|
|
|
use std::hash::Hash; |
|
|
|
|
use std::pin::Pin; |
|
|
|
|
use std::sync::{Arc, Mutex}; |
|
|
|
|
use std::task::{Context, Poll}; |
|
|
|
|
use ironforce::PublicKey; |
|
|
|
|
|
|
|
|
|
/// Render a message into an iced `Element`
|
|
|
|
|
pub fn view_message(msg: &DegMessage, pkey: PublicKey) -> Option<Element<GuiEvent>> { |
|
|
|
|
let is_from_me = pkey != msg.sender; |
|
|
|
|
match &msg.content { |
|
|
|
@ -44,18 +41,44 @@ pub fn view_message(msg: &DegMessage, pkey: PublicKey) -> Option<Element<GuiEven
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// The screens (pages) of the app
|
|
|
|
|
#[derive(Copy, Clone, Debug)] |
|
|
|
|
pub enum AppScreen { |
|
|
|
|
/// The one with the chats
|
|
|
|
|
Main, |
|
|
|
|
/// Settings and profile
|
|
|
|
|
ProfileEditor, |
|
|
|
|
/// The screen that appears if no peers are available and asks for the IP address of at least one peer
|
|
|
|
|
PeerInput, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Default for AppScreen { |
|
|
|
|
fn default() -> Self { |
|
|
|
|
AppScreen::Main |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// The main application struct (for iced)
|
|
|
|
|
#[derive(Default)] |
|
|
|
|
pub struct DegeonApp { |
|
|
|
|
/// The container for logic, data and IF
|
|
|
|
|
pub data: Degeon, |
|
|
|
|
/// Current screen
|
|
|
|
|
screen: AppScreen, |
|
|
|
|
/// Selected chat (on the main screen)
|
|
|
|
|
selected_chat: usize, |
|
|
|
|
/// Send button
|
|
|
|
|
send_button_state: iced::button::State, |
|
|
|
|
/// Message input field
|
|
|
|
|
text_input_state: iced::text_input::State, |
|
|
|
|
/// Buttons for chat previews
|
|
|
|
|
preview_button_states: Vec<button::State>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl DegeonApp { |
|
|
|
|
/// Render the list of chats with all previews
|
|
|
|
|
fn chat_list<'a>( |
|
|
|
|
chats: &'a Vec<Chat>, |
|
|
|
|
chats: &'a [Chat], |
|
|
|
|
preview_button_states: &'a mut Vec<button::State>, |
|
|
|
|
selected: usize, |
|
|
|
|
) -> Element<'a, GuiEvent> { |
|
|
|
@ -77,8 +100,9 @@ impl DegeonApp {
|
|
|
|
|
.into() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Render active chat section
|
|
|
|
|
pub fn active_chat<'a>( |
|
|
|
|
chats: &'a Vec<Chat>, |
|
|
|
|
chats: &'a [Chat], |
|
|
|
|
selected_chat: usize, |
|
|
|
|
send_button_state: &'a mut button::State, |
|
|
|
|
text_input_state: &'a mut iced::text_input::State, |
|
|
|
@ -95,20 +119,52 @@ impl DegeonApp {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl DegeonApp { |
|
|
|
|
fn main_view(&mut self) -> Element<GuiEvent> { |
|
|
|
|
let Self { |
|
|
|
|
data: Degeon { chats, .. }, |
|
|
|
|
selected_chat, |
|
|
|
|
send_button_state, |
|
|
|
|
text_input_state, |
|
|
|
|
preview_button_states, |
|
|
|
|
.. |
|
|
|
|
} = self; |
|
|
|
|
Row::new() |
|
|
|
|
.padding(20) |
|
|
|
|
.push(Self::chat_list( |
|
|
|
|
chats, |
|
|
|
|
preview_button_states, |
|
|
|
|
*selected_chat, |
|
|
|
|
)) |
|
|
|
|
.push(Self::active_chat( |
|
|
|
|
chats, |
|
|
|
|
*selected_chat, |
|
|
|
|
send_button_state, |
|
|
|
|
text_input_state, |
|
|
|
|
)) |
|
|
|
|
.height(Length::Fill) |
|
|
|
|
.into() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Application for DegeonApp { |
|
|
|
|
type Executor = iced::executor::Default; |
|
|
|
|
type Message = GuiEvent; |
|
|
|
|
type Flags = (); |
|
|
|
|
|
|
|
|
|
fn new(_: ()) -> (Self, iced::Command<GuiEvent>) { |
|
|
|
|
let mut st = Self::default(); |
|
|
|
|
st.data.chats = vec![ |
|
|
|
|
Chat::example(1, &st.data.keys), |
|
|
|
|
Chat::example(2, &st.data.keys), |
|
|
|
|
]; |
|
|
|
|
st.preview_button_states = vec![Default::default(), Default::default()]; |
|
|
|
|
st.data.my_name = "John".to_string(); |
|
|
|
|
st.data.send_multicast(ProtocolMsg::Ping).unwrap(); |
|
|
|
|
let mut data = Degeon::restore_from_file("".to_string()).unwrap(); |
|
|
|
|
data.chats = vec![Chat::example(1, &data.keys), Chat::example(2, &data.keys)]; |
|
|
|
|
data.profile.name = "John".to_string(); |
|
|
|
|
data.send_multicast(ProtocolMsg::Ping).unwrap(); |
|
|
|
|
let st = DegeonApp { |
|
|
|
|
data, |
|
|
|
|
screen: Default::default(), |
|
|
|
|
selected_chat: 0, |
|
|
|
|
send_button_state: Default::default(), |
|
|
|
|
text_input_state: Default::default(), |
|
|
|
|
preview_button_states: vec![Default::default(), Default::default()], |
|
|
|
|
}; |
|
|
|
|
let data_clone = st.data.clone(); |
|
|
|
|
std::thread::spawn(move || { |
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(10)); |
|
|
|
@ -142,19 +198,20 @@ impl Application for DegeonApp {
|
|
|
|
|
.push(new_msg.clone()); |
|
|
|
|
let data_cloned = self.data.clone(); |
|
|
|
|
let target = self.data.chats[self.selected_chat].pkey.clone(); |
|
|
|
|
std::thread::spawn(move || data_cloned.send_message(ProtocolMsg::NewMessage(new_msg), &target).unwrap()); |
|
|
|
|
} |
|
|
|
|
GuiEvent::NewChat(pkey) => { |
|
|
|
|
if self.data.chat_with(&pkey).is_none() { |
|
|
|
|
self.data.chats.push(Chat::new(pkey)) |
|
|
|
|
} |
|
|
|
|
std::thread::spawn(move || { |
|
|
|
|
data_cloned |
|
|
|
|
.send_message(ProtocolMsg::NewMessage(new_msg), &target) |
|
|
|
|
.unwrap() |
|
|
|
|
}); |
|
|
|
|
self.data.save_to_file("".to_string()).unwrap(); |
|
|
|
|
} |
|
|
|
|
GuiEvent::NewMessageInChat(pkey, msg) => { |
|
|
|
|
if self.data.chat_with(&pkey).is_none() { |
|
|
|
|
self.data.chats.push(Chat::new(pkey.clone())) |
|
|
|
|
} |
|
|
|
|
let ind = self.data.chat_with(&pkey).unwrap(); |
|
|
|
|
self.data.chats[ind].messages.push(msg) |
|
|
|
|
self.data.chats[ind].messages.push(msg); |
|
|
|
|
self.data.save_to_file("".to_string()).unwrap(); |
|
|
|
|
} |
|
|
|
|
GuiEvent::SetProfile(pkey, name) => { |
|
|
|
|
if self.data.chat_with(&pkey).is_none() { |
|
|
|
@ -162,6 +219,7 @@ impl Application for DegeonApp {
|
|
|
|
|
} |
|
|
|
|
let ind = self.data.chat_with(&pkey).unwrap(); |
|
|
|
|
self.data.chats[ind].profile = name; |
|
|
|
|
self.data.save_to_file("".to_string()).unwrap(); |
|
|
|
|
} |
|
|
|
|
GuiEvent::None => {} |
|
|
|
|
GuiEvent::WeHaveToSendProfile(target) => { |
|
|
|
@ -169,8 +227,9 @@ impl Application for DegeonApp {
|
|
|
|
|
return self.data.get_send_command( |
|
|
|
|
ProtocolMsg::ProfileResponse(self.data.get_profile()), |
|
|
|
|
&target, |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
GuiEvent::ChangeScreen(sc) => self.screen = sc, |
|
|
|
|
} |
|
|
|
|
iced::Command::none() |
|
|
|
|
} |
|
|
|
@ -179,29 +238,11 @@ impl Application for DegeonApp {
|
|
|
|
|
iced::Subscription::from_recipe(self.data.clone()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn view(&mut self) -> Element<GuiEvent> { |
|
|
|
|
let Self { |
|
|
|
|
data: Degeon { chats, .. }, |
|
|
|
|
selected_chat, |
|
|
|
|
send_button_state, |
|
|
|
|
text_input_state, |
|
|
|
|
preview_button_states, |
|
|
|
|
.. |
|
|
|
|
} = self; |
|
|
|
|
Row::new() |
|
|
|
|
.padding(20) |
|
|
|
|
.push(Self::chat_list( |
|
|
|
|
chats, |
|
|
|
|
preview_button_states, |
|
|
|
|
*selected_chat, |
|
|
|
|
)) |
|
|
|
|
.push(Self::active_chat( |
|
|
|
|
chats, |
|
|
|
|
*selected_chat, |
|
|
|
|
send_button_state, |
|
|
|
|
text_input_state, |
|
|
|
|
)) |
|
|
|
|
.height(Length::Fill) |
|
|
|
|
.into() |
|
|
|
|
fn view(&mut self) -> Element<'_, Self::Message> { |
|
|
|
|
match self.screen { |
|
|
|
|
AppScreen::Main => self.main_view(), |
|
|
|
|
AppScreen::ProfileEditor => todo!(), |
|
|
|
|
AppScreen::PeerInput => todo!(), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|