From fb7312cb804d2494a792301a836dd01354539c83 Mon Sep 17 00:00:00 2001 From: ardocrat Date: Wed, 11 Sep 2024 21:13:52 +0300 Subject: [PATCH] desktop: request window focus on data --- src/gui/app.rs | 17 +++++++++++++++++ src/gui/platform/desktop/mod.rs | 24 +++++++++++------------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/gui/app.rs b/src/gui/app.rs index dbbbcd3..3fdfdb0 100644 --- a/src/gui/app.rs +++ b/src/gui/app.rs @@ -33,12 +33,17 @@ lazy_static! { pub struct App { /// Platform specific callbacks handler. pub platform: Platform, + /// Main content. content: Content, + /// Last window resize direction. resize_direction: Option, + /// Flag to check if it's first draw. first_draw: bool, + /// Flag to check if attention required after window focus. + attention_required: bool, } impl App { @@ -48,6 +53,7 @@ impl App { content: Content::default(), resize_direction: None, first_draw: true, + attention_required: false, } } @@ -109,8 +115,19 @@ impl App { // Provide incoming data to wallets. if let Some(data) = self.platform.consume_data() { self.content.wallets.on_data(ui, Some(data), &self.platform); + self.attention_required = true; } }); + + // Check if desktop window was focused after requested attention. + if View::is_desktop() && self.attention_required + && ctx.input(|i| i.viewport().focused.unwrap_or(true)) { + self.attention_required = false; + ctx.send_viewport_cmd( + ViewportCommand::RequestUserAttention(egui::UserAttentionType::Reset) + ); + ctx.send_viewport_cmd(ViewportCommand::WindowLevel(egui::WindowLevel::Normal)); + } } /// Draw custom resizeable window content. diff --git a/src/gui/platform/desktop/mod.rs b/src/gui/platform/desktop/mod.rs index 9b4b210..dbd266a 100644 --- a/src/gui/platform/desktop/mod.rs +++ b/src/gui/platform/desktop/mod.rs @@ -19,7 +19,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use parking_lot::RwLock; use lazy_static::lazy_static; -use egui::{UserAttentionType, ViewportCommand}; +use egui::{UserAttentionType, ViewportCommand, WindowLevel}; use rfd::FileDialog; use crate::gui::platform::PlatformCallbacks; @@ -126,14 +126,6 @@ impl PlatformCallbacks for Desktop { r_data.is_some() }; if has_data { - // Reset window state. - let r_ctx = self.ctx.read(); - if r_ctx.is_some() { - let ctx = r_ctx.as_ref().unwrap(); - ctx.send_viewport_cmd( - ViewportCommand::RequestUserAttention(UserAttentionType::Reset) - ); - } // Clear data. let mut w_data = PASSED_DATA.write(); let data = w_data.clone(); @@ -160,16 +152,22 @@ impl Desktop { let mut w_data = PASSED_DATA.write(); *w_data = Some(data); - // Bring focus on window. let r_ctx = self.ctx.read(); if r_ctx.is_some() { let ctx = r_ctx.as_ref().unwrap(); - ctx.send_viewport_cmd(ViewportCommand::Visible(true)); - ctx.send_viewport_cmd(ViewportCommand::Minimized(false)); + // Request attention on taskbar. ctx.send_viewport_cmd( ViewportCommand::RequestUserAttention(UserAttentionType::Informational) ); - ctx.send_viewport_cmd(ViewportCommand::Focus); + // Un-minimize window. + if ctx.input(|i| i.viewport().minimized.unwrap_or(false)) { + ctx.send_viewport_cmd(ViewportCommand::Minimized(false)); + } + // Focus to window. + if !ctx.input(|i| i.viewport().focused.unwrap_or(false)) { + ctx.send_viewport_cmd(ViewportCommand::WindowLevel(WindowLevel::AlwaysOnTop)); + ctx.send_viewport_cmd(ViewportCommand::Focus); + } ctx.request_repaint(); } }