worldwideportal/src/main.rs

87 lines
2.6 KiB
Rust
Raw Normal View History

use std::cell::RefCell;
2024-07-21 17:01:56 +10:00
use std::rc::Rc;
2024-08-22 22:25:05 +10:00
use term_split::TermSplit;
2024-07-21 17:01:56 +10:00
use yew::prelude::*;
pub mod command_handler;
pub mod lineengine;
pub mod lua_state;
pub mod parsing;
pub mod split_panel;
2024-08-22 22:25:05 +10:00
pub mod term_split;
2024-07-21 17:01:56 +10:00
pub mod term_view;
pub mod websocket;
use crate::lua_state::{install_lua_globals, LuaState};
use crate::split_panel::*;
2024-07-21 17:01:56 +10:00
use crate::term_view::*;
use crate::websocket::RegisteredWebSockets;
2024-07-21 17:01:56 +10:00
#[derive(Properties)]
2024-08-23 23:37:58 +10:00
pub struct GlobalMemoState {
// No strong references allowed between each of these groups of state.
frame_registry: RefCell<RegisteredTermFrames>,
lua_engine: RefCell<LuaState>,
ws_registry: RefCell<RegisteredWebSockets>,
2024-08-23 23:37:58 +10:00
// A cache of the latest layout info (separate from the state).
// Updating this doesn't force a relayout, so only update the cache when
// you also emit a change to the real layout.
layout: RefCell<Rc<GlobalLayoutState>>,
}
2024-08-23 23:37:58 +10:00
type GlobalMemoCell = Rc<GlobalMemoState>;
2024-08-23 23:37:58 +10:00
// Used only for yew. Lua and Frame Registry excluded, as
// changes there should never cause a re-render.
impl PartialEq for GlobalMemoState {
fn eq(&self, _other: &Self) -> bool {
true
}
}
2024-08-23 23:37:58 +10:00
// Used for state that impacts the entire layout. Changes here will
// cause a re-render
#[derive(PartialEq, Properties)]
pub struct GlobalLayoutState {
term_splits: TermSplit,
}
// Note: Despite interior mutability, you still should always call the
// setter to force a re-render. Interior mutability is used to allow this
// to be baked into the Lua closures and still allow access to the current
// value.
type GlobalLayoutCell = Rc<GlobalLayoutState>;
2024-07-21 17:01:56 +10:00
#[function_component(App)]
fn app() -> Html {
2024-08-23 23:37:58 +10:00
let global_layout: UseStateHandle<GlobalLayoutCell> = use_state(|| {
Rc::new(GlobalLayoutState {
term_splits: TermSplit::Term {
frame: TermFrame(1),
},
})
});
let global_memo = use_memo((), |_| GlobalMemoState {
frame_registry: RegisteredTermFrames::new().into(),
ws_registry: RegisteredWebSockets::new().into(),
lua_engine: LuaState::setup().expect("Can create interpreter").into(),
2024-08-23 23:37:58 +10:00
layout: RefCell::new((*global_layout).clone()),
});
use_memo((), |_| {
install_lua_globals(&global_memo, global_layout.setter())
.expect("Couldn't install Lua globals")
});
2024-07-21 17:01:56 +10:00
html! {
<div class="toplevel">
2024-08-23 23:37:58 +10:00
<TermViewTree global_memo={global_memo.clone()}
global_layout={global_layout.clone()}/>
2024-07-21 17:01:56 +10:00
</div>
}
}
fn main() {
console_error_panic_hook::set_once();
2024-07-21 17:01:56 +10:00
yew::Renderer::<App>::new().render();
}