Skip to content

Commit

Permalink
handle_messages impls: do not expect cx.last_child().is_some()
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Dec 18, 2023
1 parent f2030b8 commit 6791f6e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
17 changes: 9 additions & 8 deletions crates/kas-widgets/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ impl_scope! {
if self.message_handlers.is_empty() {
return;
}
let mut update = false;
let mut cx = AdaptEventCx::new(cx, self.id());
let index = cx.last_child().expect("message not sent from self");
for handler in self.message_handlers.iter() {
update |= handler(&mut cx, data, index);
}
if update {
cx.update(self.as_node(data));
if let Some(index) = cx.last_child() {
let mut update = false;
let mut cx = AdaptEventCx::new(cx, self.id());
for handler in self.message_handlers.iter() {
update |= handler(&mut cx, data, index);
}
if update {
cx.update(self.as_node(data));
}
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions crates/kas-widgets/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,15 @@ impl_scope! {
if self.message_handlers.is_empty() {
return;
}
let mut update = false;
let mut cx = AdaptEventCx::new(cx, self.id());
let index = cx.last_child().expect("message not sent from self");
for handler in self.message_handlers.iter() {
update |= handler(&mut cx, data, index);
}
if update {
cx.update(self.as_node(data));
if let Some(index) = cx.last_child() {
let mut update = false;
let mut cx = AdaptEventCx::new(cx, self.id());
for handler in self.message_handlers.iter() {
update |= handler(&mut cx, data, index);
}
if update {
cx.update(self.as_node(data));
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/kas-widgets/src/scroll_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,13 +559,13 @@ impl_scope! {

impl Events for Self {
fn handle_messages(&mut self, cx: &mut EventCx, _: &Self::Data) {
let index = cx.last_child().expect("message not sent from self");
if index == widget_index![self.horiz_bar] {
let index = cx.last_child();
if index == Some(widget_index![self.horiz_bar]) {
if let Some(ScrollMsg(x)) = cx.try_pop() {
let offset = Offset(x, self.inner.scroll_offset().1);
self.inner.set_scroll_offset(cx, offset);
}
} else if index == widget_index![self.vert_bar] {
} else if index == Some(widget_index![self.vert_bar]) {
if let Some(ScrollMsg(y)) = cx.try_pop() {
let offset = Offset(self.inner.scroll_offset().0, y);
self.inner.set_scroll_offset(cx, offset);
Expand Down
17 changes: 9 additions & 8 deletions crates/kas-widgets/src/splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,15 @@ impl_scope! {
}

fn handle_messages(&mut self, cx: &mut EventCx, _: &Self::Data) {
let index = cx.last_child().expect("message not sent from self");
if (index & 1) == 1 {
if let Some(GripMsg::PressMove(offset)) = cx.try_pop() {
let n = index >> 1;
assert!(n < self.handles.len());
let action = self.handles[n].set_offset(offset).1;
cx.action(&self, action);
self.adjust_size(&mut cx.config_cx(), n);
if let Some(index) = cx.last_child() {
if (index & 1) == 1 {
if let Some(GripMsg::PressMove(offset)) = cx.try_pop() {
let n = index >> 1;
assert!(n < self.handles.len());
let action = self.handles[n].set_offset(offset).1;
cx.action(&self, action);
self.adjust_size(&mut cx.config_cx(), n);
}
}
}
}
Expand Down

0 comments on commit 6791f6e

Please sign in to comment.