From 82fad654a463e0671f9a9f5aab4d1ba9559a3a34 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Wed, 28 Aug 2024 15:10:21 +0000 Subject: [PATCH] live-preview: Parse Color in property data --- tools/lsp/preview/ui.rs | 62 +++++++++++++++++++++++++++++++++++++++++ tools/lsp/ui/api.slint | 25 ++++++++++------- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/tools/lsp/preview/ui.rs b/tools/lsp/preview/ui.rs index 445f8940cdd..507e0c23357 100644 --- a/tools/lsp/preview/ui.rs +++ b/tools/lsp/preview/ui.rs @@ -280,6 +280,10 @@ fn simplify_value( let mut kind = PropertyValueKind::Code; let mut value_bool = false; + let mut value_red = 0; + let mut value_green = 0; + let mut value_blue = 0; + let mut value_alpha = 0; let mut value_int = 0; let mut value_float = 0.0; let mut value_string = String::new(); @@ -318,6 +322,27 @@ fn simplify_value( kind = PropertyValueKind::Integer; } } + langtype::Type::Color => { + eprintln!("Looking at color!"); + if let Some(expression) = expression { + if let Some(text) = expression.child_text(SyntaxKind::ColorLiteral) { + if let Some(color) = literals::parse_color_literal(&text) { + kind = PropertyValueKind::Color; + value_alpha = ((color & 0xff000000) >> 24) as i32; + value_red = ((color & 0x00ff0000) >> 16) as i32; + value_green = ((color & 0x0000ff00) >> 8) as i32; + value_blue = (color & 0x000000ff) as i32; + } else { + // TODO: Extract `Foo.bar` as Palette `Foo`, entry `bar`. + // This makes no sense right now, as we have no way to get any + // information on the palettes. + } + } + } else if code.is_empty() { + kind = PropertyValueKind::Color; + } + } + langtype::Type::Bool => { if let Some(expression) = expression { let qualified_name = @@ -384,6 +409,10 @@ fn simplify_value( PropertyValue { kind, value_bool, + value_red, + value_green, + value_blue, + value_alpha, value_string: value_string.into(), value_int, value_float, @@ -767,4 +796,37 @@ export component Test { in property test1; }"#, assert_eq!(result.kind, PropertyValueKind::Code); assert_eq!(result.value_int, 0); } + + #[test] + fn test_property_color() { + let result = + property_conversion_test(r#"export component Test { in property test1; }"#, 0); + assert_eq!(result.kind, PropertyValueKind::Color); + assert_eq!(result.value_red, 0); + assert_eq!(result.value_green, 0); + assert_eq!(result.value_blue, 0); + assert_eq!(result.value_alpha, 0); + + let result = property_conversion_test( + r#"export component Test { in property test1: #10203040; }"#, + 1, + ); + assert_eq!(result.kind, PropertyValueKind::Color); + assert_eq!(result.value_red, 0x10); + assert_eq!(result.value_green, 0x20); + assert_eq!(result.value_blue, 0x30); + assert_eq!(result.value_alpha, 0x40); + + let result = property_conversion_test( + r#"export component Test { in property test1: #10203040.darker(0.5); }"#, + 1, + ); + assert_eq!(result.kind, PropertyValueKind::Code); + + let result = property_conversion_test( + r#"export component Test { in property test1: Colors.red; }"#, + 0, + ); + assert_eq!(result.kind, PropertyValueKind::Code); + } } diff --git a/tools/lsp/ui/api.slint b/tools/lsp/ui/api.slint index 2847cc95c5a..f633a6d76fe 100644 --- a/tools/lsp/ui/api.slint +++ b/tools/lsp/ui/api.slint @@ -72,6 +72,7 @@ export struct Range { export enum PropertyValueKind { Boolean, Code, + Color, Enum, Float, Integer, @@ -80,17 +81,21 @@ export enum PropertyValueKind { /// Data about the property value for use in "simple" mode export struct PropertyValue { - value-bool: bool, - is-translatable: bool, + value-bool: bool, // Boolean + is-translatable: bool, // String kind: PropertyValueKind, - value-float: float, - value-int: int, - default-selection: int, - value-string: string, - visual-items: [string], - tr-context: string, - tr-plural: string, - code: string, + value-red: int, // Color + value-green: int, // Color + value-blue: int, // Color + value-alpha: int, // Color + value-float: float, // Float + value-int: int, // Integer + default-selection: int, // Enum + value-string: string, // Enum, String + visual-items: [string], // Enum + tr-context: string, // String + tr-plural: string, // String + code: string, // ALWAYS, empty if property is not explicitly defined } /// Important Ranges in the property definition