import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";import{ScreencastView}from"./ScreencastView.js";const UIStrings={toggleScreencast:"Toggle screencast"},str_=i18n.i18n.registerUIStrings("screencast/ScreencastApp.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let appInstance,toolbarButtonProviderInstance,screencastAppProviderInstance;export class ScreencastApp{constructor(){this._enabledSetting=Common.Settings.Settings.instance().createSetting("screencastEnabled",!0),this._toggleButton=new UI.Toolbar.ToolbarToggle(i18nString(UIStrings.toggleScreencast),"largeicon-phone"),this._toggleButton.setToggled(this._enabledSetting.get()),this._toggleButton.setEnabled(!1),this._toggleButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this._toggleButtonClicked,this),SDK.SDKModel.TargetManager.instance().observeModels(SDK.ScreenCaptureModel.ScreenCaptureModel,this)}static _instance(){return appInstance||(appInstance=new ScreencastApp),appInstance}presentUI(e){const t=new UI.RootView.RootView;this._rootSplitWidget=new UI.SplitWidget.SplitWidget(!1,!0,"InspectorView.screencastSplitViewState",300,300),this._rootSplitWidget.setVertical(!0),this._rootSplitWidget.setSecondIsSidebar(!0),this._rootSplitWidget.show(t.element),this._rootSplitWidget.hideMain(),this._rootSplitWidget.setSidebarWidget(UI.InspectorView.InspectorView.instance()),UI.InspectorView.InspectorView.instance().setOwnerSplit(this._rootSplitWidget),t.attachToDocument(e),t.focus()}modelAdded(e){this._screenCaptureModel||(this._screenCaptureModel=e,this._toggleButton.setEnabled(!0),this._screencastView=new ScreencastView(e),this._rootSplitWidget&&this._rootSplitWidget.setMainWidget(this._screencastView),this._screencastView.initialize(),this._onScreencastEnabledChanged())}modelRemoved(e){this._screenCaptureModel===e&&(delete this._screenCaptureModel,this._toggleButton.setEnabled(!1),this._screencastView&&(this._screencastView.detach(),delete this._screencastView),this._onScreencastEnabledChanged())}_toggleButtonClicked(){const e=!this._toggleButton.toggled();this._enabledSetting.set(e),this._onScreencastEnabledChanged()}_onScreencastEnabledChanged(){if(!this._rootSplitWidget)return;const e=Boolean(this._enabledSetting.get()&&this._screencastView);this._toggleButton.setToggled(e),e?this._rootSplitWidget.showBoth():this._rootSplitWidget.hideMain()}}export class ToolbarButtonProvider{static instance(e={forceNew:!1}){const{forceNew:t}=e;return toolbarButtonProviderInstance&&!t||(toolbarButtonProviderInstance=new ToolbarButtonProvider),toolbarButtonProviderInstance}item(){return ScreencastApp._instance()._toggleButton}}export class ScreencastAppProvider{static instance(e={forceNew:!1}){const{forceNew:t}=e;return screencastAppProviderInstance&&!t||(screencastAppProviderInstance=new ScreencastAppProvider),screencastAppProviderInstance}createApp(){return ScreencastApp._instance()}}