Tauri as a simple native web view

Created: Oct 14, 2022Published: Nov 01, 2022Last modified: Apr 24, 2023
Word count: 1015Backlinks: 4

So you want to create a quick native dashboard. You know enough web dev to be dangerous, and now you're hoping to create something useful that isn't stuck in one of your browser tabs.

Here I'll touch on the path I took through Electron that eventually lead to Tauri, then include links to the way I'm currently using Tauri in Clawe.

Some 'Clawe' and 'Doctor' background

In Clawe, I'm ricing (read: overengineering) my machine with a slant toward developer productivity and interactive development. The goal is to make working on every part of it supported by a repl and good feedback loops.

The core of Clawe is mostly executed by Babashka - the clawe namespace is essentially a slew of stateless scripts (functions) that support keybindings and commands invoked via rofi or the command line.

Just above that fast and 'simple' (stateless) core, there's a server and ui layer I call Doctor. This term is chosen specifically to bring out the health-checking aspect. Ideally, Doctor knows the state of your systems and provides useful dashboards surfacing the status and options for 'correcting' problems, or cheatsheets/quick help for making changes to that state (e.g. dependency management, git repo status checks, etc).

So... electron?

I don't really have anything against electron - it seems like folks are pretty outspoken against it, I think because they find it to be bloated or difficult to work with. I've not really used it in anger tho.

I initially built up these dashboard with electron, and things worked pretty well. I wrote a small wrapper here: russmatney/clover. The name was intended as a clojure-overlay of sorts. I might reprise it with a Tauri implementation soon.

In order to control windows in clawe, I need some way to identify it - this comes down to setting the window's title, so that's the bit I had to implement in Clover. The title gets passed in as a command line arg, so you can arbitrarily run electron instances with urls and titles of your choosing.

It worked pretty well!

At some point Tauri came around, and it fills the same use-case, and I was encouraged to check out. I dove in, and it was fine, if not a bit faster, so, now I'm in Tauri.

It does feel lighter and like a modern, less-baggage version of Electron.

Tauri bits to share

> Caveat for this section: I built up the Tauri knowledge before Tauri hit 1.0, > and before this clojurescript template was added - that might be a better > starting point for folks getting started today.

My Tauri goals were the same as with electron - I want to arbitrarily create webviews, and ideally pass the window title and app url in on the command line.

I was able to achive that without too much trouble - after that I use the window manager itself to resize and control the app's behavior.

My tauri code at the time of writing is all here, in clawe's src-tauri directory.

> main.rs

I had to write a bit of rust here to handle the incoming params and set some default options.

Notable is the title, label, and url (the basics), and also flags for resizable, transparency, and decorations.

This was done via tauri's command/subcommand abstraction, but doesn't feel like especially clean rust code to me.... I'm open to suggestions!

> tauri.conf.json

This conf correlates to this page from the docs: https://tauri.app/v1/api/config

It's important for opting into features that are defaulted toward more secure, closed apps.

The "cli" section correlates to the options handled in the main.rs script above.

You can also some of the tweaks I had to make when adapting tauri to run on osx.

> command line usage

cargo tauri dev eventually creates an executable: ./src-tauri/target/debug/doctor

This is our command line tool, which can be run like:

./src-tauri/target/debug/doctor create-window \
    --title my-title --label my-label \
    --url http://localhost:3333

> I struggled to get a working 'production' build - at the time I was between a constellation of tauri beta versions, so eventually just settled for running the dev executable. :shrug: works for me!

> bb.edn consumers

My actual consumption of this is as tasks via babashka.

A simplified bb.edn:

{:tasks
 {:requires ([babashka.process :as p])
  :init
  (do
    (def doctor-fe-url "http://localhost:3333")
    (def doctor-tauri-executable "./src-tauri/target/debug/doctor")
    (defn run-doctor-tauri-app
      ([name] (run-doctor-tauri-app nil name))
      ([opts name]
       (let [url   (:url opts (str doctor-fe-url "/" name))
             title (str "tauri/doctor-" name)
             label name
             cmd   (str doctor-tauri-executable " create-window"
                        " --title " title " --label " label " --url " url)]
         (shell {:dir (str (home-dir) "/russmatney/clawe")} cmd)))))

  topbar          {:doc  "Run the doctor topbar via tauri. Consumed from systemd service."
                   :task (run-doctor-tauri-app "topbar")}
  popup           {:doc  "Run the doctor popup window via tauri."
                   :task (run-doctor-tauri-app "popup")}
  todo            {:doc  "Run the doctor todo window via tauri."
                   :task (run-doctor-tauri-app "todo")}
  clerk           {:doc  "Run the doctor clerk window via tauri."
                   :task (run-doctor-tauri-app {:url "http://localhost:3334/notebooks/clawe"} "clerk")}
  clerk-workspace {:doc  "Run the doctor clerk-workspace window via tauri."
                   :task (run-doctor-tauri-app {:url "http://localhost:3334/notebooks/wallpapers"} "clerk-workspace")}}

My full bb.edn is pretty large, but it's here in case the above is too out-of-context.

Caveats/Extras

I noted some of these above, but just to call them out again.

> My work predates Tauri 1.0

Some things may have changed!

> There is some cljs-tauri starter work done

This is worth looking at! My understanding is that you can select 'clojurescript' in the yarn create-tauri-app flow, and a shadowcljs app and tauri will be setup for you.

My work was a few months before this was started - this might already be a more modern approach.

Though, I think I'd like to actually create a standalone repo for tauri that just seves the webview and exposes settings as command line options - it's a pain to carry the rust and src-tauri baggage along when it's not really integrating into the rest of my repos.

> I use a dev build, prod was a struggle

I assume this was a combination of libs and my environment problem.

> Rust in emacs seems to eat all my computing resources

I didn't spend much time getting to the bottom of this.

I use the typical doom rust module, and my machine was grinding to a halt.

I eventually disabled a bunch of the 'tools' and just wrote, compiled, and tested the build until things worked. Pretty much a terrible dev loop. I'm sure this is not the intended experience.

Future

I hope to reprise russmatney/clover with a simple binary for running Tauri with passed params.

Ping me if you think this would be useful, as I'm sure there are more cli opts to include, and I could always use a little motivation. maybe you like window decorations? who knows?

Update!

Today ( 10/18/22 ) I created russmatney/clove as a successor to the dead russmatney/clover. It is based on a fresh cargo create-tauri-app, so is up to date as of today, at least.

Check out the repo for install and usage.


Backlinks

A video overview of the components that make up the clawe monorepo.

Relevant April 2023.

I'll post a link here once it exists! For now, if you're reading this, I could stream this any day, so take a look at my schedule or ping me to see when it's going to happen.

A thin wrapper around tauri, providing a cli for running a webapp/url 'natively'.

Useful for running local dashboards or web apps that you don't want taking up a tab in your browser.

See also:

Successor to russmatney/clover.