Refactor hasScrollbarGutter into a function

the code to determine hasScrollbarGutter can run while the document.body is not loaded yet.
setting it as a function will trigger it when the actual UI is loaded, and document.body should exist.
This commit is contained in:
Ron Lavi 2022-12-25 22:21:16 +02:00
parent 3553a451d8
commit b180a4f4ba
2 changed files with 23 additions and 21 deletions

View File

@ -1335,7 +1335,7 @@ const UI = {
let brokenScrollbars = false; let brokenScrollbars = false;
if (!hasScrollbarGutter) { if (!hasScrollbarGutter()) {
if (isIOS() || isAndroid() || isMac() || isChromeOS()) { if (isIOS() || isAndroid() || isMac() || isChromeOS()) {
brokenScrollbars = true; brokenScrollbars = true;
} }

View File

@ -45,30 +45,32 @@ try {
export const supportsCursorURIs = _supportsCursorURIs; export const supportsCursorURIs = _supportsCursorURIs;
let _hasScrollbarGutter = true; export const hasScrollbarGutter = () => {
try { let _hasScrollbarGutter = true;
// Create invisible container try {
const container = document.createElement('div'); // Create invisible container
container.style.visibility = 'hidden'; const container = document.createElement('div');
container.style.overflow = 'scroll'; // forcing scrollbars container.style.visibility = 'hidden';
document.body.appendChild(container); container.style.overflow = 'scroll'; // forcing scrollbars
document.body.appendChild(container);
// Create a div and place it in the container // Create a div and place it in the container
const child = document.createElement('div'); const child = document.createElement('div');
container.appendChild(child); container.appendChild(child);
// Calculate the difference between the container's full width // Calculate the difference between the container's full width
// and the child's width - the difference is the scrollbars // and the child's width - the difference is the scrollbars
const scrollbarWidth = (container.offsetWidth - child.offsetWidth); const scrollbarWidth = (container.offsetWidth - child.offsetWidth);
// Clean up // Clean up
container.parentNode.removeChild(container); container.parentNode.removeChild(container);
_hasScrollbarGutter = scrollbarWidth != 0; _hasScrollbarGutter = scrollbarWidth != 0;
} catch (exc) { } catch (exc) {
Log.Error("Scrollbar test exception: " + exc); Log.Error("Scrollbar test exception: " + exc);
} }
export const hasScrollbarGutter = _hasScrollbarGutter; return _hasScrollbarGutter;
};
/* /*
* The functions for detection of platforms and browsers below are exported * The functions for detection of platforms and browsers below are exported