Skip to content

Instantly share code, notes, and snippets.

@a-liashenko
Last active October 14, 2024 20:38
Show Gist options
  • Save a-liashenko/d506263a74f716fbeaaf6a790efb8ffe to your computer and use it in GitHub Desktop.
Save a-liashenko/d506263a74f716fbeaaf6a790efb8ffe to your computer and use it in GitHub Desktop.
use serde::{ser::SerializeStruct, Serialize, Serializer};
use slint::{Model, SharedString};
// Implement Serialize for Slint sturct
impl Serialize for ExampleStruct {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut ser = serializer.serialize_struct("ExampleStruct", 3)?;
ser.serialize_field("attr-1", &self.attr_1.iter().collect::<Vec<_>>())?;
ser.serialize_field("attr-2", &self.attr_2.iter().collect::<Vec<_>>())?;
ser.serialize_field("attr-3", &self.attr_3)?;
ser.end()
}
}
fn main() -> Result<(), slint::PlatformError> {
let ui = AppWindow::new()?;
ui.on_as_json(|v| {
let json = serde_json::to_string_pretty(&v).unwrap();
SharedString::from(json)
});
ui.run()
}
slint::slint!(
import { TextEdit } from "std-widgets.slint";
@rust-attr(derive(serde::Serialize))
export enum ExampleType {
First,
Second
}
@rust-attr(derive(serde::Serialize))
export struct ExampleNested {
nested-1: string,
nested-2: string
}
export struct ExampleStruct {
attr-1: [ExampleType],
attr-2: [string],
attr-3: ExampleNested,
}
export component AppWindow inherits Window {
property <ExampleStruct> example: {
attr-1: [ExampleType.First, ExampleType.Second, ExampleType.First],
attr-2: ["example 1", "example 2", "example 3"],
attr-3: { nested-1: "Nested 1", nested-2: "Nested 2" },
};
pure callback as-json(ExampleStruct) -> string;
preferred-height: 320px;
preferred-width: 480px;
TextEdit {
height: 100%;
width: 100%;
text: as-json(root.example);
read-only: true;
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment