Skip to content

Commit 9b44cde

Browse files
committed
BUGFIX: changing inner state with a collection wouldn't reload the collection
1 parent 04a8d0f commit 9b44cde

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

anathema-runtime/src/runtime/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl<'rt, 'bp, G: GlobalEventHandler> Frame<'rt, 'bp, G> {
270270
match event {
271271
ComponentEvent::Noop => return,
272272
ComponentEvent::Stop => todo!(),
273+
273274
// Component specific event
274275
ComponentEvent::Blur | ComponentEvent::Focus | ComponentEvent::Key(_) => {
275276
let Some(Index {
@@ -395,6 +396,10 @@ impl<'rt, 'bp, G: GlobalEventHandler> Frame<'rt, 'bp, G> {
395396
#[cfg(feature = "profile")]
396397
puffin::profile_function!();
397398

399+
// TODO: let's keep some memory around to drain this into instead of allocating
400+
// a new vector every time.
401+
// E.g `self.deferred_components.drain_into(&mut self.deferred_buffer)`
402+
// Nb: Add drain_into to DeferredComponents
398403
let commands = self.deferred_components.drain().collect::<Vec<_>>();
399404
for mut cmd in commands {
400405
// Blur the current component if the message is a `Focus` message

anathema-state/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ impl Hex {
5757
pub const GREEN: Self = Self { r: 0, g: 255, b: 0 };
5858
pub const RED: Self = Self { r: 255, g: 0, b: 0 };
5959
pub const WHITE: Self = Self { r: 255, g: 255, b: 255 };
60+
61+
pub const fn red_f32(&self) -> f32 {
62+
self.r as f32 / u8::MAX as f32
63+
}
64+
65+
pub const fn green_f32(&self) -> f32 {
66+
self.g as f32 / u8::MAX as f32
67+
}
68+
69+
pub const fn blue_f32(&self) -> f32 {
70+
self.b as f32 / u8::MAX as f32
71+
}
6072
}
6173

6274
impl Display for Hex {

anathema-widgets/src/components/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,23 +314,23 @@ impl<'a> Event<'a> {
314314
self.external_ident
315315
}
316316

317-
/// Try to cast the event payload to a specific type
318-
pub fn data<T: 'static>(&self) -> Option<&'a T> {
319-
self.data.downcast_ref()
320-
}
321-
322317
/// Cast the event payload to a specific type
323318
///
324319
/// # Panics
325320
///
326321
/// This will panic if the type is incorrect
327-
pub fn data_unchecked<T: 'static>(&self) -> &'a T {
322+
pub fn data<T: 'static>(&self) -> &'a T {
328323
match self.data.downcast_ref() {
329324
Some(data) => data,
330-
None => panic!("invalid type"),
325+
None => panic!("invalid type when casting event data"),
331326
}
332327
}
333328

329+
/// Try to cast the event payload to a specific type
330+
pub fn data_checked<T: 'static>(&self) -> Option<&'a T> {
331+
self.data.downcast_ref()
332+
}
333+
334334
pub fn should_stop_propagation(&self) -> bool {
335335
self.stop_propagation
336336
}

anathema-widgets/src/nodes/loops.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ impl<'bp> For<'bp> {
7676

7777
tree.relative_remove(&[*index as u16])
7878
}
79-
Change::Dropped => tree.truncate_children(),
80-
Change::Changed => {
79+
Change::Dropped | Change::Changed => {
8180
// If the collection has changed to a different collection
8281
// then truncate the tree
8382
self.collection.reload(attribute_storage);

examples/message-passing.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use anathema::backend::tui::TuiBackend;
2-
use anathema::component::{Children, Component, MouseEvent};
3-
use anathema::prelude::Context;
2+
use anathema::component::{Children, Component, Context, MouseEvent};
43
use anathema::runtime::Runtime;
54
use anathema::state::{List, State, Value};
65
use anathema::templates::Document;

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ pub mod prelude {
1717
pub use crate::backend::tui::TuiBackend;
1818
pub use crate::runtime::Runtime;
1919
pub use crate::templates::{ComponentBlueprintId, Document, SourceKind, ToSourceKind};
20-
pub use crate::widgets::components::{Context, Event};
2120
}
2221

2322
pub mod component {
2423
pub use crate::state::{Color, List, Map, Maybe, Nullable, State, Value};
2524
pub use crate::widgets::components::events::{
2625
ComponentEvent, KeyCode, KeyEvent, KeyState, MouseButton, MouseEvent, MouseState,
2726
};
28-
pub use crate::widgets::components::{Component, ComponentId, Context, Emitter};
27+
pub use crate::widgets::components::{Component, ComponentId, Context, Emitter, Event};
2928
pub use crate::widgets::query::Children;
3029
}

0 commit comments

Comments
 (0)