index.html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Tauri</title>
  7. </head>
  8. <body>
  9. <h1>Tauri Commands</h1>
  10. <div>Response: <span id="response"></span></div>
  11. <div>Without Args: <span id="response-optional"></span></div>
  12. <div id="container"></div>
  13. <script>
  14. function runCommand(commandName, args, optional) {
  15. const id = optional ? '#response-optional' : '#response'
  16. const result = document.querySelector(id)
  17. window.__TAURI__
  18. .invoke(commandName, args)
  19. .then((response) => {
  20. result.innerText = `Ok(${response})`
  21. })
  22. .catch((error) => {
  23. result.innerText = `Err(${error})`
  24. })
  25. }
  26. const container = document.querySelector('#container')
  27. const commands = [
  28. { name: 'borrow_cmd' },
  29. { name: 'window_label' },
  30. { name: 'simple_command' },
  31. { name: 'stateful_command' },
  32. { name: 'async_simple_command' },
  33. { name: 'async_simple_command_snake' },
  34. { name: 'future_simple_command' },
  35. { name: 'async_stateful_command' },
  36. { name: 'simple_command_with_result' },
  37. // snake
  38. { name: 'future_simple_command_snake' },
  39. { name: 'future_simple_command_with_return_snake' },
  40. { name: 'future_simple_command_with_result_snake' },
  41. { name: 'force_async_snake' },
  42. { name: 'force_async_with_result_snake' },
  43. { name: 'simple_command_with_result_snake' },
  44. { name: 'stateful_command_with_result_snake' },
  45. // state
  46. { name: 'stateful_command_with_result' },
  47. { name: 'async_simple_command_with_result' },
  48. { name: 'future_simple_command_with_return' },
  49. { name: 'future_simple_command_with_result' },
  50. { name: 'async_stateful_command_with_result' },
  51. { name: 'command_arguments_wild' },
  52. {
  53. name: 'command_arguments_struct',
  54. args: { person: { name: 'ferris', age: 6 } }
  55. },
  56. {
  57. name: 'command_arguments_tuple_struct',
  58. args: { inlinePerson: ['ferris', 6] }
  59. }
  60. ]
  61. for (const command of commands) {
  62. const { name } = command
  63. const args = command.args ?? {
  64. [name.endsWith('snake') ? 'the_argument' : 'theArgument']: 'value'
  65. }
  66. const button = document.createElement('button')
  67. button.innerHTML = `Run ${name}`
  68. button.addEventListener('click', function () {
  69. runCommand(name, args, false)
  70. runCommand(name, Object.create(null), true)
  71. })
  72. container.appendChild(button)
  73. }
  74. </script>
  75. </body>
  76. </html>