mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-17 22:39:42 +02:00
Try fix oauth window creation
This commit is contained in:
@@ -88,9 +88,9 @@ export async function getAuthorizationCode(
|
|||||||
const code = await new Promise<string>(async (resolve, reject) => {
|
const code = await new Promise<string>(async (resolve, reject) => {
|
||||||
let foundCode = false;
|
let foundCode = false;
|
||||||
const { close } = await ctx.window.openUrl({
|
const { close } = await ctx.window.openUrl({
|
||||||
|
dataDirKey,
|
||||||
url: authorizationUrlStr,
|
url: authorizationUrlStr,
|
||||||
label: 'oauth-authorization-url',
|
label: 'oauth-authorization-url',
|
||||||
dataDirKey,
|
|
||||||
async onClose() {
|
async onClose() {
|
||||||
if (!foundCode) {
|
if (!foundCode) {
|
||||||
reject(new Error('Authorization window closed'));
|
reject(new Error('Authorization window closed'));
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { Context } from '@yaakapp/api';
|
import type { Context } from '@yaakapp/api';
|
||||||
import type { AccessToken, AccessTokenRawResponse } from '../store';
|
import type { AccessToken, AccessTokenRawResponse} from '../store';
|
||||||
import { getToken, storeToken } from '../store';
|
import { getDataDirKey , getToken, storeToken } from '../store';
|
||||||
import { isTokenExpired } from '../util';
|
import { isTokenExpired } from '../util';
|
||||||
|
|
||||||
export async function getImplicit(
|
export async function getImplicit(
|
||||||
@@ -60,7 +60,9 @@ export async function getImplicit(
|
|||||||
const newToken = await new Promise<AccessToken>(async (resolve, reject) => {
|
const newToken = await new Promise<AccessToken>(async (resolve, reject) => {
|
||||||
let foundAccessToken = false;
|
let foundAccessToken = false;
|
||||||
const authorizationUrlStr = authorizationUrl.toString();
|
const authorizationUrlStr = authorizationUrl.toString();
|
||||||
|
const dataDirKey = await getDataDirKey(ctx, contextId);
|
||||||
const { close } = await ctx.window.openUrl({
|
const { close } = await ctx.window.openUrl({
|
||||||
|
dataDirKey,
|
||||||
url: authorizationUrlStr,
|
url: authorizationUrlStr,
|
||||||
label: 'oauth-authorization-url',
|
label: 'oauth-authorization-url',
|
||||||
async onClose() {
|
async onClose() {
|
||||||
|
|||||||
@@ -1198,13 +1198,13 @@ async fn cmd_new_child_window(
|
|||||||
title: &str,
|
title: &str,
|
||||||
inner_size: (f64, f64),
|
inner_size: (f64, f64),
|
||||||
) -> YaakResult<()> {
|
) -> YaakResult<()> {
|
||||||
window::create_child_window(&parent_window, url, label, title, inner_size);
|
window::create_child_window(&parent_window, url, label, title, inner_size)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn cmd_new_main_window(app_handle: AppHandle, url: &str) -> YaakResult<()> {
|
async fn cmd_new_main_window(app_handle: AppHandle, url: &str) -> YaakResult<()> {
|
||||||
window::create_main_window(&app_handle, url);
|
window::create_main_window(&app_handle, url)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,16 +7,16 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use cookie::Cookie;
|
use cookie::Cookie;
|
||||||
use log::warn;
|
use log::{error, warn};
|
||||||
use tauri::{AppHandle, Emitter, Manager, Runtime, State};
|
use tauri::{AppHandle, Emitter, Manager, Runtime, State};
|
||||||
use tauri_plugin_clipboard_manager::ClipboardExt;
|
use tauri_plugin_clipboard_manager::ClipboardExt;
|
||||||
use yaak_models::models::{HttpResponse, Plugin};
|
use yaak_models::models::{HttpResponse, Plugin};
|
||||||
use yaak_models::query_manager::QueryManagerExt;
|
use yaak_models::query_manager::QueryManagerExt;
|
||||||
use yaak_models::util::UpdateSource;
|
use yaak_models::util::UpdateSource;
|
||||||
use yaak_plugins::events::{
|
use yaak_plugins::events::{
|
||||||
Color, DeleteKeyValueResponse, EmptyPayload, FindHttpResponsesResponse, GetCookieValueResponse,
|
Color, DeleteKeyValueResponse, EmptyPayload, ErrorResponse, FindHttpResponsesResponse,
|
||||||
GetHttpRequestByIdResponse, GetKeyValueResponse, Icon, InternalEvent, InternalEventPayload,
|
GetCookieValueResponse, GetHttpRequestByIdResponse, GetKeyValueResponse, Icon, InternalEvent,
|
||||||
ListCookieNamesResponse, PluginWindowContext, RenderGrpcRequestResponse,
|
InternalEventPayload, ListCookieNamesResponse, PluginWindowContext, RenderGrpcRequestResponse,
|
||||||
RenderHttpRequestResponse, SendHttpRequestResponse, SetKeyValueResponse, ShowToastRequest,
|
RenderHttpRequestResponse, SendHttpRequestResponse, SetKeyValueResponse, ShowToastRequest,
|
||||||
TemplateRenderResponse, WindowNavigateEvent,
|
TemplateRenderResponse, WindowNavigateEvent,
|
||||||
};
|
};
|
||||||
@@ -124,6 +124,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
|||||||
Some(InternalEventPayload::TemplateRenderResponse(TemplateRenderResponse { data }))
|
Some(InternalEventPayload::TemplateRenderResponse(TemplateRenderResponse { data }))
|
||||||
}
|
}
|
||||||
InternalEventPayload::ErrorResponse(resp) => {
|
InternalEventPayload::ErrorResponse(resp) => {
|
||||||
|
error!("Plugin error: {}: {:?}", resp.error, resp);
|
||||||
let toast_event = plugin_handle.build_event_to_send(
|
let toast_event = plugin_handle.build_event_to_send(
|
||||||
&window_context,
|
&window_context,
|
||||||
&InternalEventPayload::ShowToastRequest(ShowToastRequest {
|
&InternalEventPayload::ShowToastRequest(ShowToastRequest {
|
||||||
@@ -218,20 +219,29 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
InternalEventPayload::OpenWindowRequest(req) => {
|
InternalEventPayload::OpenWindowRequest(req) => {
|
||||||
let label = req.label;
|
|
||||||
let (navigation_tx, mut navigation_rx) = tokio::sync::mpsc::channel(128);
|
let (navigation_tx, mut navigation_rx) = tokio::sync::mpsc::channel(128);
|
||||||
let (close_tx, mut close_rx) = tokio::sync::mpsc::channel(128);
|
let (close_tx, mut close_rx) = tokio::sync::mpsc::channel(128);
|
||||||
let win_config = CreateWindowConfig {
|
let win_config = CreateWindowConfig {
|
||||||
url: &req.url,
|
url: &req.url,
|
||||||
label: &label.clone(),
|
label: &req.label,
|
||||||
title: &req.title.unwrap_or_default(),
|
title: &req.title.clone().unwrap_or_default(),
|
||||||
navigation_tx: Some(navigation_tx),
|
navigation_tx: Some(navigation_tx),
|
||||||
close_tx: Some(close_tx),
|
close_tx: Some(close_tx),
|
||||||
inner_size: req.size.map(|s| (s.width, s.height)),
|
inner_size: req.size.clone().map(|s| (s.width, s.height)),
|
||||||
data_dir_key: req.data_dir_key,
|
data_dir_key: req.data_dir_key.clone(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
create_window(app_handle, win_config);
|
if let Err(e) = create_window(app_handle, win_config) {
|
||||||
|
let error_event = plugin_handle.build_event_to_send(
|
||||||
|
&window_context,
|
||||||
|
&InternalEventPayload::ErrorResponse(ErrorResponse {
|
||||||
|
error: format!("Failed to create window: {:?}", e),
|
||||||
|
}),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
Box::pin(handle_plugin_event(app_handle, &error_event, plugin_handle)).await;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let event_id = event.id.clone();
|
let event_id = event.id.clone();
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ use tauri::{
|
|||||||
};
|
};
|
||||||
use tauri_plugin_opener::OpenerExt;
|
use tauri_plugin_opener::OpenerExt;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
use crate::error::Error::GenericError;
|
||||||
|
use crate::error::Result;
|
||||||
|
|
||||||
const DEFAULT_WINDOW_WIDTH: f64 = 1100.0;
|
const DEFAULT_WINDOW_WIDTH: f64 = 1100.0;
|
||||||
const DEFAULT_WINDOW_HEIGHT: f64 = 600.0;
|
const DEFAULT_WINDOW_HEIGHT: f64 = 600.0;
|
||||||
@@ -32,9 +34,9 @@ pub(crate) struct CreateWindowConfig<'s> {
|
|||||||
pub(crate) fn create_window<R: Runtime>(
|
pub(crate) fn create_window<R: Runtime>(
|
||||||
handle: &AppHandle<R>,
|
handle: &AppHandle<R>,
|
||||||
config: CreateWindowConfig,
|
config: CreateWindowConfig,
|
||||||
) -> WebviewWindow<R> {
|
) -> Result<WebviewWindow<R>> {
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
let menu = app_menu(handle).unwrap();
|
let menu = app_menu(handle)?;
|
||||||
|
|
||||||
// This causes the window to not be clickable (in AppImage), so disable on Linux
|
// This causes the window to not be clickable (in AppImage), so disable on Linux
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
@@ -55,12 +57,12 @@ pub(crate) fn create_window<R: Runtime>(
|
|||||||
#[cfg(not(target_os = "macos"))]
|
#[cfg(not(target_os = "macos"))]
|
||||||
{
|
{
|
||||||
use std::fs;
|
use std::fs;
|
||||||
let dir = handle.path().temp_dir().unwrap().join("yaak_sessions").join(key);
|
let dir = handle.path().app_data_dir()?.join("window-sessions").join(key);
|
||||||
fs::create_dir_all(dir.clone()).unwrap();
|
fs::create_dir_all(dir.clone())?;
|
||||||
win_builder = win_builder.data_directory(dir);
|
win_builder = win_builder.data_directory(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
// macOS doesn't support data dir so must use this fn instead
|
// macOS doesn't support `data_directory()` so must use this fn instead
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
{
|
{
|
||||||
let hash = md5::compute(key.as_bytes());
|
let hash = md5::compute(key.as_bytes());
|
||||||
@@ -108,11 +110,11 @@ pub(crate) fn create_window<R: Runtime>(
|
|||||||
|
|
||||||
if let Some(w) = handle.webview_windows().get(config.label) {
|
if let Some(w) = handle.webview_windows().get(config.label) {
|
||||||
info!("Webview with label {} already exists. Focusing existing", config.label);
|
info!("Webview with label {} already exists. Focusing existing", config.label);
|
||||||
w.set_focus().unwrap();
|
w.set_focus()?;
|
||||||
return w.to_owned();
|
return Ok(w.to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
let win = win_builder.build().unwrap();
|
let win = win_builder.build()?;
|
||||||
|
|
||||||
if let Some(tx) = config.close_tx {
|
if let Some(tx) = config.close_tx {
|
||||||
win.on_window_event(move |event| match event {
|
win.on_window_event(move |event| match event {
|
||||||
@@ -174,10 +176,10 @@ pub(crate) fn create_window<R: Runtime>(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
win
|
Ok(win)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn create_main_window(handle: &AppHandle, url: &str) -> WebviewWindow {
|
pub(crate) fn create_main_window(handle: &AppHandle, url: &str) -> Result<WebviewWindow> {
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
let label = loop {
|
let label = loop {
|
||||||
let label = format!("{MAIN_WINDOW_PREFIX}{counter}");
|
let label = format!("{MAIN_WINDOW_PREFIX}{counter}");
|
||||||
@@ -211,7 +213,7 @@ pub(crate) fn create_child_window(
|
|||||||
label: &str,
|
label: &str,
|
||||||
title: &str,
|
title: &str,
|
||||||
inner_size: (f64, f64),
|
inner_size: (f64, f64),
|
||||||
) -> WebviewWindow {
|
) -> Result<WebviewWindow> {
|
||||||
let app_handle = parent_window.app_handle();
|
let app_handle = parent_window.app_handle();
|
||||||
let label = format!("{OTHER_WINDOW_PREFIX}_{label}");
|
let label = format!("{OTHER_WINDOW_PREFIX}_{label}");
|
||||||
let scale_factor = parent_window.scale_factor().unwrap();
|
let scale_factor = parent_window.scale_factor().unwrap();
|
||||||
@@ -235,7 +237,7 @@ pub(crate) fn create_child_window(
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let child_window = create_window(&app_handle, config);
|
let child_window = create_window(&app_handle, config)?;
|
||||||
|
|
||||||
// NOTE: These listeners will remain active even when the windows close. Unfortunately,
|
// NOTE: These listeners will remain active even when the windows close. Unfortunately,
|
||||||
// there's no way to unlisten to events for now, so we just have to be defensive.
|
// there's no way to unlisten to events for now, so we just have to be defensive.
|
||||||
@@ -272,5 +274,5 @@ pub(crate) fn create_child_window(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
child_window
|
Ok(child_window)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user