@@ -13,7 +13,6 @@ use tauri_macros::{command_enum, module_command_handler, CommandModule};
1313use crate :: { api:: notification:: Notification , Env , Manager } ;
1414
1515// `Granted` response from `request_permission`. Matches the Web API return value.
16- #[ cfg( notification_all) ]
1716const PERMISSION_GRANTED : & str = "granted" ;
1817// `Denied` response from `request_permission`. Matches the Web API return value.
1918const PERMISSION_DENIED : & str = "denied" ;
@@ -49,13 +48,6 @@ impl Cmd {
4948 context : InvokeContext < R > ,
5049 options : NotificationOptions ,
5150 ) -> super :: Result < ( ) > {
52- let allowed = match is_permission_granted ( & context) {
53- Some ( p) => p,
54- None => request_permission ( & context) ,
55- } ;
56- if !allowed {
57- return Err ( crate :: Error :: NotificationNotAllowed . into_anyhow ( ) ) ;
58- }
5951 let mut notification =
6052 Notification :: new ( context. config . tauri . bundle . identifier . clone ( ) ) . title ( options. title ) ;
6153 if let Some ( body) = options. body {
@@ -68,80 +60,23 @@ impl Cmd {
6860 Ok ( ( ) )
6961 }
7062
71- #[ cfg( notification_all) ]
72- fn request_notification_permission < R : Runtime > (
73- context : InvokeContext < R > ,
74- ) -> super :: Result < & ' static str > {
75- if request_permission ( & context) {
76- Ok ( PERMISSION_GRANTED )
77- } else {
78- Ok ( PERMISSION_DENIED )
79- }
80- }
81-
82- #[ cfg( not( notification_all) ) ]
8363 fn request_notification_permission < R : Runtime > (
8464 _context : InvokeContext < R > ,
8565 ) -> super :: Result < & ' static str > {
86- Ok ( PERMISSION_DENIED )
87- }
88-
89- #[ cfg( notification_all) ]
90- fn is_notification_permission_granted < R : Runtime > (
91- context : InvokeContext < R > ,
92- ) -> super :: Result < Option < bool > > {
93- if let Some ( allow_notification) = is_permission_granted ( & context) {
94- Ok ( Some ( allow_notification) )
66+ Ok ( if cfg ! ( notification_all) {
67+ PERMISSION_GRANTED
9568 } else {
96- Ok ( None )
97- }
69+ PERMISSION_DENIED
70+ } )
9871 }
9972
100- #[ cfg( not( notification_all) ) ]
10173 fn is_notification_permission_granted < R : Runtime > (
10274 _context : InvokeContext < R > ,
103- ) -> super :: Result < Option < bool > > {
104- Ok ( Some ( false ) )
75+ ) -> super :: Result < bool > {
76+ Ok ( cfg ! ( notification_all ) )
10577 }
10678}
10779
108- #[ cfg( notification_all) ]
109- fn request_permission < R : Runtime > ( context : & InvokeContext < R > ) -> bool {
110- let mut settings = crate :: settings:: read_settings (
111- & context. config ,
112- & context. package_info ,
113- context. window . state :: < Env > ( ) . inner ( ) ,
114- ) ;
115- if let Some ( allow_notification) = settings. allow_notification {
116- return allow_notification;
117- }
118- let answer = crate :: api:: dialog:: blocking:: ask (
119- Some ( & context. window ) ,
120- "Permissions" ,
121- "This app wants to show notifications. Do you allow?" ,
122- ) ;
123-
124- settings. allow_notification = Some ( answer) ;
125- let _ = crate :: settings:: write_settings (
126- & context. config ,
127- & context. package_info ,
128- context. window . state :: < Env > ( ) . inner ( ) ,
129- settings,
130- ) ;
131-
132- answer
133- }
134-
135- #[ cfg( notification_all) ]
136- fn is_permission_granted < R : Runtime > ( context : & InvokeContext < R > ) -> Option < bool > {
137- crate :: settings:: read_settings (
138- & context. config ,
139- & context. package_info ,
140- context. window . state :: < Env > ( ) . inner ( ) ,
141- )
142- . allow_notification
143- }
144-
14580#[ cfg( test) ]
14681mod tests {
14782 use super :: NotificationOptions ;
@@ -163,16 +98,21 @@ mod tests {
16398 fn request_notification_permission ( ) {
16499 assert_eq ! (
165100 super :: Cmd :: request_notification_permission( crate :: test:: mock_invoke_context( ) ) . unwrap( ) ,
166- super :: PERMISSION_DENIED
101+ if cfg!( notification_all) {
102+ super :: PERMISSION_GRANTED
103+ } else {
104+ super :: PERMISSION_DENIED
105+ }
167106 )
168107 }
169108
170109 #[ cfg( not( notification_all) ) ]
171110 #[ test]
172111 fn is_notification_permission_granted ( ) {
112+ let expected = cfg ! ( notification_all) ;
173113 assert_eq ! (
174114 super :: Cmd :: is_notification_permission_granted( crate :: test:: mock_invoke_context( ) ) . unwrap( ) ,
175- Some ( false )
115+ expected ,
176116 ) ;
177117 }
178118
0 commit comments