Skip to content

Commit

Permalink
add dialog.clipboard_paste(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
manatlan committed May 2, 2024
1 parent 8e4e4be commit c7229ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ dialog = ui.Dialog( self )

size is a float to force the percent of width on the dialog box. If None, it will use the default from the ui used.

### method dialog.confirm(obj, cbresponse=lambda bool:bool)
### method dialog.confirm(obj, cbresponse=lambda bool)

(like js window.confirm(...)) Display a modal dialog box containing the object 'obj' (obj must be str'able), and let the user click on Yes|No buttons, which will call the cbresponse callback with True or False ...

### method dialog.prompt(title, value=None, cbresponse=lambda val:val)
### method dialog.prompt(title, value=None, cbresponse=lambda val)

(like js window.prompt(...)) Display a modal dialog letting the user edit the `value` in an Input box, with a `title` (title must be str'able). When the user click the OK button the value is sent in the callback cbresponse. (clicking the cancel button does nothing, except close the dialog)

Expand Down Expand Up @@ -273,6 +273,10 @@ Close programatically, the current ui dialog.

Copy the txt in the clipboard

### method dialog.clipboard_paste( lambda content )

Call the callback with the text content from the clipboard. (user may need to authorize the interaction from the navigator)

### method dialog.download( name:str, content:bytes ):

Force the navigator to download a file named 'name', with the content bytes 'content' into the browser.
Expand Down
8 changes: 8 additions & 0 deletions htagui/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def init(self,parent):
def clipboard_copy(self,txt:str):
self.call(f"""navigator.clipboard.writeText(`{txt}`);""")

def clipboard_paste(self,callback=lambda x:x):
self.callback_clipboard_paste = callback
self.call("""navigator.clipboard.readText().then( self._clipboard_paste )""")

@expose
def _clipboard_paste(self,content:str):
return self.callback_clipboard_paste(content)

def download(self,name:str,content:bytes):
self.call( f"""
let anchor = document.createElement('a');
Expand Down
16 changes: 9 additions & 7 deletions manual_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,19 @@ def init(self,root):
self <= ui.Button("confirm", _onclick=lambda ev: self.ui.confirm("kkkk", self.ui.notify))
self <= ui.Button("prompt", _onclick=lambda ev: self.ui.prompt("What's your name?","value", self.ui.notify) )

self<= Tag.hr()+"dialog poppers"
self <= ui.Button("drawer left", _onclick=lambda ev: self.ui.drawer( ui.Menu(entries),"left" ))
self<= Tag.hr()+"dialog drawers"
self <= ui.Button("drawer left", _onclick=lambda ev: self.ui.drawer( ui.Menu(entries) + "click on the menu should auto-close the dialog","left" ) )
self <= ui.Button("drawer right", _onclick=lambda ev: self.ui.drawer( "yo","right" ))
self <= ui.Button("drawer top", _onclick=lambda ev: self.ui.drawer( "yo","top" ))
self <= ui.Button("drawer bottom", _onclick=lambda ev: self.ui.drawer( "yo","bottom" ))

self<= Tag.hr()+"dialog blockers"

def block(ev):
self.ui.block( Tag.div(ui.Spinner()+ui.Button("unblock",_onclick=lambda ev: self.ui.close() ) + ui.Menu(entries)) )
self.ui.block( Tag.div(ui.Spinner()+ui.Button("unblock",_onclick=lambda ev: self.ui.close() ) + ui.Menu(entries)) + "click on the menu should auto-close the dialog" )

def page(ev):
self.ui.page( ui.Menu(entries) + Tag.h3("Click the image to quit")+Tag.img(_src="https://picsum.photos/501/501",_onclick=lambda ev: self.ui.page()) )
self.ui.page( "click on the menu shouldn't close the dialog !!!"+ui.Menu(entries) + Tag.h3("Click the image to quit")+Tag.img(_src="https://picsum.photos/501/501",_onclick=lambda ev: self.ui.page()) )

self <= ui.Button("block", _onclick=block)
self <= ui.Button("page", _onclick=page)
Expand All @@ -171,15 +171,17 @@ def test_yield(v):
self <= ui.Button("test cb (async) yield", _onclick=lambda ev: self.ui.prompt("value","?", atest_yield))
self <= ui.Button("test cb yield", _onclick=lambda ev: self.ui.prompt("value","?", test_yield))

self <= Tag.hr()
self <= Tag.hr() + "specials"

def copy(ev):
self.ui.clipboard_copy(str(time.time()))
self.ui.notify("copied")

self <= ui.Button("copy into clipboard", _onclick=copy)
self <= ui.Button("paste from clipboard", _onclick=lambda ev: self.ui.clipboard_paste(lambda content: self.ui.notify(f'Content from clipboard {content}')))
self <= ui.Button("download", _onclick=lambda ev: self.ui.download("myfile.txt",b"my content"))

self <= Tag.hr()
self<= Tag.hr()+"dialog poppers"

def pop_in_dialog(ev):
o=Tag.div("hello")
Expand Down Expand Up @@ -229,8 +231,8 @@ def init(self):
self <= Tag.my("basics",_onclick=lambda ev: self.restart_with("basics"),_class=f("basics"))
self <= Tag.my("bulma",_onclick=lambda ev: self.restart_with("bulma"),_class=f("bulma"))
self <= Tag.my("shoelace",_onclick=lambda ev: self.restart_with("shoelace"),_class=f("shoelace"))
self <= Tag.my("md",_onclick=lambda ev: self.restart_with("md"),_class=f("md"))
self <= Tag.my("fluent",_onclick=lambda ev: self.restart_with("fluent"),_class=f("fluent"))
self <= Tag.my("md (the worst)",_onclick=lambda ev: self.restart_with("md"),_class=f("md"))

def setter(o,testobject):
for i in o.parent.childs:
Expand Down

0 comments on commit c7229ec

Please sign in to comment.