View events

This commit is contained in:
Jeremy Rose
2022-09-21 20:10:06 -05:00
parent e50312f7f7
commit c26f8701b4
5 changed files with 27 additions and 2 deletions

View File

@@ -23,6 +23,14 @@ Process: [Main](../glossary.md#main-process)
Creates a new `View`.
### Instance Events
Objects created with `new View` emit the following events:
#### Event: 'bounds-changed'
**TODO**
### Instance Methods
Objects created with `new View` have the following instance methods:

View File

@@ -1,3 +1,6 @@
import { EventEmitter } from 'events';
const { View } = process._linkedBinding('electron_browser_view');
Object.setPrototypeOf((View as any).prototype, EventEmitter.prototype);
export default View;

View File

@@ -143,6 +143,10 @@ require('@electron/internal/browser/api/web-contents');
// Load web-frame-main module to ensure it is populated on app ready
require('@electron/internal/browser/api/web-frame-main');
// Required because `new BrowserWindow` calls some WebContentsView stuff, so
// the inheritance needs to be set up before that happens.
require('@electron/internal/browser/api/web-contents-view');
// Set main startup script of the app.
const mainStartupScript = packageJson.main || 'index.js';

View File

@@ -161,11 +161,13 @@ class JSLayoutManager : public views::LayoutManagerBase {
View::View(views::View* view) : view_(view) {
view_->set_owned_by_client();
view_->AddObserver(this);
}
View::View() : View(new views::View()) {}
View::~View() {
view_->RemoveObserver(this);
if (delete_view_)
delete view_;
}
@@ -257,6 +259,10 @@ std::vector<v8::Local<v8::Value>> View::GetChildren() {
void View::SetBackgroundColor(absl::optional<WrappedSkColor> color) {
view()->SetBackground(color ? views::CreateSolidBackground(*color) : nullptr);
}
void View::OnViewBoundsChanged(views::View* observed_view) {
Emit("bounds-changed");
}
#endif
// static

View File

@@ -10,13 +10,14 @@
#include "electron/buildflags/buildflags.h"
#include "gin/handle.h"
#include "shell/common/color_util.h"
#include "shell/common/gin_helper/wrappable.h"
#include "shell/common/gin_helper/event_emitter.h"
#include "ui/views/view.h"
#include "ui/views/view_observer.h"
#include "v8/include/v8-value.h"
namespace electron::api {
class View : public gin_helper::Wrappable<View> {
class View : public gin_helper::EventEmitter<View>, public views::ViewObserver {
public:
static gin_helper::WrappableBase* New(gin::Arguments* args);
static gin::Handle<View> Create(v8::Isolate* isolate);
@@ -37,6 +38,9 @@ class View : public gin_helper::Wrappable<View> {
void SetLayout(v8::Isolate* isolate, v8::Local<v8::Object> value);
std::vector<v8::Local<v8::Value>> GetChildren();
void SetBackgroundColor(absl::optional<WrappedSkColor> color);
// views::ViewObserver
void OnViewBoundsChanged(views::View* observed_view) override;
#endif
views::View* view() const { return view_; }