-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from Enraged-Dun-Cookie-Development-Team/master
🔖 release 1.0.3
- Loading branch information
Showing
12 changed files
with
263 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use tauri::command; | ||
|
||
#[command] | ||
pub fn should_silence() -> tauri::Result<bool> { | ||
#[cfg(windows)] | ||
{ | ||
use std::mem::size_of; | ||
use windows::Win32::{ | ||
Foundation::{RECT}, | ||
Graphics::Gdi::{GetMonitorInfoW, MonitorFromWindow, MONITORINFO, MONITOR_DEFAULTTOPRIMARY}, | ||
UI::WindowsAndMessaging::{ | ||
GetDesktopWindow, GetForegroundWindow, GetShellWindow, GetWindowRect, | ||
}, | ||
}; | ||
|
||
let shell = unsafe { GetShellWindow() }; | ||
let desktop = unsafe { GetDesktopWindow() }; | ||
let hwnd = unsafe { GetForegroundWindow() }; | ||
if hwnd.0 == 0 ||( hwnd == shell || hwnd == desktop ){ | ||
Ok(false) | ||
} else { | ||
let mut rect = RECT::default(); | ||
unsafe { GetWindowRect(hwnd, &mut rect) }; | ||
|
||
let mut monitor_info = MONITORINFO { | ||
cbSize: size_of::<MONITORINFO>() as u32, | ||
..Default::default() | ||
}; | ||
let monitor = unsafe { MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY) }; | ||
unsafe { GetMonitorInfoW(monitor, &mut monitor_info) }; | ||
|
||
let monitor_size = monitor_info.rcMonitor; | ||
|
||
Ok(rect.left == monitor_size.left && rect.right == monitor_size.right && rect.top == monitor_size.top && rect.bottom == monitor_size.bottom) | ||
} | ||
} | ||
#[cfg(not(windows))] | ||
Ok(false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use serde::{Deserialize, Serialize, Serializer}; | ||
|
||
use tauri::{command, AppHandle, Window}; | ||
use tracing::instrument; | ||
|
||
use url::Url; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum NotifyError { | ||
#[error(transparent)] | ||
TauriApi(#[from] tauri::api::Error), | ||
#[cfg(windows)] | ||
#[error(transparent)] | ||
Tauri(#[from] tauri::Error), | ||
#[cfg(windows)] | ||
#[error(transparent)] | ||
Toast(#[from] winrt_toast::WinToastError), | ||
#[cfg(windows)] | ||
#[error(transparent)] | ||
Request(#[from] reqwest::Error), | ||
#[cfg(windows)] | ||
#[error(transparent)] | ||
RequestMiddle(#[from] reqwest_middleware::Error), | ||
#[cfg(windows)] | ||
#[error(transparent)] | ||
Io(#[from] std::io::Error), | ||
} | ||
|
||
impl Serialize for NotifyError { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
self.to_string().serialize(serializer) | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct NotificationPayload { | ||
title: String, | ||
body: String, | ||
#[serde(default)] | ||
has_sound: bool, | ||
time: String, | ||
image_url: Option<Url>, | ||
} | ||
#[command(async)] | ||
#[instrument(name = "SendToastNotify", skip(app, window), err)] | ||
pub async fn send_system_notification( | ||
window: Window, | ||
app: AppHandle, | ||
payload: NotificationPayload, | ||
) -> Result<(), NotifyError> { | ||
#[cfg(windows)] | ||
{ | ||
use crate::{request_client::RequestClient, state::get_cache_dir}; | ||
use http::{HeaderValue, Method}; | ||
use std::sync::OnceLock; | ||
use tracing::{error, info}; | ||
use winrt_toast::{ | ||
content::text::TextPlacement, Image, Scenario, Text, Toast, ToastManager, | ||
}; | ||
|
||
static MANAGER: OnceLock<ToastManager> = OnceLock::new(); | ||
let manager = MANAGER | ||
.get_or_init(|| ToastManager::new(app.config().tauri.bundle.identifier.as_str())); | ||
|
||
let mut toast = Toast::new(); | ||
toast.launch("click:cookie"); | ||
toast.text1(payload.title); | ||
toast.text2(Text::new(payload.body)); | ||
if let Some(img) = payload.image_url { | ||
let dir = get_cache_dir(app.clone()); | ||
let client = RequestClient::get_this(app.clone()); | ||
let resp = client | ||
.request(Method::GET, img) | ||
.header("Referer", HeaderValue::from_static("https://weibo.com/")) | ||
.send() | ||
.await?; | ||
let byte = resp.bytes().await?; | ||
let tmp_file = dir.join("notify_img.jpg"); | ||
std::fs::write(&tmp_file, byte)?; | ||
|
||
toast.image(1, Image::new_local(tmp_file)?); | ||
} | ||
|
||
toast.text3( | ||
Text::new(format!("at: {}", payload.time)).with_placement(TextPlacement::Attribution), | ||
); | ||
toast.scenario(Scenario::Reminder); | ||
manager.show_with_callbacks( | ||
&toast, | ||
Some(Box::new(move |launch| { | ||
let re = launch | ||
.map_err(NotifyError::from) | ||
.and_then(|_| window.show().map_err(Into::into)); | ||
match re { | ||
Ok(_) => {} | ||
Err(err) => { | ||
error!(Action="Respond To User Select", Error = %err); | ||
} | ||
}; | ||
})), | ||
Some(Box::new(|reason| match reason { | ||
Ok(reason) => { | ||
info!(Action="Notify Dismiss",reason= ?reason); | ||
} | ||
Err(err) => { | ||
error!(Action="Notify Dismiss", Error = %err); | ||
} | ||
})), | ||
Some(Box::new(|err| { | ||
error!(Action="Notify Failure", Error = %err); | ||
}) as _), | ||
)?; | ||
} | ||
#[cfg(not(windows))] | ||
{ | ||
let mut notify = | ||
tauri::api::notification::Notification::new(&app.config().tauri.bundle.identifier) | ||
.title(payload.title) | ||
.body(payload.body); | ||
if payload.has_sound { | ||
notify = notify.sound(tauri::api::notification::Sound::Default) | ||
} | ||
notify.show()?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.