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
|
import React, { useCallback, useContext, useLayoutEffect, useRef, useState } from 'react';
import { useSelector } from 'react-redux';
import { useHistory } from 'react-router';
import { colors } from '../../config.json';
import { messages } from '../../shared/gettext';
import useActions from '../lib/actionsHook';
import { useCombinedRefs } from '../lib/utilityHooks';
import { IReduxState } from '../redux/store';
import userInterface from '../redux/userinterface/actions';
import CustomScrollbars, { IScrollEvent } from './CustomScrollbars';
import {
StyledBackBarItemButton,
StyledBackBarItemIcon,
StyledBackBarItemLabel,
StyledCloseBarItemButton,
StyledCloseBarItemIcon,
StyledNavigationBar,
StyledNavigationBarSeparator,
StyledNavigationBarWrapper,
StyledTitleBarItemContainer,
StyledTitleBarItemLabel,
StyledTitleBarItemMeasuringLabel,
} from './NavigationBarStyles';
export { StyledNavigationItems as NavigationItems } from './NavigationBarStyles';
interface INavigationContainerProps {
children?: React.ReactNode;
}
interface INavigationContainerState {
showsBarTitle: boolean;
showsBarSeparator: boolean;
}
const NavigationScrollContext = React.createContext({
showsBarTitle: false,
showsBarSeparator: false,
onScroll(_event: IScrollEvent): void {
throw Error('NavigationScrollContext provider missing');
},
});
export class NavigationContainer extends React.Component<
INavigationContainerProps,
INavigationContainerState
> {
public state = {
showsBarTitle: false,
showsBarSeparator: false,
};
private scrollEventListeners: Array<(event: IScrollEvent) => void> = [];
public componentDidMount() {
this.updateBarAppearance({ scrollLeft: 0, scrollTop: 0 });
}
public render() {
return (
<NavigationScrollContext.Provider
value={{
...this.state,
onScroll: this.onScroll,
}}>
{this.props.children}
</NavigationScrollContext.Provider>
);
}
public onScroll = (event: IScrollEvent) => {
this.notifyScrollEventListeners(event);
this.updateBarAppearance(event);
};
public addScrollEventListener(fn: (event: IScrollEvent) => void) {
const index = this.scrollEventListeners.indexOf(fn);
if (index === -1) {
this.scrollEventListeners.push(fn);
}
}
public removeScrollEventListener(fn: (event: IScrollEvent) => void) {
const index = this.scrollEventListeners.indexOf(fn);
if (index !== -1) {
this.scrollEventListeners.splice(index, 1);
}
}
private notifyScrollEventListeners(event: IScrollEvent) {
this.scrollEventListeners.forEach((listener) => listener(event));
}
private updateBarAppearance(event: IScrollEvent) {
// that's where SettingsHeader.HeaderTitle intersects the navigation bar
const showsBarSeparator = event.scrollTop > 11;
// that's when SettingsHeader.HeaderTitle goes behind the navigation bar
const showsBarTitle = event.scrollTop > 20;
if (
this.state.showsBarSeparator !== showsBarSeparator ||
this.state.showsBarTitle !== showsBarTitle
) {
this.setState({ showsBarSeparator, showsBarTitle });
}
}
}
interface INavigationScrollbarsProps {
onScroll?: (value: IScrollEvent) => void;
className?: string;
fillContainer?: boolean;
children?: React.ReactNode;
}
export const NavigationScrollbars = React.forwardRef(function NavigationScrollbarsT(
props: INavigationScrollbarsProps,
forwardedRef?: React.Ref<CustomScrollbars>,
) {
const history = useHistory();
const { onScroll } = useContext(NavigationScrollContext);
const ref = useRef<CustomScrollbars>();
const combinedRefs = useCombinedRefs(forwardedRef, ref);
const { addScrollPosition, removeScrollPosition } = useActions(userInterface);
const scrollPosition = useSelector(
(state: IReduxState) => state.userInterface.scrollPosition[history.location.pathname],
);
useLayoutEffect(() => {
const path = history.location.pathname;
if (history.action === 'POP' && scrollPosition) {
ref.current?.scrollTo(...scrollPosition);
removeScrollPosition(path);
}
return () => {
if (history.action === 'PUSH' && ref.current) {
addScrollPosition(path, ref.current.getScrollPosition());
}
};
}, []);
const handleScroll = useCallback((event: IScrollEvent) => {
onScroll(event);
props.onScroll?.(event);
}, []);
return (
<CustomScrollbars
ref={combinedRefs}
className={props.className}
fillContainer={props.fillContainer}
onScroll={handleScroll}>
{props.children}
</CustomScrollbars>
);
});
const TitleBarItemContext = React.createContext({
titleAdjustment: 0,
visible: false,
get titleContainerRef(): React.RefObject<HTMLDivElement> {
throw Error('Missing TitleBarItemContext provider');
},
get measuringTitleRef(): React.RefObject<HTMLHeadingElement> {
throw Error('Missing TitleBarItemContext provider');
},
});
interface INavigationBarProps {
children?: React.ReactNode;
alwaysDisplayBarTitle?: boolean;
}
export const NavigationBar = function NavigationBarT(props: INavigationBarProps) {
const { showsBarSeparator, showsBarTitle } = useContext(NavigationScrollContext);
const unpinnedWindow = useSelector(
(state: IReduxState) => state.settings.guiSettings.unpinnedWindow,
);
const [titleAdjustment, setTitleAdjustment] = useState(0);
const titleContainerRef = useRef() as React.RefObject<HTMLDivElement>;
const measuringTitleRef = useRef() as React.RefObject<HTMLHeadingElement>;
const navigationBarRef = useRef() as React.RefObject<HTMLDivElement>;
useLayoutEffect(() => {
const titleContainerRect = titleContainerRef.current?.getBoundingClientRect();
const measuringTitleRect = measuringTitleRef.current?.getBoundingClientRect();
const navigationBarRect = navigationBarRef.current?.getBoundingClientRect();
if (titleContainerRect && measuringTitleRect && navigationBarRect) {
// calculate the width of the elements preceding the title view container
const leadingSpace = titleContainerRect.x - navigationBarRect.x;
// calculate the width of the elements succeeding the title view container
const trailingSpace = navigationBarRect.width - titleContainerRect.width - leadingSpace;
// calculate the adjustment needed to center the title view within navigation bar
const titleAdjustment = Math.floor(trailingSpace - leadingSpace);
// calculate the maximum possible adjustment that when applied should keep the text fully
// visible, unless the title container itself is smaller than the space needed to accommodate
// the text
const maxTitleAdjustment = Math.floor(
Math.max(titleContainerRect.width - measuringTitleRect.width, 0),
);
// cap the adjustment to remain within the allowed bounds
const cappedTitleAdjustment = Math.min(
Math.max(-maxTitleAdjustment, titleAdjustment),
maxTitleAdjustment,
);
setTitleAdjustment(cappedTitleAdjustment);
}
});
return (
<StyledNavigationBar unpinnedWindow={unpinnedWindow}>
<StyledNavigationBarWrapper ref={navigationBarRef}>
<TitleBarItemContext.Provider
value={{
titleAdjustment: titleAdjustment,
visible: props.alwaysDisplayBarTitle || showsBarTitle,
titleContainerRef,
measuringTitleRef,
}}>
{props.children}
</TitleBarItemContext.Provider>
</StyledNavigationBarWrapper>
{showsBarSeparator && <StyledNavigationBarSeparator />}
</StyledNavigationBar>
);
};
interface ITitleBarItemProps {
children?: React.ReactText;
}
export const TitleBarItem = React.memo(function TitleBarItemT(props: ITitleBarItemProps) {
const { measuringTitleRef, titleAdjustment, titleContainerRef, visible } = useContext(
TitleBarItemContext,
);
return (
<StyledTitleBarItemContainer ref={titleContainerRef}>
<StyledTitleBarItemLabel titleAdjustment={titleAdjustment} visible={visible}>
{props.children}
</StyledTitleBarItemLabel>
<StyledTitleBarItemMeasuringLabel
titleAdjustment={0}
ref={measuringTitleRef}
aria-hidden={true}>
{props.children}
</StyledTitleBarItemMeasuringLabel>
</StyledTitleBarItemContainer>
);
});
interface ICloseBarItemProps {
action: () => void;
}
export function CloseBarItem(props: ICloseBarItemProps) {
// Use the arrow down icon on Linux, to avoid confusion with the close button in the window
// title bar.
const unpinnedWindow = useSelector(
(state: IReduxState) => state.settings.guiSettings.unpinnedWindow,
);
const iconName = unpinnedWindow ? 'icon-close-down' : 'icon-close';
return (
<StyledCloseBarItemButton aria-label={messages.gettext('Close')} onClick={props.action}>
<StyledCloseBarItemIcon
height={24}
width={24}
source={iconName}
tintColor={colors.white60}
tintHoverColor={colors.white80}
/>
</StyledCloseBarItemButton>
);
}
interface IBackBarItemProps {
children?: React.ReactText;
action: () => void;
}
export function BackBarItem(props: IBackBarItemProps) {
return (
<StyledBackBarItemButton onClick={props.action}>
<StyledBackBarItemIcon source="icon-back" tintColor={colors.white60} />
<StyledBackBarItemLabel>{props.children}</StyledBackBarItemLabel>
</StyledBackBarItemButton>
);
}
|