What code mistake can cause egui to not detect a click? - rust

I have created my first egui app. The update loop looks like
impl eframe::App for App {
fn update(&mut self, ctx: &Context, _frame: &mut Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.with_layout(Layout::top_down(Align::Center), |ui| {
ui.add(Label::new("bacon"));
let clicked = ui.add(Button::new("foo")).clicked();
if clicked {
println!("foo")
}
let clicked = ui.add(FurnaceGraph::default()).clicked();
if clicked {
println!("clicked");
}
})
});
}
and the ui for FurnaceGraph looks like
impl Widget for FurnaceGraph {
fn ui(mut self, ui: &mut Ui) -> Response {
let (_id, rect) = ui.allocate_space(Vec2::new(self.width as f32, self.height as f32));
let response = ui.allocate_response(rect.size(), Sense::click());
// println!("enabled? {}", ui.is_enabled());
let texture = self.get_texture(ui);
let texture_size = texture.size_vec2();
let img = Image::new(texture, texture_size);
img.paint_at(ui, Rect::from_min_max(rect.min, rect.min + img.size()));
response
}
}
I think I am asking to Sense::click()s when I allocate_response, but it never prints "clicked" when I click on the graph. I do get "foo" when I click on the foo button.
What do I have to tweak to not miss clicks?

I eventually discovered that the rect returned by allocate_space is different than the one returned by allocate_response. I was allocating one rectangle to draw in, and a different one to harvest clicks from. I got rid of the call to allocate_space because allocate_response calls it for me.

Related

Tui-rs: flickering when drawing multiple widgets

Good evening!
I'm trying to write a very simple terminal application that draws two textboxes on screen, accepting input on one and showing output on the other, using Rust and tui-rs. The first part works perfectly, but my problems arose when i tried to draw two blocks at the same time: for some reason, it only shows the second block (in order of drawing) and if i move my mouse, it flickers between the two in a weird way. My best guess is that this is due to my drawing implementation, which somehow "clears" the screen whenever it needs to draw something, but if that's the case, i couldn't find any doc on it, and i wouldn't know how to go about working around this. I've provided some code that should be enough to replicate the issue on a smaller scale.
#![allow(unused_imports)]
#![allow(unused_variables)]
use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use std::io
use tui::{
backend::CrosstermBackend,
layout::Rect,
widgets::{Block, Borders},
Terminal,
};
struct FirstStruct {}
impl FirstStruct {
pub fn draw(&self, term: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<()> {
term.draw(|f| {
let size = f.size();
let (w, h) = (size.width / 2, size.height);
let (x, y) = (size.x, size.y);
let rect = Rect::new(x, y, w, h);
let block = Block::default()
.title("One")
.borders(Borders::ALL);
f.render_widget(block, rect)
})?;
Ok(())
}
}
struct SecondStruct { }
impl SecondStruct {
pub fn draw(&self, term: &mut Terminal<CrosstermBackend<io::Stdout>>) -> io::Result<()> {
term.draw(|f| {
let size = f.size();
let (w, h) = (size.width / 2, size.height);
let (x, y) = (size.x + w, size.y);
let rect = Rect::new(x, y, w, h);
let block = Block::default()
.title("Two")
.borders(Borders::ALL);
f.render_widget(block, rect)
})?;
Ok(())
}
}
fn main() -> io::Result<()>{
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let first = FirstStruct {};
let second = SecondStruct {};
let mut running = true;
while running {
if let Event::Key(key) = event::read()? {
running = false;
}
second.draw(&mut terminal)?;
first.draw(&mut terminal)?;
}
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
Ok(())
}
Does anybody know how i can fix this issue? Thanks in advance!!
Every time you call Terminal::draw(), you must draw everything that you want to be visible at once. Instead of passing Terminal to your own draw functions, pass the Frame that you get from Terminal::draw(). That is, replace
second.draw(&mut terminal)?;
first.draw(&mut terminal)?;
with
terminal.draw(|f| {
first.draw(f)?;
second.draw(f)?;
});
and change the signature of FirstStruct and SecondStruct to match.
Also, it would be more usual to, instead of computing the rectangle for each widget in the individual functions, decide at the top level (using Layout, perhaps) and pass Rects down to the drawing functions. That way, they can be positioned differently in different situations. What you have will work, but it's not as easy to change.
Layout code from the documentation's example, adjusted to your situation:
terminal.draw(|f| {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage(50),
Constraint::Percentage(50),
].as_ref()
)
.split(f.size());
first.draw(f, chunks[0])?;
second.draw(f, chunks[1])?;
});

RUST + FLTK: accessing another widget from widget on_push() function

I used FLTK to create a window and two buttons inside, the btn_A has a callback and should change the btn_B label, but I dont see any non-monstrous approach do to this, ples halp? =''[
fn main() {
showMainWindow();
}
pub fn showMainWindow() {
//WINDOW
let application=app::App::default();
let mut win = window::Window::default().with_size(500,300);
//BTN_A
let mut btn_A:Listener<_> = button::Button::new(100,100,100,50,"btn_A").into();
//BTN_B
let mut btn_B:Listener<_> = button::Button::new(300,100,100,50,"btn_B").into();
//BTN_A_CALLBACK
btn_A.handle(|elem,evt| match evt {
enums::Event::Push => { btn_A(elem); true }
_ => { false }
});
win.end();
win.show();
application.run().unwrap();
}
pub fn btn_A(elem:&mut button::Button) {
elem.deactivate(); //deactivate itself
//but how do I access btn_B here?
}
In principle all that is needed is to pass a mutable reference to btn_B to your handler function:
pub fn btn_A(elem:&mut button::Button, btn_B: &mut button::Button) {
...
}
However there is one slight problem with your code: You named the function the same as the variable that holds your button.
Apart from that in the most recent version of the fltk crate (v.1.2.23, that I used because you did not specify which version you used in your question) there does not seem to be a Listener<_> type.
Here is an example based on the snippet you posted for changing the label of btn_B:
use fltk::{prelude::{WidgetExt, GroupExt, WidgetBase}, window, app, button, enums};
fn main() {
showMainWindow();
}
pub fn showMainWindow() {
//WINDOW
let application = app::App::default();
let mut win = window::Window::default().with_size(500, 300);
//BTN_A
let mut btn_A = button::Button::new(100, 100, 100, 50, "btn_A");
//BTN_B
let mut btn_B = button::Button::new(300, 100, 100, 50, "btn_B");
//BTN_A_CALLBACK
btn_A.handle(move |elem, evt| match evt {
enums::Event::Push => {
btn_A_click(elem, &mut btn_B);
true
}
_ => false,
});
win.end();
win.show();
application.run().unwrap();
}
pub fn btn_A_click(elem: &mut button::Button, btn_B: &mut button::Button) {
elem.deactivate(); //deactivate itself
//but how do I access btn_B here?
btn_B.set_label("New title.")
}
Also note, that the handle closure now takes ownership of btn_B because of the move keyword.

Reading the Position of a Mouse click in Bevy

I am trying to see where the mouse is pressed so I can select my character.
I have tried the following
#[derive(Default)]
struct State { // Set up from example
mouse_button_event_reader: EventReader<MouseButtonInput>,
cursor_moved_event_reader: EventReader<CursorMoved>,
}
fn select_character(
mut state: ResMut<State>,
mouse_button_input_events: Res<Events<MouseButtonInput>>,
cursor_moved_events: Res<Events<CursorMoved>>,
) {
for (cursor_event, mouse_event) in state
.cursor_moved_event_reader
.iter(&cursor_moved_events)
.zip(
state
.mouse_button_event_reader
.iter(&mouse_button_input_events),
)
{
println!("{:?}", cursor_event);
println!("{:?}", mouse_event);
}
}
This kind of works, but the mouse needs to be moving while it is clicked. Is there a way of getting the position once the mouse is pressed?
Edit:
I thought .find_latest might work, getting it to return the latest Some value.
for event in state
.mouse_button_event_reader
.iter(&mouse_button_input_events)
{
let cursor_event = state
.cursor_moved_event_reader
.find_latest(&cursor_moved_events, |x| x.is_Some() // Not sure how to only return values that are Some(x)
);
println!("{:?}", event);
println!("{:?}", cursor_event);
}
It appears that .find_latest is for finding the next unread value. Which means you would probably also require movement. Since, it is likely is a click was the last event, the movement was already captured.
Now, I can't promise that this is idiomatic since we're all new at this, but there is a solution:
Add two floating-point variables to your state-struct. Then, when the cursor is moved, store that position. When the mouse is clicked, recall that information. Since it becomes a part of the resource (state) that information will be available for read and write.
Implemented
#[derive(Default)]
struct State { // Set up from example
mouse_button_event_reader: EventReader<MouseButtonInput>,
cursor_moved_event_reader: EventReader<CursorMoved>,
}
struct MouseLoc(Vec2);
fn select_character(
mut state: ResMut<State>,
mouse_pos: ResMut<MouseLoc>,
mouse_button_input_events: Res<Events<MouseButtonInput>>,
) {
for event in state
.mouse_button_event_reader
.iter(&mouse_button_input_events)
{
println!("event: {:?} position: {:?}", event, mouse_pos.0);
}
}
fn mouse_movement_updating_system(
mut mouse_pos: ResMut<MouseLoc>,
mut state: ResMut<State>,
cursor_moved_events: Res<Events<CursorMoved>>,
) {
for event in state.cursor_moved_event_reader.iter(&cursor_moved_events) {
mouse_pos.0 = event.position;
}
}
fn main() {
App::build()
.add_default_plugins()
...
.add_resource(MouseLoc(Vec2::new(0.0, 0.0)))
.add_system(mouse_movement_updating_system.system());
.add_system(position_mouse_click_system.system());
...
.run();
}
I think what you want is something like this:
fn handle_mouse_clicks(mouse_input: Res<Input<MouseButton>>, windows: Res<Windows>) {
let win = windows.get_primary().expect("no primary window");
if mouse_input.just_pressed(MouseButton::Left) {
println!("click at {:?}", win.cursor_position());
}
}
This works for version 0.5.0 of bevy: https://docs.rs/bevy/0.5.0/bevy/window/struct.Window.html#method.cursor_position
You could implement your own object always tracking the current position (via an event reader) as well, but in my tests, the method described above gives a more (temporal) accurate result (you get the position at the time the click comes through not an arbitrarily old position) - but I am not an expert on this!

Gtk-rs application crashes at window.show_all() function

My Gtk-rs application crashes whenever I try calling the show_all() function.
In a simple application window, I have added a headerbar and a label. If I compile without adding the headerbar, the window works and shows the label as intended. However if I add the headerbar the window crashes.
use gio::prelude::*;
use gtk::{
prelude::*,
HeaderBarExt,
GtkWindowExt
};
fn gen_header_bar(subtitle: Option<String>) -> gtk::HeaderBar {
let header_bar = gtk::HeaderBar::new();
header_bar.set_title(Some(crate::consts::APP_NAME));
header_bar.set_show_close_button(true);
match subtitle {
Some(subtitle) => {
header_bar.set_subtitle(Some(&subtitle));
},
_ => {
}
}
header_bar
}
pub fn build_application_window() -> Result<(), Box<dyn std::error::Error>> {
let application = gtk::Application::new(
Some(crate::consts::APP_ID),
gio::ApplicationFlags::FLAGS_NONE,
)?;
application.connect_activate(move |app| {
let window = gtk::ApplicationWindow::new(app);
window.set_title(crate::consts::APP_NAME);
window.set_default_size(32 * 10, 200); // golden ratio
window.set_position(gtk::WindowPosition::Center);
let header_bar = gen_header_bar(None);
window.set_titlebar(Some(&header_bar));
window.add(&{
let label = gtk::Label::new(Some("Welcome!"));
label
});
window.show_all(); // crashes here
});
application.run(&std::env::args().collect::<Vec<_>>());
Ok(())
}
What is causing this?

Split/gather pattern for jobs

I have a set of jobs that I am trying to run in parallel. I want to run each task on its own thread and gather the responses on the calling thread.
Some jobs may take much longer than others, so I'd like to start using each result as it comes in, and not have to wait for all jobs to complete.
Here is an attempt:
struct Container<T> {
items : Vec<T>
}
#[derive(Debug)]
struct Item {
x: i32
}
impl Item {
fn foo (&mut self) {
self.x += 1; //consider an expensive mutating computation
}
}
fn main() {
use std;
use std::sync::{Mutex, Arc};
use std::collections::RingBuf;
//set up a container with 2 items
let mut item1 = Item { x: 0};
let mut item2 = Item { x: 1};
let container = Container { items: vec![item1, item2]};
//set a gather system for our results
let ringBuf = Arc::new(Mutex::new(RingBuf::<Item>::new()));
//farm out each job to its own thread...
for item in container.items {
std::thread::Thread::spawn(|| {
item.foo(); //job
ringBuf.lock().unwrap().push_back(item); //push item back to caller
});
}
loop {
let rb = ringBuf.lock().unwrap();
if rb.len() > 0 { //gather results as soon as they are available
println!("{:?}",rb[0]);
rb.pop_front();
}
}
}
For starters, this does not compile due to the impenetrable cannot infer an appropriate lifetime due to conflicting requirements error.
What am I doing wrong and how do I do it right?
You've got a couple compounding issues, but the first one is a misuse / misunderstanding of Arc. You need to give each thread it's own copy of the Arc. Arc itself will make sure that changes are synchronized. The main changes were the addition of .clone() and the move keyword:
for item in container.items {
let mrb = ringBuf.clone();
std::thread::Thread::spawn(move || {
item.foo(); //job
mrb.lock().unwrap().push_back(item); //push item back to caller
});
}
After changing this, you'll run into some simpler errors about forgotten mut qualifiers, and then you hit another problem - you are trying to send mutable references across threads. Your for loop will need to return &mut Item to call foo, but this doesn't match your Vec. Changing it, we can get to something that compiles:
for mut item in container.items.into_iter() {
let mrb = ringBuf.clone();
std::thread::Thread::spawn(move || {
item.foo(); //job
mrb.lock().unwrap().push_back(item); //push item back to caller
});
}
Here, we consume the input vector, moving each of the Items to the worker thread. Unfortunately, this hits the Playpen timeout, so there's probably some deeper issue.
All that being said, I'd highly recommend using channels:
#![feature(std_misc)]
use std::sync::mpsc::channel;
#[derive(Debug)]
struct Item {
x: i32
}
impl Item {
fn foo(&mut self) { self.x += 1; }
}
fn main() {
let items = vec![Item { x: 0 }, Item { x: 1 }];
let rx = {
let (tx, rx) = channel();
for item in items.into_iter() {
let my_tx = tx.clone();
std::thread::Thread::spawn(move || {
let mut item = item;
item.foo();
my_tx.send(item).unwrap();
});
}
rx
};
for item in rx.iter() {
println!("{:?}", item);
}
}
This also times-out in the playpen, but works fine when compiled and run locally.

Resources