forked from ParadiseSS13/Paradise
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port and use VirtualList in TTSSeedsExplorer (#1448)
<!-- Пишите **НИЖЕ** заголовков и **ВЫШЕ** комментариев, иначе что то может пойти не так. --> <!-- Вы можете прочитать Contributing.MD, если хотите узнать больше. --> ## Что этот PR делает Кое-какой порт VirtualList с ТГ, и использование его в ТТС эксплорере Плюс слегка переделанный интерфейс под стать ТГ ## Почему это хорошо для игры Чуть меньше лагает, чуть лучше выглядит ## Изображения изменений ![image](https://github.com/user-attachments/assets/255d8ed9-58a0-4b6b-bd94-fa3bd4f0e597) ## Changelog :cl: tweak: ТТС эксплорер стал ещё немного лучше /:cl:
- Loading branch information
Showing
3 changed files
with
227 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Component, createRef } from 'inferno'; | ||
|
||
interface Props { | ||
children: any; | ||
} | ||
|
||
/** | ||
* A vertical list that renders items to fill space up to the extents of the | ||
* current window, and then defers rendering of other items until they come | ||
* into view. | ||
*/ | ||
export class VirtualList extends Component<Props, any> { | ||
containerRef: any; | ||
intervalId: any; | ||
|
||
constructor(props: Props) { | ||
super(props); | ||
|
||
this.containerRef = createRef(); | ||
this.state = { | ||
visibleElements: 1, | ||
padding: 0, | ||
}; | ||
} | ||
|
||
adjustExtents = () => { | ||
const { children } = this.props; | ||
const { visibleElements } = this.state; | ||
const current = this.containerRef.current; | ||
|
||
if (!children || !Array.isArray(children) || !current || visibleElements >= children.length) { | ||
return; | ||
} | ||
|
||
const unusedArea = document.body.offsetHeight - current.getBoundingClientRect().bottom; | ||
const averageItemHeight = Math.ceil(current.offsetHeight / visibleElements); | ||
|
||
if (unusedArea > 0) { | ||
const newVisibleElements = Math.min( | ||
children.length, | ||
visibleElements + Math.max(1, Math.ceil(unusedArea / averageItemHeight)) | ||
); | ||
|
||
this.setState({ | ||
visibleElements: newVisibleElements, | ||
padding: (children.length - newVisibleElements) * averageItemHeight, | ||
}); | ||
} | ||
}; | ||
|
||
componentDidMount() { | ||
this.adjustExtents(); | ||
this.intervalId = setInterval(this.adjustExtents, 100); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.intervalId); | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
const { visibleElements, padding } = this.state; | ||
|
||
return ( | ||
<div className={'VirtualList'}> | ||
<div className={'VirtualList__Container'} ref={this.containerRef}> | ||
{Array.isArray(children) ? children.slice(0, visibleElements) : null} | ||
</div> | ||
<div className={'VirtualList__Padding'} style={{ 'padding-bottom': `${padding}px` }} /> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.