Skip to content

Commit

Permalink
Merge branch 'main' of github.com:p2panda/aardvark into feature/ci-wi…
Browse files Browse the repository at this point in the history
…n-mac-builds
  • Loading branch information
silviot committed Dec 8, 2024
2 parents 0f1cf43 + a175070 commit 59ccef4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 25 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# Aardvark

MVP local-first text editor :)

## Getting Started

The [GNOME Builder IDE](https://builder.readthedocs.io/) is
required to build and run the project. It can be installed with flatpak.

1. [Install flatpak](https://flatpak.org/setup/) for your distribution.

2. Install [Builder](https://flathub.org/apps/org.gnome.Builder) for GNOME:

`flatpak install flathub org.gnome.Builder`

3. Clone the aardvark repo:

`git clone [email protected]:p2panda/aardvark.git && cd aardvark`

4. Open the Builder application and navigate to the aardvark repo.
- You may be prompted to install or update the SDK in Builder.

5. Run the project with `Shift+Ctrl+Space` or click the ► icon (top-middle
of the Builder appication).
44 changes: 35 additions & 9 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/

use std::cell::RefCell;
use std::thread::JoinHandle;

use adw::prelude::*;
use adw::subclass::prelude::*;
use automerge::AutoCommit;
use anyhow::Result;
use automerge::transaction::Transactable;
use automerge::{AutoCommit, ObjId, ObjType};
use gettextrs::gettext;
use gtk::{gio, glib};
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::{mpsc, oneshot};

use crate::config::VERSION;
use crate::glib::closure_local;
use crate::network;
use crate::AardvarkWindow;
use crate::glib::closure_local;
use automerge::transaction::Transactable;
use automerge::ObjType;
use std::cell::RefCell;
use automerge::ObjId;

mod imp {
use super::*;
Expand All @@ -40,13 +43,25 @@ mod imp {
pub struct AardvarkApplication {
automerge: RefCell<AutoCommit>,
root: ObjId,
backend_shutdown_tx: oneshot::Sender<()>,
tx: mpsc::Sender<Vec<u8>>,
}

impl AardvarkApplication {
fn update_text(&self, text: &str) {
println!("app: {}", text);
let mut doc = self.automerge.borrow_mut();
doc.update_text(&self.root, text).unwrap();

{
let bytes = doc.save();
let tx = self.tx.clone();
glib::spawn_future_local(async move {
if let Err(e) = tx.send(bytes).await {
println!("{}", e);
}
});
}
}
}

Expand All @@ -58,11 +73,24 @@ mod imp {

fn new() -> Self {
let mut am = AutoCommit::new();
let root = am.put_object(automerge::ROOT, "root", ObjType::Text).unwrap();
let root = am
.put_object(automerge::ROOT, "root", ObjType::Text)
.unwrap();
let automerge = RefCell::new(am);

let (backend_shutdown_tx, tx, mut rx) = network::run().expect("running p2p backend");

glib::spawn_future_local(async move {
while let Some(msg) = rx.recv().await {
println!("got {:?}", msg);
}
});

AardvarkApplication {
automerge,
root,
backend_shutdown_tx,
tx,
}
}
}
Expand Down Expand Up @@ -97,8 +125,6 @@ mod imp {
window.upcast()
});

network::run().expect("running p2p backend");

// Ask the window manager/compositor to present the window
window.present();
}
Expand Down
35 changes: 21 additions & 14 deletions src/network.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Arc, RwLock};
use std::thread::JoinHandle;

Expand All @@ -14,6 +13,7 @@ use p2panda_sync::log_sync::LogSyncProtocol;
use p2panda_sync::{TopicMap, TopicQuery};
use serde::{Deserialize, Serialize};
use tokio::runtime::Builder;
use tokio::sync::{mpsc, oneshot};

#[derive(Clone, Default, Debug, PartialEq, Eq, std::hash::Hash, Serialize, Deserialize)]
struct TextDocument([u8; 32]);
Expand Down Expand Up @@ -78,17 +78,23 @@ impl TopicMap<TextDocument, HashMap<PublicKey, Vec<LogId>>> for TextDocumentStor
}
}

pub fn run() -> Result<(Sender<Vec<u8>>, Receiver<Vec<u8>>)> {
let (to_network, from_app) = std::sync::mpsc::channel::<Vec<u8>>();
let (to_app, from_network) = std::sync::mpsc::channel();
pub fn run() -> Result<(
oneshot::Sender<()>,
mpsc::Sender<Vec<u8>>,
mpsc::Receiver<Vec<u8>>,
)> {
let (to_network, mut from_app) = mpsc::channel::<Vec<u8>>(512);
let (to_app, from_network) = mpsc::channel(512);

let rt_handle: JoinHandle<Result<()>> = std::thread::spawn(|| {
let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

std::thread::spawn(move || {
let runtime = Builder::new_current_thread()
.enable_all()
.build()
.expect("backend runtime ready to spawn tasks");

runtime.block_on(async {
runtime.block_on(async move {
let network_id = Hash::new(b"aardvark <3");
let private_key = PrivateKey::new();

Expand All @@ -101,12 +107,13 @@ pub fn run() -> Result<(Sender<Vec<u8>>, Receiver<Vec<u8>>)> {
let mut network = NetworkBuilder::new(*network_id.as_bytes())
.sync(sync_config)
.private_key(private_key)
.discovery(LocalDiscovery::new()?)
.discovery(LocalDiscovery::new().unwrap())
.build()
.await?;
.await
.unwrap();

let test_document = TextDocument(Hash::new(b"my first doc <3").into());
let (topic_tx, mut topic_rx, ready) = network.subscribe(test_document).await?;
let (topic_tx, mut topic_rx, ready) = network.subscribe(test_document).await.unwrap();

tokio::task::spawn(async move {
while let Some(message) = topic_rx.recv().await {
Expand All @@ -129,12 +136,12 @@ pub fn run() -> Result<(Sender<Vec<u8>>, Receiver<Vec<u8>>)> {
// 3) persist the operation
// 4) forward the payload onto the app

to_app.send(bytes).expect("can send on channel");
to_app.send(bytes).await.expect("can send on channel");
}
});

tokio::task::spawn(async move {
while let Ok(bytes) = from_app.recv() {
while let Some(bytes) = from_app.recv().await {
println!("New message from app");

// 1) encode operation
Expand All @@ -145,9 +152,9 @@ pub fn run() -> Result<(Sender<Vec<u8>>, Receiver<Vec<u8>>)> {
}
});

Ok(())
})
shutdown_rx.await.unwrap();
});
});

Ok((to_network, from_network))
Ok((shutdown_tx, to_network, from_network))
}
6 changes: 6 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,10 @@ impl AardvarkWindow {
.property("application", application)
.build()
}

pub fn set_text(&self, text: &str) {
let window = self.imp();
let buffer = window.text_view.buffer();
buffer.set_text(text);
}
}
8 changes: 6 additions & 2 deletions src/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,18 @@
<object class="GtkLabel">
<property name="wrap">True</property>
<property name="justify">GTK_JUSTIFY_CENTER</property>
<property name="max-width-chars">30</property>
<property name="max-width-chars">25</property>
<property name="natural-wrap-mode">GTK_NATURAL_WRAP_WORD</property>
<property name="label" translatable="true">Give others access to this document by sharing this invite code with them:</property>
</object>
</child>
<child>
<object class="GtkLabel">
<property name="label" translatable="true">sdf032jbn39aw0y23l0nd9dkeb200b21m289he </property>
<property name="wrap">True</property>
<property name="wrap-mode">char</property>
<property name="justify">GTK_JUSTIFY_CENTER</property>
<property name="max-width-chars">25</property>
<property name="label" translatable="true">sdf0398p2jbytc6n4wpq339aw0y23l0nd9dk032jbn39accw0y23bl0n78deb200b21m289he </property>
<style>
<class name="invite-code" />
<class name="monospace" />
Expand Down

0 comments on commit 59ccef4

Please sign in to comment.