namespace pxt . winrt { type ActivationArgs = Windows . ApplicationModel . Activation . IActivatedEventArgs ; type SuspendingArgs = Windows . ApplicationModel . ISuspendingEventArgs ; type ResumingArgs = any ; pxt . BrowserUtils . isWinRT = isWinRT ; export function promisify < T > ( p ) { return new Promise < T > ( ( resolve , reject ) => { p . then ( v => resolve ( v ) , e => reject ( e ) ) ; } ) ; } export function toArray < T > ( v ) { let r = [ ] ; let length = v . length ; for ( let i = 0 ; i < length ; ++ i ) r . push ( v [ i ] ) ; return r ; } /**
     * Detects if the script is running in a browser on windows
     */ export function isWindows ( ) { return ! ! navigator && /Win32/i . test ( navigator . platform ) ; } export function isWinRT ( ) { return typeof Windows !== "undefined" ; } export function initAsync ( importHexImpl ? ) { if ( ! isWinRT ( ) || pxt . BrowserUtils . isIFrame ( ) ) return Promise . resolve ( ) ; const uiCore = Windows . UI . Core ; const navMgr = uiCore . SystemNavigationManager . getForCurrentView ( ) ; const app = Windows . UI . WebUI . WebUIApplication ; app . addEventListener ( "suspending" , suspendingHandler ) ; app . addEventListener ( "resuming" , resumingHandler ) ; navMgr . onbackrequested = ( e ) => { pxt . log ( "BACK NAVIGATION" ) ; navMgr . appViewBackButtonVisibility = uiCore . AppViewBackButtonVisibility . collapsed ; e . handled = true ; } ; initSerial ( ) ; return hasActivationProjectAsync ( ) . then ( ( ) => { if ( importHexImpl ) { importHex = importHexImpl ; app . removeEventListener ( "activated" , initialActivationHandler ) ; app . addEventListener ( "activated" , fileActivationHandler ) ; } } ) ; } export function captureInitialActivation ( ) { if ( ! isWinRT ( ) ) { return ; } initialActivationDeferred = pxt . Util . defer < ActivationArgs > ( ) ; const app = Windows . UI . WebUI . WebUIApplication as any ; app . addEventListener ( "activated" , initialActivationHandler ) ; } export function loadActivationProject ( ) { return initialActivationDeferred . promise . then ( ( args ) => fileActivationHandler ( args , true ) ) ; } export function hasActivationProjectAsync ( ) { if ( ! isWinRT ( ) ) { return Promise . resolve ( false ) ; } initialActivationDeferred . resolve ( null ) ; return initialActivationDeferred . promise . then ( ( args ) => { return Promise . resolve ( args && args . kind === Windows . ApplicationModel . Activation . ActivationKind . file ) ; } ) ; } export function releaseAllDevicesAsync ( ) { if ( ! isWinRT ( ) ) { return Promise . resolve ( ) ; } return Promise . resolve ( ) . then ( ( ) => { if ( packetIO ) { pxt . log ( `disconnecting packetIO` ) ; return packetIO . disconnectAsync ( ) ; } return Promise . resolve ( ) ; } ) . catch ( ( e ) => { e . message = `error disconnecting packetIO: ${ e . message }` ; pxt . reportException ( e ) ; } ) . then ( ( ) => { pxt . log ( "suspending serial" ) ; return suspendSerialAsync ( ) ; } ) . catch ( ( e ) => { e . message = `error suspending serial: ${ e . message }` ; pxt . reportException ( e ) ; } ) ; } function initialActivationHandler ( args ) { Windows . UI . WebUI . WebUIApplication . removeEventListener ( "activated" , initialActivationHandler ) ; initialActivationDeferred . resolve ( args ) ; } function suspendingHandler ( args ) { pxt . log ( `suspending` ) ; const suspensionDeferral = args . suspendingOperation . getDeferral ( ) ; return releaseAllDevicesAsync ( ) . then ( ( ) => suspensionDeferral . complete ( ) , ( e ) => suspensionDeferral . complete ( ) ) . then ( ) ; } function resumingHandler ( args ) { pxt . log ( `resuming` ) ; if ( packetIO ) { pxt . log ( `reconnet pack io` ) ; packetIO . reconnectAsync ( ) ; } initSerial ( ) ; } let initialActivationDeferred ; let importHex ; function fileActivationHandler ( args , openHomeIfFailed = false ) { if ( args . kind === Windows . ApplicationModel . Activation . ActivationKind . file ) { let info = args as Windows . UI . WebUI . WebUIFileActivatedEventArgs ; let file = info . files . getAt ( 0 ) ; if ( file && file . isOfType ( Windows . Storage . StorageItemTypes . file ) ) { let f = file as Windows . Storage . StorageFile ; Windows . Storage . FileIO . readBufferAsync ( f ) . then ( buffer => { let ar = [ ] ; let dataReader = Windows . Storage . Streams . DataReader . fromBuffer ( buffer ) ; while ( dataReader . unconsumedBufferLength ) { ar . push ( dataReader . readByte ( ) ) ; } dataReader . close ( ) ; return pxt . cpp . unpackSourceFromHexAsync ( new Uint8Array ( ar ) ) ; } ) . then ( ( hex ) => importHex ( hex as unknown as pxt . cpp . HexFile , { openHomeIfFailed } ) ) ; } } } }