2020-09-06 02:03:35 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2020-2022 Yomichan Authors
|
2020-09-06 02:03:35 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* This class represents a popup that is hosted in a new native window.
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
class PopupWindow extends EventDispatcher {
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {object} details Details about how to set up the instance.
|
2021-11-01 01:45:57 +00:00
|
|
|
* @param {string} details.id The ID of the popup.
|
|
|
|
* @param {number} details.depth The depth of the popup.
|
|
|
|
* @param {number} details.frameId The ID of the host frame.
|
|
|
|
*/
|
2020-11-23 20:23:47 +00:00
|
|
|
constructor({
|
|
|
|
id,
|
|
|
|
depth,
|
2021-02-10 03:56:04 +00:00
|
|
|
frameId
|
2020-11-23 20:23:47 +00:00
|
|
|
}) {
|
2020-09-06 02:03:35 +00:00
|
|
|
super();
|
|
|
|
this._id = id;
|
|
|
|
this._depth = depth;
|
|
|
|
this._frameId = frameId;
|
|
|
|
this._popupTabId = null;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* The ID of the popup.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
get id() {
|
|
|
|
return this._id;
|
|
|
|
}
|
|
|
|
|
|
|
|
get parent() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* The parent of the popup, which is always `null` for `PopupWindow` instances,
|
|
|
|
* since any potential parent popups are in a different frame.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {Popup} _value The parent to assign.
|
|
|
|
* @throws {Error} Throws an error, since this class doesn't support children.
|
2021-11-01 01:45:57 +00:00
|
|
|
*/
|
2022-05-20 14:28:38 +00:00
|
|
|
set parent(_value) {
|
2021-11-21 17:34:57 +00:00
|
|
|
throw new Error('Not supported on PopupWindow');
|
2020-09-06 02:03:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* The popup child popup, which is always null for `PopupWindow` instances,
|
|
|
|
* since any potential child popups are in a different frame.
|
|
|
|
* @type {Popup}
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
get child() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Attempts to set the child popup.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {Popup} _value The child to assign.
|
2021-11-01 01:45:57 +00:00
|
|
|
* @throws Throws an error, since this class doesn't support children.
|
|
|
|
*/
|
2022-05-20 14:28:38 +00:00
|
|
|
set child(_value) {
|
2021-11-21 17:34:57 +00:00
|
|
|
throw new Error('Not supported on PopupWindow');
|
2020-09-06 02:03:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* The depth of the popup.
|
|
|
|
* @type {numer}
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
get depth() {
|
|
|
|
return this._depth;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the content window of the frame. This value is null,
|
|
|
|
* since the window is hosted in a different frame.
|
|
|
|
* @type {Window}
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
get frameContentWindow() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the DOM node that contains the frame.
|
|
|
|
* @type {Element}
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
get container() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the ID of the frame.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
get frameId() {
|
|
|
|
return this._frameId;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the options context for the popup.
|
|
|
|
* @param {object} optionsContext The options context object.
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2021-11-21 17:34:57 +00:00
|
|
|
setOptionsContext(optionsContext) {
|
2021-11-21 20:54:58 +00:00
|
|
|
return this._invoke(false, 'Display.setOptionsContext', {id: this._id, optionsContext});
|
2020-09-06 02:03:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Hides the popup. This does nothing for `PopupWindow`.
|
|
|
|
* @param {boolean} _changeFocus Whether or not the parent popup or host frame should be focused.
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
hide(_changeFocus) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Returns whether or not the popup is currently visible.
|
|
|
|
* @returns {Promise<boolean>} `true` if the popup is visible, `false` otherwise.
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
async isVisible() {
|
2021-02-14 20:53:35 +00:00
|
|
|
return (this._popupTabId !== null && await yomichan.api.isTabSearchPopup(this._popupTabId));
|
2020-09-06 02:03:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Force assigns the visibility of the popup.
|
|
|
|
* @param {boolean} _value Whether or not the popup should be visible.
|
|
|
|
* @param {number} _priority The priority of the override.
|
|
|
|
* @returns {Promise<string?>} A token used which can be passed to `clearVisibleOverride`,
|
|
|
|
* or null if the override wasn't assigned.
|
|
|
|
*/
|
2021-02-10 04:14:29 +00:00
|
|
|
async setVisibleOverride(_value, _priority) {
|
2020-09-06 02:03:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Clears a visibility override that was generated by `setVisibleOverride`.
|
|
|
|
* @param {string} _token The token returned from `setVisibleOverride`.
|
|
|
|
* @returns {boolean} `true` if the override existed and was removed, `false` otherwise.
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
clearVisibleOverride(_token) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a point is contained within the popup's rect.
|
|
|
|
* @param {number} _x The x coordinate.
|
|
|
|
* @param {number} _y The y coordinate.
|
|
|
|
* @returns {Promise<boolean>} `true` if the point is contained within the popup's rect, `false` otherwise.
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
async containsPoint(_x, _y) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Shows and updates the positioning and content of the popup.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {Popup.ContentDetails} _details Settings for the outer popup.
|
2022-05-17 01:45:22 +00:00
|
|
|
* @param {Display.ContentDetails} displayDetails The details parameter passed to `Display.setContent`.
|
2021-11-01 01:45:57 +00:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2020-09-26 17:48:06 +00:00
|
|
|
async showContent(_details, displayDetails) {
|
|
|
|
if (displayDetails === null) { return; }
|
2021-11-21 20:54:58 +00:00
|
|
|
await this._invoke(true, 'Display.setContent', {id: this._id, details: displayDetails});
|
2020-09-06 02:03:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the custom styles for the popup content.
|
|
|
|
* @param {string} css The CSS rules.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @returns {Promise<void>}
|
2021-11-01 01:45:57 +00:00
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
setCustomCss(css) {
|
2021-11-21 20:54:58 +00:00
|
|
|
return this._invoke(false, 'Display.setCustomCss', {id: this._id, css});
|
2020-09-06 02:03:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Stops the audio auto-play timer, if one has started.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @returns {Promise<void>}
|
2021-11-01 01:45:57 +00:00
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
clearAutoPlayTimer() {
|
2021-11-21 20:54:58 +00:00
|
|
|
return this._invoke(false, 'Display.clearAutoPlayTimer', {id: this._id});
|
2020-09-06 02:03:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the scaling factor of the popup content.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {number} _scale The scaling factor.
|
2021-11-01 01:45:57 +00:00
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
setContentScale(_scale) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Returns whether or not the popup is currently visible, synchronously.
|
|
|
|
* @throws An exception is thrown for `PopupWindow` since it cannot synchronously detect visibility.
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
isVisibleSync() {
|
|
|
|
throw new Error('Not supported on PopupWindow');
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Updates the outer theme of the popup.
|
|
|
|
*/
|
2022-04-03 20:20:55 +00:00
|
|
|
async updateTheme() {
|
2020-09-06 02:03:35 +00:00
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the custom styles for the outer popup container.
|
|
|
|
* This does nothing for `PopupWindow`.
|
|
|
|
* @param {string} _css The CSS rules.
|
|
|
|
* @param {boolean} _useWebExtensionApi Whether or not web extension APIs should be used to inject the rules.
|
|
|
|
* When web extension APIs are used, a DOM node is not generated, making it harder to detect the changes.
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
async setCustomOuterCss(_css, _useWebExtensionApi) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the rectangle of the DOM frame, synchronously.
|
2022-05-17 01:45:22 +00:00
|
|
|
* @returns {Popup.ValidRect} The rect.
|
2021-11-01 01:45:57 +00:00
|
|
|
* `valid` is `false` for `PopupProxy`, since the DOM node is hosted in a different frame.
|
|
|
|
*/
|
2020-09-06 02:03:35 +00:00
|
|
|
getFrameRect() {
|
2022-05-17 01:45:22 +00:00
|
|
|
return {left: 0, top: 0, right: 0, bottom: 0, valid: false};
|
2020-09-06 02:03:35 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Gets the size of the DOM frame.
|
|
|
|
* @returns {Promise<{width: number, height: number, valid: boolean}>} The size and whether or not it is valid.
|
|
|
|
*/
|
2020-12-09 01:31:02 +00:00
|
|
|
async getFrameSize() {
|
|
|
|
return {width: 0, height: 0, valid: false};
|
|
|
|
}
|
|
|
|
|
2021-11-01 01:45:57 +00:00
|
|
|
/**
|
|
|
|
* Sets the size of the DOM frame.
|
|
|
|
* @param {number} _width The desired width of the popup.
|
|
|
|
* @param {number} _height The desired height of the popup.
|
|
|
|
* @returns {Promise<boolean>} `true` if the size assignment was successful, `false` otherwise.
|
|
|
|
*/
|
2020-12-09 01:31:02 +00:00
|
|
|
async setFrameSize(_width, _height) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-06 02:03:35 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
async _invoke(open, action, params={}, defaultReturnValue) {
|
|
|
|
if (yomichan.isExtensionUnloaded) {
|
|
|
|
return defaultReturnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const frameId = 0;
|
|
|
|
if (this._popupTabId !== null) {
|
|
|
|
try {
|
2021-02-14 20:53:35 +00:00
|
|
|
return await yomichan.crossFrame.invokeTab(this._popupTabId, frameId, 'popupMessage', {action, params});
|
2020-09-06 02:03:35 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (yomichan.isExtensionUnloaded) {
|
|
|
|
open = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._popupTabId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!open) {
|
|
|
|
return defaultReturnValue;
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
const {tabId} = await yomichan.api.getOrCreateSearchPopup({focus: 'ifCreated'});
|
2020-09-06 02:03:35 +00:00
|
|
|
this._popupTabId = tabId;
|
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
return await yomichan.crossFrame.invokeTab(this._popupTabId, frameId, 'popupMessage', {action, params});
|
2020-09-06 02:03:35 +00:00
|
|
|
}
|
|
|
|
}
|