-
Notifications
You must be signed in to change notification settings - Fork 236
/
profile-table-view.tsx
366 lines (330 loc) · 10.2 KB
/
profile-table-view.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import {h, Component} from 'preact'
import {StyleSheet, css} from 'aphrodite'
import {Profile, Frame} from '../lib/profile'
import {sortBy, formatPercent} from '../lib/utils'
import {FontSize, Colors, Sizes, commonStyle} from './style'
import {ColorChit} from './color-chit'
import {ScrollableListView, ListItem} from './scrollable-list-view'
import {actions} from '../store/actions'
import {Dispatch, createContainer} from '../lib/typed-redux'
import {ApplicationState} from '../store'
import {createGetCSSColorForFrame, getFrameToColorBucket} from '../store/getters'
import {ActiveProfileState} from './application'
export enum SortField {
SYMBOL_NAME,
SELF,
TOTAL,
}
export enum SortDirection {
ASCENDING,
DESCENDING,
}
export interface SortMethod {
field: SortField
direction: SortDirection
}
interface HBarProps {
perc: number
}
class HBarDisplay extends Component<HBarProps, {}> {
render() {
return (
<div className={css(style.hBarDisplay)}>
<div className={css(style.hBarDisplayFilled)} style={{width: `${this.props.perc}%`}} />
</div>
)
}
}
interface SortIconProps {
activeDirection: SortDirection | null
}
class SortIcon extends Component<SortIconProps, {}> {
render() {
const {activeDirection} = this.props
const upFill = activeDirection === SortDirection.ASCENDING ? Colors.GRAY : Colors.LIGHT_GRAY
const downFill = activeDirection === SortDirection.DESCENDING ? Colors.GRAY : Colors.LIGHT_GRAY
return (
<svg
width="8"
height="10"
viewBox="0 0 8 10"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={css(style.sortIcon)}
>
<path d="M0 4L4 0L8 4H0Z" fill={upFill} />
<path d="M0 4L4 0L8 4H0Z" transform="translate(0 10) scale(1 -1)" fill={downFill} />
</svg>
)
}
}
interface ProfileTableViewProps {
profile: Profile
profileIndex: number
selectedFrame: Frame | null
getCSSColorForFrame: (frame: Frame) => string
sortMethod: SortMethod
setSelectedFrame: (frame: Frame | null) => void
setSortMethod: (sortMethod: SortMethod) => void
}
export class ProfileTableView extends Component<ProfileTableViewProps, void> {
renderRow(frame: Frame, index: number) {
const {profile, selectedFrame} = this.props
const totalWeight = frame.getTotalWeight()
const selfWeight = frame.getSelfWeight()
const totalPerc = 100.0 * totalWeight / profile.getTotalNonIdleWeight()
const selfPerc = 100.0 * selfWeight / profile.getTotalNonIdleWeight()
const selected = frame === selectedFrame
// We intentionally use index rather than frame.key here as the tr key
// in order to re-use rows when sorting rather than creating all new elements.
return (
<tr
key={`${index}`}
onClick={this.props.setSelectedFrame.bind(null, frame)}
className={css(
style.tableRow,
index % 2 == 0 && style.tableRowEven,
selected && style.tableRowSelected,
)}
>
<td className={css(style.numericCell)}>
{profile.formatValue(totalWeight)} ({formatPercent(totalPerc)})
<HBarDisplay perc={totalPerc} />
</td>
<td className={css(style.numericCell)}>
{profile.formatValue(selfWeight)} ({formatPercent(selfPerc)})
<HBarDisplay perc={selfPerc} />
</td>
<td title={frame.file} className={css(style.textCell)}>
<ColorChit color={this.props.getCSSColorForFrame(frame)} />
{frame.name}
</td>
</tr>
)
}
onSortClick = (field: SortField, ev: MouseEvent) => {
ev.preventDefault()
const {sortMethod} = this.props
if (sortMethod.field == field) {
// Toggle
this.props.setSortMethod({
field,
direction:
sortMethod.direction === SortDirection.ASCENDING
? SortDirection.DESCENDING
: SortDirection.ASCENDING,
})
} else {
// Set a sane default
switch (field) {
case SortField.SYMBOL_NAME: {
this.props.setSortMethod({field, direction: SortDirection.ASCENDING})
break
}
case SortField.SELF: {
this.props.setSortMethod({field, direction: SortDirection.DESCENDING})
break
}
case SortField.TOTAL: {
this.props.setSortMethod({field, direction: SortDirection.DESCENDING})
break
}
}
}
}
private getFrameList = (): Frame[] => {
const {profile, sortMethod} = this.props
const frameList: Frame[] = []
profile.forEachFrame(f => frameList.push(f))
// TODO(jlfwong): This is pretty inefficient to do this on every render, but doesn't
// seem to be a bottleneck, so we'll leave it alone.
switch (sortMethod.field) {
case SortField.SYMBOL_NAME: {
sortBy(frameList, f => f.name.toLowerCase())
break
}
case SortField.SELF: {
sortBy(frameList, f => f.getSelfWeight())
break
}
case SortField.TOTAL: {
sortBy(frameList, f => f.getTotalWeight())
break
}
}
if (sortMethod.direction === SortDirection.DESCENDING) {
frameList.reverse()
}
return frameList
}
private listView: ScrollableListView | null = null
private listViewRef = (listView: ScrollableListView | null) => {
if (listView === this.listView) return
this.listView = listView
const {selectedFrame} = this.props
if (!selectedFrame || !listView) return
const index = this.getFrameList().indexOf(selectedFrame)
if (index === -1) return
listView.scrollIndexIntoView(index)
}
render() {
const {sortMethod} = this.props
const frameList = this.getFrameList()
const renderItems = (firstIndex: number, lastIndex: number) => {
const rows: JSX.Element[] = []
for (let i = firstIndex; i <= lastIndex; i++) {
rows.push(this.renderRow(frameList[i], i))
}
return <table className={css(style.tableView)}>{rows}</table>
}
const listItems: ListItem[] = frameList.map(f => ({size: Sizes.FRAME_HEIGHT}))
return (
<div className={css(commonStyle.vbox, style.profileTableView)}>
<table className={css(style.tableView)}>
<thead className={css(style.tableHeader)}>
<tr>
<th
className={css(style.numericCell)}
onClick={ev => this.onSortClick(SortField.TOTAL, ev)}
>
<SortIcon
activeDirection={
sortMethod.field === SortField.TOTAL ? sortMethod.direction : null
}
/>
Total
</th>
<th
className={css(style.numericCell)}
onClick={ev => this.onSortClick(SortField.SELF, ev)}
>
<SortIcon
activeDirection={
sortMethod.field === SortField.SELF ? sortMethod.direction : null
}
/>
Self
</th>
<th
className={css(style.textCell)}
onClick={ev => this.onSortClick(SortField.SYMBOL_NAME, ev)}
>
<SortIcon
activeDirection={
sortMethod.field === SortField.SYMBOL_NAME ? sortMethod.direction : null
}
/>
Symbol Name
</th>
</tr>
</thead>
</table>
<ScrollableListView
ref={this.listViewRef}
axis={'y'}
items={listItems}
className={css(style.scrollView)}
renderItems={renderItems}
/>
</div>
)
}
}
const style = StyleSheet.create({
profileTableView: {
background: Colors.WHITE,
height: '100%',
},
scrollView: {
overflowY: 'auto',
overflowX: 'hidden',
},
tableView: {
width: '100%',
fontSize: FontSize.LABEL,
background: Colors.WHITE,
},
tableHeader: {
borderBottom: `2px solid ${Colors.LIGHT_GRAY}`,
textAlign: 'left',
color: Colors.GRAY,
userSelect: 'none',
},
sortIcon: {
position: 'relative',
top: 1,
marginRight: Sizes.FRAME_HEIGHT / 4,
},
tableRow: {
height: Sizes.FRAME_HEIGHT,
},
tableRowEven: {
background: Colors.OFF_WHITE,
},
tableRowSelected: {
background: Colors.DARK_BLUE,
color: Colors.WHITE,
},
numericCell: {
textOverflow: 'ellipsis',
overflow: 'hidden',
whiteSpace: 'nowrap',
position: 'relative',
textAlign: 'right',
paddingRight: Sizes.FRAME_HEIGHT,
width: 6 * Sizes.FRAME_HEIGHT,
minWidth: 6 * Sizes.FRAME_HEIGHT,
},
textCell: {
textOverflow: 'ellipsis',
overflow: 'hidden',
whiteSpace: 'nowrap',
width: '100%',
maxWidth: 0,
},
hBarDisplay: {
position: 'absolute',
background: Colors.TRANSPARENT_GREEN,
bottom: 2,
height: 2,
width: `calc(100% - ${2 * Sizes.FRAME_HEIGHT}px)`,
right: Sizes.FRAME_HEIGHT,
},
hBarDisplayFilled: {
height: '100%',
position: 'absolute',
background: Colors.GREEN,
right: 0,
},
})
interface ProfileTableViewContainerProps {
activeProfileState: ActiveProfileState
}
export const ProfileTableViewContainer = createContainer(
ProfileTableView,
(state: ApplicationState, dispatch: Dispatch, ownProps: ProfileTableViewContainerProps) => {
const {activeProfileState} = ownProps
const {profile, sandwichViewState, index} = activeProfileState
if (!profile) throw new Error('profile missing')
const {tableSortMethod} = state
const {callerCallee} = sandwichViewState
const selectedFrame = callerCallee ? callerCallee.selectedFrame : null
const frameToColorBucket = getFrameToColorBucket(profile)
const getCSSColorForFrame = createGetCSSColorForFrame(frameToColorBucket)
const setSelectedFrame = (selectedFrame: Frame | null) => {
dispatch(actions.sandwichView.setSelectedFrame({profileIndex: index, args: selectedFrame}))
}
const setSortMethod = (sortMethod: SortMethod) => {
dispatch(actions.sandwichView.setTableSortMethod(sortMethod))
}
return {
profile,
profileIndex: activeProfileState.index,
selectedFrame,
getCSSColorForFrame,
sortMethod: tableSortMethod,
setSelectedFrame,
setSortMethod,
}
},
)