diff --git a/.changes/serde-tagged.md b/.changes/serde-tagged.md new file mode 100644 index 0000000..d075ed8 --- /dev/null +++ b/.changes/serde-tagged.md @@ -0,0 +1,28 @@ +--- +"tray-icon": minor +--- + +**Breaking change** Changed `serde` derive implementation for `TrayIconEvent` to use `serde(tag = "type")` and `rename_all = "camelCase"` on variants so the expected JSON serialization would look like this + +```json +{ + "type": "Click", + "button": "Left", + "buttonState": "Down", + "id": "some id", + "position": { + "x": 0, + "y": 0 + }, + "rect": { + "size": { + "width": 0, + "height": 0 + }, + "position": { + "x": 0, + "y": 0 + } + } +} +``` diff --git a/Cargo.toml b/Cargo.toml index 1ab5de4..044236e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,3 +77,4 @@ winit = "0.29" tao = "0.30" image = "0.25" eframe = "0.27" +serde_json = "1" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 79591e9..9c18227 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -413,9 +413,11 @@ impl TrayIcon { /// and will still show a context menu on right click. #[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(tag = "type"))] #[non_exhaustive] pub enum TrayIconEvent { /// A click happened on the tray icon. + #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] Click { /// Id of the tray icon which triggered this event. id: TrayIconId, @@ -566,3 +568,45 @@ impl TrayIconEvent { } } } + +#[cfg(test)] +mod tests { + + #[cfg(feature = "serde")] + #[test] + fn it_serializes() { + use super::*; + let event = TrayIconEvent::Click { + button: MouseButton::Left, + button_state: MouseButtonState::Down, + id: TrayIconId::new("id"), + position: dpi::PhysicalPosition::default(), + rect: Rect::default(), + }; + + let value = serde_json::to_value(&event).unwrap(); + assert_eq!( + value, + serde_json::json!({ + "type": "Click", + "button": "Left", + "buttonState": "Down", + "id": "id", + "position": { + "x": 0.0, + "y": 0.0, + }, + "rect": { + "size": { + "width": 0, + "height": 0, + }, + "position": { + "x": 0.0, + "y": 0.0, + }, + } + }) + ) + } +}