at what level are effects re-run #617
-
i want to make a reactive history stack for a file browser i intend to make. // std
use std::{cell::RefCell, path::PathBuf, rc::Rc};
// deps
use floem::{
reactive::{create_signal, ReadSignal, SignalGet, SignalUpdate, WriteSignal},
views::{button, text},
IntoView, View,
};
fn app() -> impl IntoView {
let router = Rc::new(RefCell::new(Router::new()));
let mut view: Vec<Box<dyn View>> = vec![];
let location = router.borrow().location().get();
let components = location.components();
for (index, ref c) in components.enumerate() {
let router = router.clone();
let path_until: PathBuf = location
.ancestors()
.nth(location.components().count() - index - 1)
.unwrap()
.into();
let path_until_clone = Rc::new(path_until);
view.push(Box::new(
button(c.as_os_str().to_str().unwrap_or("err").to_string())
.action(move || router.borrow_mut().push((*path_until_clone).clone())),
));
view.push(Box::new(text("/")));
}
view
}
struct Router {
location: ReadSignal<PathBuf>,
set_location: WriteSignal<PathBuf>,
history: im::Vector<PathBuf>,
}
#[allow(unused)]
impl Router {
fn new() -> Self {
let cwd: PathBuf = std::env::var("HOME").unwrap_or("/".into()).into();
let (location, set_location) = create_signal(cwd.clone());
Self {
history: im::vector![cwd],
location,
set_location,
}
}
fn push(&mut self, path: PathBuf) {
self.history.push_back(path.clone());
self.set_location.set(path);
}
fn pop(&mut self) -> Option<PathBuf> {
if self.history.len() > 1 {
self.set_location.set(self.history.last().unwrap().clone());
self.history.pop_back()
} else {
None
}
}
fn location(&self) -> ReadSignal<PathBuf> {
self.location
}
}
fn main() {
floem::launch(app)
} The router gives out a ReadSignal of the last item of the history stack using the So in the For example: So at what level does the effect re-run to update the UI? Does the signal have to be used inside the struct that implements |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I have checked by placing a println in the action, that the signal is indeed being updated on button press: button(c.as_os_str().to_str().unwrap_or("err").to_string())
.action(move || {
println!("currentlocation: {:?}", router.borrow().location().get());
router.borrow_mut().push((*path_until_clone).clone())
}), |
Beta Was this translation helpful? Give feedback.
You need
dyn_stack
for this.Here is an example https://github.com/lapce/floem/blob/main/examples/stacks/src/dyn_stack.rs