mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-17 22:08:39 +00:00
live-preview: Understand conic gradient
The slint part is missing
This commit is contained in:
parent
0796bb1db4
commit
cb13a0bd2c
5 changed files with 72 additions and 2 deletions
|
@ -241,6 +241,15 @@ fn eval_expression(
|
|||
},
|
||||
)),
|
||||
)),
|
||||
Expression::ConicGradient { stops } => Value::Brush(slint::Brush::ConicGradient(
|
||||
i_slint_core::graphics::ConicGradientBrush::new(stops.iter().map(|(color, stop)| {
|
||||
let color =
|
||||
eval_expression(color, local_context, None).try_into().unwrap_or_default();
|
||||
let position =
|
||||
eval_expression(stop, local_context, None).try_into().unwrap_or_default();
|
||||
i_slint_core::graphics::GradientStop { color, position }
|
||||
})),
|
||||
)),
|
||||
Expression::EnumerationValue(value) => {
|
||||
Value::EnumerationValue(value.enumeration.name.to_string(), value.to_string())
|
||||
}
|
||||
|
|
|
@ -740,6 +740,25 @@ fn map_value_and_type(
|
|||
..Default::default()
|
||||
});
|
||||
}
|
||||
slint::Brush::ConicGradient(cg) => {
|
||||
mapping.headers.push(mapping.name_prefix.clone());
|
||||
mapping.current_values.push(PropertyValue {
|
||||
display_string: SharedString::from("Conic Gradient"),
|
||||
kind: PropertyValueKind::Brush,
|
||||
value_kind: PropertyValueKind::Brush,
|
||||
brush_kind: BrushKind::Conic,
|
||||
value_brush: slint::Brush::ConicGradient(cg.clone()),
|
||||
gradient_stops: Rc::new(VecModel::from(
|
||||
cg.stops()
|
||||
.map(|gs| GradientStop { color: gs.color, position: gs.position })
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
.into(),
|
||||
accessor_path: mapping.name_prefix.clone(),
|
||||
code: get_code(value),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
_ => {
|
||||
mapping.headers.push(mapping.name_prefix.clone());
|
||||
mapping.current_values.push(PropertyValue {
|
||||
|
|
|
@ -7,6 +7,7 @@ use slint::{ComponentHandle, Model, VecModel};
|
|||
|
||||
use crate::preview::ui;
|
||||
|
||||
use itertools::Itertools as _;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn setup(ui: &ui::PreviewUi) {
|
||||
|
@ -95,10 +96,20 @@ fn as_slint_brush(
|
|||
match kind {
|
||||
ui::BrushKind::Solid => color_to_string(color),
|
||||
ui::BrushKind::Linear => {
|
||||
format!("@linear-gradient({angle}deg{})", stops_as_string(stops)).into()
|
||||
slint::format!("@linear-gradient({angle}deg{})", stops_as_string(stops))
|
||||
}
|
||||
ui::BrushKind::Radial => {
|
||||
format!("@radial-gradient(circle{})", stops_as_string(stops)).into()
|
||||
slint::format!("@radial-gradient(circle{})", stops_as_string(stops))
|
||||
}
|
||||
ui::BrushKind::Conic => {
|
||||
let stops = sorted_gradient_stops(stops);
|
||||
slint::format!(
|
||||
"@conic-gradient({})",
|
||||
stops
|
||||
.iter()
|
||||
.map(|s| format!("{} {}deg", color_to_string(s.color), s.position * 360.0))
|
||||
.join(", ")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -131,6 +142,9 @@ pub fn create_brush(
|
|||
ui::BrushKind::Radial => slint::Brush::RadialGradient(
|
||||
i_slint_core::graphics::RadialGradientBrush::new_circle(stops.drain(..)),
|
||||
),
|
||||
ui::BrushKind::Conic => slint::Brush::ConicGradient(
|
||||
i_slint_core::graphics::ConicGradientBrush::new(stops.drain(..)),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -970,6 +970,7 @@ export component Test { in property <Foobar> test1; }"#,
|
|||
|
||||
#[test]
|
||||
fn test_property_brush() {
|
||||
use crate::preview::ui::GradientStop;
|
||||
let result =
|
||||
property_conversion_test(r#"export component Test { in property <brush> test1; }"#, 0);
|
||||
assert_eq!(result.value_kind, ui::PropertyValueKind::Brush);
|
||||
|
@ -1060,6 +1061,32 @@ export component Test { in property <Foobar> test1; }"#,
|
|||
assert_eq!(result.value_kind, ui::PropertyValueKind::Brush);
|
||||
assert_eq!(result.kind, ui::PropertyValueKind::Code);
|
||||
assert!(matches!(result.brush_kind, ui::BrushKind::Radial));
|
||||
|
||||
let result = property_conversion_test(
|
||||
r#"export component Test { in property <brush> test1: @conic-gradient(white 36deg, #239 126deg, red 306deg); }"#,
|
||||
1,
|
||||
);
|
||||
assert_eq!(result.value_kind, ui::PropertyValueKind::Brush);
|
||||
assert_eq!(result.kind, ui::PropertyValueKind::Brush);
|
||||
assert_eq!(result.brush_kind, ui::BrushKind::Conic);
|
||||
assert_eq!(
|
||||
result.gradient_stops.iter().collect::<Vec<_>>(),
|
||||
[
|
||||
GradientStop {
|
||||
color: slint::Color::from_rgb_u8(0xff, 0xff, 0xff,),
|
||||
position: 36.0 / 360.0
|
||||
},
|
||||
GradientStop {
|
||||
color: slint::Color::from_rgb_u8(0x22, 0x33, 0x99),
|
||||
position: 126.0 / 360.0
|
||||
},
|
||||
GradientStop {
|
||||
color: slint::Color::from_rgb_u8(0xff, 0x00, 0x00),
|
||||
position: 306.0 / 360.0
|
||||
},
|
||||
]
|
||||
);
|
||||
assert_eq!(result.code, "@conic-gradient(white 36deg, #239 126deg, red 306deg)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -118,6 +118,7 @@ export enum BrushKind {
|
|||
solid,
|
||||
linear,
|
||||
radial,
|
||||
conic,
|
||||
}
|
||||
|
||||
export struct GradientStop {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue