feat(auth): refine embedded webview integration

This commit is contained in:
Kevin Yue committed 2026-07-21 22:21:17 +08:00
1 parent 537e12d706
commit c0c517806e
12 files changed
+401 -44

No files matched your search

+27 -8
View File
@@ -1,19 +1,38 @@
use super::lock_file::gpservice_lock_info;
use std::path::Path;
async fn read_port() -> anyhow::Result<String> {
let lock_info = gpservice_lock_info().await?;
Ok(lock_info.port.to_string())
}
use super::lock_file::{gpservice_lock_info, gpservice_lock_info_at};
pub async fn http_endpoint() -> anyhow::Result<String> {
let port = read_port().await?;
let port = gpservice_lock_info().await?.port;
Ok(format!("http://127.0.0.1:{}", port))
}
pub async fn ws_endpoint() -> anyhow::Result<String> {
let port = read_port().await?;
let port = gpservice_lock_info().await?.port;
Ok(format!("ws://127.0.0.1:{}/ws", port))
}
pub async fn ws_endpoint_from_lock_file(path: impl AsRef<Path>) -> anyhow::Result<String> {
let port = gpservice_lock_info_at(path).await?.port;
Ok(format!("ws://127.0.0.1:{}/ws", port))
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn websocket_endpoint_uses_the_selected_lock_file() {
let temp = tempfile::tempdir().unwrap();
let lock_file = temp.path().join("gpservice.lock");
std::fs::write(&lock_file, "123:4242").unwrap();
assert_eq!(
ws_endpoint_from_lock_file(lock_file).await.unwrap(),
"ws://127.0.0.1:4242/ws"
);
}
}
+18 -2
View File
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use common::constants::GP_SERVICE_LOCK_FILE;
use thiserror::Error;
@@ -9,6 +9,10 @@ pub struct LockFile {
pid: u32,
}
pub fn dev_service_lock_file_path(bootstrap_socket: impl AsRef<Path>) -> PathBuf {
bootstrap_socket.as_ref().with_file_name("gpservice.lock")
}
impl LockFile {
pub fn new<P: Into<PathBuf>>(path: P, pid: u32) -> Self {
Self { path: path.into(), pid }
@@ -95,7 +99,11 @@ impl LockInfo {
}
pub async fn gpservice_lock_info() -> Result<LockInfo, LockFileError> {
LockInfo::from_file(GP_SERVICE_LOCK_FILE).await
gpservice_lock_info_at(GP_SERVICE_LOCK_FILE).await
}
pub async fn gpservice_lock_info_at(path: impl AsRef<Path>) -> Result<LockInfo, LockFileError> {
LockInfo::from_file(path).await
}
#[cfg(test)]
@@ -126,4 +134,12 @@ mod tests {
Err(LockFileError::InvalidPort(_))
));
}
#[test]
fn dev_service_lock_file_is_next_to_the_bootstrap_socket() {
assert_eq!(
dev_service_lock_file_path("/var/run/gpservice-dev-123/dev-bootstrap.sock"),
PathBuf::from("/var/run/gpservice-dev-123/gpservice.lock")
);
}
}
+48 -1
View File
@@ -1,11 +1,40 @@
use tauri::WebviewWindow;
/// Returns the frame-to-content height difference for a standard titled macOS window.
/// This is nonzero only on macOS.
pub fn get_title_bar_height() -> f64 {
#[cfg(target_os = "macos")]
{
macos_title_bar_height(os_info::get().version())
}
#[cfg(not(target_os = "macos"))]
{
0.0
}
}
#[cfg(target_os = "macos")]
fn macos_title_bar_height(version: &os_info::Version) -> f64 {
if version.ge(&os_info::Version::from_string("26.0.0")) {
32.0
} else {
28.0
}
}
pub trait WindowExt {
fn raise(&self) -> anyhow::Result<()>;
}
impl WindowExt for WebviewWindow {
#[cfg(any(target_os = "macos", target_os = "windows"))]
#[cfg(target_os = "macos")]
fn raise(&self) -> anyhow::Result<()> {
self.show()?;
self.set_focus()?;
Ok(())
}
#[cfg(target_os = "windows")]
fn raise(&self) -> anyhow::Result<()> {
self.show()?;
Ok(())
@@ -17,6 +46,24 @@ impl WindowExt for WebviewWindow {
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(target_os = "macos")]
#[test]
fn uses_the_current_macos_title_bar_height() {
assert_eq!(macos_title_bar_height(&os_info::Version::from_string("25.9.0")), 28.0);
assert_eq!(macos_title_bar_height(&os_info::Version::from_string("26.0.0")), 32.0);
}
#[cfg(not(target_os = "macos"))]
#[test]
fn native_title_bar_height_is_zero_without_a_native_macos_title_bar() {
assert_eq!(get_title_bar_height(), 0.0);
}
}
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
mod unix {
use std::{process::ExitStatus, time::Duration};