@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 11.1 kB
JavaScript
import*as DOMExtension from"../../core/dom_extension/dom_extension.js";import*as Platform from"../../core/platform/platform.js";import*as Helpers from"../components/helpers/helpers.js";import{Constraints,Size}from"./Geometry.js";import*as ThemeSupport from"./theme_support/theme_support.js";import*as Utils from"./utils/utils.js";import{XWidget}from"./XWidget.js";export class WidgetElement extends HTMLDivElement{__widget;__widgetCounter;constructor(){super()}}export class Widget{element;contentElement;shadowRoot;isWebComponent;visibleInternal;isRoot;isShowingInternal;childrenInternal;hideOnDetach;notificationDepth;invalidationsSuspended;defaultFocusedChild;parentWidgetInternal;registeredCSSFiles;defaultFocusedElement;cachedConstraints;constraintsInternal;invalidationsRequested;externallyManaged;constructor(t,e){this.contentElement=document.createElement("div"),this.contentElement.classList.add("widget"),t?(this.element=document.createElement("div"),this.element.classList.add("vbox"),this.element.classList.add("flex-auto"),this.shadowRoot=Utils.createShadowRootWithCoreStyles(this.element,{cssFile:void 0,delegatesFocus:e}),this.shadowRoot.appendChild(this.contentElement)):this.element=this.contentElement,this.isWebComponent=t,this.element.__widget=this,this.visibleInternal=!1,this.isRoot=!1,this.isShowingInternal=!1,this.childrenInternal=[],this.hideOnDetach=!1,this.notificationDepth=0,this.invalidationsSuspended=0,this.defaultFocusedChild=null,this.parentWidgetInternal=null,this.registeredCSSFiles=!1}static incrementWidgetCounter(t,e){const i=(e.__widgetCounter||0)+(e.__widget?1:0);if(!i)return;let n=t;for(;n;)n.__widgetCounter=(n.__widgetCounter||0)+i,n=parentWidgetElementOrShadowHost(n)}static decrementWidgetCounter(t,e){const i=(e.__widgetCounter||0)+(e.__widget?1:0);if(!i)return;let n=t;for(;n;)n.__widgetCounter&&(n.__widgetCounter-=i),n=parentWidgetElementOrShadowHost(n)}static assert(t,e){if(!t)throw new Error(e)}markAsRoot(){Widget.assert(!this.element.parentElement,"Attempt to mark as root attached node"),this.isRoot=!0}parentWidget(){return this.parentWidgetInternal}children(){return this.childrenInternal}childWasDetached(t){}isShowing(){return this.isShowingInternal}shouldHideOnDetach(){if(!this.element.parentElement)return!1;if(this.hideOnDetach)return!0;for(const t of this.childrenInternal)if(t.shouldHideOnDetach())return!0;return!1}setHideOnDetach(){this.hideOnDetach=!0}inNotification(){return Boolean(this.notificationDepth)||Boolean(this.parentWidgetInternal&&this.parentWidgetInternal.inNotification())}parentIsShowing(){return!!this.isRoot||null!==this.parentWidgetInternal&&this.parentWidgetInternal.isShowing()}callOnVisibleChildren(t){const e=this.childrenInternal.slice();for(let i=0;i<e.length;++i)e[i].parentWidgetInternal===this&&e[i].visibleInternal&&t.call(e[i])}processWillShow(){this.callOnVisibleChildren(this.processWillShow),this.isShowingInternal=!0}processWasShown(){this.inNotification()||(this.restoreScrollPositions(),this.notify(this.wasShown),this.callOnVisibleChildren(this.processWasShown))}processWillHide(){this.inNotification()||(this.storeScrollPositions(),this.callOnVisibleChildren(this.processWillHide),this.notify(this.willHide),this.isShowingInternal=!1)}processWasHidden(){this.callOnVisibleChildren(this.processWasHidden)}processOnResize(){this.inNotification()||this.isShowing()&&(this.notify(this.onResize),this.callOnVisibleChildren(this.processOnResize))}notify(t){++this.notificationDepth;try{t.call(this)}finally{--this.notificationDepth}}wasShown(){}willHide(){}onResize(){}onLayout(){}onDetach(){}async ownerViewDisposed(){}show(t,e){if(Widget.assert(t,"Attempt to attach widget with no parent element"),!this.isRoot){let e=t;for(;e&&!e.__widget;)e=parentWidgetElementOrShadowHost(e);if(!e||!e.__widget)throw new Error("Attempt to attach widget to orphan node");this.attach(e.__widget)}this.showWidgetInternal(t,e)}attach(t){t!==this.parentWidgetInternal&&(this.parentWidgetInternal&&this.detach(),this.parentWidgetInternal=t,this.parentWidgetInternal.childrenInternal.push(this),this.isRoot=!1)}showWidget(){if(!this.visibleInternal){if(!this.element.parentElement)throw new Error("Attempt to show widget that is not hidden using hideWidget().");this.showWidgetInternal(this.element.parentElement,this.element.nextSibling)}}showWidgetInternal(t,e){let i=t;for(;i&&!i.__widget;)i=parentWidgetElementOrShadowHost(i);this.isRoot?Widget.assert(!i,"Attempt to show root widget under another widget"):Widget.assert(i&&i.__widget===this.parentWidgetInternal,"Attempt to show under node belonging to alien widget");const n=this.visibleInternal;n&&this.element.parentElement===t||(this.visibleInternal=!0,!n&&this.parentIsShowing()&&this.processWillShow(),this.element.classList.remove("hidden"),this.element.parentElement!==t&&(this.externallyManaged||Widget.incrementWidgetCounter(t,this.element),e?DOMExtension.DOMExtension.originalInsertBefore.call(t,this.element,e):DOMExtension.DOMExtension.originalAppendChild.call(t,this.element)),!n&&this.parentIsShowing()&&this.processWasShown(),this.parentWidgetInternal&&this.hasNonZeroConstraints()?this.parentWidgetInternal.invalidateConstraints():this.processOnResize())}hideWidget(){this.visibleInternal&&this.hideWidgetInternal(!1)}hideWidgetInternal(t){this.visibleInternal=!1;const e=this.element.parentElement;this.parentIsShowing()&&this.processWillHide(),t?(Widget.decrementWidgetCounter(e,this.element),DOMExtension.DOMExtension.originalRemoveChild.call(e,this.element),this.onDetach()):this.element.classList.add("hidden"),this.parentIsShowing()&&this.processWasHidden(),this.parentWidgetInternal&&this.hasNonZeroConstraints()&&this.parentWidgetInternal.invalidateConstraints()}detach(t){if(!this.parentWidgetInternal&&!this.isRoot)return;const e=t||!this.shouldHideOnDetach();if(this.visibleInternal)this.hideWidgetInternal(e);else if(e&&this.element.parentElement){const t=this.element.parentElement;Widget.decrementWidgetCounter(t,this.element),DOMExtension.DOMExtension.originalRemoveChild.call(t,this.element)}if(this.parentWidgetInternal){const t=this.parentWidgetInternal.childrenInternal.indexOf(this);Widget.assert(t>=0,"Attempt to remove non-child widget"),this.parentWidgetInternal.childrenInternal.splice(t,1),this.parentWidgetInternal.defaultFocusedChild===this&&(this.parentWidgetInternal.defaultFocusedChild=null),this.parentWidgetInternal.childWasDetached(this),this.parentWidgetInternal=null}else Widget.assert(this.isRoot,"Removing non-root widget from DOM")}detachChildWidgets(){const t=this.childrenInternal.slice();for(let e=0;e<t.length;++e)t[e].detach()}elementsToRestoreScrollPositionsFor(){return[this.element]}storeScrollPositions(){const t=this.elementsToRestoreScrollPositionsFor();for(const e of t)storedScrollPositions.set(e,{scrollLeft:e.scrollLeft,scrollTop:e.scrollTop})}restoreScrollPositions(){const t=this.elementsToRestoreScrollPositionsFor();for(const e of t){const t=storedScrollPositions.get(e);t&&(e.scrollLeft=t.scrollLeft,e.scrollTop=t.scrollTop)}}doResize(){this.isShowing()&&(this.inNotification()||this.callOnVisibleChildren(this.processOnResize))}doLayout(){this.isShowing()&&(this.notify(this.onLayout),this.doResize())}registerRequiredCSS(t){this.isWebComponent?ThemeSupport.ThemeSupport.instance().appendStyle(this.shadowRoot,t):ThemeSupport.ThemeSupport.instance().appendStyle(this.element,t)}registerCSSFiles(t){let e;e=this.isWebComponent&&void 0!==this.shadowRoot?this.shadowRoot:Helpers.GetRootNode.getRootNode(this.contentElement),e.adoptedStyleSheets=e.adoptedStyleSheets.concat(t),this.registeredCSSFiles=!0}printWidgetHierarchy(){const t=[];this.collectWidgetHierarchy("",t),console.log(t.join("\n"))}collectWidgetHierarchy(t,e){e.push(t+"["+this.element.className+"]"+(this.childrenInternal.length?" {":""));for(let i=0;i<this.childrenInternal.length;++i)this.childrenInternal[i].collectWidgetHierarchy(t+" ",e);this.childrenInternal.length&&e.push(t+"}")}setDefaultFocusedElement(t){this.defaultFocusedElement=t}setDefaultFocusedChild(t){Widget.assert(t.parentWidgetInternal===this,"Attempt to set non-child widget as default focused."),this.defaultFocusedChild=t}focus(){if(!this.isShowing())return;const t=this.defaultFocusedElement;if(t)t.hasFocus()||t.focus();else if(this.defaultFocusedChild&&this.defaultFocusedChild.visibleInternal)this.defaultFocusedChild.focus();else{for(const t of this.childrenInternal)if(t.visibleInternal)return void t.focus();let t=this.contentElement.traverseNextNode(this.contentElement);for(;t;){if(t instanceof XWidget)return void t.focus();t=t.traverseNextNode(this.contentElement)}}}hasFocus(){return this.element.hasFocus()}calculateConstraints(){return new Constraints}constraints(){return void 0!==this.constraintsInternal?this.constraintsInternal:(void 0===this.cachedConstraints&&(this.cachedConstraints=this.calculateConstraints()),this.cachedConstraints)}setMinimumAndPreferredSizes(t,e,i,n){this.constraintsInternal=new Constraints(new Size(t,e),new Size(i,n)),this.invalidateConstraints()}setMinimumSize(t,e){this.constraintsInternal=new Constraints(new Size(t,e)),this.invalidateConstraints()}hasNonZeroConstraints(){const t=this.constraints();return Boolean(t.minimum.width||t.minimum.height||t.preferred.width||t.preferred.height)}suspendInvalidations(){++this.invalidationsSuspended}resumeInvalidations(){--this.invalidationsSuspended,!this.invalidationsSuspended&&this.invalidationsRequested&&this.invalidateConstraints()}invalidateConstraints(){if(this.invalidationsSuspended)return void(this.invalidationsRequested=!0);this.invalidationsRequested=!1;const t=this.cachedConstraints;delete this.cachedConstraints;!this.constraints().isEqual(t||null)&&this.parentWidgetInternal?this.parentWidgetInternal.invalidateConstraints():this.doLayout()}markAsExternallyManaged(){Widget.assert(!this.parentWidgetInternal,"Attempt to mark widget as externally managed after insertion to the DOM"),this.externallyManaged=!0}}const storedScrollPositions=new WeakMap;export class VBox extends Widget{constructor(t,e){super(t,e),this.contentElement.classList.add("vbox")}calculateConstraints(){let t=new Constraints;return this.callOnVisibleChildren((function(){const e=this.constraints();t=t.widthToMax(e),t=t.addHeight(e)})),t}}export class HBox extends Widget{constructor(t){super(t),this.contentElement.classList.add("hbox")}calculateConstraints(){let t=new Constraints;return this.callOnVisibleChildren((function(){const e=this.constraints();t=t.addWidth(e),t=t.heightToMax(e)})),t}}export class VBoxWithResizeCallback extends VBox{resizeCallback;constructor(t){super(),this.resizeCallback=t}onResize(){this.resizeCallback()}}export class WidgetFocusRestorer{widget;previous;constructor(t){this.widget=t,this.previous=Platform.DOMUtilities.deepActiveElement(t.element.ownerDocument),t.focus()}restore(){this.widget&&(this.widget.hasFocus()&&this.previous&&this.previous.focus(),this.previous=null,this.widget=null)}}function parentWidgetElementOrShadowHost(t){return t.parentElementOrShadowHost()}