From 6a04692ad3ec1a6e651f61b2a6678f44e5ec0b9e Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 3 Nov 2024 02:27:17 +0200 Subject: [PATCH] replace GestureClick with StateFlags implementation --- .../item/page/navigation/request/widget.rs | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/app/browser/window/tab/item/page/navigation/request/widget.rs b/src/app/browser/window/tab/item/page/navigation/request/widget.rs index 51226e02..17a6f78d 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/widget.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/widget.rs @@ -3,11 +3,10 @@ mod database; use database::Database; use gtk::{ - gdk::BUTTON_PRIMARY, gio::SimpleAction, glib::{timeout_add_local, ControlFlow, GString, SourceId}, prelude::{ActionExt, EditableExt, EntryExt, ToVariant, WidgetExt}, - Entry, GestureClick, + Entry, StateFlags, }; use sqlite::Transaction; use std::{cell::RefCell, sync::Arc, time::Duration}; @@ -40,20 +39,12 @@ impl Widget { source_id: RefCell::new(None), }); - // Init additional controllers - let primary_button_controller = GestureClick::builder().button(BUTTON_PRIMARY).build(); - // Init widget let gobject = Entry::builder() .placeholder_text(PLACEHOLDER_TEXT) .hexpand(true) .build(); - gobject - .first_child() - .unwrap() // text widget should be there - .add_controller(primary_button_controller.clone()); - // Connect events gobject.connect_changed(move |_| { action_update.activate(Some(&"".to_variant())); // @TODO @@ -63,20 +54,17 @@ impl Widget { action_page_reload.activate(None); }); - primary_button_controller.connect_pressed({ - let gobject = gobject.clone(); - move |_, _, _, _| { - let gobject = gobject.clone(); - // Select entire text on first focus at entry - // this behavior implemented in most web-browsers, - // to simply overwrite current request with new value - if !gobject.first_child().unwrap().has_focus() { - // Small trick to overwrite default GTK behavior - // @TODO find another way to prevent defaults but with timeout - timeout_add_local(Duration::from_millis(100), move || { - gobject.select_region(0, gobject.text_length().into()); - ControlFlow::Break - }); + gobject.connect_state_flags_changed(|this, state| { + // Select entire text on key release + // this behavior implemented in most web-browsers, + // to simply overwrite current request with new value + // Note: + // * Custom GestureClick is not option here, as GTK Entry already has default one + println!("{:?}", state); + if state.contains(StateFlags::ACTIVE) && state.contains(StateFlags::FOCUS_WITHIN) { + // Continue on first focus event + if this.selection_bounds().is_none() { + this.select_region(0, this.text_length().into()); } } });