@@ -23,8 +23,8 @@ use crate::{
2323 sealed:: { ManagerBase , RuntimeOrDispatch } ,
2424 utils:: config:: Config ,
2525 utils:: { assets:: Assets , resources:: resource_relpath, Env } ,
26- Context , EventLoopMessage , Invoke , InvokeError , InvokeResponse , Manager , Runtime , Scopes ,
27- StateManager , Theme , Window ,
26+ Context , DeviceEventFilter , EventLoopMessage , Invoke , InvokeError , InvokeResponse , Manager ,
27+ Runtime , Scopes , StateManager , Theme , Window ,
2828} ;
2929
3030#[ cfg( shell_scope) ]
@@ -805,6 +805,35 @@ impl<R: Runtime> App<R> {
805805 . set_activation_policy ( activation_policy) ;
806806 }
807807
808+ /// Change the device event filter mode.
809+ ///
810+ /// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
811+ /// will ignore them by default for unfocused windows on Windows. This method allows changing
812+ /// the filter to explicitly capture them again.
813+ ///
814+ /// ## Platform-specific
815+ ///
816+ /// - ** Linux / macOS / iOS / Android**: Unsupported.
817+ ///
818+ /// # Examples
819+ /// ```,no_run
820+ /// let mut app = tauri::Builder::default()
821+ /// // on an actual app, remove the string argument
822+ /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
823+ /// .expect("error while building tauri application");
824+ /// app.set_device_event_filter(tauri::DeviceEventFilter::Always);
825+ /// app.run(|_app_handle, _event| {});
826+ /// ```
827+ ///
828+ /// [`tao`]: https://crates.io/crates/tao
829+ pub fn set_device_event_filter ( & mut self , filter : DeviceEventFilter ) {
830+ self
831+ . runtime
832+ . as_mut ( )
833+ . unwrap ( )
834+ . set_device_event_filter ( filter) ;
835+ }
836+
808837 /// Gets the argument matches of the CLI definition configured in `tauri.conf.json`.
809838 ///
810839 /// # Examples
@@ -1008,6 +1037,9 @@ pub struct Builder<R: Runtime> {
10081037 /// The updater configuration.
10091038 #[ cfg( updater) ]
10101039 updater_settings : UpdaterSettings ,
1040+
1041+ /// The device event filter.
1042+ device_event_filter : DeviceEventFilter ,
10111043}
10121044
10131045impl < R : Runtime > Builder < R > {
@@ -1036,6 +1068,7 @@ impl<R: Runtime> Builder<R> {
10361068 system_tray_event_listeners : Vec :: new ( ) ,
10371069 #[ cfg( updater) ]
10381070 updater_settings : Default :: default ( ) ,
1071+ device_event_filter : Default :: default ( ) ,
10391072 }
10401073 }
10411074
@@ -1486,6 +1519,28 @@ impl<R: Runtime> Builder<R> {
14861519 self
14871520 }
14881521
1522+ /// Change the device event filter mode.
1523+ ///
1524+ /// Since the DeviceEvent capture can lead to high CPU usage for unfocused windows, [`tao`]
1525+ /// will ignore them by default for unfocused windows on Windows. This method allows changing
1526+ /// the filter to explicitly capture them again.
1527+ ///
1528+ /// ## Platform-specific
1529+ ///
1530+ /// - ** Linux / macOS / iOS / Android**: Unsupported.
1531+ ///
1532+ /// # Examples
1533+ /// ```,no_run
1534+ /// tauri::Builder::default()
1535+ /// .device_event_filter(tauri::DeviceEventFilter::Always);
1536+ /// ```
1537+ ///
1538+ /// [`tao`]: https://crates.io/crates/tao
1539+ pub fn device_event_filter ( mut self , filter : DeviceEventFilter ) -> Self {
1540+ self . device_event_filter = filter;
1541+ self
1542+ }
1543+
14891544 /// Builds the application.
14901545 #[ allow( clippy:: type_complexity) ]
14911546 pub fn build < A : Assets > ( mut self , context : Context < A > ) -> crate :: Result < App < R > > {
@@ -1531,13 +1586,15 @@ impl<R: Runtime> Builder<R> {
15311586 }
15321587
15331588 #[ cfg( any( windows, target_os = "linux" ) ) ]
1534- let runtime = if self . runtime_any_thread {
1589+ let mut runtime = if self . runtime_any_thread {
15351590 R :: new_any_thread ( ) ?
15361591 } else {
15371592 R :: new ( ) ?
15381593 } ;
15391594 #[ cfg( not( any( windows, target_os = "linux" ) ) ) ]
1540- let runtime = R :: new ( ) ?;
1595+ let mut runtime = R :: new ( ) ?;
1596+
1597+ runtime. set_device_event_filter ( self . device_event_filter ) ;
15411598
15421599 let runtime_handle = runtime. handle ( ) ;
15431600
0 commit comments