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

Add methods to control Scrollable programmatically #607

Merged
merged 5 commits into from
Jun 7, 2021

Conversation

yusdacra
Copy link
Contributor

@yusdacra yusdacra commented Nov 9, 2020

This PR adds methods:

  • snap_to_bottom and on_scroll to Scrollable
    • snap_to_bottom takes a bool and if true makes the Scrollable snap to bottom when the user scrolls to bottom. If the contents of the Scrollable change, it will automatically snap to bottom. Whenever the user scrolls up it will stop snapping to bottom. By default a Scrollable won't snap to bottom.
    • on_scroll takes a function whose arguments are two f32s and produces a Message. First argument is to pass the current offset of the Scrollable to provide the user with "where is this Scrollable at right now" and second argument is to pass the previous offset of the Scrollable to provide the user with "where was this Scrollable before this scroll event".
  • scroll_to_percentage and scroll_to_bottom to scrollable::State
    • scroll_to_percentage takes a value between 0.0 and 1.0. It marks the Scrollable to scroll to the specified percentage in the next draw call.
    • scroll_to_bottom is just a shorthand for scroll_to_percentage(1.0).

This should fix #307

I wonder what people think about this so please post your thoughts!

@yusdacra yusdacra force-pushed the scrollable_programmatically branch from 4730e65 to 20529ec Compare November 9, 2020 18:35
@yusdacra yusdacra force-pushed the scrollable_programmatically branch 2 times, most recently from 4c57afd to 34b381c Compare November 10, 2020 11:59
@yusdacra yusdacra marked this pull request as ready for review November 10, 2020 12:37
@yusdacra yusdacra force-pushed the scrollable_programmatically branch from 34b381c to e84b213 Compare November 11, 2020 17:13
native/src/widget/scrollable.rs Outdated Show resolved Hide resolved
native/src/widget/scrollable.rs Outdated Show resolved Hide resolved
@yusdacra yusdacra force-pushed the scrollable_programmatically branch 2 times, most recently from 98fe127 to 38b0171 Compare November 17, 2020 09:13
@hecrj hecrj added the feature New feature or request label Nov 25, 2020
@yusdacra yusdacra force-pushed the scrollable_programmatically branch 2 times, most recently from b1b5f8b to dec0e0a Compare November 26, 2020 21:24
@agraven
Copy link

agraven commented Dec 4, 2020

snap_to_bottom is a feature I need to make the chat client I'm building work right, it being merged would be strong cause for celebration for me.

@yusdacra
Copy link
Contributor Author

yusdacra commented Dec 4, 2020

snap_to_bottom is a feature I need to make the chat client I'm building work right, it being merged would be strong cause for celebration for me.

Haha, same. It's why I implemented this feature in the first place :D

@twitchyliquid64
Copy link

I've also found myself needing this, as I am implementing a search results list and need to scroll so that the result focused after a <tab> is visible. Plz! 🤞 🤞 🤞

@twitchyliquid64
Copy link

In the meantime here's a disgusting hack to scroll_to_bottom on iced 0.2/0.3, while we wait for this to land.

self.scroll = unsafe {
    let mut tmp = std::mem::transmute::<_, (Option<f32>, f32)>(self.scroll);
    tmp.1 = 999999.0;
    std::mem::transmute::<_, scrollable::State>(tmp)
};

(Assuming self.scroll is your scrollable::State)

Caveat: Dirty, disgusting, unsafe, brittle, use at your own risk.

@yusdacra yusdacra force-pushed the scrollable_programmatically branch from dec0e0a to 21e8ad5 Compare December 16, 2020 14:18
@yusdacra yusdacra force-pushed the scrollable_programmatically branch 2 times, most recently from e850e80 to 73750fc Compare January 4, 2021 23:16
@yusdacra yusdacra force-pushed the scrollable_programmatically branch from 73750fc to cc80110 Compare January 18, 2021 06:11
@yusdacra yusdacra mentioned this pull request Feb 5, 2021
@yusdacra yusdacra force-pushed the scrollable_programmatically branch from cc80110 to 5a376ae Compare February 15, 2021 16:37
@pleshevskiy
Copy link

О! That's just what I need! Thank you!

@hecrj hecrj added this to the 0.4.0 milestone Jun 4, 2021
@hecrj hecrj force-pushed the scrollable_programmatically branch from 5a376ae to f7d6e40 Compare June 4, 2021 12:30
@hecrj
Copy link
Member

hecrj commented Jun 4, 2021

I made some changes that I believe simplify everything overall.

Specifically:

  • We avoid the RefCell by introducing a new Offset enum and deferring the absolute computation until the next scrolling event.
  • Instead of a snap_to_bottom, we introduce a snap_to method that allows snapping to any given relative position.
  • The scroll_to API remains unchanged.

I have also updated the scrollable example to showcase the new API:

image

Let me know what you think!

@yusdacra
Copy link
Contributor Author

yusdacra commented Jun 4, 2021

I made some changes that I believe simplify everything overall.

Specifically:

  • We avoid the RefCell by introducing a new Offset enum and deferring the absolute computation until the next scrolling event.
  • Instead of a snap_to_bottom, we introduce a snap_to method that allows snapping to any given relative position.
  • The scroll_to API remains unchanged.

Looks nice! Deferring the computation is nice, I like that. Can it be merged soon then?

@hecrj
Copy link
Member

hecrj commented Jun 7, 2021

@yusdacra Yes! Let's merge and fix any potential issues and implement new use cases as they come up.

@hecrj
Copy link
Member

hecrj commented Jun 7, 2021

Thank you everyone! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scrolling programmatically
6 participants