Add Tauri v2 desktop app alongside Electron
- New tauri/ project: frameless 1280x800 window, loads the Vite dev server in dev and tauri/frontend_build in release builds. - Starts the backend on launch (node server.js in dev, pkg backend.exe sidecar in release) and stops it on exit through a Windows job object; reuses a backend already listening on port 4000. - Custom title bar works in Tauri (minimize, maximize, close, drag region) with the matching window capabilities. - react_frontend: dev:tauri / build:tauri scripts, shared platform flags so Electron and Tauri builds both use the local API and hash router. Web and Electron builds are unchanged. - tauri/README.md: prerequisites, dev, build, and gaps vs Electron.
This commit is contained in:
1 parent
06bf902ad3
commit
3144bd96c9
30 files changed
+5121
-14
No files matched your search
Generated
+4376
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,31 @@
|
||||
[package]
|
||||
name = "showy"
|
||||
version = "2.0.0"
|
||||
description = "Showy Desktop App"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
name = "showy_lib"
|
||||
crate-type = ["staticlib", "cdylib", "rlib"]
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2", features = [] }
|
||||
|
||||
[dependencies]
|
||||
tauri = { version = "2", features = [] }
|
||||
serde_json = "1"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.59", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_Security",
|
||||
"Win32_System_JobObjects",
|
||||
"Win32_System_Threading",
|
||||
] }
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1
|
||||
lto = true
|
||||
opt-level = "s"
|
||||
panic = "abort"
|
||||
strip = true
|
||||
Whitespace-only changes.
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
tauri_build::build()
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "default",
|
||||
"description": "Permissions for the main Showy window (custom title bar controls).",
|
||||
"windows": ["main"],
|
||||
"permissions": [
|
||||
"core:default",
|
||||
"core:window:allow-minimize",
|
||||
"core:window:allow-toggle-maximize",
|
||||
"core:window:allow-internal-toggle-maximize",
|
||||
"core:window:allow-close",
|
||||
"core:window:allow-start-dragging"
|
||||
]
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 4.5 KiB |
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 83 KiB |
@@ -0,0 +1,222 @@
|
||||
//! Starts the Showy Node backend next to the Tauri window and stops it on exit.
|
||||
//!
|
||||
//! - Debug builds (`tauri dev`) run `node server.js` from the repo's `backend/` folder.
|
||||
//! - Release builds run the `pkg`-packaged `backend.exe` sidecar that the bundler
|
||||
//! places next to the app executable.
|
||||
//! - If something already listens on the backend port, nothing is spawned and the
|
||||
//! app talks to that process (handy when the backend runs in its own terminal).
|
||||
//!
|
||||
//! Environment overrides:
|
||||
//! - `SHOWY_SKIP_BACKEND=1` never spawn a backend
|
||||
//! - `SHOWY_BACKEND_DIR=<path>` backend folder used in debug builds
|
||||
//! - `SHOWY_BACKEND_EXE=<path>` run this executable instead (any build)
|
||||
|
||||
use std::fs::{self, File};
|
||||
use std::net::{SocketAddr, TcpStream};
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
/// The frontend calls `http://localhost:4000/api/v1` in desktop builds.
|
||||
pub const BACKEND_PORT: u16 = 4000;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct BackendState {
|
||||
child: Mutex<Option<Child>>,
|
||||
#[cfg(windows)]
|
||||
job: Mutex<Option<job::Job>>,
|
||||
}
|
||||
|
||||
impl BackendState {
|
||||
pub fn stop(&self) {
|
||||
if let Some(mut child) = self.child.lock().unwrap().take() {
|
||||
log(&format!("stopping backend (pid {})", child.id()));
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
}
|
||||
// Closing the job handle kills anything the backend spawned (e.g. ffmpeg).
|
||||
#[cfg(windows)]
|
||||
drop(self.job.lock().unwrap().take());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start(app: &AppHandle) -> BackendState {
|
||||
let state = BackendState::default();
|
||||
|
||||
if std::env::var_os("SHOWY_SKIP_BACKEND").is_some() {
|
||||
log("SHOWY_SKIP_BACKEND is set, not starting the backend");
|
||||
return state;
|
||||
}
|
||||
if port_in_use(BACKEND_PORT) {
|
||||
log(&format!(
|
||||
"port {BACKEND_PORT} is already in use, using the backend that is already running"
|
||||
));
|
||||
return state;
|
||||
}
|
||||
|
||||
let mut cmd = match backend_command() {
|
||||
Ok(cmd) => cmd,
|
||||
Err(err) => {
|
||||
log(&format!("cannot start backend: {err}"));
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
// Same contract as the Electron shell: tell the backend where the desktop
|
||||
// app keeps its own data.
|
||||
let data_dir = app.path().app_data_dir().ok();
|
||||
cmd.env(
|
||||
"PARENT_PROCESS",
|
||||
serde_json::json!({ "tauri_path": data_dir }).to_string(),
|
||||
);
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
// Backend logs go to the same terminal as `tauri dev`.
|
||||
cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit());
|
||||
} else {
|
||||
// No console in release builds: write the backend output to a log file.
|
||||
let log_file = app.path().app_log_dir().ok().and_then(|dir| {
|
||||
fs::create_dir_all(&dir).ok()?;
|
||||
File::create(dir.join("backend.log")).ok()
|
||||
});
|
||||
match log_file.and_then(|f| f.try_clone().ok().map(|f2| (f, f2))) {
|
||||
Some((out, err)) => {
|
||||
cmd.stdout(out).stderr(err);
|
||||
}
|
||||
None => {
|
||||
cmd.stdout(Stdio::null()).stderr(Stdio::null());
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::process::CommandExt;
|
||||
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
|
||||
cmd.creation_flags(CREATE_NO_WINDOW);
|
||||
}
|
||||
}
|
||||
cmd.stdin(Stdio::null());
|
||||
|
||||
match cmd.spawn() {
|
||||
Ok(child) => {
|
||||
log(&format!("backend started (pid {})", child.id()));
|
||||
#[cfg(windows)]
|
||||
if let Some(job) = job::Job::kill_on_close() {
|
||||
if job.assign(&child) {
|
||||
*state.job.lock().unwrap() = Some(job);
|
||||
}
|
||||
}
|
||||
*state.child.lock().unwrap() = Some(child);
|
||||
}
|
||||
Err(err) => log(&format!("failed to spawn backend: {err}")),
|
||||
}
|
||||
state
|
||||
}
|
||||
|
||||
fn backend_command() -> Result<Command, String> {
|
||||
if let Some(exe) = std::env::var_os("SHOWY_BACKEND_EXE") {
|
||||
let exe = PathBuf::from(exe);
|
||||
return exe_command(exe);
|
||||
}
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
let dir = std::env::var_os("SHOWY_BACKEND_DIR")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|| {
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../backend")
|
||||
});
|
||||
let dir = dir.canonicalize().map_err(|e| format!("{}: {e}", dir.display()))?;
|
||||
if !dir.join("server.js").is_file() {
|
||||
return Err(format!("no server.js in {}", dir.display()));
|
||||
}
|
||||
let mut cmd = Command::new("node");
|
||||
cmd.arg("server.js").current_dir(dir);
|
||||
Ok(cmd)
|
||||
} else {
|
||||
let exe_dir = std::env::current_exe()
|
||||
.map_err(|e| e.to_string())?
|
||||
.parent()
|
||||
.map(PathBuf::from)
|
||||
.ok_or("cannot resolve the app folder")?;
|
||||
let name = if cfg!(windows) { "backend.exe" } else { "backend" };
|
||||
exe_command(exe_dir.join(name))
|
||||
}
|
||||
}
|
||||
|
||||
fn exe_command(exe: PathBuf) -> Result<Command, String> {
|
||||
if !exe.is_file() {
|
||||
return Err(format!("backend executable not found: {}", exe.display()));
|
||||
}
|
||||
let mut cmd = Command::new(&exe);
|
||||
if let Some(dir) = exe.parent() {
|
||||
cmd.current_dir(dir);
|
||||
}
|
||||
Ok(cmd)
|
||||
}
|
||||
|
||||
fn port_in_use(port: u16) -> bool {
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], port));
|
||||
TcpStream::connect_timeout(&addr, Duration::from_millis(300)).is_ok()
|
||||
}
|
||||
|
||||
fn log(msg: &str) {
|
||||
eprintln!("[showy] {msg}");
|
||||
}
|
||||
|
||||
/// A Windows job object that kills every process in it when its handle closes,
|
||||
/// including when the app crashes, so the backend never outlives the window.
|
||||
#[cfg(windows)]
|
||||
mod job {
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use std::process::Child;
|
||||
|
||||
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};
|
||||
use windows_sys::Win32::System::JobObjects::{
|
||||
AssignProcessToJobObject, CreateJobObjectW, JobObjectExtendedLimitInformation,
|
||||
SetInformationJobObject, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,
|
||||
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
|
||||
};
|
||||
|
||||
pub struct Job(HANDLE);
|
||||
|
||||
// The handle is only used behind a Mutex and closed once.
|
||||
unsafe impl Send for Job {}
|
||||
|
||||
impl Job {
|
||||
pub fn kill_on_close() -> Option<Job> {
|
||||
unsafe {
|
||||
let handle = CreateJobObjectW(std::ptr::null(), std::ptr::null());
|
||||
if handle.is_null() {
|
||||
return None;
|
||||
}
|
||||
let mut info: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = std::mem::zeroed();
|
||||
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
|
||||
let ok = SetInformationJobObject(
|
||||
handle,
|
||||
JobObjectExtendedLimitInformation,
|
||||
&info as *const _ as *const _,
|
||||
std::mem::size_of_val(&info) as u32,
|
||||
);
|
||||
if ok == 0 {
|
||||
CloseHandle(handle);
|
||||
return None;
|
||||
}
|
||||
Some(Job(handle))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assign(&self, child: &Child) -> bool {
|
||||
unsafe { AssignProcessToJobObject(self.0, child.as_raw_handle() as HANDLE) != 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Job {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
CloseHandle(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
mod backend;
|
||||
|
||||
use tauri::{Manager, RunEvent};
|
||||
|
||||
pub fn run() {
|
||||
let app = tauri::Builder::default()
|
||||
.setup(|app| {
|
||||
let state = backend::start(app.handle());
|
||||
app.manage(state);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
if std::env::var("SHOWY_NO_DEVTOOLS").is_err() {
|
||||
if let Some(window) = app.get_webview_window("main") {
|
||||
window.open_devtools();
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while building the Showy Tauri app");
|
||||
|
||||
app.run(|handle, event| {
|
||||
if let RunEvent::Exit = event {
|
||||
if let Some(state) = handle.try_state::<backend::BackendState>() {
|
||||
state.stop();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// Prevents an additional console window on Windows in release builds.
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
fn main() {
|
||||
showy_lib::run()
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"bundle": {
|
||||
"externalBin": ["binaries/backend"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "Showy",
|
||||
"version": "2.0.0",
|
||||
"identifier": "x4lo.showy",
|
||||
"build": {
|
||||
"beforeDevCommand": "pnpm --dir ../react_frontend dev:tauri",
|
||||
"devUrl": "http://localhost:4200",
|
||||
"beforeBuildCommand": "pnpm --dir ../react_frontend build:tauri",
|
||||
"frontendDist": "../frontend_build"
|
||||
},
|
||||
"app": {
|
||||
"windows": [
|
||||
{
|
||||
"label": "main",
|
||||
"title": "Showy",
|
||||
"width": 1280,
|
||||
"height": 800,
|
||||
"minWidth": 900,
|
||||
"minHeight": 600,
|
||||
"decorations": false,
|
||||
"backgroundColor": "#09090b",
|
||||
"center": true
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": null
|
||||
}
|
||||
},
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"targets": ["nsis"],
|
||||
"icon": [
|
||||
"icons/32x32.png",
|
||||
"icons/128x128.png",
|
||||
"icons/128x128@2x.png",
|
||||
"icons/icon.icns",
|
||||
"icons/icon.ico"
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user