offsetFraction
Parameters
- self:ScrollInfo
- ScrollInfo
- dir:enums.Direction
- enums.Direction
Source
Implementation
pub fn offsetFraction(self: ScrollInfo, dir: enums.Direction) f32 {
const viewport_start = switch (dir) {
.vertical => self.viewport.y,
.horizontal => self.viewport.x,
};
const viewport_size = switch (dir) {
.vertical => self.viewport.h,
.horizontal => self.viewport.w,
};
const virtual_size = switch (dir) {
.vertical => self.virtual_size.h,
.horizontal => self.virtual_size.w,
};
if (viewport_size == 0) return 0;
const max_hard_scroll = self.scrollMax(dir);
var length = @max(viewport_size, virtual_size);
if (viewport_start < 0) {
// temporarily adding the dead space we are showing
length += -viewport_start;
} else if (viewport_start > max_hard_scroll) {
length += (viewport_start - max_hard_scroll);
}
const max_scroll = @max(0, length - viewport_size);
if (max_scroll == 0) return 0;
return @max(0, @min(1.0, viewport_start / max_scroll));
}