Skip to content

Commit

Permalink
updated the bindings for version 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
tareksander committed Nov 13, 2021
1 parent 0b06902 commit ec0f84f
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 11 deletions.
178 changes: 177 additions & 1 deletion TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Now it displays "Hello World!" for 5 seconds, "Goodbye world!" for 5 seconds and
With that you just created your first dynamic layout.
Congratulations!

[helloworld.py](tutorial/helloworld.py)<!-- @IGNORE PREVIOUS: link -->

## Layout hierarchy

Expand Down Expand Up @@ -118,14 +119,15 @@ button = tg.createbutton(main, a, "Click here!", root)


# Now we give the Layout priority to our content Textview so it is bigger than the Button and the title.
# This priority value is called weight and determines how much of the available space goes to each View.
tg.setlinearlayoutparams(main, a, content, 10)



time.sleep(5)
```


[hellolayout.py](tutorial/hellolayout.py)<!-- @IGNORE PREVIOUS: link -->

## Events

Expand Down Expand Up @@ -173,6 +175,8 @@ time.sleep(5)
print("button pressed", count, "times")
```

[helloevents.py](tutorial/helloevents.py)<!-- @IGNORE PREVIOUS: link -->

## Picture-in-picture and images

Now let's make something you would actually want to use:
Expand Down Expand Up @@ -207,25 +211,197 @@ time.sleep(5)

The program will display the image you specified as a command line argument for 5 seconds in a small window.

[helloimage.py](tutorial/helloimage.py)<!-- @IGNORE PREVIOUS: link -->

## Advanced LinearLayout usage

Currently only LinearLayout is supported for laying out the Views on the screen.
It's a simple Layout, but if you nest multiple LinearLayouts and configure them, it can be very flexible.

It happens often that you want Views to only occupy the space they need in a LinearLayout instead of getting an equal share of the space.

```python
import termuxgui as tg
import sys
import time


ret = tg.connect()
if ret == None:
sys.exit()
main, event = ret

a, t = tg.activity(main)



layout = tg.createlinearlayout(main, a)

# Create 3 TextViews
tv1 = tg.createtextview(main, a, "TextView 1", layout)
tv2 = tg.createtextview(main, a, "TextView 2", layout)
tv3 = tg.createtextview(main, a, "TextView 3", layout)

# Now we make them only occupy the space they need.
# We first have to set the Layout weight to 0 to prevent them from using the free space.
tg.setlinearlayoutparams(main, a, tv1, 0)
tg.setlinearlayoutparams(main, a, tv2, 0)
tg.setlinearlayoutparams(main, a, tv3, 0)

# Then we have to set the height to "WRAP_CONTENT".
# You can specify width and height in 3 ways: as an integer in dp, "WRAP_CONTENT" and "MATCH_PARENT".
# "WRAP_CONTENT" makes a View occupy only the space it needs.
# "MATCH_PARENT" makes a view as large as the parent Layout in that dimension.

# Since the TextViews are displayed in a list, we set the height to "WRAP_CONTENT".
tg.setheight(main, a, tv1, "WRAP_CONTENT")
tg.setheight(main, a, tv2, "WRAP_CONTENT")
tg.setheight(main, a, tv3, "WRAP_CONTENT")


time.sleep(5)
```

[linearlayout1.py](tutorial/linearlayout1.py)<!-- @IGNORE PREVIOUS: link -->


Let's add a row of buttons and also make them as small as they need to be.


```python
import termuxgui as tg
import sys
import time


ret = tg.connect()
if ret == None:
sys.exit()
main, event = ret

a, t = tg.activity(main)



layout = tg.createlinearlayout(main, a)

# Create 3 TextViews
tv1 = tg.createtextview(main, a, "TextView 1", layout)
tv2 = tg.createtextview(main, a, "TextView 2", layout)
buttons = tg.createlinearlayout(main, a, layout, False) # use False to create this as a horizontal Layout
tv3 = tg.createtextview(main, a, "TextView 3", layout)

# Now we make them only occupy the space they need.
# We first have to set the Layout weight to 0 to prevent them from using the free space.
tg.setlinearlayoutparams(main, a, tv1, 0)
tg.setlinearlayoutparams(main, a, tv2, 0)
tg.setlinearlayoutparams(main, a, buttons, 0)
tg.setlinearlayoutparams(main, a, tv3, 0)

# Then we have to set the height to "WRAP_CONTENT".
# You can specify width and height in 3 ways: as an integer in dp, "WRAP_CONTENT" and "MATCH_PARENT".
# "WRAP_CONTENT" makes a View occupy only the space it needs.
# "MATCH_PARENT" makes a view as large as the parent Layout in that dimension.

# Since the TextViews are displayed in a list, we set the height to "WRAP_CONTENT".
tg.setheight(main, a, tv1, "WRAP_CONTENT")
tg.setheight(main, a, tv2, "WRAP_CONTENT")
tg.setheight(main, a, buttons, "WRAP_CONTENT")
tg.setheight(main, a, tv3, "WRAP_CONTENT")


tg.createbutton(main, a, "Button1", buttons)
tg.createbutton(main, a, "Button2", buttons)
tg.createbutton(main, a, "Button3", buttons)

time.sleep(5)
```

As you can see, for nested LinearLayouts it is enough to set the height and weight of the nested Layout "WRAP_CONTENT" and 0.


[linearlayout2.py](tutorial/linearlayout2.py)<!-- @IGNORE PREVIOUS: link -->


## Inputs

Currently supported inputs are EditText, Button and Checkbox.
Let's use our new LineaLayout knowledge to make a custom input dialog.
The make it a practical example, we will make a dialog frontend for the `youtubedr` package to download videos.
You can install that package if you want to try it out, but the UI works without that.

```python
import termuxgui as tg
import sys
import time
from subprocess import run

ret = tg.connect()
if ret == None:
sys.exit()
main, event = ret

a, t = tg.activity(main, dialog=True) # make this activity a dialog

layout = tg.createlinearlayout(main, a)

title = tg.createtextview(main, a, "Download Video", layout)
tg.settextsize(main, a, title, 30)

# Let's also create a small margin around the title so it looks nicer.
tg.setmargin(main, a, title, 5)


# For dialogs, we don't need to set "WRAP_CONTENT", in dialogs views are automatically packed as close as possible.

tv1 = tg.createtextview(main, a, "Video link:", layout)
et1 = tg.createedittext(main, a, "", layout)

tv2 = tg.createtextview(main, a, "Filename (empty for automatic filename):", layout)
et2 = tg.createedittext(main, a, "", layout)


# This creates an unchecked Checkbox
check = tg.createcheckbox(main, a, "high quality", False, layout)

# Create 2 buttons next to each other
buttons = tg.createlinearlayout(main, a, layout, True)

dl = tg.createbutton(main, a, "download", buttons)
cancel = tg.createbutton(main, a, "cancel", buttons)


hd = False

while True:
ev = tg.getevent(event)
if ev["type"] == "destroy" and ev["value"]["finishing"]:
sys.exit()
# Checkboxes also emit a click event when clicked, but they have the extra value "set" indicating whether the box is now checked or unchecked
if ev["type"] == "click" and ev["value"]["id"] == check:
hd = ev["value"]["set"]
if ev["type"] == "click" and ev["value"]["id"] == dl:
link = tg.gettext(main, a, et1)
name = tg.gettext(main, a, et2)
args = ["youtubedr", "download"]
if len(name) != 0:
args.extend(["-o", name])
if hd:
args.extend(["-q", "1080p"])
args.append(link)
if len(link) != 0:
try:
tg.finishactivity(main, a)
run(args)
except:
pass
tg.finishtask(main, t)
if ev["type"] == "click" and ev["value"]["id"] == cancel:
tg.finishactivity(main, a) # this handily also exits the program, because finishing the activity destroys it, and that event is send to us
```


[inputs.py](tutorial/inputs.py)<!-- @IGNORE PREVIOUS: link -->



Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = termuxgui
version = 0.1.1
version = 0.1.2
author = Tarek Sander
description = Python bindings for the Termux:GUI plugin.
long_description = file: README.md
Expand Down
2 changes: 1 addition & 1 deletion src/termuxgui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__all__ = ["activity", "bringtasktofront", "connect", "create", "event", "finishactivity",
"finishtask", "setinputmode", "setpipparams", "settaskdescription", "settheme", "totermux", "viewactions"]
"finishtask", "setinputmode", "setpipmode", "setpipparams", "settaskdescription", "settheme", "toast", "totermux", "viewactions"]

for m in __all__:
exec("from termuxgui."+m+" import *")
Expand Down
30 changes: 28 additions & 2 deletions src/termuxgui/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,39 @@

from termuxgui.__send_read_msg import __send_read_msg

def activity(mainSocket, tid=None,flags=0,dialog=None,pip=False):
def activity(mainSocket, tid=None,dialog=None,pip=False,overlay=None,lockscreen=None):
'''Creates and Activity and returns the Activity and Task id.'''
params = {"flags": flags}
params = {}
if dialog != None:
params["dialog"] = dialog
if tid != None:
params["tid"] = tid
if pip != None:
params["pip"] = pip
if overlay != None:
params["overlay"] = overlay
if lockscreen != None:
params["lockscreen"] = lockscreen
return __send_read_msg(mainSocket, dumps({"method": "newActivity", "params": params}))



def keepscreenon(mainSocket, aid, on):
__send_msg(mainSocket, dumps({"method": "keepScreenOn", "params": {"aid": aid, "on": on}}))



def setorientation(mainSocket, aid, orientation):
__send_msg(mainSocket, dumps({"method": "setOrientation", "params": {"aid": aid, "orientation": orientation}}))


def setposition(mainSocket, aid, x, y):
__send_msg(mainSocket, dumps({"method": "setPosition", "params": {"aid": aid, "x": x, "y": y}}))


def setposition(mainSocket, aid, x, y):
__send_msg(mainSocket, dumps({"method": "setPosition", "params": {"aid": aid, "x": x, "y": y}}))

def sendoverlayevents(mainSocket, aid, send):
__send_msg(mainSocket, dumps({"method": "sendOverlayTouchEvent", "params": {"aid": aid, "send": send}}))

4 changes: 4 additions & 0 deletions src/termuxgui/bringtasktofront.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
def bringtasktofront(mainSocket, tid):
'''Bring an existing Task to the front and shows it.'''
__send_msg(mainSocket, dumps({"method": "bringTaskToFront", "params": {"tid": tid}}))


def movetasktoback(mainSocket, tid):
__send_msg(mainSocket, dumps({"method": "moveTaskToBack", "params": {"tid": tid}}))
12 changes: 6 additions & 6 deletions src/termuxgui/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from termuxgui.__send_read_msg import __send_read_msg


def createlinearlayout(mainSocket, aid, parent=None):
args = {"aid": aid}
def createlinearlayout(mainSocket, aid, parent=None, vertical=True):
args = {"aid": aid, "vertical": vertical}
if parent != None:
args["parent"] = parent
return __send_read_msg(mainSocket, dumps({"method": "createLinearLayout", "params": args}))
Expand All @@ -30,8 +30,8 @@ def createtextview(mainSocket, aid, text, parent=None):
return __send_read_msg(mainSocket, dumps({"method": "createTextView", "params": args}))


def createedittext(mainSocket, aid, text, parent=None):
args = {"aid": aid, "text": text}
def createedittext(mainSocket, aid, text, parent=None, singleline=False, line=True):
args = {"aid": aid, "text": text, "singleline": singleline, "line": line}
if parent != None:
args["parent"] = parent
return __send_read_msg(mainSocket, dumps({"method": "createEditText", "params": args}))
Expand All @@ -43,8 +43,8 @@ def createbutton(mainSocket, aid, text, parent=None):
return __send_read_msg(mainSocket, dumps({"method": "createButton", "params": args}))


def createcheckbox(mainSocket, aid, checked=False, parent=None):
args = {"aid": aid, "checked": checked}
def createcheckbox(mainSocket, aid, text, checked=False, parent=None):
args = {"aid": aid, "checked": checked, "text": text}
if parent != None:
args["parent"] = parent
return __send_read_msg(mainSocket, dumps({"method": "createCheckbox", "params": args}))
Expand Down
9 changes: 9 additions & 0 deletions src/termuxgui/setpipmode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from json import dumps

from termuxgui.__send_msg import __send_msg

def setpipmode(mainSocket, aid, pip):
__send_msg(mainSocket, dumps({"method": "setPiPMode", "params": {"aid": aid, "pip": pip}}))

def setpipmodeauto(mainSocket, aid, pip):
__send_msg(mainSocket, dumps({"method": "setPiPModeAuto", "params": {"aid": aid, "pip": pip}}))
6 changes: 6 additions & 0 deletions src/termuxgui/toast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from json import dumps

from termuxgui.__send_msg import __send_msg

def toast(mainSocket, text, long=False):
__send_msg(mainSocket, dumps({"method": "toast", "params": {"text": text, "long": long}}))
13 changes: 13 additions & 0 deletions src/termuxgui/viewactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
from termuxgui.__send_msg import __send_msg



def showcursor(mainSocket, aid, id, show):
__send_msg(mainSocket, dumps({"method": "showCursor", "params": {"aid": aid, "id": id, "show": show}}))


def deleteview(mainSocket, aid, id):
__send_msg(mainSocket, dumps({"method": "deleteView", "params": {"aid": aid, "id": id}}))

def clearchildren(mainSocket, aid, id):
__send_msg(mainSocket, dumps({"method": "deleteChildren", "params": {"aid": aid, "id": id}}))

def settextsize(mainSocket, aid, id, size):
__send_msg(mainSocket, dumps({"method": "setTextSize", "params": {"aid": aid, "id": id, "size": size}}))

Expand All @@ -28,6 +36,11 @@ def setmargin(mainSocket, aid, id, margin, dir=None):
def setlinearlayoutparams(mainSocket, aid, id, weight):
__send_msg(mainSocket, dumps({"method": "setLinearLayoutParams", "params": {"aid": aid, "id": id, "weight": weight}}))

def setwidth(mainSocket, aid, id, width):
__send_msg(mainSocket, dumps({"method": "setWidth", "params": {"aid": aid, "id": id, "width": width}}))

def setheight(mainSocket, aid, id, height):
__send_msg(mainSocket, dumps({"method": "setHeight", "params": {"aid": aid, "id": id, "height": height}}))

def settext(mainSocket, aid, id, text):
__send_msg(mainSocket, dumps({"method": "setText", "params": {"aid": aid, "id": id, "text": text}}))
Expand Down
2 changes: 2 additions & 0 deletions tutorial/helloevents.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python3

import termuxgui as tg
import sys
import time
Expand Down
2 changes: 2 additions & 0 deletions tutorial/helloimage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python3

import termuxgui as tg
import sys
import time
Expand Down
2 changes: 2 additions & 0 deletions tutorial/hellolayout.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python3

import termuxgui as tg
import sys
import time
Expand Down
Loading

0 comments on commit ec0f84f

Please sign in to comment.