split notedeck into crates

This splits notedeck into crates, separating the browser chrome and
individual apps:

* notedeck: binary file, browser chrome
* notedeck_columns: our columns app
* enostr: same as before

We still need to do more work to cleanly separate the chrome apis
from the app apis. Soon I will create notedeck-notebook to see what
makes sense to be shared between the apps.

Some obvious ones that come to mind:

1. ImageCache

We will likely want to move this to the notedeck crate, as most apps
will want some kind of image cache. In web browsers, web pages do not
need to worry about this, so we will likely have to do something similar

2. Ndb

Since NdbRef is threadsafe and Ndb is an Arc<NdbRef>, it can be safely
copied to each app. This will simplify things. In the future we might
want to create an abstraction over this? Maybe each app shouldn't have
access to the same database... we assume the data in DBs are all public
anyways, but if we have unwrapped giftwraps that could be a problem.

3. RelayPool / Subscription Manager

The browser should probably maintain these. Then apps can use ken's
high level subscription manager api and not have to worry about
connection pool details

4. Accounts

Accounts and key management should be handled by the chrome. Apps should
only have a simple signer interface.

That's all for now, just something to think about!

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-12-11 02:53:05 -08:00
parent 10cbdf15f0
commit 74c5f0c748
156 changed files with 194 additions and 252 deletions

View File

@@ -0,0 +1,224 @@
use crate::route::{Route, Router};
use crate::timeline::{Timeline, TimelineId};
use indexmap::IndexMap;
use std::iter::Iterator;
use std::sync::atomic::{AtomicU32, Ordering};
use tracing::warn;
#[derive(Clone)]
pub struct Column {
router: Router<Route>,
}
impl Column {
pub fn new(routes: Vec<Route>) -> Self {
let router = Router::new(routes);
Column { router }
}
pub fn router(&self) -> &Router<Route> {
&self.router
}
pub fn router_mut(&mut self) -> &mut Router<Route> {
&mut self.router
}
}
#[derive(Default)]
pub struct Columns {
/// Columns are simply routers into settings, timelines, etc
columns: IndexMap<u32, Column>,
/// Timeline state is not tied to routing logic separately, so that
/// different columns can navigate to and from settings to timelines,
/// etc.
pub timelines: IndexMap<u32, Timeline>,
/// The selected column for key navigation
selected: i32,
}
static UIDS: AtomicU32 = AtomicU32::new(0);
impl Columns {
pub fn new() -> Self {
Columns::default()
}
pub fn add_new_timeline_column(&mut self, timeline: Timeline) {
let id = Self::get_new_id();
let routes = vec![Route::timeline(timeline.id)];
self.timelines.insert(id, timeline);
self.columns.insert(id, Column::new(routes));
}
pub fn add_timeline_to_column(&mut self, col: usize, timeline: Timeline) {
let col_id = self.get_column_id_at_index(col);
self.column_mut(col)
.router_mut()
.route_to_replaced(Route::timeline(timeline.id));
self.timelines.insert(col_id, timeline);
}
pub fn new_column_picker(&mut self) {
self.add_column(Column::new(vec![Route::AddColumn(
crate::ui::add_column::AddColumnRoute::Base,
)]));
}
pub fn insert_intermediary_routes(&mut self, intermediary_routes: Vec<IntermediaryRoute>) {
let id = Self::get_new_id();
let routes = intermediary_routes
.into_iter()
.map(|r| match r {
IntermediaryRoute::Timeline(timeline) => {
let route = Route::timeline(timeline.id);
self.timelines.insert(id, timeline);
route
}
IntermediaryRoute::Route(route) => route,
})
.collect();
self.columns.insert(id, Column::new(routes));
}
fn get_new_id() -> u32 {
UIDS.fetch_add(1, Ordering::Relaxed)
}
pub fn add_column_at(&mut self, column: Column, index: u32) {
self.columns.insert(index, column);
}
pub fn add_column(&mut self, column: Column) {
self.columns.insert(Self::get_new_id(), column);
}
pub fn columns_mut(&mut self) -> Vec<&mut Column> {
self.columns.values_mut().collect()
}
pub fn num_columns(&self) -> usize {
self.columns.len()
}
// Get the first router in the columns if there are columns present.
// Otherwise, create a new column picker and return the router
pub fn get_first_router(&mut self) -> &mut Router<Route> {
if self.columns.is_empty() {
self.new_column_picker();
}
self.columns
.get_index_mut(0)
.expect("There should be at least one column")
.1
.router_mut()
}
pub fn timeline_mut(&mut self, timeline_ind: usize) -> &mut Timeline {
self.timelines
.get_index_mut(timeline_ind)
.expect("expected index to be in bounds")
.1
}
pub fn column(&self, ind: usize) -> &Column {
self.columns
.get_index(ind)
.expect("Expected index to be in bounds")
.1
}
pub fn columns(&self) -> Vec<&Column> {
self.columns.values().collect()
}
pub fn get_column_id_at_index(&self, ind: usize) -> u32 {
*self
.columns
.get_index(ind)
.expect("expected index to be within bounds")
.0
}
pub fn selected(&mut self) -> &mut Column {
self.columns
.get_index_mut(self.selected as usize)
.expect("Expected selected index to be in bounds")
.1
}
pub fn timelines_mut(&mut self) -> Vec<&mut Timeline> {
self.timelines.values_mut().collect()
}
pub fn timelines(&self) -> Vec<&Timeline> {
self.timelines.values().collect()
}
pub fn find_timeline_mut(&mut self, id: TimelineId) -> Option<&mut Timeline> {
self.timelines_mut().into_iter().find(|tl| tl.id == id)
}
pub fn find_timeline(&self, id: TimelineId) -> Option<&Timeline> {
self.timelines().into_iter().find(|tl| tl.id == id)
}
pub fn column_mut(&mut self, ind: usize) -> &mut Column {
self.columns
.get_index_mut(ind)
.expect("Expected index to be in bounds")
.1
}
pub fn find_timeline_for_column_index(&self, ind: usize) -> Option<&Timeline> {
let col_id = self.get_column_id_at_index(ind);
self.timelines.get(&col_id)
}
pub fn select_down(&mut self) {
warn!("todo: implement select_down");
}
pub fn select_up(&mut self) {
warn!("todo: implement select_up");
}
pub fn select_left(&mut self) {
if self.selected - 1 < 0 {
return;
}
self.selected -= 1;
}
pub fn select_right(&mut self) {
if self.selected + 1 >= self.columns.len() as i32 {
return;
}
self.selected += 1;
}
pub fn delete_column(&mut self, index: usize) {
if let Some((key, _)) = self.columns.get_index_mut(index) {
self.timelines.shift_remove(key);
}
self.columns.shift_remove_index(index);
if self.columns.is_empty() {
self.new_column_picker();
}
}
}
pub enum IntermediaryRoute {
Timeline(Timeline),
Route(Route),
}
pub enum ColumnsAction {
// Switch(usize), TODO: could use for keyboard selection
Remove(usize),
}