Skip to main content

btcli_lib/ui/
lovely_items.rs

1// Copyright (C) 2026 S.A. (@snoware)
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use cursive::{
8    Cursive,
9    views::{Dialog, TextView},
10};
11
12/// 构建错误窗口对话框
13///
14/// # 参数
15/// - `error_message`: 要显示的错误信息
16///
17/// # 返回
18/// 返回一个Dialog实例,可直接添加到Cursive界面中
19pub fn messagebox(title: &str, message: &str) -> Dialog {
20    Dialog::around(TextView::new(message))
21        .title(title)
22        .button("确定", |s| {
23            s.pop_layer();
24        })
25}
26
27/// 显示错误窗口的便捷函数
28///
29/// # 参数
30/// - `s`: Cursive实例引用
31/// - `error_message`: 要显示的错误信息
32pub fn show_error(s: &mut Cursive, error_message: &str) {
33    s.add_layer(messagebox("错误", error_message));
34}
35
36pub fn show_info(s: &mut Cursive, error_message: &str) {
37    s.add_layer(messagebox("提示", error_message));
38}
39
40/// 显示确认对话框(询问框)
41///
42/// # 参数
43/// - `s`: Cursive实例引用
44/// - `title`: 对话框标题
45/// - `message`: 要显示的消息
46/// - `on_yes`: 用户点击"是"时的回调函数
47/// - `on_no`: 用户点击"否"时的回调函数
48pub fn show_confirmation<F1, F2>(s: &mut Cursive, title: &str, message: &str, on_yes: F1, on_no: F2)
49where
50    F1: Fn(&mut Cursive) + 'static + Send + Sync,
51    F2: Fn(&mut Cursive) + 'static + Send + Sync,
52{
53    let dialog = Dialog::around(TextView::new(message))
54        .title(title)
55        .button("是", move |s| {
56            on_yes(s);
57            s.pop_layer();
58        })
59        .button("否", move |s| {
60            on_no(s);
61            s.pop_layer();
62        });
63
64    s.add_layer(dialog);
65}