Python: Add support for assigning colors directly to brushes

This commit is contained in:
Simon Hausmann 2024-03-06 16:20:05 +01:00
parent 3c9b57ecf8
commit 3e66b98121
3 changed files with 12 additions and 2 deletions

View file

@ -32,7 +32,7 @@ enum PyColorInput {
#[pyclass] #[pyclass]
#[derive(Clone)] #[derive(Clone)]
pub struct PyColor { pub struct PyColor {
color: slint_interpreter::Color, pub color: slint_interpreter::Color,
} }
#[pymethods] #[pymethods]

View file

@ -31,7 +31,7 @@ def test_property_access():
in property <bool> boolprop: true; in property <bool> boolprop: true;
in property <image> imgprop; in property <image> imgprop;
in property <brush> brushprop: Colors.rgb(255, 0, 255); in property <brush> brushprop: Colors.rgb(255, 0, 255);
in property <color> colprop; in property <color> colprop: Colors.rgb(0, 255, 0);
in property <[string]> modelprop; in property <[string]> modelprop;
in property <MyStruct> structprop: { in property <MyStruct> structprop: {
title: "builtin", title: "builtin",
@ -104,6 +104,12 @@ def test_property_access():
brushval = instance.get_property("brushprop") brushval = instance.get_property("brushprop")
assert str(brushval.color) == "argb(255, 128, 128, 128)" assert str(brushval.color) == "argb(255, 128, 128, 128)"
brushval = instance.get_property("colprop")
assert str(brushval.color) == "argb(255, 0, 255, 0)"
instance.set_property("colprop", Color("rgb(128, 128, 128)"))
brushval = instance.get_property("colprop")
assert str(brushval.color) == "argb(255, 128, 128, 128)"
with pytest.raises(ValueError, match="no such property"): with pytest.raises(ValueError, match="no such property"):
instance.set_global_property("nonexistent", "theglobalprop", 42) instance.set_global_property("nonexistent", "theglobalprop", 42)
with pytest.raises(ValueError, match="no such property"): with pytest.raises(ValueError, match="no such property"):

View file

@ -75,6 +75,10 @@ impl FromPyObject<'_> for PyValue {
ob.extract::<PyRef<'_, crate::brush::PyBrush>>() ob.extract::<PyRef<'_, crate::brush::PyBrush>>()
.map(|pybrush| slint_interpreter::Value::Brush(pybrush.brush.clone())) .map(|pybrush| slint_interpreter::Value::Brush(pybrush.brush.clone()))
}) })
.or_else(|_| {
ob.extract::<PyRef<'_, crate::brush::PyColor>>()
.map(|pycolor| slint_interpreter::Value::Brush(pycolor.color.clone().into()))
})
.or_else(|_| { .or_else(|_| {
ob.extract::<PyRef<'_, crate::models::PyModelBase>>() ob.extract::<PyRef<'_, crate::models::PyModelBase>>()
.map(|pymodel| slint_interpreter::Value::Model(pymodel.as_model())) .map(|pymodel| slint_interpreter::Value::Model(pymodel.as_model()))