Skip to content

Commit

Permalink
Add simple 'user-click' event pub/sub schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Yassen Damyanov committed Jul 20, 2021
1 parent b30fc79 commit 0259fed
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,35 @@ fn say_hello(name: String) -> String {
response.into()
}

use tauri::Manager;

#[derive(Clone, serde::Serialize)]
struct Payload {
message: String,
}

fn main() {
println!("** Tauri app about to start");
tauri::Builder::default()
.setup(|app| {
// listen to the `event-name` (emitted on any window)
let id = app.listen_global("user-click", |event| {
println!("got event with payload {:?}", event.payload());
});
// unlisten to the event using the `id` returned on the `listen_global` function
// an `once_global` API is also exposed on the `App` struct
app.unlisten(id);

// emit the `event-name` event to all webview windows on the frontend
app.emit_all(
"user-click",
Payload {
message: "Tauri is awesome!".into(),
},
)
.unwrap();
Ok(())
})
.invoke_handler(tauri::generate_handler![say_hello,])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
},
"build": {
"distDir": "../www",
"devPath": "../www",
"beforeDevCommand": "",
"devPath": "http://localhost:8000",
"beforeDevCommand": "python3 -m http.server -d www/",
"beforeBuildCommand": "",
"withGlobalTauri": true
},
Expand Down
24 changes: 21 additions & 3 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,34 @@

<script type="text/javascript">
var Tauri = null;
import('/invoke.js').then(imported => {
import('/invoke.js').then((imported) => {
Tauri = imported;
});

const sayHello = () => {
console.log('sayHello() called')
console.log('sayHello() called');
let name = 'Alice';
Tauri.invoke_cmd('say_hello', {'name': name}).then(ret => {
Tauri.invoke_cmd('say_hello', { name: name }).then((ret) => {
console.log(`say_hello('${name}') returned '${ret}'`);
});
};

var sendEvent = null;
import('/modules/@tauri-apps/api/event.js').then((eventMod) => {
const onUserClick = eventMod.listen('user-click', (event) => {
// event.event is the event name (useful if you want to use a single callback fn for multiple event types)
// event.payload is the payload object
console.log('onUserClick() called');
console.log(event.event);
console.log(event.payload);
});

sendEvent = () => {
console.log('sendEvent() called');
eventMod.emit('user-click', '{"message": "Mmmm ... Tauri looks good!"}');
};
});

</script>

</head>
Expand All @@ -26,5 +43,6 @@
<h2>Tauri Events (Minimal Example)</h2>
<p>Minimal example for a Tauri events-based application.</p>
<button onclick="sayHello();">Say Hello!</button>
<button onclick="sendEvent();">Send Event</button>
</body>
</html>

0 comments on commit 0259fed

Please sign in to comment.