-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtools_subscreen_chooser_panel.rs
More file actions
243 lines (229 loc) · 11.1 KB
/
Copy pathtools_subscreen_chooser_panel.rs
File metadata and controls
243 lines (229 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use crate::context::AppContext;
use crate::spv::CoreBackendMode;
use crate::ui::RootScreenType;
use crate::ui::theme::{DashColors, Shadow, Shape, Spacing, Typography};
use crate::{app::AppAction, ui};
use egui::{Frame, Margin, Panel, RichText, ScrollArea, Ui};
#[derive(PartialEq)]
pub enum ToolsSubscreen {
PlatformInfo,
AddressBalance,
ProofLog,
TransactionViewer,
DocumentViewer,
ProofViewer,
ContractViewer,
GroveSTARK,
MasternodeListDiff,
DPNS,
}
impl ToolsSubscreen {
/// Returns `true` when the tool only works with an RPC connection to a
/// local Dash Core node and must be disabled while the app is running on
/// its built-in SPV backend.
fn requires_core_rpc(&self) -> bool {
matches!(self, Self::MasternodeListDiff)
}
}
impl ToolsSubscreen {
pub fn display_name(&self) -> &'static str {
match self {
Self::PlatformInfo => "Platform info",
Self::AddressBalance => "Address balance",
Self::ProofLog => "Proof logs",
Self::TransactionViewer => "Transaction deserializer",
Self::ProofViewer => "Proof deserializer",
Self::DocumentViewer => "Document deserializer",
Self::ContractViewer => "Contract deserializer",
Self::GroveSTARK => "ZK Proofs",
Self::MasternodeListDiff => "Masternode list diff inspector",
Self::DPNS => "DPNS",
}
}
}
pub fn add_tools_subscreen_chooser_panel(ui: &mut Ui, app_context: &AppContext) -> AppAction {
let mut action = AppAction::None;
let dark_mode = ui.ctx().global_style().visuals.dark_mode;
let is_rpc_mode = app_context.core_backend_mode() == CoreBackendMode::Rpc;
let subscreens = vec![
ToolsSubscreen::PlatformInfo,
ToolsSubscreen::AddressBalance,
ToolsSubscreen::ProofLog,
ToolsSubscreen::ProofViewer,
ToolsSubscreen::TransactionViewer,
ToolsSubscreen::DocumentViewer,
ToolsSubscreen::ContractViewer,
ToolsSubscreen::GroveSTARK,
ToolsSubscreen::MasternodeListDiff,
ToolsSubscreen::DPNS,
];
let active_screen = match app_context.get_settings() {
Ok(Some(settings)) => match settings.root_screen_type {
ui::RootScreenType::RootScreenToolsPlatformInfoScreen => ToolsSubscreen::PlatformInfo,
ui::RootScreenType::RootScreenToolsAddressBalanceScreen => {
ToolsSubscreen::AddressBalance
}
ui::RootScreenType::RootScreenToolsProofLogScreen => ToolsSubscreen::ProofLog,
ui::RootScreenType::RootScreenToolsTransitionVisualizerScreen => {
ToolsSubscreen::TransactionViewer
}
ui::RootScreenType::RootScreenToolsProofVisualizerScreen => ToolsSubscreen::ProofViewer,
ui::RootScreenType::RootScreenToolsDocumentVisualizerScreen => {
ToolsSubscreen::DocumentViewer
}
ui::RootScreenType::RootScreenToolsContractVisualizerScreen => {
ToolsSubscreen::ContractViewer
}
ui::RootScreenType::RootScreenToolsMasternodeListDiffScreen => {
ToolsSubscreen::MasternodeListDiff
}
ui::RootScreenType::RootScreenToolsGroveSTARKScreen => ToolsSubscreen::GroveSTARK,
ui::RootScreenType::RootScreenDPNSActiveContests
| ui::RootScreenType::RootScreenDPNSPastContests
| ui::RootScreenType::RootScreenDPNSOwnedNames
| ui::RootScreenType::RootScreenDPNSScheduledVotes => ToolsSubscreen::DPNS,
_ => ToolsSubscreen::PlatformInfo,
},
_ => ToolsSubscreen::PlatformInfo, // Fallback to Active screen if settings unavailable
};
Panel::left("tools_subscreen_chooser_panel")
.resizable(false)
.default_size(270.0)
.frame(
Frame::new()
.fill(DashColors::background(dark_mode))
.inner_margin(Margin::symmetric(10, 10)),
)
.show_inside(ui, |ui| {
let available_height = ui.available_height();
Frame::new()
.fill(DashColors::surface(dark_mode))
.stroke(egui::Stroke::new(1.0, DashColors::border_light(dark_mode)))
.inner_margin(Margin::same(Spacing::XL as i8))
.corner_radius(egui::CornerRadius::same(Shape::RADIUS_LG))
.shadow(Shadow::elevated())
.show(ui, |ui| {
ui.set_min_height(available_height - 2.0 - (Spacing::XL * 2.0));
ScrollArea::vertical().show(ui, |ui| {
ui.add_space(Spacing::SM);
for subscreen in subscreens {
let is_active = active_screen == subscreen;
let requires_core_rpc = subscreen.requires_core_rpc();
let is_enabled = is_rpc_mode || !requires_core_rpc;
let button = if is_active {
egui::Button::new(
RichText::new(subscreen.display_name())
.color(DashColors::WHITE)
.size(Typography::SCALE_SM),
)
.fill(DashColors::DASH_BLUE)
.stroke(egui::Stroke::NONE)
.corner_radius(egui::CornerRadius::same(Shape::RADIUS_MD))
.min_size(egui::Vec2::new(150.0, 28.0))
} else {
egui::Button::new(
RichText::new(subscreen.display_name())
.color(DashColors::text_primary(dark_mode))
.size(Typography::SCALE_SM),
)
.fill(DashColors::glass_white(dark_mode))
.stroke(egui::Stroke::new(1.0, DashColors::border(dark_mode)))
.corner_radius(egui::CornerRadius::same(Shape::RADIUS_MD))
.min_size(egui::Vec2::new(150.0, 28.0))
};
// Show the subscreen name as a clickable option. Disable
// tools that require a Core RPC connection while running
// on the built-in SPV backend.
let mut response = ui.add_enabled(is_enabled, button);
if !is_enabled {
response = response.on_disabled_hover_text(
"This tool requires a local Dash Core node. Open Settings, switch to Expert mode, and select Local Dash Core node to enable it.",
);
}
if response.clicked() {
// Handle navigation based on which subscreen is selected
match subscreen {
ToolsSubscreen::PlatformInfo => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenToolsPlatformInfoScreen,
)
}
ToolsSubscreen::AddressBalance => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenToolsAddressBalanceScreen,
)
}
ToolsSubscreen::ProofLog => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenToolsProofLogScreen,
)
}
ToolsSubscreen::TransactionViewer => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenToolsTransitionVisualizerScreen,
)
}
ToolsSubscreen::ProofViewer => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenToolsProofVisualizerScreen,
)
}
ToolsSubscreen::DocumentViewer => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenToolsDocumentVisualizerScreen,
)
}
ToolsSubscreen::ContractViewer => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenToolsContractVisualizerScreen,
)
}
ToolsSubscreen::MasternodeListDiff => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenToolsMasternodeListDiffScreen)
}
ToolsSubscreen::GroveSTARK => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenToolsGroveSTARKScreen)
}
ToolsSubscreen::DPNS => {
action = AppAction::SetMainScreen(
RootScreenType::RootScreenDPNSActiveContests)
}
}
}
ui.add_space(Spacing::SM);
}
});
});
});
action
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn masternode_list_diff_requires_core_rpc() {
assert!(ToolsSubscreen::MasternodeListDiff.requires_core_rpc());
}
#[test]
fn spv_safe_tools_do_not_require_core_rpc() {
for tool in [
ToolsSubscreen::PlatformInfo,
ToolsSubscreen::AddressBalance,
ToolsSubscreen::ProofLog,
ToolsSubscreen::TransactionViewer,
ToolsSubscreen::DocumentViewer,
ToolsSubscreen::ProofViewer,
ToolsSubscreen::ContractViewer,
ToolsSubscreen::GroveSTARK,
ToolsSubscreen::DPNS,
] {
assert!(
!tool.requires_core_rpc(),
"Tool {} should be available in SPV mode",
tool.display_name()
);
}
}
}