10000 docs: views API: support setting content view by zcbenz · Pull Request #254 · electron/governance · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

docs: views API: support setting content view #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions wg-api/spec-documents/views-api-1-content-view.md
6DED
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Views API (Part 1): Support Setting Content View

## Summary

This spec proposes to make parts of `views` related APIs public, which should be
enough to implement a simple splash screen for apps.

(`views` is the name of Chromium's UI library.)

## Platforms

All.

## Impetus

I would like to extend Electron's APIs to provide ability to build native UI,
but it would too much work to do it all at once. So I plan to do it piece by
piece, and with each addition the apps should be able to do something that was
impossible before.

With this spec, users would be able to change the content view of windows. One
possible use is to show a splash image when the app is loading, and change the
splash image to WebContents after loading is finished.

```js
const {app, nativeImage, webContents, ImageView, TopLevelWindow, WebContentsView} = require('electron')

app.once('ready', () => {
const win = new TopLevelWindow({})
const imageView = new ImageView()
imageView.setImage(__dirname + '/splash.png')
win.setContentView(imageView)

const web = new WebContentsView({})
web.webContents.once('did-finish-load', () => win.setContentView(web))
web.webContents.loadURL('https://github.com/')
})

app.on('window-all-closed', () => {
app.quit()
})
```

## API Design

There are several new APIs in this spec, most of them are actually internal
APIs that already being used. The main work would be documenting them and making
them public.

### `TopLevelWindow`

The `TopLevelWindow` class represents a native window, which implements all
window related APIs and has an empty content view.

The `BrowserWindow` class inherits from `TopLevelWindow` and uses
`WebContentsView` as its content view.

### `TopLevelWindow.setContentView(view)`

This changes the content view of the window.

The `view` is an instance of `View` class, with this spec there will be 2 types
of `View`: `WebContentsView` and `ImageView`, and they inherit from the `View`
class.

### `TopLevelWindow.getContentView()`

Returns the content view of the window. For `BrowserWindow` it would be a
`WebContentsView`.

### `View`

The base class for native UI elements. In following specs I will add ability to
add child views, in this spec it does not have any ability.

### `ImageView`

A native UI element that displays image.

Currently it only displays static image, an we may want to add ability to
display GIF animations in future.

### `WebContentsView`

A native UI element that displays the content of `WebContents`.

### `new WebContentsView(webPreferences)`

Create an instance of `WebContentsView` class, the `webPreferences` is the same
options that passed to `BrowserWindow`.

### `WebContentsView.webContents`

Returns the WebContents hosted by this view.

## Rollout Plan

This will live in Electron `v10.0.0` and beyond.
0