Added 'show' & 'hide' events to browser-window, fixed visibilitychange event in renderer
This commit is contained in:
parent
ee61ab2d26
commit
c1267b2320
8 changed files with 88 additions and 60 deletions
|
@ -191,6 +191,14 @@ void Window::OnWindowFocus() {
|
||||||
Emit("focus");
|
Emit("focus");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Window::OnWindowShow() {
|
||||||
|
Emit("show");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Window::OnWindowHide() {
|
||||||
|
Emit("hide");
|
||||||
|
}
|
||||||
|
|
||||||
void Window::OnWindowMaximize() {
|
void Window::OnWindowMaximize() {
|
||||||
Emit("maximize");
|
Emit("maximize");
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,6 +58,8 @@ class Window : public mate::TrackableObject<Window>,
|
||||||
void OnWindowClosed() override;
|
void OnWindowClosed() override;
|
||||||
void OnWindowBlur() override;
|
void OnWindowBlur() override;
|
||||||
void OnWindowFocus() override;
|
void OnWindowFocus() override;
|
||||||
|
void OnWindowShow() override;
|
||||||
|
void OnWindowHide() override;
|
||||||
void OnWindowMaximize() override;
|
void OnWindowMaximize() override;
|
||||||
void OnWindowUnmaximize() override;
|
void OnWindowUnmaximize() override;
|
||||||
void OnWindowMinimize() override;
|
void OnWindowMinimize() override;
|
||||||
|
|
|
@ -210,6 +210,8 @@ class NativeWindow : public base::SupportsUserData,
|
||||||
void NotifyWindowClosed();
|
void NotifyWindowClosed();
|
||||||
void NotifyWindowBlur();
|
void NotifyWindowBlur();
|
||||||
void NotifyWindowFocus();
|
void NotifyWindowFocus();
|
||||||
|
void NotifyWindowShow();
|
||||||
|
void NotifyWindowHide();
|
||||||
void NotifyWindowMaximize();
|
void NotifyWindowMaximize();
|
||||||
void NotifyWindowUnmaximize();
|
void NotifyWindowUnmaximize();
|
||||||
void NotifyWindowMinimize();
|
void NotifyWindowMinimize();
|
||||||
|
|
|
@ -42,6 +42,12 @@ class NativeWindowObserver {
|
||||||
// Called when window gains focus.
|
// Called when window gains focus.
|
||||||
virtual void OnWindowFocus() {}
|
virtual void OnWindowFocus() {}
|
||||||
|
|
||||||
|
// Called when window is shown.
|
||||||
|
virtual void OnWindowShow() {}
|
||||||
|
|
||||||
|
// Called when window is hidden.
|
||||||
|
virtual void OnWindowHide() {}
|
||||||
|
|
||||||
// Called when window state changed.
|
// Called when window state changed.
|
||||||
virtual void OnWindowMaximize() {}
|
virtual void OnWindowMaximize() {}
|
||||||
virtual void OnWindowUnmaximize() {}
|
virtual void OnWindowUnmaximize() {}
|
||||||
|
|
|
@ -302,6 +302,8 @@ bool NativeWindowViews::IsFocused() {
|
||||||
void NativeWindowViews::Show() {
|
void NativeWindowViews::Show() {
|
||||||
window_->native_widget_private()->ShowWithWindowState(GetRestoredState());
|
window_->native_widget_private()->ShowWithWindowState(GetRestoredState());
|
||||||
|
|
||||||
|
NotifyWindowShow();
|
||||||
|
|
||||||
#if defined(USE_X11)
|
#if defined(USE_X11)
|
||||||
if (global_menu_bar_)
|
if (global_menu_bar_)
|
||||||
global_menu_bar_->OnWindowMapped();
|
global_menu_bar_->OnWindowMapped();
|
||||||
|
@ -311,6 +313,8 @@ void NativeWindowViews::Show() {
|
||||||
void NativeWindowViews::ShowInactive() {
|
void NativeWindowViews::ShowInactive() {
|
||||||
window_->ShowInactive();
|
window_->ShowInactive();
|
||||||
|
|
||||||
|
NotifyWindowShow();
|
||||||
|
|
||||||
#if defined(USE_X11)
|
#if defined(USE_X11)
|
||||||
if (global_menu_bar_)
|
if (global_menu_bar_)
|
||||||
global_menu_bar_->OnWindowMapped();
|
global_menu_bar_->OnWindowMapped();
|
||||||
|
@ -320,6 +324,8 @@ void NativeWindowViews::ShowInactive() {
|
||||||
void NativeWindowViews::Hide() {
|
void NativeWindowViews::Hide() {
|
||||||
window_->Hide();
|
window_->Hide();
|
||||||
|
|
||||||
|
NotifyWindowHide();
|
||||||
|
|
||||||
#if defined(USE_X11)
|
#if defined(USE_X11)
|
||||||
if (global_menu_bar_)
|
if (global_menu_bar_)
|
||||||
global_menu_bar_->OnWindowUnmapped();
|
global_menu_bar_->OnWindowUnmapped();
|
||||||
|
|
|
@ -245,6 +245,14 @@ Emitted when the window loses focus.
|
||||||
|
|
||||||
Emitted when the window gains focus.
|
Emitted when the window gains focus.
|
||||||
|
|
||||||
|
### Event: 'show'
|
||||||
|
|
||||||
|
Emitted when the window is shown.
|
||||||
|
|
||||||
|
### Event: 'hide'
|
||||||
|
|
||||||
|
Emitted when the window is hidden.
|
||||||
|
|
||||||
### Event: 'maximize'
|
### Event: 'maximize'
|
||||||
|
|
||||||
Emitted when window is maximized.
|
Emitted when window is maximized.
|
||||||
|
|
|
@ -20,7 +20,7 @@ BrowserWindow.prototype._init = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make new windows requested by links behave like "window.open"
|
// Make new windows requested by links behave like "window.open"
|
||||||
this.webContents.on('-new-window', function(event, url, frameName) {
|
this.webContents.on('-new-window', (event, url, frameName) => {
|
||||||
var options;
|
var options;
|
||||||
options = {
|
options = {
|
||||||
show: true,
|
show: true,
|
||||||
|
@ -32,27 +32,21 @@ BrowserWindow.prototype._init = function() {
|
||||||
|
|
||||||
// window.resizeTo(...)
|
// window.resizeTo(...)
|
||||||
// window.moveTo(...)
|
// window.moveTo(...)
|
||||||
this.webContents.on('move', (function(_this) {
|
this.webContents.on('move', (event, title) => {
|
||||||
return function(event, size) {
|
return this.setBounds(size);
|
||||||
return _this.setBounds(size);
|
});
|
||||||
};
|
|
||||||
})(this));
|
|
||||||
|
|
||||||
// Hide the auto-hide menu when webContents is focused.
|
// Hide the auto-hide menu when webContents is focused.
|
||||||
this.webContents.on('activate', (function(_this) {
|
this.webContents.on('activate', () => {
|
||||||
return function() {
|
if (process.platform !== 'darwin' && this.isMenuBarAutoHide() && this.isMenuBarVisible()) {
|
||||||
if (process.platform !== 'darwin' && _this.isMenuBarAutoHide() && _this.isMenuBarVisible()) {
|
return this.setMenuBarVisibility(false);
|
||||||
return _this.setMenuBarVisibility(false);
|
}
|
||||||
}
|
});
|
||||||
};
|
|
||||||
})(this));
|
|
||||||
|
|
||||||
// Forward the crashed event.
|
// Forward the crashed event.
|
||||||
this.webContents.on('crashed', (function(_this) {
|
this.webContents.on('crashed', () => {
|
||||||
return function() {
|
return this.emit('crashed');
|
||||||
return _this.emit('crashed');
|
});
|
||||||
};
|
|
||||||
})(this));
|
|
||||||
|
|
||||||
// Change window title to page title.
|
// Change window title to page title.
|
||||||
this.webContents.on('page-title-updated', (event, title) => {
|
this.webContents.on('page-title-updated', (event, title) => {
|
||||||
|
@ -77,48 +71,40 @@ BrowserWindow.prototype._init = function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Redirect focus/blur event to app instance too.
|
// Redirect focus/blur event to app instance too.
|
||||||
this.on('blur', (function(_this) {
|
this.on('blur', (event) => {
|
||||||
return function(event) {
|
return app.emit('browser-window-blur', event, this);
|
||||||
return app.emit('browser-window-blur', event, _this);
|
});
|
||||||
};
|
this.on('focus', (event) => {
|
||||||
})(this));
|
return app.emit('browser-window-focus', event, this);
|
||||||
this.on('focus', (function(_this) {
|
});
|
||||||
return function(event) {
|
|
||||||
return app.emit('browser-window-focus', event, _this);
|
|
||||||
};
|
|
||||||
})(this));
|
|
||||||
|
|
||||||
// Evented visibilityState changes
|
// Evented visibilityState changes
|
||||||
this.on('minimize', (function(_this) {
|
this.on('show', () => {
|
||||||
return function() {
|
return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', true, this.isMinimized());
|
||||||
return _this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', false);
|
});
|
||||||
};
|
this.on('hide', () => {
|
||||||
})(this));
|
return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', false, this.isMinimized());
|
||||||
this.on('restore', (function(_this) {
|
});
|
||||||
return function() {
|
this.on('minimize', () => {
|
||||||
return _this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', true);
|
return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), true);
|
||||||
};
|
});
|
||||||
})(this));
|
this.on('restore', () => {
|
||||||
|
return this.webContents.send('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', this.isVisible(), false);
|
||||||
|
});
|
||||||
|
|
||||||
// Notify the creation of the window.
|
// Notify the creation of the window.
|
||||||
app.emit('browser-window-created', {}, this);
|
app.emit('browser-window-created', {}, this);
|
||||||
|
|
||||||
// Be compatible with old APIs.
|
// Be compatible with old APIs.
|
||||||
this.webContents.on('devtools-focused', (function(_this) {
|
this.webContents.on('devtools-focused', () => {
|
||||||
return function() {
|
return this.emit('devtools-focused');
|
||||||
return _this.emit('devtools-focused');
|
});
|
||||||
};
|
this.webContents.on('devtools-opened', () => {
|
||||||
})(this));
|
return this.emit('devtools-opened');
|
||||||
this.webContents.on('devtools-opened', (function(_this) {
|
});
|
||||||
return function() {
|
this.webContents.on('devtools-closed', () => {
|
||||||
return _this.emit('devtools-opened');
|
return this.emit('devtools-closed');
|
||||||
};
|
});
|
||||||
})(this));
|
|
||||||
this.webContents.on('devtools-closed', (function(_this) {
|
|
||||||
return function() {
|
|
||||||
return _this.emit('devtools-closed');
|
|
||||||
};
|
|
||||||
})(this));
|
|
||||||
return Object.defineProperty(this, 'devToolsWebContents', {
|
return Object.defineProperty(this, 'devToolsWebContents', {
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: false,
|
configurable: false,
|
||||||
|
|
|
@ -4,10 +4,13 @@ const remote = require('electron').remote;
|
||||||
var slice = [].slice;
|
var slice = [].slice;
|
||||||
|
|
||||||
// Cache browser window visibility
|
// Cache browser window visibility
|
||||||
var _isVisible = (function() {
|
var _isVisible = true;
|
||||||
|
var _isMinimized = false;
|
||||||
|
(function() {
|
||||||
var currentWindow;
|
var currentWindow;
|
||||||
currentWindow = remote.getCurrentWindow();
|
currentWindow = remote.getCurrentWindow();
|
||||||
return currentWindow.isMinimized() || !currentWindow.isVisible();
|
_isVisible = currentWindow.isVisible();
|
||||||
|
_isMinimized = currentWindow.isMinimized();
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// Helper function to resolve relative url.
|
// Helper function to resolve relative url.
|
||||||
|
@ -177,10 +180,15 @@ if (process.openerId != null) {
|
||||||
window.opener = BrowserWindowProxy.getOrCreate(process.openerId);
|
window.opener = BrowserWindowProxy.getOrCreate(process.openerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
ipcRenderer.on('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, isVisible) {
|
ipcRenderer.on('ATOM_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, isVisible, isMinimized) {
|
||||||
_isVisible = isVisible;
|
var hasChanged = _isVisible != isVisible || _isMinimized != isMinimized;
|
||||||
|
|
||||||
|
if (hasChanged) {
|
||||||
|
_isVisible = isVisible;
|
||||||
|
_isMinimized = isMinimized;
|
||||||
|
|
||||||
document.dispatchEvent(new Event('visibilitychange'));
|
document.dispatchEvent(new Event('visibilitychange'));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
ipcRenderer.on('ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', function(event, sourceId, message, sourceOrigin) {
|
ipcRenderer.on('ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', function(event, sourceId, message, sourceOrigin) {
|
||||||
|
@ -227,12 +235,14 @@ Object.defineProperty(window.history, 'length', {
|
||||||
|
|
||||||
// Make document.hidden and document.visibilityState return the correct value.
|
// Make document.hidden and document.visibilityState return the correct value.
|
||||||
Object.defineProperty(document, 'hidden', {
|
Object.defineProperty(document, 'hidden', {
|
||||||
get: !_isVisible
|
get: function () {
|
||||||
|
return _isMinimized || !_isVisible;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.defineProperty(document, 'visibilityState', {
|
Object.defineProperty(document, 'visibilityState', {
|
||||||
get: function() {
|
get: function() {
|
||||||
if (_isVisible) {
|
if (_isVisible && !_isMinimized) {
|
||||||
return "visible";
|
return "visible";
|
||||||
} else {
|
} else {
|
||||||
return "hidden";
|
return "hidden";
|
||||||
|
|
Loading…
Reference in a new issue