From 3b287d4548a9125f3fdcb6e15ea8d85b1167c21a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 2 Dec 2021 21:45:29 +0100 Subject: Add simple posting GUI implementation Signed-off-by: Matthias Beyer --- src/gui/mod.rs | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 128 insertions(+), 12 deletions(-) diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 97751b0..584392e 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -1,5 +1,15 @@ +use std::sync::Arc; + use anyhow::Result; use iced::Application; +use iced::Column; +use iced::Container; +use iced::Element; +use iced::Length; +use iced::Scrollable; +use iced::TextInput; +use iced::scrollable; +use iced::text_input; use ipfs_api_backend_hyper::TryFromUri; use crate::client::Client; @@ -7,27 +17,51 @@ use crate::config::Config; use crate::ipfs_client::IpfsClient; #[derive(Debug)] -struct DistroxGui; +enum Distrox { + Loading, + Loaded(State), + FailedToStart, +} #[derive(Debug)] +struct State { + client: Arc, + + scroll: scrollable::State, + input: text_input::State, + input_value: String, +} + +#[derive(Debug, Clone)] enum Message { - Loaded(Result), + Loaded(Arc), + FailedToLoad, + + InputChanged(String), + CreatePost, + + PostCreated(crate::cid::Cid), + PostCreationFailed(String), } -impl Application for DistroxGui { +impl Application for Distrox { type Executor = iced::executor::Default; // tokio type Message = Message; type Flags = (); fn new(_flags: ()) -> (Self, iced::Command) { ( - DistroxGui, + Distrox::Loading, iced::Command::perform(async { - let ipfs = IpfsClient::from_str("http://localhost:5001")?; - let config = Config::default(); - let client = Client::new(ipfs, config); - Ok(client) - }, Message::Loaded) + match IpfsClient::from_str("http://localhost:5001") { + Err(_) => Message::FailedToLoad, + Ok(ipfs) => { + let config = Config::default(); + let client = Client::new(ipfs, config); + Message::Loaded(Arc::new(client)) + } + } + }, |m: Message| -> Message { m }) ) } @@ -35,12 +69,94 @@ impl Application for DistroxGui { String::from("distrox") } - fn update(&mut self, _message: Self::Message, _clipboard: &mut iced::Clipboard) -> iced::Command { + fn update(&mut self, message: Self::Message, _clipboard: &mut iced::Clipboard) -> iced::Command { + match self { + Distrox::Loading => { + match message { + Message::Loaded(client) => { + let state = State { + client: client, + scroll: scrollable::State::default(), + input: text_input::State::default(), + input_value: String::default(), + }; + *self = Distrox::Loaded(state); + } + + Message::FailedToLoad => { + log::error!("Failed to load"); + *self = Distrox::FailedToStart; + } + + _ => {} + + } + } + + Distrox::Loaded(state) => { + match message { + Message::InputChanged(input) => { + state.input_value = input; + } + + Message::CreatePost => { + if !state.input_value.is_empty() { + let client = state.client.clone(); + iced::Command::perform(async move { + client.post_text_blob(state.input_value.clone()).await + }, + |res| match res { + Ok(cid) => Message::PostCreated(cid), + Err(e) => Message::PostCreationFailed(e.to_string()) + }); + } + } + + _ => {} + } + } + + Distrox::FailedToStart => { + unimplemented!() + } + } iced::Command::none() } fn view(&mut self) -> iced::Element { - iced::Text::new("Hello, world!").into() + match self { + Distrox::Loading => { + unimplemented!() + } + + Distrox::Loaded(state) => { + let input = TextInput::new( + &mut state.input, + "What do you want to tell the world?", + &mut state.input_value, + Message::InputChanged, + ) + .padding(15) + .size(30) + .on_submit(Message::CreatePost); + + let content = Column::new() + .max_width(800) + .spacing(20) + .push(input); + + Scrollable::new(&mut state.scroll) + .padding(40) + .push( + Container::new(content).width(Length::Fill).center_x(), + ) + .into() + } + + Distrox::FailedToStart => { + unimplemented!() + } + } } } @@ -58,5 +174,5 @@ pub fn run() -> Result<()> { ..iced::Settings::default() }; - DistroxGui::run(settings).map_err(anyhow::Error::from) + Distrox::run(settings).map_err(anyhow::Error::from) } -- cgit v1.2.3