Applying a scrollbar to a widget - hpi-swa-teaching/MaterialDesignWidgets GitHub Wiki

class MDScrollableWidget

Methods to override: contentHeight, maxScrollDelta

To apply a scrollbar to a widget let it inherit from MDScrollableWidget.

MDScrollableWidget handles mouse wheel events and calls:

  • scrollUp: aNumber
  • scrollDown: aNumber

These two methods update the scrollOffset which can be accessed via self scrollOffset.

But the most important methods to override are as mentioned above contentHeight and maxScrollDelta which help us calculate the height and the position of the scrollbar.

In MDList we use it like the following:

contentHeight

    ^ self listItems size * (self class listItemHeight + self class defaultListItemPadding)


maxScrollDelta

    | maxScrollDelta |
    maxScrollDelta := self listItems size - ((self height / (self class listItemHeight + self listItemPadding)) floor).
    maxScrollDelta := (self class listItemHeight + self listItemPadding) * maxScrollDelta - self listItemPadding.

    ^ maxScrollDelta