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

Drop general extensions in favor of widget-specific extensions #127

Merged
merged 4 commits into from
Mar 9, 2022
Merged

Drop general extensions in favor of widget-specific extensions #127

merged 4 commits into from
Mar 9, 2022

Conversation

MaksymShcherbak
Copy link
Member

  1. Remove RelmRemovableExt::remove_all. It relies on widget internal structure and may break between releases, which would be hard to maintain.
  2. Remove RelmWidgetExt::iter_children, RelmWidgetExt::iter_children_reverse and RelmWidgetExt::for_each_children. Those aren't really reliable and can yeild surprising results on some widgets, like gtk::HeaderBar (see link).
  3. Instead, introduce widget-specific extensions: RelmBoxExt, RelmListBoxExt, RelmFlowBoxExt and RelmGridExt. Add tests for those extensions.

GTK4 encourages widget-specific implementations, instead of generalizations. See link.

Return `RelmRemovableExt` to its previous state, remove `RelmWidgetExt::iter_children` and add widget-specific extensions instead. Add tests for the widget-specific extensions.
Copy link
Member

@AaronErhardt AaronErhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! Looks really good overall.
In the long term, some of this functionality could potentially even be upstreamed to gtk-rs.

src/extensions/mod.rs Outdated Show resolved Hide resolved
src/extensions/mod.rs Outdated Show resolved Hide resolved
Copy link
Member

@AaronErhardt AaronErhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work! This is shaping up nicely and I really like the clean implementations for RelmIterChildrenExt and the DoubleEndedIterator implementation. I couldn't have done this better :)

There are just a few small things, I'd like to sort out before merging it.

src/extensions/mod.rs Outdated Show resolved Hide resolved
src/extensions/removable.rs Outdated Show resolved Hide resolved
src/extensions/tests.rs Outdated Show resolved Hide resolved
@MaksymShcherbak
Copy link
Member Author

Done. Also I've noticed incorrect behavior of ChildrenIterator in some cases - it's fixed now. If you have any Ideas how to simplify the implementation - I'd love to hear them.

Comment on lines 141 to 169
if !self.init_start {
self.init_start = true;
return self.start.clone();
}

std::iter::from_fn(move || {
if let Some(child) = widget.take() {
widget = child.next_sibling();
return Some(child);
if self.start == self.end {
if self.init_end {
return None;
} else {
self.init_end = true;
return self.start.clone();
}
}

if let Some(start) = self.start.take() {
self.start = start.next_sibling().map(|child| {
child
.downcast::<T::Child>()
.expect("The type of children does not match.")
});
if self.start == self.end {
if self.init_end {
return None;
} else {
self.init_end = true;
return self.start.clone();
}
}
return self.start.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this?

if self.done {
    None
} else {
   // Handle cases where only one child exists and
   // when all but one widget were consumed
   if self.start == self.end {
       self.done = true;
       self.start.clone()
   } else if let Some(start) = self.start.take() {
       // "Increment" the start child
       self.start = start.next_sibling().map(|child| {
            child
                .downcast::<T::Child>()
                .expect("The type of children does not match.")
        });
        // Just to make sure the iterator ends next time 
        // because all widgets were consumed
        self.done = self.start.is_none();
        Some(start)
    } else {
        None
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait a minute, I think I've overlooked a case where you use next_back().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested your proposal, seems like it's working - all tests pass.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I actually think it's correct to use what I suggested, if DoubleEndedIterator does the same thing in reverse. Just had to go through some cases in my mind, but couldn't find one that fails.

Copy link
Member

@AaronErhardt AaronErhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done, excellent work! I think we can merge this now.

@AaronErhardt AaronErhardt merged commit b987614 into Relm4:new-approach Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants