Files
cinny-desktop/src-tauri/src/lib.rs
T

80 lines
3.2 KiB
Rust
Raw Normal View History

2026-03-03 23:16:04 +11:00
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
2026-04-13 15:20:44 +08:00
use tauri::{webview::{NewWindowResponse, WebviewWindowBuilder}, WebviewUrl};
use tauri_plugin_opener::OpenerExt;
2026-03-03 23:16:04 +11:00
pub fn run() {
let port: u16 = 44548;
let context = tauri::generate_context!();
let builder = tauri::Builder::default();
builder
.plugin(tauri_plugin_localhost::Builder::new(port).build())
.plugin(tauri_plugin_window_state::Builder::default().build())
2026-04-13 15:20:44 +08:00
.plugin(tauri_plugin_opener::init())
2026-03-03 23:16:04 +11:00
.setup(move |app| {
2026-03-17 21:29:22 +08:00
#[cfg(debug_assertions)]
let window_url = WebviewUrl::App(Default::default());
#[cfg(not(debug_assertions))]
let window_url = {
let url = format!("http://localhost:{}", port).parse().unwrap();
WebviewUrl::External(url)
};
2026-04-13 15:20:44 +08:00
let app_handle = app.handle().clone();
let window = WebviewWindowBuilder::new(app, "main".to_string(), window_url)
2026-03-03 23:16:04 +11:00
.title("Cinny")
.disable_drag_drop_handler()
2026-04-13 15:20:44 +08:00
.on_new_window(move |url, _features| {
let _ = app_handle.opener().open_url(url.as_str(), None::<&str>);
NewWindowResponse::Deny
})
.build()?;
// Grant camera and microphone to WebView2 automatically.
// Windows requires an explicit PermissionRequested COM event handler.
#[cfg(target_os = "windows")]
window.with_webview(|webview| {
use webview2_com::{
Microsoft::Web::WebView2::Win32::{
COREWEBVIEW2_PERMISSION_KIND,
COREWEBVIEW2_PERMISSION_KIND_CAMERA,
COREWEBVIEW2_PERMISSION_KIND_MICROPHONE,
COREWEBVIEW2_PERMISSION_STATE_ALLOW,
},
PermissionRequestedEventHandler,
};
let controller = webview.controller();
if let Ok(core) = unsafe { controller.CoreWebView2() } {
let handler = PermissionRequestedEventHandler::create(Box::new(
|_sender, args| {
if let Some(args) = args {
let mut kind = COREWEBVIEW2_PERMISSION_KIND(0);
unsafe { args.PermissionKind(&mut kind) }?;
if kind == COREWEBVIEW2_PERMISSION_KIND_MICROPHONE
|| kind == COREWEBVIEW2_PERMISSION_KIND_CAMERA
{
unsafe {
args.SetState(COREWEBVIEW2_PERMISSION_STATE_ALLOW)
}?;
}
}
Ok(())
},
));
let mut token = Default::default();
let _ = unsafe { core.add_PermissionRequested(&handler, &mut token) };
}
})?;
2026-03-03 23:16:04 +11:00
Ok(())
})
.run(context)
.expect("error while building tauri application");
}