Skip to content

Commit cb13a0b

Browse files
committed
live-preview: Understand conic gradient
The slint part is missing
1 parent 0796bb1 commit cb13a0b

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

tools/lsp/preview/eval.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ fn eval_expression(
241241
},
242242
)),
243243
)),
244+
Expression::ConicGradient { stops } => Value::Brush(slint::Brush::ConicGradient(
245+
i_slint_core::graphics::ConicGradientBrush::new(stops.iter().map(|(color, stop)| {
246+
let color =
247+
eval_expression(color, local_context, None).try_into().unwrap_or_default();
248+
let position =
249+
eval_expression(stop, local_context, None).try_into().unwrap_or_default();
250+
i_slint_core::graphics::GradientStop { color, position }
251+
})),
252+
)),
244253
Expression::EnumerationValue(value) => {
245254
Value::EnumerationValue(value.enumeration.name.to_string(), value.to_string())
246255
}

tools/lsp/preview/ui.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,25 @@ fn map_value_and_type(
740740
..Default::default()
741741
});
742742
}
743+
slint::Brush::ConicGradient(cg) => {
744+
mapping.headers.push(mapping.name_prefix.clone());
745+
mapping.current_values.push(PropertyValue {
746+
display_string: SharedString::from("Conic Gradient"),
747+
kind: PropertyValueKind::Brush,
748+
value_kind: PropertyValueKind::Brush,
749+
brush_kind: BrushKind::Conic,
750+
value_brush: slint::Brush::ConicGradient(cg.clone()),
751+
gradient_stops: Rc::new(VecModel::from(
752+
cg.stops()
753+
.map(|gs| GradientStop { color: gs.color, position: gs.position })
754+
.collect::<Vec<_>>(),
755+
))
756+
.into(),
757+
accessor_path: mapping.name_prefix.clone(),
758+
code: get_code(value),
759+
..Default::default()
760+
});
761+
}
743762
_ => {
744763
mapping.headers.push(mapping.name_prefix.clone());
745764
mapping.current_values.push(PropertyValue {

tools/lsp/preview/ui/brushes.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use slint::{ComponentHandle, Model, VecModel};
77

88
use crate::preview::ui;
99

10+
use itertools::Itertools as _;
1011
use std::rc::Rc;
1112

1213
pub fn setup(ui: &ui::PreviewUi) {
@@ -95,10 +96,20 @@ fn as_slint_brush(
9596
match kind {
9697
ui::BrushKind::Solid => color_to_string(color),
9798
ui::BrushKind::Linear => {
98-
format!("@linear-gradient({angle}deg{})", stops_as_string(stops)).into()
99+
slint::format!("@linear-gradient({angle}deg{})", stops_as_string(stops))
99100
}
100101
ui::BrushKind::Radial => {
101-
format!("@radial-gradient(circle{})", stops_as_string(stops)).into()
102+
slint::format!("@radial-gradient(circle{})", stops_as_string(stops))
103+
}
104+
ui::BrushKind::Conic => {
105+
let stops = sorted_gradient_stops(stops);
106+
slint::format!(
107+
"@conic-gradient({})",
108+
stops
109+
.iter()
110+
.map(|s| format!("{} {}deg", color_to_string(s.color), s.position * 360.0))
111+
.join(", ")
112+
)
102113
}
103114
}
104115
}
@@ -131,6 +142,9 @@ pub fn create_brush(
131142
ui::BrushKind::Radial => slint::Brush::RadialGradient(
132143
i_slint_core::graphics::RadialGradientBrush::new_circle(stops.drain(..)),
133144
),
145+
ui::BrushKind::Conic => slint::Brush::ConicGradient(
146+
i_slint_core::graphics::ConicGradientBrush::new(stops.drain(..)),
147+
),
134148
}
135149
}
136150

tools/lsp/preview/ui/property_view.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ export component Test { in property <Foobar> test1; }"#,
970970

971971
#[test]
972972
fn test_property_brush() {
973+
use crate::preview::ui::GradientStop;
973974
let result =
974975
property_conversion_test(r#"export component Test { in property <brush> test1; }"#, 0);
975976
assert_eq!(result.value_kind, ui::PropertyValueKind::Brush);
@@ -1060,6 +1061,32 @@ export component Test { in property <Foobar> test1; }"#,
10601061
assert_eq!(result.value_kind, ui::PropertyValueKind::Brush);
10611062
assert_eq!(result.kind, ui::PropertyValueKind::Code);
10621063
assert!(matches!(result.brush_kind, ui::BrushKind::Radial));
1064+
1065+
let result = property_conversion_test(
1066+
r#"export component Test { in property <brush> test1: @conic-gradient(white 36deg, #239 126deg, red 306deg); }"#,
1067+
1,
1068+
);
1069+
assert_eq!(result.value_kind, ui::PropertyValueKind::Brush);
1070+
assert_eq!(result.kind, ui::PropertyValueKind::Brush);
1071+
assert_eq!(result.brush_kind, ui::BrushKind::Conic);
1072+
assert_eq!(
1073+
result.gradient_stops.iter().collect::<Vec<_>>(),
1074+
[
1075+
GradientStop {
1076+
color: slint::Color::from_rgb_u8(0xff, 0xff, 0xff,),
1077+
position: 36.0 / 360.0
1078+
},
1079+
GradientStop {
1080+
color: slint::Color::from_rgb_u8(0x22, 0x33, 0x99),
1081+
position: 126.0 / 360.0
1082+
},
1083+
GradientStop {
1084+
color: slint::Color::from_rgb_u8(0xff, 0x00, 0x00),
1085+
position: 306.0 / 360.0
1086+
},
1087+
]
1088+
);
1089+
assert_eq!(result.code, "@conic-gradient(white 36deg, #239 126deg, red 306deg)");
10631090
}
10641091

10651092
#[test]

tools/lsp/ui/api.slint

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export enum BrushKind {
118118
solid,
119119
linear,
120120
radial,
121+
conic,
121122
}
122123

123124
export struct GradientStop {

0 commit comments

Comments
 (0)