Skip to content

Commit

Permalink
live-preview: Parse Color in property data
Browse files Browse the repository at this point in the history
  • Loading branch information
hunger committed Aug 29, 2024
1 parent 4bb8238 commit 82fad65
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
62 changes: 62 additions & 0 deletions tools/lsp/preview/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -767,4 +796,37 @@ export component Test { in property <Foobar> 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 <color> 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 <color> 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 <color> test1: #10203040.darker(0.5); }"#,
1,
);
assert_eq!(result.kind, PropertyValueKind::Code);

let result = property_conversion_test(
r#"export component Test { in property <int> test1: Colors.red; }"#,
0,
);
assert_eq!(result.kind, PropertyValueKind::Code);
}
}
25 changes: 15 additions & 10 deletions tools/lsp/ui/api.slint
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export struct Range {
export enum PropertyValueKind {
Boolean,
Code,
Color,
Enum,
Float,
Integer,
Expand All @@ -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
Expand Down

0 comments on commit 82fad65

Please sign in to comment.