move re-hide to main request handler

This commit is contained in:
Joseph Montanaro 2022-12-21 16:04:12 -08:00
parent 5ffa55c03c
commit bf4c46238e
6 changed files with 51 additions and 33 deletions

View File

@ -1,8 +1,5 @@
use core::time::Duration;
use serde::{Serialize, Deserialize};
use tauri::State;
use tokio::time::sleep;
use crate::clientinfo::Client;
use crate::state::{AppState, Session, Credentials};
@ -30,20 +27,9 @@ pub enum Approval {
#[tauri::command]
pub fn respond(
response: RequestResponse,
window: tauri::Window,
app_state: State<'_, AppState>
) -> Result<(), String> {
pub fn respond(response: RequestResponse, app_state: State<'_, AppState>) -> Result<(), String> {
app_state.send_response(response)
.map_err(|e| format!("Error responding to request: {e}"))?;
tauri::async_runtime::spawn(async move {
sleep(Duration::from_secs(3)).await;
let _ = window.hide();
});
Ok(())
.map_err(|e| format!("Error responding to request: {e}"))
}
@ -74,5 +60,5 @@ pub async fn save_credentials(
) -> Result<(), String> {
app_state.save_creds(credentials, &passphrase)
.await
.map_err(|e| e.to_string())
.map_err(|e| {eprintln!("{e:?}"); e.to_string()})
}

View File

@ -1,8 +1,10 @@
use core::time::Duration;
use std::io;
use std::net::{SocketAddr, SocketAddrV4};
use tokio::net::{TcpListener, TcpStream};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::oneshot;
use tokio::time::sleep;
use tauri::{AppHandle, Manager};
@ -49,7 +51,8 @@ impl Handler {
}
let req = Request {id: self.request_id, clients};
self.notify_frontend(&req).await?;
self.app.emit_all("credentials-request", &req)?;
let starting_visibility = self.ensure_visible()?;
match self.wait_for_response().await? {
Approval::Approved => self.send_credentials().await?,
@ -61,6 +64,17 @@ impl Handler {
}
}
// only hide the window if a) it was hidden to start with
// and b) there are no other pending requests
let state = self.app.state::<AppState>();
if !starting_visibility && state.req_count() == 0 {
let handle = self.app.app_handle();
tauri::async_runtime::spawn(async move {
sleep(Duration::from_secs(3)).await;
let _ = handle.get_window("main").map(|w| w.hide());
});
}
Ok(())
}
@ -90,15 +104,15 @@ impl Handler {
clients.iter().any(|c| state.is_banned(c))
}
async fn notify_frontend(&self, req: &Request) -> Result<(), RequestError> {
self.app.emit_all("credentials-request", req)?;
fn ensure_visible(&self) -> Result<bool, RequestError> {
let window = self.app.get_window("main").ok_or(RequestError::NoMainWindow)?;
if !window.is_visible()? {
let starting_visibility = window.is_visible()?;
if !starting_visibility {
window.unminimize()?;
window.show()?;
}
window.set_focus()?;
Ok(())
Ok(starting_visibility)
}
async fn wait_for_response(&mut self) -> Result<Approval, RequestError> {

View File

@ -65,8 +65,10 @@ pub struct AppState {
impl AppState {
pub fn new() -> Result<Self, SetupError> {
let conf = AppConfig::default();
let conn_opts = SqliteConnectOptions::new()
.filename("creddy.db")
.filename(&conf.db_path)
.create_if_missing(true);
let pool_opts = SqlitePoolOptions::new();
@ -75,7 +77,7 @@ impl AppState {
let creds = runtime::block_on(Self::load_creds(&pool))?;
let state = AppState {
config: RwLock::new(AppConfig::default()),
config: RwLock::new(conf),
session: RwLock::new(creds),
request_count: RwLock::new(0),
open_requests: RwLock::new(HashMap::new()),
@ -161,6 +163,11 @@ impl AppState {
open_requests.remove(&id);
}
pub fn req_count(&self) -> usize {
let open_requests = self.open_requests.read().unwrap();
open_requests.len()
}
pub fn send_response(&self, response: ipc::RequestResponse) -> Result<(), SendResponseError> {
let mut open_requests = self.open_requests.write().unwrap();
let chan = open_requests

View File

@ -5,20 +5,27 @@
export let appState;
const dispatch = createEventDispatcher();
let error = null;
let AccessKeyId, SecretAccessKey, passphrase
async function save() {
try {
console.log('Saving credentials.');
let credentials = {AccessKeyId, SecretAccessKey};
console.log(credentials);
await invoke('save_credentials', {credentials, passphrase});
if (appState.currentRequest) {
dispatch('navigate', {target: 'ShowApproved'})
try {
await invoke('save_credentials', {credentials, passphrase});
if (appState.currentRequest) {
dispatch('navigate', {target: 'ShowApproved'})
}
else {
dispatch('navigate', {target: 'Home'})
}
}
else {
dispatch('navigate', {target: 'Home'})
catch (e) {
error = e;
}
}
catch (e) {
console.log("Error saving credentials:", e);
@ -26,16 +33,19 @@
}
</script>
{#if error}
<div class="text-red-400">{error}</div>
{/if}
<form action="#" on:submit|preventDefault="{save}">
<div class="text-gray-200">AWS Access Key ID</div>
<input class="text-gray-200 bg-zinc-800" type="text" bind:value="{AccessKeyId}" />
<div class="text-gray-200">AWS Secret Access Key</div>
<input class="text-gray-200 bg-zinc-800" type="text" bind:value="{SecretAccessKey}" />
<input class="text-gray-200 bg-zinc-800" type="password" bind:value="{SecretAccessKey}" />
<div class="text-gray-200">Passphrase</div>
<input class="text-gray-200 bg-zinc-800" type="text" bind:value="{passphrase}" />
<input class="text-gray-200 bg-zinc-800" type="password" bind:value="{passphrase}" />
<input class="text-gray-200" type="submit" />
</form>

View File

@ -28,7 +28,7 @@
</script>
{#if error}
<div class="text-red-400">Error attempting to send approval: {error}</div>
<div class="text-red-400">{error}</div>
{:else}
<h1 class="text-4xl text-gray-300">Approved!</h1>
{/if}

View File

@ -30,6 +30,7 @@
{#if error}
<div class="text-red-400">{error}</div>
{/if}
<form action="#" on:submit|preventDefault="{unlock}">
<div class="text-gray-200">Enter your passphrase:</div>
<input autofocus class="text-gray-200 bg-zinc-800" type="password" placeholder="correct horse battery staple" bind:value="{passphrase}" />