#Create a native application window for preferences?

145 messages · Page 1 of 1 (latest)

formal lava
#
  1. js/ts files alone are not enough as a windows source
  2. browsers don't understand typescript, tsx even less so
    that's why Settings.tsx doesn't work.

You'll have to either go for a multi-page app https://vitejs.dev/guide/build.html#multi-page-app or use a router (i'd recommend that for a start) and have the settings page be a separate route then you can use something like /Settings as the url (depending on your router config ofc)

wind pewter
#

wish there was 1 example of tauri implementing calling a seperate window for preferences or any other window for that matter

#

Then use a /src/Settings.tx with: ```import React from 'react';

export default function Settings(){
return (
<>
<div>
settings window
</div>
</>
);
}

#

i only see the main content of the main.tsx that gets loaded true my app.tsx what am i missing can't be that hard?

formal lava
#

i'm surprised that this compiles without errors to be honest

#

it expects different html files as entry points, not just javascript

wind pewter
#

im not quite sure this was in the vite config i followed that exactly

formal lava
#

what vite config did you follow?

#

not the one i linked

wind pewter
#

the one from your link the official one in the docs

formal lava
#

btw i deleted your messages in general, let's keep the convo here

#

so anyway, you will need 2 html files, the default one and then a settings.html file for example (or settings/index.html if you prefer or whatever)

wind pewter
#

but im using .tsx as file extension? im using react with vite

#

if i define them like your example i get an error that __dirname is not existant

formal lava
#

yes, but typescript/javascript is not your entry file

#

you must have a html file for the multi-page mode to work

#

it looks basically the same as your existing index.html file (which btw is your main entry point)

wind pewter
#

ah nevermind im messing things up. it works like this yes

formal lava
wind pewter
#

I have them defined like rollupOptions: { input: { main: resolve(__dirname, 'index.html'), settings: resolve(__dirname, 'settings.html'), }, }, now

formal lava
#

yea

wind pewter
#

strange thing, if i go to localhost:1420/settings i should see the contents of my Settings.tsx component not? and it does not

#

using the web version not the tauri dev version

formal lava
#

no. can you give me a few minutes until i'm back at my pc? I will just show you what i mean with the "new root" stuff

#

but basically you duplicate the stuff you have for your main app, including the react createRoot stuff

wind pewter
#

really appreciated FabianLars im a bit confused

formal lava
wind pewter
#

ok that makes lots of sense, but following that tutorial exactly still doesent make it work i think tauri perhaps needs some different config lets see

formal lava
#

yeah, you'll have to use settings.html as the url of the second window, did you do that?

wind pewter
#

i followed along exactly but still a mistake i think. config is now ```import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { resolve } from 'path'

const root = resolve(__dirname, 'src');
const outDir = resolve(__dirname, 'dist');

// https://vitejs.dev/config/
export default defineConfig(async () => ({
root,
plugins: [react()],

// Vite options tailored for Tauri development and only applied in tauri dev or tauri build
// prevent vite from obscuring rust errors
clearScreen: false,
// tauri expects a fixed port, fail if that port is not available
server: {
port: 1420,
strictPort: true,
},
// to make use of TAURI_DEBUG and other env variables
// https://tauri.studio/v1/api/config#buildconfig.beforedevcommand
envPrefix: ["VITE_", "TAURI_"],
build: {
outDir,
emptyOutDir: true,
// Tauri supports es2021
target: process.env.TAURI_PLATFORM == "windows" ? "chrome105" : "safari13",
// don't minify for debug builds
minify: !process.env.TAURI_DEBUG ? "esbuild" : false,
// produce sourcemaps for debug builds
sourcemap: !!process.env.TAURI_DEBUG,
rollupOptions: {
input: {
main: resolve(root, 'main.html'),
settings: resolve(root, 'settings', 'index.html'),
},
},
},
}));

#

and this structure

#

ah nevermind again a mistake

#

corrected error but white page is only output build goes ok now

#

i see you use the html as entry point in the "router" then it just loads the .tsx component there with ```<html>
<head>
</head>
<body>

<div id="root"></div>
<script type="module" src="./settings.tsx"></script>
</body>
</html>

#

and in settings.tsx ```import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
<React.StrictMode>
<h1>Welcome to the settings page</h1>
</React.StrictMode>,
document.getElementById('root')
);

#

probably not fully correct. I wonder about the App.tsx which seems not loaded at all now

formal lava
#

well, you moved the main.html file into the src dir, did you also change its <script> thingy to reflect that?

#

for example, by default it was <script type="module" src="/src/main.tsx"></script> but now it has to be <script type="module" src="./main.tsx"></script> or something

wind pewter
#

my main.html has a <script type="module" src="./main.tsx"></script>

formal lava
#

are there any errors in the devtools console? (right click on the window -> inspect) should also be visible in the browser if the devserver is running.

wind pewter
#

no nothing and only a white page. build gives this vite v4.2.1 building for production... ✓ 38 modules transformed. ../dist/settings/index.html 0.22 kB ../dist/main.html 0.28 kB ../dist/assets/main-9647ac61.css 1.32 kB │ gzip: 0.65 kB ../dist/assets/settings-3467d3b1.js 0.17 kB │ gzip: 0.16 kB ../dist/assets/main-615917f3.js 1.11 kB │ gzip: 0.65 kB ../dist/assets/index-b872d324.js 142.76 kB │ gzip: 45.82 kB

#

I wonder if the modified vite config from the video is correct with this export default defineConfig(async () => ({ root, plugins: [react()],

#

or the build section build: { outDir, emptyOutDir: true, // Tauri supports es2021 target: process.env.TAURI_PLATFORM == "windows" ? "chrome105" : "safari13", // don't minify for debug builds minify: !process.env.TAURI_DEBUG ? "esbuild" : false, // produce sourcemaps for debug builds sourcemap: !!process.env.TAURI_DEBUG, rollupOptions: { input: { main: resolve(root, 'main.html'), settings: resolve(root, 'settings', 'index.html'), }, }, },

formal lava
#

well, it looks correct (even tho there were unnecessary changes), and the build output does contain all the correct files so i don't really know what's going wrong

wind pewter
#

i see the video has an repro too missed that so i copy the exact code over. think i have made some mistakes he did not show

#

i missed this line ``` document.getElementById('root')

#

but still white page 🤕

formal lava
#

in dev or build mode?

#

and the settings page shows correctly?

wind pewter
#

pnpm dev i run

#

no all white pages, i copyed and triple checked his example repro but it makes no difference

#

of course his repro works i cloned and tested it 😅

#

its more a vite thing thank your pointing me in the right direction have to figure this out not really tauri related so far

#

very strange copying every file over it loads the index.html in /src/ but /settings still shows that content and not my settings/index.html i double checked theres a hello to settings page text.

#

there is no tauri example with multipages setup? i must overlook something

formal lava
#

this is the version with the least amount of changes (and imo easiest type of changes) compared to the base create-tauri-app react template

#

what i did was:

  1. pnpm create tauri-app selecting react+typescript
  2. change the vite config to include this: ```ts
    rollupOptions: {
    input: {
    main: resolve(__dirname, 'index.html'),
    settings: resolve(__dirname, 'settings.html')
    }
    }
3) add a settings.html file at the root which simply loads src/settings.tsx: <https://github.com/FabianLars/tauri-multipage-react/blob/main/settings.html>
4) create the settings.tsx which is basically the same as main.tsx (i thought about naming it main-settings.tsx to not confuse it with a component source file): <https://github.com/FabianLars/tauri-multipage-react/blob/main/src/settings.tsx>
5) Use `settings.html` in `a` elements' href attributes to go to that page
wind pewter
#

ah right i see now the mistake i think, my settings html was inside /src folder just like the example from the video

#

🙏 🙏 big thanks i probably had not figured it out that was the problem

formal lava
#

i mean, you can make it work inside the src dir too, but i wanted to keep it as simple as possible

wind pewter
#

it feels strange thougt that settings.html has to be outside the src folder. normally all source application files are in /src/ as im used to

formal lava
#

in vite the index html file is always outside of the src folder by default

#

i agree though. it took me a while to get used to that too

wind pewter
#

it still fails on my new repro when i copy your modifications, i try just to clone your repro dont get this simplicity gets me doubt my skills

#

i did start from scratch like you and did exactly those changes

#

arghh! i did /settings

#

but it has to be settings.html

#

wtf.. i totally missed that ( i noticed when i clicked the link in your main app intro text )

#

im so used to /settings that i not even ever thought it should have been .html :\

formal lava
#

yeah i did that same mistake which is why i mentioned it as step 5 :D

wind pewter
#

so basically tauri loads well vite loads the .html that in itself loads the settings.tsx file

#

its a strange way of workings that im not used to at all but then i have never used vite.

formal lava
#

the thing is

#

you basically have 2 react apps here, settings.html and main.html/index.html are doing effectively the same thing, both with their own react app.

wind pewter
#

well at leat then i can finally try to get a tray implemented with a temperature next to it and a seperate window for preferences. :\ hope that goes a bit more smooth

formal lava
wind pewter
#

yes im a bit stubborn i should better follow your directions on that. sorry about that.

#

i reviewed both and the non normal route seemed more easy better documented 😅

formal lava
wind pewter
#

well anyway its a good learn curve using new tools will bring breaking methods in working that could s* you quite bad

#

many thank. do you have some 'buy me a coffee' link or something similar?

#

i think i study some more examples probably i place the html files in a pages or routes folder to get things up a bit more clean

formal lava
wind pewter
#

still strange those .html and .tsx files together. is that how vite always works?

#

i also looked at adding next to tauri had some good examples but it seems to be just overkill and another layer of debugging for a very simple app in the end

formal lava
#

Apart from that vite is pretty similar to for example create-react-app where you'd normally go for react-router etc

#

or even similar to nextjs but instead of having a file based router you have a code based router (react-router), or multiple react apps (multi page mode). the latter is pretty rare

#

All that said, Tauri is a bit of a special case, because it's not rare to have multiple completely different windows, while in normal webdev you often only have a single spa or something (and ofc single tab / window)

wind pewter
#

if i where to place those "routes" in a folder like this ( im used to this in remix it is this way ) still looks odd but it is what it is.

#

not sure if the main.tsx is mandatory or i can rename it to index, makes more sense to me though

formal lava
#

i'm somewhat sure create-tauri-app names it main.html but create-vite names it index.html, not sure tho, and no idea why the hell we use main.html

wind pewter
#

yeah it seems if one renames it will not find the entry point anymore and is just white screen again ;P

formal lava
#

yeah, as i said, gotta update the imports. If you rename main.tsx you need to update main.html
If you rename main.html you need to update the vite config

#

i hope i didn't miss anything 😂

#

and of course the a href links too

wind pewter
#

you cannot rename main.tsx but you can rename main.html to index.html or anything, i keep simple just put all in /src/ would you care to put me in right direction regarding the original question?

#

i haved defined the windows like so "windows": [ { "label": "Main", "title": "Tauri - Main", "fullscreen": false, "tabbingIdentifier": "Main", "height": 450, "width": 300, "decorations": false }, { "label": "settings", "title": "Settings", "titleBarStyle": "Overlay", "hiddenTitle": true, "fullscreen": false, "tabbingIdentifier": "settings", "width": 600, "height": 400 } ] now with this multi page in place i have the foundation to build that.

#

and i would need to disable the main window regular view it has to be tray only i had that before in my old repro so can port it over

#

i think to keep the questions on topic i should first create a link to load a new window with the route settings.html loaded. I had some code before but think it was probably incorrect

formal lava
wind pewter
#

if i where to use an event on the tray to load the seperate window i guess i go for the ```Create a window in JavaScript

Using the Tauri API you can easily create a window at runtime by importing the WebviewWindow class.``` ?

#

i must admit its a bit danting with all the options to do single things ;p

formal lava
#

the settings window will be opened from the tray icon menu right? In this case you'll need rust. If you want to open it from the main window, using rust is still possible, but javascript is easier

wind pewter
#

yes it should be opened true the settings menu option in the tray i try to be as native as possible or just personal preference, i want to mimic the osx toolbar app structure as much as possible.

formal lava
#

that expects you to not have the window configured in tauri.conf.json though, creating it from scratch

#

if you always want to have the window around (and consuming system resources) so that it starts faster, declaring it in tauri.conf with "visibile": false would be the way to go. then you'll just show() it on demand

wind pewter
#

allright yes i had a method for that and was wondering before why i duplicated the config window definition

#

it doesent have to be in the background its just incidental and on request

#

really helpfull i dont know if i would figure this out myself so easy to make those mistakes. so 1. remove the window config for settings only the main window config stays.

#

then i need to implement that tray package you posted in the other post. and add that event listener to trigger the settings menu with a custom tauri command

#

am i on track with my thoughts like that ;p

#

i should have posted 1 simple question outlining my problem - dident knew it would overlap so much.

formal lava
wind pewter
#

yes i used that one it was not that complex well i used "system-tray" in cargo it was basically implemented in buildog app i used for reference.

#

have to put my app in the showcase i think its common what i want to build but no minimal example of it yet. beside buildlog

formal lava
#

in other words, no it won't link to tray-icon

#

tauri doesn't "know" about that crate and you gotta handle it all yourself

wind pewter
#

ah right that makes totally sense yes. the feature list is for build in features

#

in cargo.toml one adds tauri-tray-icon = "0.6.0" then cargo build

formal lava
#

just tray-icon = "0.6.0"

#

again, totally tauri agnostic

wind pewter
#

allright i had that before you posted ;p but got some dep problems it seems that gdk-sys = "0.16.0" is required but adding that more dep issues

#

feels like npm dependency hell haha i have this ```error: failed to select a version for gdk-sys.
... required by package webkit2gtk v0.18.2
... which satisfies dependency webkit2gtk = "^0.18.2" of package tauri v1.4.0
... which satisfies dependency tauri = "^1.4" of package monitor v0.0.0 (/Users/pascal/Desktop/monitor/client/src-tauri)
versions that meet the requirements ^0.15.0 are: 0.15.1

the package gdk-sys links to the native library gdk-3, but it conflicts with a previous package which links to gdk-3 as well:
package gdk-sys v0.16.0
... which satisfies dependency gdk = "^0.16" of package gtk-sys v0.16.0
... which satisfies dependency gtk-sys = "^0.16" of package libappindicator v0.8.0
... which satisfies dependency libappindicator = "^0.8" of package tray-icon v0.6.0
... which satisfies dependency tray-icon = "^0.6.0" of package monitor v0.0.0 (/Users/pascal/Desktop/monitor/client/src-tauri)
Only one package in the dependency graph may specify the same links value. This helps ensure that only one copy of a native library is linked in the final binary. Try to adjust your dependencies so that only one package uses the links ='gdk-sys' value. For more information, see https://doc.rust-lang.org/cargo/reference/resolver.html#links.

failed to select a version for gdk-sys which could resolve this conflict```

formal lava
#

alright, in this case i take back what i said a while back. It's not possible to use tray-icon together with tauri v1

#

cause you can't resolve that dependency conflict

wind pewter
#

ah thats a bummer. then i guess i have to switch to v2 anyway and see if it at least runs without to much issue ( i dont plan to do complex stuff )

#

its basically the tray icon + menu + main window + settings window and thats it.

#

but really need the beach info on the tray in sight at least on osx else the app has to be opened everytime

#

*beach info surfer related data

#

could i upgrade the dep to tauri 2 or does it have breaking api so i need to scaffold a v2 from start in that case what do you think

formal lava
#

it's a bit more involved since the last few alpha versions since we split most of the apis into plugins. since you're not using any yet it shouldn't matter

#

but after upgrading you'll have to run pnpm tauri migrate or something, can't remember the exact command

#

but since you're still at the beginning i'd recommend starting from scratch to get it right, just do pnpm create tauri-app --alpha

wind pewter
#

ok thank you Fabian, ported multi page etc to new alpha codebase added the tray dep now on cargo build ;p