From d497c550f3acce6b15bc60f792f5d4c25693fef8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Wed, 8 Dec 2021 18:38:48 +0100 Subject: Restructure module layout Signed-off-by: Matthias Beyer --- gui/src/app.rs | 187 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gui/src/gui/mod.rs | 187 ----------------------------------------------------- gui/src/main.rs | 4 +- 3 files changed, 189 insertions(+), 189 deletions(-) create mode 100644 gui/src/app.rs delete mode 100644 gui/src/gui/mod.rs diff --git a/gui/src/app.rs b/gui/src/app.rs new file mode 100644 index 0000000..1b4b98e --- /dev/null +++ b/gui/src/app.rs @@ -0,0 +1,187 @@ +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 distrox_lib::profile::Profile; +use distrox_lib::config::Config; +use distrox_lib::ipfs_client::IpfsClient; + +#[derive(Debug)] +enum Distrox { + Loading, + Loaded(State), + FailedToStart, +} + +#[derive(Debug)] +struct State { + profile: Arc, + + scroll: scrollable::State, + input: text_input::State, + input_value: String, +} + +#[derive(Debug, Clone)] +enum Message { + Loaded(Arc), + FailedToLoad, + + InputChanged(String), + CreatePost, + + PostCreated(cid::Cid), + PostCreationFailed(String), +} + +impl Application for Distrox { + type Executor = iced::executor::Default; // tokio + type Message = Message; + type Flags = String; + + fn new(name: String) -> (Self, iced::Command) { + ( + Distrox::Loading, + iced::Command::perform(async move { + match Profile::new_inmemory(Config::default(), &name).await { + Err(_) => Message::FailedToLoad, + Ok(instance) => { + Message::Loaded(Arc::new(instance)) + } + } + }, |m: Message| -> Message { m }) + ) + } + + fn title(&self) -> String { + String::from("distrox") + } + + fn update(&mut self, message: Self::Message) -> iced::Command { + match self { + Distrox::Loading => { + match message { + Message::Loaded(profile) => { + let state = State { + profile, + 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 profile = state.profile.clone(); + let input = state.input_value.clone(); + iced::Command::perform(async move { + profile.client().post_text_blob(input).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 { + match self { + Distrox::Loading => { + let text = iced::Text::new("Loading"); + + let content = Column::new() + .max_width(800) + .spacing(20) + .push(text); + + Container::new(content) + .width(Length::Fill) + .center_x() + .into() + } + + 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!() + } + } + } + +} + +pub fn run(name: String) -> Result<()> { + let settings = iced::Settings { + window: iced::window::Settings { + resizable: true, + decorations: true, + transparent: false, + always_on_top: false, + ..iced::window::Settings::default() + }, + flags: name, + exit_on_close_request: true, + ..iced::Settings::default() + }; + + Distrox::run(settings).map_err(anyhow::Error::from) +} diff --git a/gui/src/gui/mod.rs b/gui/src/gui/mod.rs deleted file mode 100644 index 1b4b98e..0000000 --- a/gui/src/gui/mod.rs +++ /dev/null @@ -1,187 +0,0 @@ -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 distrox_lib::profile::Profile; -use distrox_lib::config::Config; -use distrox_lib::ipfs_client::IpfsClient; - -#[derive(Debug)] -enum Distrox { - Loading, - Loaded(State), - FailedToStart, -} - -#[derive(Debug)] -struct State { - profile: Arc, - - scroll: scrollable::State, - input: text_input::State, - input_value: String, -} - -#[derive(Debug, Clone)] -enum Message { - Loaded(Arc), - FailedToLoad, - - InputChanged(String), - CreatePost, - - PostCreated(cid::Cid), - PostCreationFailed(String), -} - -impl Application for Distrox { - type Executor = iced::executor::Default; // tokio - type Message = Message; - type Flags = String; - - fn new(name: String) -> (Self, iced::Command) { - ( - Distrox::Loading, - iced::Command::perform(async move { - match Profile::new_inmemory(Config::default(), &name).await { - Err(_) => Message::FailedToLoad, - Ok(instance) => { - Message::Loaded(Arc::new(instance)) - } - } - }, |m: Message| -> Message { m }) - ) - } - - fn title(&self) -> String { - String::from("distrox") - } - - fn update(&mut self, message: Self::Message) -> iced::Command { - match self { - Distrox::Loading => { - match message { - Message::Loaded(profile) => { - let state = State { - profile, - 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 profile = state.profile.clone(); - let input = state.input_value.clone(); - iced::Command::perform(async move { - profile.client().post_text_blob(input).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 { - match self { - Distrox::Loading => { - let text = iced::Text::new("Loading"); - - let content = Column::new() - .max_width(800) - .spacing(20) - .push(text); - - Container::new(content) - .width(Length::Fill) - .center_x() - .into() - } - - 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!() - } - } - } - -} - -pub fn run(name: String) -> Result<()> { - let settings = iced::Settings { - window: iced::window::Settings { - resizable: true, - decorations: true, - transparent: false, - always_on_top: false, - ..iced::window::Settings::default() - }, - flags: name, - exit_on_close_request: true, - ..iced::Settings::default() - }; - - Distrox::run(settings).map_err(anyhow::Error::from) -} diff --git a/gui/src/main.rs b/gui/src/main.rs index bfa133e..67a09ec 100644 --- a/gui/src/main.rs +++ b/gui/src/main.rs @@ -1,7 +1,7 @@ use anyhow::Result; +mod app; mod cli; -mod gui; use distrox_lib::*; @@ -12,7 +12,7 @@ fn main() -> Result<()> { match matches.subcommand() { None => { let name = matches.value_of("name").map(String::from).unwrap(); // safe by clap - crate::gui::run(name) + crate::app::run(name) }, Some((other, _)) => { log::error!("No subcommand {} implemented", other); -- cgit v1.2.3