summaryrefslogtreecommitdiffstats
path: root/gui/src
diff options
context:
space:
mode:
Diffstat (limited to 'gui/src')
-rw-r--r--gui/src/cli.rs12
-rw-r--r--gui/src/gui/mod.rs186
-rw-r--r--gui/src/main.rs26
3 files changed, 224 insertions, 0 deletions
diff --git a/gui/src/cli.rs b/gui/src/cli.rs
new file mode 100644
index 0000000..6ebdfb9
--- /dev/null
+++ b/gui/src/cli.rs
@@ -0,0 +1,12 @@
+use clap::crate_authors;
+use clap::crate_version;
+use clap::App;
+use clap::Arg;
+
+pub fn app<'a>() -> App<'a> {
+ App::new("distrox-gui")
+ .author(crate_authors!())
+ .version(crate_version!())
+ .about("Distributed social network, GUI frontend")
+}
+
diff --git a/gui/src/gui/mod.rs b/gui/src/gui/mod.rs
new file mode 100644
index 0000000..2eb936e
--- /dev/null
+++ b/gui/src/gui/mod.rs
@@ -0,0 +1,186 @@
+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<Profile>,
+
+ scroll: scrollable::State,
+ input: text_input::State,
+ input_value: String,
+}
+
+#[derive(Debug, Clone)]
+enum Message {
+ Loaded(Arc<Profile>),
+ FailedToLoad,
+
+ InputChanged(String),
+ CreatePost,
+
+ PostCreated(cid::Cid),
+ PostCreationFailed(String),
+}
+
+impl Application for Distrox {
+ type Executor = iced::executor::Default; // tokio
+ type Message = Message;
+ type Flags = ();
+
+ fn new(_flags: ()) -> (Self, iced::Command<Self::Message>) {
+ (
+ Distrox::Loading,
+ iced::Command::perform(async {
+ match Profile::new_inmemory(Config::default()).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, _clipboard: &mut iced::Clipboard) -> iced::Command<Self::Message> {
+ 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<Self::Message> {
+ match self {
+ Distrox::Loading => {
+ let text = iced_native::widget::text::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() -> Result<()> {
+ let settings = iced::Settings {
+ window: iced::window::Settings {
+ resizable: true,
+ decorations: true,
+ transparent: false,
+ always_on_top: false,
+ ..iced::window::Settings::default()
+ },
+ 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
new file mode 100644
index 0000000..ea47a93
--- /dev/null
+++ b/gui/src/main.rs
@@ -0,0 +1,26 @@
+use anyhow::Result;
+
+mod cli;
+mod gui;
+
+use distrox_lib::*;
+
+fn main() -> Result<()> {
+ let _ = env_logger::try_init()?;
+ let matches = crate::cli::app().get_matches();
+
+ match matches.subcommand() {
+ None => crate::gui::run(),
+ Some((other, _)) => {
+ log::error!("No subcommand {} implemented", other);
+ Ok(())
+ },
+
+ _ => {
+ log::error!("Don't know what to do");
+ Ok(())
+ },
+ }
+}
+
+