remove extra event listener loop

This commit is contained in:
yggverse 2025-03-18 17:31:34 +02:00
parent e91930a9d9
commit e0e21b4926
2 changed files with 14 additions and 20 deletions

View File

@ -11,7 +11,7 @@ use gtk::{
Box, Label, Orientation, TextView, Box, Label, Orientation, TextView,
gio::SimpleAction, gio::SimpleAction,
glib::{Uri, UriHideFlags, uuid_string_random}, glib::{Uri, UriHideFlags, uuid_string_random},
prelude::{ActionExt, BoxExt, DisplayExt, WidgetExt}, prelude::{ActionExt, BoxExt, DisplayExt, TextBufferExt, TextViewExt, WidgetExt},
}; };
use std::rc::Rc; use std::rc::Rc;
@ -38,12 +38,11 @@ impl Response for Box {
size_limit: Option<usize>, size_limit: Option<usize>,
) -> Self { ) -> Self {
// Init local actions // Init local actions
let action_update = SimpleAction::new(&uuid_string_random(), None);
let action_send = SimpleAction::new(&uuid_string_random(), None); let action_send = SimpleAction::new(&uuid_string_random(), None);
// Init components // Init components
let control = Rc::new(Control::build(action_send.clone())); let control = Rc::new(Control::build(action_send.clone()));
let form = TextView::form(action_update.clone()); let text_view = TextView::form();
let title = Label::title(title); let title = Label::title(title);
// Init main widget // Init main widget
@ -57,21 +56,21 @@ impl Response for Box {
.build(); .build();
g_box.append(&title); g_box.append(&title);
g_box.append(&form); g_box.append(&text_view);
g_box.append(&control.g_box); g_box.append(&control.g_box);
// Init events // Init events
action_update.connect_activate({ text_view.buffer().connect_changed({
let base = base.clone(); let base = base.clone();
let control = control.clone(); let control = control.clone();
let form = form.clone(); let text_view = text_view.clone();
move |_, _| { move |_| {
control.update( control.update(
form.text().is_empty(), text_view.text().is_empty(),
size_limit.map(|limit| { size_limit.map(|limit| {
limit as isize limit as isize
- ((base.to_string_partial(UriHideFlags::QUERY).len() - ((base.to_string_partial(UriHideFlags::QUERY).len()
+ Uri::escape_string(&form.text(), None, false).len()) + Uri::escape_string(&text_view.text(), None, false).len())
as isize) as isize)
}), }),
) )
@ -79,20 +78,20 @@ impl Response for Box {
}); });
action_send.connect_activate({ action_send.connect_activate({
let form = form.clone(); let text_view = text_view.clone();
move |_, _| { move |_, _| {
item_action.load.activate( item_action.load.activate(
Some(&format!( Some(&format!(
"{}?{}", "{}?{}",
base.to_string_partial(UriHideFlags::QUERY), base.to_string_partial(UriHideFlags::QUERY),
Uri::escape_string(&form.text(), None, false), Uri::escape_string(&text_view.text(), None, false),
)), )),
false, false,
) )
} }
}); });
form.add_controller({ text_view.add_controller({
const SHORTCUT: &str = "<Primary>Return"; // @TODO optional const SHORTCUT: &str = "<Primary>Return"; // @TODO optional
/*control /*control

View File

@ -1,8 +1,7 @@
use gtk::{ use gtk::{
TextView, WrapMode, TextView, WrapMode,
gio::SimpleAction,
glib::GString, glib::GString,
prelude::{ActionExt, TextBufferExt, TextViewExt, WidgetExt}, prelude::{TextBufferExt, TextViewExt, WidgetExt},
}; };
use libspelling::{Checker, TextBufferAdapter}; use libspelling::{Checker, TextBufferAdapter};
use sourceview::Buffer; use sourceview::Buffer;
@ -10,7 +9,7 @@ use sourceview::Buffer;
const MARGIN: i32 = 8; const MARGIN: i32 = 8;
pub trait Form { pub trait Form {
fn form(action_update: SimpleAction) -> Self; fn form() -> Self;
fn text(&self) -> GString; fn text(&self) -> GString;
} }
@ -18,7 +17,7 @@ impl Form for TextView {
// Constructors // Constructors
/// Build new `Self` /// Build new `Self`
fn form(action_update: SimpleAction) -> Self { fn form() -> Self {
// Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer // Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer
let buffer = Buffer::builder().build(); let buffer = Buffer::builder().build();
@ -44,10 +43,6 @@ impl Form for TextView {
text_view.set_size_request(-1, 38); // @TODO [#635](https://gitlab.gnome.org/GNOME/pygobject/-/issues/635) text_view.set_size_request(-1, 38); // @TODO [#635](https://gitlab.gnome.org/GNOME/pygobject/-/issues/635)
// Init events // Init events
text_view.buffer().connect_changed(move |_| {
action_update.activate(None);
});
text_view.connect_realize(|this| { text_view.connect_realize(|this| {
this.grab_focus(); this.grab_focus();
}); });