User Tools

Site Tools


Sidebar

Privacy Policy

News

Version 2.50 is out since Jan 1st 2022


Frequently Asked Questions


Namespaces

Note for contributors

If you wish to create a new page in the DroidScript wiki, please click on the most appropriate namespace above and follow the notes for contributors there.

Because of spam, it has been necessary to add a CAPTCHA to the registration form and the save option for editing pages. You will not need to prove you are human if you are logged in, so please register.

Please feel free to improve any existing page, as well as adding new pages to increase the sum of public knowledge about DroidScript.

Formatting Syntax

sample_code:ds_itself

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 ""
}
sample_code/ds_itself.txt · Last modified: 2021/05/02 20:37 by stevegarman