#Keep only backend running instead of hiding

1 messages · Page 1 of 1 (latest)

cerulean lodge
#

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?

Native application system tray.

narrow horizon
#

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.

cerulean lodge
#

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?

narrow horizon
#

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

cerulean lodge
#

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

narrow horizon
cerulean lodge
#

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

narrow horizon
#

don't worry about that

#

it's something chromium internal

#

you can make chromium browsers print that same warning

cerulean lodge
#

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

narrow horizon
#

not possible

cerulean lodge
#

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?

narrow horizon
#

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

cerulean lodge
#

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

narrow horizon
cerulean lodge
#

So I guess it's not possible for the Tauri js http to ignore cert errors either

narrow horizon
#

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

cerulean lodge
#

okay, thanks

cerulean lodge
#

i guess i would have to use rust for websockets as well?

narrow horizon
#

yeah, same thing really

cerulean lodge
#

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?

The Tauri event system is a multi-producer multi-consumer communication primitive that allows message passing between the frontend and the backend.

narrow horizon
#

generally yes

#

but that mostly depends on the size of your payload

cerulean lodge
#

usually strings that are a couple hundred characters representing json objects

cerulean lodge
#

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

narrow horizon
#

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)

cerulean lodge
#

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)?

narrow horizon
#

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)

cerulean lodge
#

when i replaced my mutex with a tokio mutex it hangs on the await

#

it doesn't get past the first line

narrow horizon
#

That should mean that it's still locked somewhere else

cerulean lodge
#

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?

narrow horizon
#

Yes

#

You can manually unlock it by dropping the variable

#

So like drop(locked_thing);

cerulean lodge
#

what should be dropped here? with both state and data i get a cannot move out of because borrowed error

narrow horizon
#

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

cerulean lodge
#

ah okay

#

is it possible to drop a tokio mutex?

#

oh wait yea it seems to be working now, thanks

cerulean lodge
#

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()

narrow horizon
#

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

cerulean lodge
#

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?

narrow horizon
#

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

cerulean lodge
#

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?

cerulean lodge
#

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

narrow horizon
narrow horizon
cerulean lodge
#

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?

narrow horizon
#

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.

cerulean lodge
#

ohh you can access the state through apphandle?

narrow horizon
#

yeah

#

(actually the state method but manage has the examples)

cerulean lodge
#

ah okay, thanks

cerulean lodge
#

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

GitHub

Describe the problem while preserving smooth scrolling, disabled scroll rubber banding, like native app Describe the solution you'd like Whether cocoa has related API, I don't know Alternat...

cerulean lodge
#

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

narrow horizon
cerulean lodge
#

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?

dusty verge
# cerulean lodge is it safe to include an API key hardcoded into the javascript? or how should i ...

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.

cerulean lodge
#

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?

dusty verge
#

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.

cerulean lodge
#

is it possible to generate an installer that only works once if you don't want your application to be shared?

dusty verge
#

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.

cerulean lodge
#

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

narrow horizon
#

I asked for that too (and did my own research) but nobody knows how to fix it.

cerulean lodge
#

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

narrow horizon
#

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)

cerulean lodge
#

is there any update for the alt tab icons in v2

narrow horizon
#

no

cerulean lodge
#

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

dusty verge
#

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");
}
cerulean lodge
#

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

narrow horizon
cerulean lodge
#

ohh thanks, i was using the app instead of the handle

cerulean lodge
#

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

narrow horizon
#

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.

cerulean lodge
#

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)

narrow horizon
cerulean lodge
#

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