select entire text on first request entry focus

This commit is contained in:
yggverse 2024-10-30 02:48:00 +02:00
parent f70fde4263
commit 0912f2c68d

View File

@ -3,10 +3,11 @@ mod database;
use database::Database; use database::Database;
use gtk::{ use gtk::{
gdk::BUTTON_PRIMARY,
gio::SimpleAction, gio::SimpleAction,
glib::{timeout_add_local, ControlFlow, GString, SourceId}, glib::{timeout_add_local, ControlFlow, GString, SourceId},
prelude::{ActionExt, EditableExt, EntryExt, ToVariant}, prelude::{ActionExt, EditableExt, EntryExt, ToVariant, WidgetExt},
Entry, Entry, GestureClick,
}; };
use sqlite::Transaction; use sqlite::Transaction;
use std::{cell::RefCell, sync::Arc, time::Duration}; use std::{cell::RefCell, sync::Arc, time::Duration};
@ -39,12 +40,20 @@ impl Widget {
source_id: RefCell::new(None), source_id: RefCell::new(None),
}); });
// Init additional controllers
let primary_button_controller = GestureClick::builder().button(BUTTON_PRIMARY).build();
// Init widget // Init widget
let gobject = Entry::builder() let gobject = Entry::builder()
.placeholder_text(PLACEHOLDER_TEXT) .placeholder_text(PLACEHOLDER_TEXT)
.hexpand(true) .hexpand(true)
.build(); .build();
gobject
.first_child()
.unwrap() // text widget should be there
.add_controller(primary_button_controller.clone());
// Connect events // Connect events
gobject.connect_changed(move |_| { gobject.connect_changed(move |_| {
action_update.activate(Some(&"".to_variant())); // @TODO action_update.activate(Some(&"".to_variant())); // @TODO
@ -54,6 +63,24 @@ impl Widget {
action_tab_page_navigation_reload.activate(None); action_tab_page_navigation_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
});
}
}
});
// Return activated struct // Return activated struct
Arc::new(Self { gobject, progress }) Arc::new(Self { gobject, progress })
} }