gui_server_api/navigation/
alerts.rs

1// SPDX-FileCopyrightText: 2024-2025 Foundation Devices, Inc. <hello@foundation.xyz>
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4//! Various global alerts navigation.
5
6#[derive(Debug, rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
7pub struct InvokeAlert {
8    pub app_title: Option<String>,
9    pub title: String,
10    pub icon: String,
11    pub line1: String,
12    pub line2: Option<String>,
13    pub button1_title: String,
14    pub button2_title: Option<String>,
15    pub button3_title: Option<String>,
16}
17
18impl InvokeAlert {
19    pub fn new_warning(
20        title: &str,
21        line1: &str,
22        line2: &str,
23        button1_title: &str,
24        button2_title: &str,
25    ) -> Self {
26        InvokeAlert {
27            app_title: None,
28            title: title.to_string(),
29            icon: "alert".to_string(),
30            line1: line1.to_string(),
31            line2: Some(line2.to_string()),
32            button1_title: button1_title.to_string(),
33            button2_title: Some(button2_title.to_string()),
34            button3_title: None,
35        }
36    }
37}
38
39#[derive(Debug, rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)]
40pub enum AlertResult {
41    Button1Pressed,
42    Button2Pressed,
43    Button3Pressed,
44    Canceled,
45}
46
47impl InvokeAlert {
48    pub fn from_slice(data: &[u8]) -> Option<Self> {
49        let Ok(archived) = rkyv::access::<ArchivedInvokeAlert, rkyv::rancor::Error>(data) else {
50            return None;
51        };
52        rkyv::deserialize::<Self, rkyv::rancor::Error>(archived).ok()
53    }
54
55    pub fn serialize(&self) -> Vec<u8> { rkyv::to_bytes::<rkyv::rancor::Error>(self).unwrap().to_vec() }
56}
57
58impl AlertResult {
59    pub fn from_slice(data: &[u8]) -> Option<Self> {
60        let Ok(archived) = rkyv::access::<ArchivedAlertResult, rkyv::rancor::Error>(data) else {
61            return None;
62        };
63        rkyv::deserialize::<Self, rkyv::rancor::Error>(archived).ok()
64    }
65
66    pub fn serialize(&self) -> Vec<u8> { rkyv::to_bytes::<rkyv::rancor::Error>(self).unwrap().to_vec() }
67}