It seems like according to this link (https://tauri.app/v1/guides/features/system-tray/#preventing-the-app-from-closing) there's a difference between keeping the backend and frontend running, and hide seems to keep the frontend running, is the intended way to have an app that can be hidden and shown at will to keep the frontend running with the hide and show commands? Or is there a way to "hide" and "show" an app that keeps only the backend running?
#Keep only backend running instead of hiding
1 messages · Page 1 of 1 (latest)
I don't think i really get the question since your link answers what i understood from your question.
So if you only want the backend (tauri's rust side and event loop) then then ExitRequested prevent_exit stuff is what you want -> You need to actually spawn new windows though.
If you also want the frontend (the js side) running then you gonna use hide/show instead.
I mean it sounds like what I want is to hide the window AND stop the JS side from running, but I don't want to have to create a new windows when I show it again, I just want to stop the JS when the window is hidden
It seems like my options are: keep the JS running and hide the window, or stop the JS and kill the window, but why would I want the JS to keep running while the window is hidden?
you can think of it as a compromise for users that want a nodejs backend i guess
that's how i often think of it
on windows we could also manually tell the webview to "sleep" when it's in the background but that would still consume more resources then killing the window if that matters
and afaik macos and linux don't have similar apis
how would i get a handle to the app from within the window event listener? to change the tray item text from hide to show when the close button is clicked and its running in the background
import the Manager trait (use tauri::Manager;) and then the Window will have a app_handle() method
When I exit with std::process::exit(0) I get this message:
[0707/044623.779:ERROR:window_impl.cc(119)] Failed to unregister class Chrome_WidgetWin_0. Error = 0
It seems like everything is working fine though
don't worry about that
it's something chromium internal
you can make chromium browsers print that same warning
do you know if it's possible to use a .pem certificate file with the tauri http api? or would i have to find another way
not possible
I'm trying to port this code from kotlin to my tauri app, but I get this error, is this the error that I would get if i didn't have the proper certificate?
that's hard to tell just from that browser error. It really only says that the server refused it (which should be accurate), but not why 🤔
but i mean, it's likely that the cert is indeed the issue
what would be a good alternative? is it possible to access https.request within tauri js?
or would i have to do something in rust
no
i guess, yes. There you have more control. If you can't add a client cert there either you can at least tell reqwest to ignore cert errors
So I guess it's not possible for the Tauri js http to ignore cert errors either
nope
i mean, technically it is since it also uses rust's reqwests library
but it's not implemented and i doubt it will be anytime soon tbh
okay, thanks
i guess i would have to use rust for websockets as well?
yeah, same thing really
are tauri events (https://tauri.app/v1/guides/features/events/) fast? would they be able to used for events that happen many dozen times a second from backend to frontend, and would the frontend recieve the event in a short period of time?
usually strings that are a couple hundred characters representing json objects
What's the best way to store global data for the backend? So that the tauri commands can access it
In C++ i would use a namespace and in java/kotlin i just had a singleton class
something similar to that
should be fine then.
if you only need rust commands to access it (or anything that has access to an AppHandle or Window from tauri) i'd suggest tauri::State https://docs.rs/tauri/latest/tauri/trait.Manager.html#method.manage
If it actually needs to be global then i'd suggest the once_cell crate (the OnceCell api was upstreamed into rust std in the last release but there's still a Lazy api i often prefer)
what's a good way to access the tauri state in an async function? if i do it the way it is with normal functions (state.0.lock().unwrap()) i get an error that the function doesn't implement + Send, so I read that it's better to access it within a synchronous function and return the data from there, but if I do that twice I get a "use of moved value" error. Also, it seems like there would be a better way to extract the data if i'm not modifying it (by copying just the values out somehow)?
what's a good way to access the tauri state in an async function?
That's a bit tricky to answer in a one-fits-all style. It really depends on the context.
2 common things to do is to a) switch to tokio's mutex if the error says something about holding a lock across await
and b) to use an AppHandle to get the State instead of injecting the State directly into the command (that one doesn't seem relevant here)
when i replaced my mutex with a tokio mutex it hangs on the await
it doesn't get past the first line
That should mean that it's still locked somewhere else
ah okay, how do i unlock the mutex?
if i have an async function that runs in a infinite loop but accesses it only at the beginning of the function (before the loop starts) would that be keeping the lock?
Yes
You can manually unlock it by dropping the variable
So like drop(locked_thing);
what should be dropped here? with both state and data i get a cannot move out of because borrowed error
data is what you'd want to drop but as long as there are references to it you can't do that
I assume install_dir is just a PathBuf or String? In that case I'd just clone it instead of borrowing it
ah okay
is it possible to drop a tokio mutex?
oh wait yea it seems to be working now, thanks
is there a way to prevent a close called by js (with appWindow.close()) with the rust window event handler?
this code works when clicking the x button but doesn't seem to prevent appWindow.close()
nope. that's only for user/os events basically.
we are thinking about having 2 close functions in v2 so that we can have one that triggers the events and one you could use from the event handlers to actually kill the app
oh is v2 coming out soon?
also, is it possible to access the system tray within javascript to change the item text? or is it better to fire an event from js to rust?
or, is it possible to listen to when the window is hidden/shown in rust?
oh is v2 coming out soon?
Depends on your definition of soon. We're trying to stabilize it into beta as fast as we can but the audited stable release likely won't make it into this year.
also, is it possible to access the system tray within javascript to change the item text?
No
or, is it possible to listen to when the window is hidden/shown in rust?
i assume you mean tauri's show()/hide() apis? In this case i think the answer is no
if i try to return a serde_json::value from a tauri command, what will the javascript side get?
will it be a normal js object that matches the json values or would i have to tinker with the value to get it to that state?
also is it possible to call a tauri command from within rust? i want to create a a rust function that can access the app handle and state that can be called from both rust and js
It gets converted via JSON.parse once so it normally should be actual js objects.
kindaaa. commands are really just normal rust functions so you need to get the apphandle from somewhere else and can then call the command as a normal function
when i pass the state into the other function it gets moved, is there a way to move it back?
or actually should i just pass in a cloned version of the statE?
is there a way to move it back?
Nope.
or actually should i just pass in a cloned version of the statE?
Either that if possible, or instead of moving the state directly, move an AppHandle around and use that to get the state where you need it -> that's often easier when also dealing with lifetimes.
ohh you can access the state through apphandle?
ah okay, thanks
Why would the current code work but the bottom commented out code doesn't?
oh wait nevermind the bottomcode is working now
i was reading through this issue (https://github.com/tauri-apps/tauri/issues/4309) and the code provided solved the "rubber banding" issue but it also seems to disable scrolling altogether, and adding overflow-y: scroll on a child doesn't seem to enable it back
when tauri live reloads the javascript, is the state supposed to remain? like if i have a variable (let x = false;) then ahve a button that sets it to true, and it reloads, is it supposed to remain true or become false again until i press the button?
i want it to remain true but it doesn't seem to be working
when tauri live reloads the javascript, is the state supposed to remain?
Nope. Some framework's HMR can do that, but tauri's own hot reload, that's used if your devPath is a file path, can't or at least doesn't do anything explicit
is it safe to include an API key hardcoded into the javascript? or how should i include it to prevent it from being accidentally exposed?
No, it's not safe to hard-code anything you want to keep secret regardless of the language. You should always assume that anything sent to or stored on the end-user's computer is exposed. Whenever possible, you should get the user to generate their own unique API key; especially if it's for a service you don't control. Once they have generated and input their own API key, you can then securely store it; if you want a ready-made solution, check out the Stronghold plugin.
i don't think it's possible for my application for the user to generate their own api key, i'll check out stronghold then?
If you're shipping the same key for everyone, Stronghold won't help since all someone has to do is find the password for it. The recommended practice is to have the user choose the password. If the user knows the password, they can get the API key.
is it possible to generate an installer that only works once if you don't want your application to be shared?
It would be better to keep such checks in your app as nothing stops a user from simply copying the files that were installed to another computer.
Hey, is it possible to change the icon in Tauri in the task switcher? Like when you alt tab
This is the first result on google
I asked for that too (and did my own research) but nobody knows how to fix it.
ah okay
do you mean like nobody working on tauri knows how to fix it or jus that tauri users don't ahve any options to fix it
both, we don't know how to fix it in tauri so users don't have an option to fix it in their app. (Or well, if someone find a fix that'd work in tauri's source, it'd probably work in end user apps too without explicit support in tauri)
is there any update for the alt tab icons in v2
no
my main function looks something like this:
fn main() {
thread::spawn(|| {
loop {
// do something, if condition is true, emit an event, sleep
}
});
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
but i can't seem to figure out how to emit an event in tauri v2 in this context
You can move that thread::spawn into the Builder's setup:
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![greet])
.setup(|app| {
let handle = app.handle().clone();
thread::spawn(move || {
loop {
// do something
// if condition is true
let payload = "this can be any type that has the `serde::Serialize` trait";
handle.emit_all("example", payload);
// sleep
}
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
now i'm having a similar problem when reading data:
tauri::Builder::default()
.manage(Mutex::new(InnerData {
connected: false,
...
}))
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![greet])
.setup(move |app| {
let handle = app.handle().clone();
let state = app.state::<Mutex<InnerData>>().clone();
thread::spawn(move || {
loop {
let data = state.lock().unwrap();
drop(data);
...
this causes this error:
why would the borrowed data "escape outside of closure"
the .setup(move |app| { was something i tried, it didn't work without the move either
tldr: Use the handle inside the spawned thread to get the State.
tauri::State is basically a reference and moving that into threads that require static lifetimes won't work.
ohh thanks, i was using the app instead of the handle
if i make an http request in rust that returns an image, how can i pass that image to javascript?
i can't use javascript to make the request
hmm, you could convert the binary data to base64 (on the rust side?) and then load that as-is. Assuming you know the mime type of course.
sometimes when i declare a tauri command in rust that returns a value it works fine, sometimes it requires me to return a Result instead ( the trait _::AsyncCommandMustReturnResult is not implemented for ...). is this related to me using a state parameter (async fn champion_row_data(state: tauri::State<'_, Mutex<data::InnerData>>) -> Result<serde_json::Value, String>) since a different function seems to work fine (async fn challenge_map() -> serde_json::Value)
yep, State is basically a reference to the inner value like Arc or & and doesn't have a static lifetime which will trigger this error.
i have an issue with an invoke call not returning around 50% of the time
i put an event call right before its supposed to return and it works, but the promise doesn't return
i don't know if this is relevant but the function takes around 1-5 minutes to complete, but it does complete
the top says loading is still true, the console log from the promise hasn't returned yet, but the emitted event has been received