Show a part of view with scrollbar.
The ScrollView can show an arbitrary content view inside it. It is used to make
any View scrollable. When the content is larger than the ScrollView,
scrollbars will be optionally showed. When the content view is smaller
then the ScrollView, the content view will be resized to the size of the
ScrollView.
The scrollview supports keyboard UI and mousewheel.
It extends BaseView.
Create and control scroll views.
Process: Main
// In the main process.
const path = require("path");
const { app, BrowserView, BaseWindow, ContainerView, ScrollView, WrapperBrowserView } = require("electron");
const WEBVIEW_WIDTH = 600;
const GAP = 30;
const URLS = [
"https://theverge.com/",
"https://mashable.com",
"https://www.businessinsider.com",
"https://wired.com",
"https://macrumors.com",
"https://gizmodo.com",
"https://maps.google.com/",
"https://sheets.google.com/",
];
global.win = null;
app.whenReady().then(() => {
win = new BaseWindow({ autoHideMenuBar: true, width: 1400, height: 700 });
// The content view.
const contentView = new ContainerView();
contentView.setBackgroundColor("#1F2937");
win.setContentBaseView(contentView);
contentView.setBounds({x: 0, y: 0, width: 1378, height: 600});
// Scroll
const scroll = new ScrollView();
scroll.setHorizontalScrollBarMode("enabled");
scroll.setVerticalScrollBarMode("disabled");
contentView.addChildView(scroll);
scroll.setBounds({x: 0, y: 0, width: 1377, height: 600});
// Scroll content
const scrollContent = new ContainerView();
scroll.setContentView(scrollContent);
scrollContent.setBounds({x: 0, y: 0, width: URLS.length * (WEBVIEW_WIDTH + GAP), height: 600});
for (var i = 1; i <= URLS.length; i++) {
const browserView = new BrowserView();
browserView.setBackgroundColor("#ffffff");
const wrapperBrowserView = new WrapperBrowserView({ 'browserView': browserView });
const webContentView = new ContainerView();
webContentView.addChildView(wrapperBrowserView);
wrapperBrowserView.setBounds({x: 0, y: 0, width: 600, height: 540});
scrollContent.addChildView(webContentView);
webContentView.setBounds({x: i*(WEBVIEW_WIDTH + GAP)+GAP, y: 30, width: 600, height: 540});
browserView.webContents.loadURL(URLS[i-1]);
}
});optionsObject (optional)horizontalScrollBarModestring (optional) - Can bedisabled,enabled-but-hidden,enabled. Default isenabled.verticalScrollBarModestring (optional) - Can bedisabled,enabled-but-hidden,enabled. Default isenabled.
Creates the new scroll view.
Objects created with new ScrollView emit the following events:
Returns:
eventEvent
Emitted when the content view is being scrolled.
Returns:
eventEvent
Emitted at the beginning of user-initiated live scroll tracking (gesture scroll or scroller tracking, for example, thumb dragging).
Returns:
eventEvent
Emitted after changing the clipview bounds origin due to a user-initiated event.
Returns:
eventEvent
Emitted at the end of live scroll tracking.
Returns:
eventEventmouseEventboolean - Set totrueif generated by the mouse.scrollingDeltaXFloat - The scroll wheel’s horizontal delta.scrollingDeltaYFloat - The scroll wheel’s vertical delta.phasestring - The phase of a gesture event.momentumPhasestring - The momentum phase for a scroll or flick gesture.
Emitted when the mouse’s scroll wheel has moved.
Both phase and momentumPhase can be one of the following values:
none- The event is not associated with a phase.began- An event phase has begun.stationary- An event phase is in progress but hasn't moved since the previous event.changed- An event phase has changed.ended- The event phase ended.cancelled- The system canceled the event phase.mayBegin- The system event phase may begin.
A gesture phase corresponds to a fluid gesture event. As a gesture event occurs,
its phase begins with began and ends with either ended or canceled.
All the gesture events are sent to the view under the cursor when the began occurred.
Technically, a gesture scroll event starts with a began phase and ends with a
ended. However, when the user puts two fingers down on a trackpad, the
trackpad issues mayBegin, followed by began, cancelled, or ended.
The mayBegin event phase signals that scrolling is about to begin before the
gesture has technically started. A Magic Mouse does not issue mayBegin scroll
wheel events.
Legacy scroll wheel events (say from a Mighty Mouse) and momentum scroll wheel
events both have a phase of none. (Legacy scroll wheel events also have
a momentumPhase of none.)
The momentumPhase helps you detect momentum scrolling, in which the hardware
continues to issue scroll wheel events even though the user is no longer
physically scrolling. Devices such as Magic Mouse and Multi-Touch trackpad
enable momentum scrolling.
During non momentum scrolling, each scroll-wheel event is routed to the view
that is beneath the pointer for that event. In non momentum scroll-wheel
events, momentumPhase has the value none.
During momentum scrolling, each scroll-wheel event is routed to the view
that was beneath the pointer when momentum scrolling started. In momentum
scroll-wheel events, phase has the value none. When the device switches
from user-performed scroll events to momentum scroll wheel events,
momentumPhase is set to began. For subsequent momentum scroll wheel events,
momentumPhase is set to changed until the momentum subsides, or the user
stops the momentum scrolling; the final momentum scroll wheel event has a
momentumPhase value of ended.
It’s important to understand how the difference in the event-routing behavior
can affect the events you receive, depending on the scrolling device users are
using. When using a Multi-Touch trackpad, users must physically stop scrolling
before they can move the pointer, so you won’t receive any mouse-movement
events during a scroll. In contrast, a Magic Mouse lets users move
the pointer and click in the middle of a scroll. Because a scroll doesn’t end
until the user lifts their finger, any additional movement by a finger resting
on a Magic Mouse is still considered to be part of the original scroll, which
means that the event’s phase property is changed and not began, as you
might expect. If you process scroll-wheel events directly, be prepared to
receive mouse-movement events (such as leftMouseDown, rightMouseDragged,
and otherMouseUp) between began and ended when a Magic Mouse is in use.
The ScrollView class has the following static methods:
Returns ScrollView[] - An array of all created scroll views.
idInteger
Returns ScrollView | null - The scroll view with the given id.
Objects created with new ScrollView have the following instance methods:
contentsBaseView
Set the contents. The contents is the view that needs to scroll.
Returns BaseView - The contents of the view.
sizeSize
Set the size of the contents.
Returns Size - The size of the contents.
modestring - Can bedisabled,enabled-but-hidden,enabled. Default isenabled.
Controls how the horizontal scroll bar appears and functions.
disabled- The scrollbar is hidden, and the pane will not respond to e.g. mousewheel events even if the contents are larger than the viewport.enabled-but-hidden- The scrollbar is hidden whether or not the contents are larger than the viewport, but the pane will respond to scroll events. *enabled- The scrollbar will be visible if the contents are larger than the viewport and the pane will respond to scroll events.
Returns string - horizontal scrollbar mode.
modestring - Can bedisabled,enabled-but-hidden,enabled. Default isenabled.
Controls how the vertical scroll bar appears and functions.
disabled- The scrollbar is hidden, and the pane will not respond to e.g. mousewheel events even if the contents are larger than the viewport.enabled-but-hidden- The scrollbar is hidden whether or not the contents are larger than the viewport, but the pane will respond to scroll events. *enabled- The scrollbar will be visible if the contents are larger than the viewport and the pane will respond to scroll events.
Returns string - vertical scrollbar mode.
elasticitystring - Can beautomatic,none,allowed. Default isautomatic.
The scroll view’s horizontal scrolling elasticity mode.
A scroll view can scroll its contents past its bounds to achieve an elastic effect.
When set to automatic, scrolling the horizontal axis beyond its document
bounds only occurs if the document width is greater than the view width,
or the vertical scroller is hidden and the horizontal scroller is visible.
automatic- Automatically determine whether to allow elasticity on this axis.none- Disallow scrolling beyond document bounds on this axis.allowed- Allow content to be scrolled past its bounds on this axis in an elastic fashion.
Returns string - The scroll view’s horizontal scrolling elasticity mode.
elasticitystring - Can beautomatic,none,allowed. Default isautomatic.
The scroll view’s vertical scrolling elasticity mode.
Returns string - The scroll view’s vertical scrolling elasticity mode.
pointPoint - The point in thecontentViewto scroll to.
Scroll to the horizontal (point.x) and vertical (point.y) position.
Returns Point - The horizontal and vertical scroll position.
Returns Point - The maximum horizontal and vertical scroll position.
overlayboolean - Sets the scroller style used by the scroll view.
Returns boolean - The scroller style used by the scroll view.
enableboolean - Whether the scroll events are enabled. Default isfalse.
Returns boolean - Whether the scroll events are enabled.
swapboolean - Whether the mouse wheel should scroll horizontally. Default isfalse.
Swaps the behaviour of mouse wheel.
Returns boolean - Whether the mouse wheel scrolls horizontally.
factorDouble - Scrolling factor used for mouse wheel. Default is 1.0.
Returns Double - Scrolling factor used for mouse wheel.
minHeightInteger - The min height for the bounded scroll view.maxHeightInteger - The max height for the bounded scroll view.
Turns this scroll view into a bounded scroll view, with a fixed height. By default, a ScrollView will stretch to fill its outer container.
Returns Integer - The min height for the bounded scroll view.
This is negative value if the view is not bounded.
Returns Integer - The max height for the bounded scroll view.
This is negative value if the view is not bounded.
Returns Rectangle - The visible region of the content View.
allowboolean - Whether the left/right/up/down arrow keys attempt to scroll the view.
Returns boolean - Gets whether the keyboard arrow keys attempt to scroll the view. Default is true.
indicatorboolean - Whether to draw a white separator on the four sides of the scroll view when it overflows.
Returns boolean - Gets whether to draw a white separator on the four sides of the scroll view when it overflows. Default is true.