UNPKG

@overwolf/overwolf-api-ts

Version:

utilities and wrappers for common Overwolf API tasks

128 lines (127 loc) 3.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OWWindow = void 0; class OWWindow { constructor(name = null) { this._name = name; this._id = null; } async restore() { let that = this; return new Promise(async (resolve) => { await that.assureObtained(); let id = that._id; overwolf.windows.restore(id, result => { if (!result.success) console.error(`[restore] - an error occurred, windowId=${id}, reason=${result.error}`); resolve(); }); }); } async minimize() { let that = this; return new Promise(async (resolve) => { await that.assureObtained(); let id = that._id; overwolf.windows.minimize(id, () => { }); return resolve(); }); } async maximize() { let that = this; return new Promise(async (resolve) => { await that.assureObtained(); let id = that._id; overwolf.windows.maximize(id, () => { }); return resolve(); }); } async hide() { let that = this; return new Promise(async (resolve) => { await that.assureObtained(); let id = that._id; overwolf.windows.hide(id, () => { }); return resolve(); }); } async close() { let that = this; return new Promise(async (resolve) => { await that.assureObtained(); let id = that._id; const result = await this.getWindowState(); if (result.success && (result.window_state !== 'closed')) { await this.internalClose(); } return resolve(); }); } dragMove(elem) { elem.className = elem.className + ' draggable'; elem.onmousedown = e => { e.preventDefault(); overwolf.windows.dragMove(this._name); }; } async getWindowState() { let that = this; return new Promise(async (resolve) => { await that.assureObtained(); let id = that._id; overwolf.windows.getWindowState(id, resolve); }); } static async getCurrentInfo() { return new Promise(async (resolve) => { overwolf.windows.getCurrentWindow(result => { resolve(result.window); }); }); } obtain() { return new Promise((resolve, reject) => { const cb = res => { if (res && res.status === "success" && res.window && res.window.id) { this._id = res.window.id; if (!this._name) { this._name = res.window.name; } resolve(res.window); } else { this._id = null; reject(); } }; if (!this._name) { overwolf.windows.getCurrentWindow(cb); } else { overwolf.windows.obtainDeclaredWindow(this._name, cb); } }); } async assureObtained() { let that = this; return new Promise(async (resolve) => { await that.obtain(); return resolve(); }); } async internalClose() { let that = this; return new Promise(async (resolve, reject) => { await that.assureObtained(); let id = that._id; overwolf.windows.close(id, res => { if (res && res.success) resolve(); else reject(res); }); }); } } exports.OWWindow = OWWindow;