From 12020f685e09d627fb745e53f3473600898a973d Mon Sep 17 00:00:00 2001 From: Kirottu Date: Sat, 27 May 2023 22:12:41 +0300 Subject: [PATCH] Only create widgets once if configure event is received --- anyrun/src/main.rs | 112 ++++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 51 deletions(-) diff --git a/anyrun/src/main.rs b/anyrun/src/main.rs index f43d0f6..fa8ce5d 100644 --- a/anyrun/src/main.rs +++ b/anyrun/src/main.rs @@ -5,6 +5,7 @@ use std::{ mem, path::PathBuf, rc::Rc, + sync::Once, time::Duration, }; @@ -555,63 +556,72 @@ fn activate(app: >k::Application, runtime_data: Rc>) { }); } + // Only create the widgets once to avoid issues + let configure_once = Once::new(); + // Create widgets here for proper positioning window.connect_configure_event(move |window, event| { - let width = match runtime_data.borrow().config.width { - RelativeNum::Absolute(width) => width, - RelativeNum::Fraction(fraction) => (event.size().0 as f32 * fraction) as i32, - }; - // The GtkFixed widget is used for absolute positioning of the main box - let fixed = gtk::Fixed::builder().build(); - let main_vbox = gtk::Box::builder() - .orientation(gtk::Orientation::Vertical) - .halign(gtk::Align::Center) - .vexpand(false) - .width_request(width) - .name(style_names::MAIN) - .build(); - main_vbox.add(&entry); + let runtime_data = runtime_data.clone(); + let entry = entry.clone(); + let main_list = main_list.clone(); - // Display the error message - if !runtime_data.borrow().error_label.is_empty() { - main_vbox.add( - >k::Label::builder() - .label(&format!( - r#"{}"#, - runtime_data.borrow().error_label - )) - .use_markup(true) - .build(), + configure_once.call_once(move || { + let width = match runtime_data.borrow().config.width { + RelativeNum::Absolute(width) => width, + RelativeNum::Fraction(fraction) => (event.size().0 as f32 * fraction) as i32, + }; + // The GtkFixed widget is used for absolute positioning of the main box + let fixed = gtk::Fixed::builder().build(); + let main_vbox = gtk::Box::builder() + .orientation(gtk::Orientation::Vertical) + .halign(gtk::Align::Center) + .vexpand(false) + .width_request(width) + .name(style_names::MAIN) + .build(); + main_vbox.add(&entry); + + // Display the error message + if !runtime_data.borrow().error_label.is_empty() { + main_vbox.add( + >k::Label::builder() + .label(&format!( + r#"{}"#, + runtime_data.borrow().error_label + )) + .use_markup(true) + .build(), + ); + } + + let vertical_offset = match runtime_data.borrow().config.vertical_offset { + RelativeNum::Absolute(offset) => offset, + RelativeNum::Fraction(fraction) => (event.size().1 as f32 * fraction) as i32, + }; + + fixed.put( + &main_vbox, + (event.size().0 as i32 - width) / 2, + match runtime_data.borrow().config.position { + Position::Top => vertical_offset, + Position::Center => { + (event.size().1 as i32 - entry.allocated_height()) / 2 + vertical_offset + } + }, ); - } + window.add(&fixed); + window.show_all(); - let vertical_offset = match runtime_data.borrow().config.vertical_offset { - RelativeNum::Absolute(offset) => offset, - RelativeNum::Fraction(fraction) => (event.size().1 as f32 * fraction) as i32, - }; + // Add and show the list later, to avoid showing empty plugin categories on launch + main_vbox.add(&main_list); + main_list.show(); + entry.grab_focus(); // Grab the focus so typing is immediately accepted by the entry box - fixed.put( - &main_vbox, - (event.size().0 as i32 - width) / 2, - match runtime_data.borrow().config.position { - Position::Top => vertical_offset, - Position::Center => { - (event.size().1 as i32 - entry.allocated_height()) / 2 + vertical_offset - } - }, - ); - window.add(&fixed); - window.show_all(); - - // Add and show the list later, to avoid showing empty plugin categories on launch - main_vbox.add(&main_list); - main_list.show(); - entry.grab_focus(); // Grab the focus so typing is immediately accepted by the entry box - - if runtime_data.borrow().config.show_results_immediately { - // Get initial matches - refresh_matches(String::new(), runtime_data.clone()); - } + if runtime_data.borrow().config.show_results_immediately { + // Get initial matches + refresh_matches(String::new(), runtime_data); + } + }); false });