* feat: allow View::SetBounds to animate * fix: support width/height animations * fix: jumping on subsequent animations * fix: segfault race condition * fix: remove layer background * fix: layer clips not being reset * refactor: use gfx tween gin converter * fix: layer cleanups causing flickering views * chore: merge artifact * fix: missing private method in header * fix: return type * fix: do not set layer opacity * refactor: update animate parameter format * refactor: move animate into options object * chore: typo * docs: update * spec: add view animation test
4.1 KiB
View
Create and layout native views.
Process: Main
This module cannot be used until the ready event of the app
module is emitted.
const { BaseWindow, View } = require('electron')
const win = new BaseWindow()
const view = new View()
view.setBackgroundColor('red')
view.setBounds({ x: 0, y: 0, width: 100, height: 100 })
win.contentView.addChildView(view)
Class: View
A basic native view.
Process: Main
View is an EventEmitter.
Warning
Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.
new View()
Creates a new View.
Instance Events
Objects created with new View emit the following events:
Event: 'bounds-changed'
Emitted when the view's bounds have changed in response to being laid out. The
new bounds can be retrieved with view.getBounds().
Instance Methods
Objects created with new View have the following instance methods:
view.addChildView(view[, index])
viewView - Child view to add.indexInteger (optional) - Index at which to insert the child view. Defaults to adding the child at the end of the child list.
If the same View is added to a parent which already contains it, it will be reordered such that it becomes the topmost view.
view.removeChildView(view)
viewView - Child view to remove.
If the view passed as a parameter is not a child of this view, this method is a no-op.
view.setBounds(bounds[, options])
boundsRectangle - New bounds of the View.optionsObject (optional) - Options for setting the bounds.animateboolean | Object (optional) - If true, the bounds change will be animated. If an object is passed, it can contain the following properties:durationInteger (optional) - Duration of the animation in milliseconds. Default is250.easingstring (optional) - Easing function for the animation. Default islinear.linearease-inease-outease-in-out
view.getBounds()
Returns Rectangle - The bounds of this View, relative to its parent.
view.setBackgroundColor(color)
colorstring - Color in Hex, RGB, ARGB, HSL, HSLA or named CSS color format. The alpha channel is optional for the hex type.
Examples of valid color values:
- Hex
#fff(RGB)#ffff(ARGB)#ffffff(RRGGBB)#ffffffff(AARRGGBB)
- RGB
rgb\(([\d]+),\s*([\d]+),\s*([\d]+)\)- e.g.
rgb(255, 255, 255)
- e.g.
- RGBA
rgba\(([\d]+),\s*([\d]+),\s*([\d]+),\s*([\d.]+)\)- e.g.
rgba(255, 255, 255, 1.0)
- e.g.
- HSL
hsl\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%\)- e.g.
hsl(200, 20%, 50%)
- e.g.
- HSLA
hsla\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%,\s*([\d.]+)\)- e.g.
hsla(200, 20%, 50%, 0.5)
- e.g.
- Color name
- Options are listed in SkParseColor.cpp
- Similar to CSS Color Module Level 3 keywords, but case-sensitive.
- e.g.
bluevioletorred
- e.g.
Note
Hex format with alpha takes
AARRGGBBorARGB, notRRGGBBAAorRGB.
view.setBorderRadius(radius)
radiusInteger - Border radius size in pixels.
Note
The area cutout of the view's border still captures clicks.
view.setVisible(visible)
visibleboolean - If false, the view will be hidden from display.
view.getVisible()
Returns boolean - Whether the view should be drawn. Note that this is
different from whether the view is visible on screen—it may still be obscured
or out of view.
Instance Properties
Objects created with new View have the following properties:
view.children Readonly
A View[] property representing the child views of this view.