Python: Add bindings for Brush and Color

cc #4202
This commit is contained in:
Simon Hausmann 2024-02-26 11:41:57 +01:00 committed by Simon Hausmann
parent f62d5e0d4b
commit 80fda11e0f
8 changed files with 289 additions and 12 deletions

View file

@ -3,9 +3,13 @@
import pytest
from slint import slint as native
from slint.slint import ValueType, PyImage;
from slint.slint import ValueType, PyImage
import os
Color = native.PyColor
Brush = native.PyBrush
def test_property_access():
compiler = native.ComponentCompiler()
@ -26,7 +30,7 @@ def test_property_access():
in property <float> floatprop: 100;
in property <bool> boolprop: true;
in property <image> imgprop;
in property <brush> brushprop;
in property <brush> brushprop: Colors.rgb(255, 0, 255);
in property <color> colprop;
in property <[string]> modelprop;
in property <MyStruct> structprop: {
@ -74,7 +78,8 @@ def test_property_access():
assert isinstance(structval, dict)
assert structval == {'title': 'builtin', 'finished': True}
instance.set_property("structprop", {'title': 'new', 'finished': False})
assert instance.get_property("structprop") == {'title': 'new', 'finished': False}
assert instance.get_property("structprop") == {
'title': 'new', 'finished': False}
imageval = instance.get_property("imageprop")
assert imageval.width == 320
@ -84,7 +89,8 @@ def test_property_access():
with pytest.raises(RuntimeError, match="The image cannot be loaded"):
PyImage.load_from_path("non-existent.png")
instance.set_property("imageprop", PyImage.load_from_path(os.path.join(os.path.dirname(__file__), "../../../examples/iot-dashboard/images/humidity.png")))
instance.set_property("imageprop", PyImage.load_from_path(os.path.join(
os.path.dirname(__file__), "../../../examples/iot-dashboard/images/humidity.png")))
imageval = instance.get_property("imageprop")
assert imageval.size == (36, 36)
assert "humidity.png" in imageval.path
@ -92,6 +98,12 @@ def test_property_access():
with pytest.raises(TypeError, match="'int' object cannot be converted to 'PyString'"):
instance.set_property("structprop", {42: 'wrong'})
brushval = instance.get_property("brushprop")
assert str(brushval.color) == "argb(255, 255, 0, 255)"
instance.set_property("brushprop", Brush(Color("rgb(128, 128, 128)")))
brushval = instance.get_property("brushprop")
assert str(brushval.color) == "argb(255, 128, 128, 128)"
with pytest.raises(ValueError, match="no such property"):
instance.set_global_property("nonexistent", "theglobalprop", 42)
with pytest.raises(ValueError, match="no such property"):
@ -101,6 +113,7 @@ def test_property_access():
instance.set_global_property("TestGlobal", "theglobalprop", "Ok")
assert instance.get_global_property("TestGlobal", "theglobalprop") == "Ok"
def test_callbacks():
compiler = native.ComponentCompiler()
@ -127,7 +140,8 @@ def test_callbacks():
assert instance.invoke("test-callback", "foo") == "local foo"
assert instance.invoke_global("TestGlobal", "globallogic", "foo") == "global foo"
assert instance.invoke_global(
"TestGlobal", "globallogic", "foo") == "global foo"
with pytest.raises(RuntimeError, match="no such callback"):
instance.set_callback("non-existent", lambda x: x)
@ -138,15 +152,19 @@ def test_callbacks():
with pytest.raises(RuntimeError, match="no such callback"):
instance.set_global_callback("TestGlobal", "non-existent", lambda x: x)
instance.set_global_callback("TestGlobal", "globallogic", lambda x: "python global " + x)
assert instance.invoke_global("TestGlobal", "globallogic", "foo") == "python global foo"
instance.set_global_callback(
"TestGlobal", "globallogic", lambda x: "python global " + x)
assert instance.invoke_global(
"TestGlobal", "globallogic", "foo") == "python global foo"
instance.set_callback("void-callback", lambda : None)
instance.set_callback("void-callback", lambda: None)
instance.invoke("void-callback")
if __name__ == "__main__":
import slint
instance = slint.load_file("../../examples/printerdemo/ui/printerdemo.slint")
instance.set_global_callback("PrinterQueue", "start-job", lambda title: print(f"new print job {title}"))
instance = slint.load_file(
"../../examples/printerdemo/ui/printerdemo.slint")
instance.set_global_callback(
"PrinterQueue", "start-job", lambda title: print(f"new print job {title}"))
instance.run()