From 5dd8de7950734c3e5d0c29406c0e486102695947 Mon Sep 17 00:00:00 2001 From: ardocrat Date: Thu, 29 May 2025 23:50:26 +0300 Subject: [PATCH] modal: move focus setup to the root content --- src/gui/app.rs | 28 ++++++++++++++-------------- src/gui/views/modal.rs | 22 ++++++---------------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/gui/app.rs b/src/gui/app.rs index 5c0ff5b..85b6b92 100755 --- a/src/gui/app.rs +++ b/src/gui/app.rs @@ -13,22 +13,15 @@ // limitations under the License. use egui::epaint::RectShape; -use egui::{Align, Context, CornerRadius, CursorIcon, Layout, Modifiers, ResizeDirection, Stroke, StrokeKind, UiBuilder, ViewportCommand}; -use lazy_static::lazy_static; -use std::sync::atomic::{AtomicBool, Ordering}; +use egui::{Align, Context, CornerRadius, CursorIcon, LayerId, Layout, Modifiers, Order, ResizeDirection, Stroke, StrokeKind, UiBuilder, ViewportCommand}; use crate::gui::icons::{ARROWS_IN, ARROWS_OUT, CARET_DOWN, MOON, SUN, X}; use crate::gui::platform::PlatformCallbacks; -use crate::gui::views::{Content, Modal, TitlePanel, View}; +use crate::gui::views::{Content, KeyboardContent, Modal, TitlePanel, View}; use crate::gui::Colors; use crate::wallet::ExternalConnection; use crate::AppConfig; -lazy_static! { - /// State to check if platform Back button was pressed. - static ref BACK_BUTTON_PRESSED: AtomicBool = AtomicBool::new(false); -} - /// Implements ui entry point and contains platform-specific callbacks. pub struct App { /// Handles platform-specific functionality. @@ -75,14 +68,10 @@ impl App { } // Handle Esc keyboard key event. - let back_pressed = BACK_BUTTON_PRESSED.load(Ordering::Relaxed); - if back_pressed || ctx.input_mut(|i| i.consume_key(Modifiers::NONE, egui::Key::Escape)) { + if ctx.input_mut(|i| i.consume_key(Modifiers::NONE, egui::Key::Escape)) { if Modal::on_back() { self.content.on_back(&self.platform); } - if back_pressed { - BACK_BUTTON_PRESSED.store(false, Ordering::Relaxed); - } // Request repaint to update previous content. ctx.request_repaint(); } @@ -147,6 +136,17 @@ impl App { ctx.input(|i| i.viewport().focused.unwrap_or(true)) { self.platform.clear_user_attention(); } + + // Show modal or keyboard window above others. + ctx.move_to_top(LayerId::new(Order::Middle, egui::Id::new(Modal::WINDOW_ID))); + let keyboard_showing = if let Some(l) = ctx.top_layer_id() { + l.id == egui::Id::new(KeyboardContent::WINDOW_ID) + } else { + false + }; + if keyboard_showing { + ctx.move_to_top(LayerId::new(Order::Middle, egui::Id::new(KeyboardContent::WINDOW_ID))); + } } /// Draw custom desktop window frame content. diff --git a/src/gui/views/modal.rs b/src/gui/views/modal.rs index 7fd3849..11a253b 100755 --- a/src/gui/views/modal.rs +++ b/src/gui/views/modal.rs @@ -14,14 +14,14 @@ use egui::epaint::{RectShape, Shadow}; use egui::os::OperatingSystem; -use egui::{Align2, CornerRadius, Order, RichText, Stroke, StrokeKind, UiBuilder, Vec2}; +use egui::{Align2, CornerRadius, RichText, Stroke, StrokeKind, UiBuilder, Vec2}; use lazy_static::lazy_static; use parking_lot::RwLock; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use crate::gui::views::types::{ModalPosition, ModalState}; -use crate::gui::views::{Content, KeyboardContent, View}; +use crate::gui::views::{Content, View}; use crate::gui::Colors; lazy_static! { @@ -49,6 +49,8 @@ impl Modal { const DEFAULT_MARGIN: f32 = 8.0; /// Maximum width of the content. const DEFAULT_WIDTH: f32 = Content::SIDE_PANEL_WIDTH - (2.0 * Self::DEFAULT_MARGIN); + /// Modal content [`egui::Window`] id. + pub const WINDOW_ID: &'static str = "modal_window"; /// Create closeable [`Modal`] with center position. pub fn new(id: &'static str) -> Self { @@ -226,7 +228,7 @@ impl Modal { // Show main content window at given position. let (content_align, content_offset) = self.modal_position(); - let res = egui::Window::new("modal_window") + egui::Window::new(Self::WINDOW_ID) .title_bar(false) .resizable(false) .collapsible(false) @@ -248,19 +250,7 @@ impl Modal { title_ui(title, ui); } self.content_ui(ui, add_content); - - }).unwrap().response; - - // Show modal or keyboard window above others. - ctx.move_to_top(res.layer_id); - let keyboard_showing = if let Some(l) = ctx.top_layer_id() { - l.id == egui::Id::new(KeyboardContent::WINDOW_ID) - } else { - false - }; - if keyboard_showing { - ctx.move_to_top(egui::LayerId::new(Order::Middle, egui::Id::new(KeyboardContent::WINDOW_ID))) - } + }); // Setup first draw flag. if Self::first_draw() {