Skip to content

Commit

Permalink
livve-preview: Make the color widget editable
Browse files Browse the repository at this point in the history
... in a bare-bones fashion.
  • Loading branch information
hunger committed Sep 18, 2024
1 parent 81be4f2 commit 0e751b1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
11 changes: 9 additions & 2 deletions tools/lsp/preview/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ pub fn create_ui(style: String, experimental: bool) -> Result<PreviewUi, Platfor
api.on_set_string_binding(super::set_string_binding);
api.on_property_declaration_ranges(super::property_declaration_ranges);

api.on_string_to_color(|s| string_to_color(&s.to_string()).unwrap_or_default());
api.on_string_is_color(|s| string_to_color(&s.to_string()).is_some());

#[cfg(target_vendor = "apple")]
api.set_control_key_name("command".into());

Expand Down Expand Up @@ -332,6 +335,10 @@ fn extract_value_with_unit_impl(
None
}

fn string_to_color(text: &str) -> Option<slint::Color> {
literals::parse_color_literal(&text).map(|c| slint::Color::from_argb_encoded(c))
}

fn extract_value_with_unit(
expression: &Option<syntax_nodes::Expression>,
units: &[i_slint_compiler::expression_tree::Unit],
Expand All @@ -358,9 +365,9 @@ fn extract_color(
value: &mut PropertyValue,
) -> bool {
if let Some(text) = expression.child_text(SyntaxKind::ColorLiteral) {
if let Some(color) = literals::parse_color_literal(&text) {
if let Some(color) = string_to_color(&text) {
value.kind = kind;
value.value_brush = slint::Brush::SolidColor(slint::Color::from_argb_encoded(color));
value.value_brush = slint::Brush::SolidColor(color);
value.value_string = text.into();
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions tools/lsp/ui/api.slint
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export global Api {

// # Callbacks

// ## Custom conversion functions:
callback string-is-color(string) -> bool;
callback string-to-color(string) -> color;

// ## Style:
callback style-changed();

Expand Down
25 changes: 21 additions & 4 deletions tools/lsp/ui/views/property-view.slint
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ component ColorWidget inherits HorizontalLayout {

alignment: end;

Rectangle {
color-preview := Rectangle {
height: 1.8rem;
width: 2 * self.height;

Expand All @@ -235,9 +235,26 @@ component ColorWidget inherits HorizontalLayout {
border-color: Palette.foreground;
}

Text {
vertical-alignment: center;
text: property-information.value.value-string;
ResettingLineEdit {
default-text: property-information.value.value-string;
min-width: 10rem; // Should be enough for 9 chars;-)

edited(text) => {
self.can-compile = Api.string-is-color(text);
if self.can-compile {
color-preview.background = Api.string-to-color(text);
}
}

accepted(text) => {
Api.set-code-binding(
root.element-information.source-uri,
root.element-information.source-version,
root.element-information.range.start,
root.property-information.name,
text,
);
}
}
}
}
Expand Down

0 comments on commit 0e751b1

Please sign in to comment.