main.rs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
  5. use std::sync::Mutex;
  6. use tauri::State;
  7. struct Counter(Mutex<usize>);
  8. #[tauri::command]
  9. fn increment(counter: State<'_, Counter>) -> usize {
  10. let mut c = counter.0.lock().unwrap();
  11. *c += 1;
  12. *c
  13. }
  14. #[tauri::command]
  15. fn decrement(counter: State<'_, Counter>) -> usize {
  16. let mut c = counter.0.lock().unwrap();
  17. *c -= 1;
  18. *c
  19. }
  20. #[tauri::command]
  21. fn reset(counter: State<'_, Counter>) -> usize {
  22. let mut c = counter.0.lock().unwrap();
  23. *c = 0;
  24. *c
  25. }
  26. #[tauri::command]
  27. fn get(counter: State<'_, Counter>) -> usize {
  28. *counter.0.lock().unwrap()
  29. }
  30. fn main() {
  31. tauri::Builder::default()
  32. .manage(Counter(Mutex::new(0)))
  33. .invoke_handler(tauri::generate_handler![increment, decrement, reset, get])
  34. .run(tauri::generate_context!(
  35. "../../examples/state/tauri.conf.json"
  36. ))
  37. .expect("error while running tauri application");
  38. }