Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WidgetId revision, part 2 #265

Merged
merged 15 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/kas-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ macros_log = ["kas-macros/log"]

[dependencies]
easy-cast = "0.4.2"
lazy_static = "1.4"
log = "0.4"
smallvec = "1.6.1"
stack_dst = { version = "0.6", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/kas-core/src/core/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ impl<M: 'static> WidgetChildren for Box<dyn Widget<Msg = M>> {
self.as_mut().get_child_mut(index)
}

fn find_child_index(&self, id: WidgetId) -> Option<usize> {
fn find_child_index(&self, id: &WidgetId) -> Option<usize> {
self.as_ref().find_child_index(id)
}
fn find_widget(&self, id: WidgetId) -> Option<&dyn WidgetConfig> {
fn find_widget(&self, id: &WidgetId) -> Option<&dyn WidgetConfig> {
self.as_ref().find_widget(id)
}
fn find_widget_mut(&mut self, id: WidgetId) -> Option<&mut dyn WidgetConfig> {
fn find_widget_mut(&mut self, id: &WidgetId) -> Option<&mut dyn WidgetConfig> {
self.as_mut().find_widget_mut(id)
}
}
Expand Down
34 changes: 21 additions & 13 deletions crates/kas-core/src/core/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ pub trait WidgetCore: Any + fmt::Debug {
/// Get the widget's numeric identifier
#[inline]
fn id(&self) -> WidgetId {
self.core_data().id
self.core_data().id.clone()
}

/// Get the widget's numeric identifier
#[inline]
fn id_ref(&self) -> &WidgetId {
&self.core_data().id
}

/// Get whether the widget is disabled
Expand Down Expand Up @@ -130,7 +136,7 @@ pub trait WidgetCore: Any + fmt::Debug {
/// widgets are unaffected), unless [`WidgetConfig::hover_highlight`]
/// returns true.
fn input_state(&self, mgr: &ManagerState, disabled: bool) -> InputState {
let id = self.core_data().id;
let id = &self.core_data().id;
let (char_focus, sel_focus) = mgr.has_char_focus(id);
let mut state = InputState::empty();
if self.core_data().disabled || disabled {
Expand Down Expand Up @@ -193,29 +199,30 @@ pub trait WidgetChildren: WidgetCore {
///
/// This function assumes that `id` is a valid widget.
#[inline]
fn is_ancestor_of(&self, id: WidgetId) -> bool {
fn is_ancestor_of(&self, id: &WidgetId) -> bool {
self.id().is_ancestor_of(id)
}

/// Find the child which is an ancestor of this `id`, if any
///
/// This child may then be accessed via [`Self::get_child`] or
/// [`Self::get_child_mut`].
/// Warning: the return value is not guaranteed to be a valid child, thus
/// calls to methods like [`Self::get_child`] must handle `None` return.
///
/// This requires that the widget tree has already been configured by
/// [`event::ManagerState::configure`].
#[inline]
fn find_child_index(&self, id: WidgetId) -> Option<usize> {
fn find_child_index(&self, id: &WidgetId) -> Option<usize> {
self.id().index_of_child(id)
}

/// Find the descendant with this `id`, if any
///
/// This requires that the widget tree has already been configured by
/// [`event::ManagerState::configure`].
fn find_widget(&self, id: WidgetId) -> Option<&dyn WidgetConfig> {
if let Some(child) = self.find_child_index(id) {
self.get_child(child).unwrap().find_widget(id)
fn find_widget(&self, id: &WidgetId) -> Option<&dyn WidgetConfig> {
if let Some(index) = self.find_child_index(id) {
self.get_child(index)
.and_then(|child| child.find_widget(id))
} else if self.eq_id(id) {
return Some(self.as_widget());
} else {
Expand All @@ -227,9 +234,10 @@ pub trait WidgetChildren: WidgetCore {
///
/// This requires that the widget tree has already been configured by
/// [`ManagerState::configure`].
fn find_widget_mut(&mut self, id: WidgetId) -> Option<&mut dyn WidgetConfig> {
if let Some(child) = self.find_child_index(id) {
self.get_child_mut(child).unwrap().find_widget_mut(id)
fn find_widget_mut(&mut self, id: &WidgetId) -> Option<&mut dyn WidgetConfig> {
if let Some(index) = self.find_child_index(id) {
self.get_child_mut(index)
.and_then(|child| child.find_widget_mut(id))
} else if self.eq_id(id) {
return Some(self.as_widget_mut());
} else {
Expand Down Expand Up @@ -278,7 +286,7 @@ pub trait WidgetConfig: Layout {
/// method but instead use [`WidgetConfig::configure`]; the exception is
/// widgets with pop-ups.
fn configure_recurse(&mut self, mut cmgr: ConfigureManager) {
self.core_data_mut().id = cmgr.get_id(self.id());
self.core_data_mut().id = cmgr.get_id();
for i in 0..self.num_children() {
if let Some(w) = self.get_child_mut(i) {
w.configure_recurse(cmgr.child(i));
Expand Down
Loading