DS startup code viewer

Run this file-viewer code in DroidScript to see many of the secrets of how DroidScript runs itself

file_viewer.js.txt
"use strict"
var gTxeSearch, gCode, gLower,
      gFile = "/Sys/DroidScript.js"
 
function OnStart()
{
   var lay = app.CreateLayout("linear", "VCenter, FillXY")
   var layH = app.AddLayout(lay, "linear", "Horizontal")
   var bou = app.GetTextBounds( "WW",32 ).width
   gTxeSearch = app.AddTextEdit(layH, "", 1-bou);
   gTxeSearch.SetHint("Search")
   var btnSearch = app.AddButton(layH, "\uf00e", -1, -1,
      "fontAwesome")
   btnSearch.SetOnLongTouch();
   btnSearch.SetOnTouch(btnSearch_OnTouch);
   scroll = app.AddScroller(lay, 1, -1)
 
   var cont = app.ReadFile( gFile );
   gCode = app.CreateTextEdit(cont, -
      1, -1, "left,readonly,noKeyboard")
   scroll.AddChild(gCode)
 
   app.AddLayout(lay)
   gLower = cont.toLowerCase()
}
 
//Called when the search button is pressed
function btnSearch_OnTouch()
{
   var searchText = gTxeSearch.GetText()
   if(searchText === "") return
   var pause = doFind(searchText, gCode)
   if(this === gTxeSearch)
   {
      this.Focus()
      app.ShowKeyboard(this)
   }
}
 
function doFind(sought, thisedt, repeating)
{
   app.HideKeyboard();
   //thisedt = textEdit to be searched.
   //sought = string to be searched for.
   //both toLowerCase for case-insensitive search
   sought = sought.toLowerCase()
      //current cursor position is start for search
   var curs = thisedt.GetCursorPos()
   if(repeating) curs = -1
      //only compare against the searchable range
   var lc = gLower.slice(curs + 1)
      // try to find next occurence of sought
   var pos = lc.indexOf(sought); //string version
   //var pos = lc.search(sought);    //regex version
   if(pos > -1)
   {
      //we found it
      pos = pos + curs + 1
      thisedt.SetCursorPos(pos)
      thisedt.SetSelection(pos, pos + sought.length)
      var s = "Line:" + thisedt.GetCursorLine()
      app.ShowPopup(s, "short,bottom")
   }
   else
   {
      //not found - go to start of text
      //then search again once
      thisedt.SetCursorPos(0)
      thisedt.SetSelection(0, 0)
      if(!repeating) doFind(sought, thisedt, true)
      else app.ShowPopup("Not found", "bottom")
   }
   return ""
}