#toast #notifications #notify #windows

win-toast-notify

Windows Toast通知

5个版本

0.1.6 2024年8月1日
0.1.5 2024年7月26日
0.1.3 2024年5月29日

#4 in #toast

Download history 281/week @ 2024-05-24 39/week @ 2024-05-31 20/week @ 2024-06-07 7/week @ 2024-06-14 2/week @ 2024-06-21 104/week @ 2024-07-19 248/week @ 2024-07-26 56/week @ 2024-08-02

每月408次下载
用于 notify-send-win

MIT 许可证

35KB
458

英语 | 简体中文

win-toast-notiy

win-toast-notify是一个用于发送Windows Toast通知的Rust库。该库主要受到以下项目启发:

此库已在Windows 11上进行了测试。

重要通知:此库目前处于不稳定状态。

文档

有关详细使用和API参考,请参阅文档

变更日志

有关最近的变化和更新,请参阅变更日志

使用方法

# Cargo.toml
[dependencies]
win-toast-notify = "0.1.6"

示例

基本

cargo run --example basic
use win_toast_notify::WinToastNotify;

fn main() {
    WinToastNotify::new()
        .set_title("Title")
        .set_messages(vec![
            "This is a simple toast message"
        ])
        .show()
        .expect("Failed to show toast notification")
}

image

按钮

cargo run --example button # button_2
use win_toast_notify::{WinToastNotify, Action, ActivationType};

fn main() {
    WinToastNotify::new()
        .set_actions(vec![
            Action {
                activation_type: ActivationType::Protocol,
                action_content: "Url".to_string(),
                arguments: "https://www.google.com/".to_string(),
                image_url: None
            },
            Action {
                activation_type: ActivationType::Protocol,
                 action_content: "File".to_string(),
                 arguments: r"C:\Windows\Web\Screen\img104.jpg".to_string(),
                 image_url: None
             },
            Action {
                 activation_type: ActivationType::Protocol,
                 action_content: "Folder".to_string(),
                 arguments: r"$env:USERPROFILE\Downloads".to_string(),   // PowerShell supports using environment variables
                 image_url: None
             }
         ])
         .show()
         .expect("Failed to show toast notification");
}

image

image

高级

cargo run --example advance
use win_toast_notify::*;
use std::env;

fn main() {
    let current_dir = env::current_dir().expect("Failed to get current directory");
    let logo_path = current_dir.join("examples/album_artist.png");
    let image_path = current_dir.join("examples/album_cover.jpg");
    let introduce_url = "https://honkai-star-rail.fandom.com/wiki/Hope_Is_the_Thing_With_Feathers";
    let music_url = "https://t.co/6urFxrI6K0";
    let music_lyric = "https://x.com/honkaistarrail/status/1789149010831569254";

    WinToastNotify::new()
        .set_open(introduce_url)    // 点击通知的打开链接或文件(夹)
        .set_duration(Duration::Long)
        .set_title("Hope Is the Thing With Feathers - Robin")
        .set_messages(vec![
            "Heads up the wheels are spinning\nAcross the plains in valleys deep",
            "To dawn the wheels that sing\nAn unending dream"
        ])
        .set_logo(logo_path.to_str().expect("Path is an invalid unicode"), CropCircle::True)
        .set_image(image_path.to_str().expect("Path is an invalid unicode"), ImagePlacement::Top)
        .set_actions(vec![
            Action {
                activation_type: ActivationType::Protocol,
                action_content: "Listen".to_string(),
                arguments: music_url.to_string(),
                image_url: None,
            },
            Action {
                activation_type: ActivationType::Protocol,
                action_content: "Lyric".to_string(),
                arguments: music_lyric.to_string(),
                image_url: None,
            }
        ])
        .set_audio(Audio::WinLoopingAlarm5, Loop::True)
        .show()
        .expect("Failed to show toast notification")
}

image

进度条

cargo run --example progress_bat_2
use win_toast_notify::{WinToastNotify, CropCircle, Duration};
use std::env;

fn main() {
    let current_dir = env::current_dir().expect("Failed to get current directory");
    let logo_path = current_dir.join("examples/progress_logo.png");

    let tag = "star-rail";
    let title = "Honkai: Star Rail";
    let mut status = String::from("Downloading...");
    let mut value = 0.0;
    let mut value_string = String::from("0%");

    WinToastNotify::new()
        .set_duration(Duration::Long)   
        .set_title("Downloading miHoYo Game...")
        .set_messages(vec![
            "May This Journey Lead Us Starward"
        ])
        .set_logo(logo_path.to_str().expect("Path is an invalid unicode"), CropCircle::True)
        .set_progress(tag, title, &status, value, &value_string)
        .show()
        .expect("Failed to show toast notification");

    for i in 1..=10 {
        std::thread::sleep(std::time::Duration::from_millis(500));
        value = i as f32 / 10.0;
        if i != 10 {
            value_string = format!("{:.1}%", value * 100.0);
            WinToastNotify::progress_update(None, tag, value, &value_string).expect("Failed to update");
        } else {
            status = String::from("Completed");
            value_string = String::from("100%");
            WinToastNotify::progress_complete(None, tag, &status, &value_string).expect("Failed to complete");
        };
    };
}

image

依赖

~265KB