1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

Fix mouse input

Now on Context.create_viewport in the render function will we have viewport_id and parent_viewport_id
New problem if a windows is fucused and we interact with other window the first event will be send to the last window that was focused
This commit is contained in:
Konkitoman
2023-07-24 14:24:30 +03:00
parent 3a1d9f2e21
commit 4d883b8217
36 changed files with 261 additions and 173 deletions

View File

@@ -1,5 +1,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use std::sync::{Arc, RwLock};
use eframe::egui;
fn main() -> Result<(), eframe::Error> {
@@ -16,15 +18,20 @@ fn main() -> Result<(), eframe::Error> {
}
#[derive(Default)]
struct MyApp {
struct MyAppData {
allowed_to_close: bool,
show_confirmation_dialog: bool,
}
#[derive(Default)]
struct MyApp {
data: Arc<RwLock<MyAppData>>,
}
impl eframe::App for MyApp {
fn on_close_event(&mut self) -> bool {
self.show_confirmation_dialog = true;
self.allowed_to_close
self.data.write().unwrap().show_confirmation_dialog = true;
self.data.read().unwrap().allowed_to_close
}
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
@@ -32,23 +39,27 @@ impl eframe::App for MyApp {
ui.heading("Try to close the window");
});
if self.show_confirmation_dialog {
let show_confirmation_dialog = self.data.read().unwrap().show_confirmation_dialog;
if show_confirmation_dialog {
let data = self.data.clone();
// Show confirmation dialog:
egui::Window::new("Do you want to quit?")
.collapsible(false)
.resizable(false)
.show(ctx, |ui| {
.show(ctx, move |ui, _, _| {
ui.horizontal(|ui| {
if ui.button("Cancel").clicked() {
self.show_confirmation_dialog = false;
data.write().unwrap().show_confirmation_dialog = false;
}
if ui.button("Yes!").clicked() {
self.allowed_to_close = true;
frame.close();
data.write().unwrap().allowed_to_close = true;
}
});
});
if self.data.read().unwrap().allowed_to_close {
frame.close()
}
}
}
}

View File

@@ -12,6 +12,7 @@ publish = false
eframe = { path = "../../crates/eframe", default-features = false, features = [
# accesskit struggles with threading
"default_fonts",
"wgpu",
"glow",
# "wgpu",
] }
env_logger = "0.10"

View File

@@ -2,7 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use std::sync::mpsc;
use std::sync::{mpsc, Arc, RwLock};
use std::thread::JoinHandle;
use eframe::egui;
@@ -20,39 +20,50 @@ fn main() -> Result<(), eframe::Error> {
)
}
/// State per thread.
struct ThreadState {
struct ThreadStateData {
thread_nr: usize,
title: String,
name: String,
age: u32,
}
/// State per thread.
#[derive(Clone)]
struct ThreadState {
data: Arc<RwLock<ThreadStateData>>,
}
impl ThreadState {
fn new(thread_nr: usize) -> Self {
let title = format!("Background thread {thread_nr}");
Self {
thread_nr,
title,
name: "Arthur".into(),
age: 12 + thread_nr as u32 * 10,
data: Arc::new(RwLock::new(ThreadStateData {
thread_nr,
title,
name: "Arthur".into(),
age: 12 + thread_nr as u32 * 10,
})),
}
}
fn show(&mut self, ctx: &egui::Context) {
let pos = egui::pos2(16.0, 128.0 * (self.thread_nr as f32 + 1.0));
egui::Window::new(&self.title)
let thread_nr = self.data.read().unwrap().thread_nr;
let pos = egui::pos2(16.0, 128.0 * (thread_nr as f32 + 1.0));
let clone = self.clone();
let title = self.data.read().unwrap().title.clone();
egui::Window::new(title)
.default_pos(pos)
.show(ctx, |ui| {
.show(ctx, move |ui, _, _| {
let data = &mut *clone.data.write().unwrap();
ui.horizontal(|ui| {
ui.label("Your name: ");
ui.text_edit_singleline(&mut self.name);
ui.text_edit_singleline(&mut data.name);
});
ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
ui.add(egui::Slider::new(&mut data.age, 0..=120).text("age"));
if ui.button("Click each year").clicked() {
self.age += 1;
data.age += 1;
}
ui.label(format!("Hello '{}', age {}", self.name, self.age));
ui.label(format!("Hello '{}', age {}", data.name, data.age));
});
}
}
@@ -74,10 +85,13 @@ fn new_worker(
.expect("failed to spawn thread");
(handle, show_tx)
}
struct MyApp {
struct MyAppData {
threads: Vec<(JoinHandle<()>, mpsc::SyncSender<egui::Context>)>,
on_done_tx: mpsc::SyncSender<()>,
}
struct MyApp {
data: Arc<RwLock<MyAppData>>,
on_done_rc: mpsc::Receiver<()>,
}
@@ -87,17 +101,24 @@ impl MyApp {
let (on_done_tx, on_done_rc) = mpsc::sync_channel(0);
let mut slf = Self {
threads,
on_done_tx,
data: Arc::new(RwLock::new(MyAppData {
threads,
on_done_tx,
})),
on_done_rc,
};
slf.spawn_thread();
slf.spawn_thread();
{
let mut data = slf.data.write().unwrap();
data.spawn_thread();
data.spawn_thread();
}
slf
}
}
impl MyAppData {
fn spawn_thread(&mut self) {
let thread_nr = self.threads.len();
self.threads
@@ -107,7 +128,7 @@ impl MyApp {
impl std::ops::Drop for MyApp {
fn drop(&mut self) {
for (handle, show_tx) in self.threads.drain(..) {
for (handle, show_tx) in self.data.write().unwrap().threads.drain(..) {
std::mem::drop(show_tx);
handle.join().unwrap();
}
@@ -116,17 +137,25 @@ impl std::ops::Drop for MyApp {
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::Window::new("Main thread").show(ctx, |ui| {
let data = self.data.clone();
egui::Window::new("Main thread").show(ctx, move |ui, _, parent_id| {
if ui.button("Spawn another thread").clicked() {
self.spawn_thread();
data.write().unwrap().spawn_thread();
ui.ctx().request_repaint_viewport(parent_id);
}
});
for (_handle, show_tx) in &self.threads {
let _ = show_tx.send(ctx.clone());
let threads_len;
{
let data = self.data.read().unwrap();
threads_len = data.threads.len();
for (_handle, show_tx) in &data.threads {
let _ = show_tx.send(ctx.clone());
}
}
for _ in 0..self.threads.len() {
for _ in 0..threads_len {
let _ = self.on_done_rc.recv();
}
}

View File

@@ -22,8 +22,7 @@ fn main() -> Result<(), eframe::Error> {
eframe::run_simple_native("My egui App", options, move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.label(format!(
"Current window: {}, Current rendering window: {}",
ctx.current_viewport(),
"Current rendering window: {}",
ctx.current_rendering_viewport()
));
ui.heading("My egui Application");
@@ -42,12 +41,11 @@ fn main() -> Result<(), eframe::Error> {
egui::CollapsingHeader::new("Show Test1").show(ui, |ui| {
egui::Window::new("Test1")
.embedded(embedded)
.show(ctx, move |ui| {
.show(ctx, move |ui, _, _| {
ui.checkbox(&mut *clone.write().unwrap(), "Should embedd?");
let ctx = ui.ctx().clone();
ui.label(format!(
"Current window: {}, Current rendering window: {}",
ctx.current_viewport(),
"Current rendering window: {}",
ctx.current_rendering_viewport()
));
});
@@ -57,12 +55,11 @@ fn main() -> Result<(), eframe::Error> {
egui::CollapsingHeader::new("Shout Test2").show(ui, |ui| {
egui::Window::new("Test2")
.embedded(embedded)
.show(ctx, move |ui| {
.show(ctx, move |ui, _, _| {
ui.checkbox(&mut *clone.write().unwrap(), "Should embedd?");
let ctx = ui.ctx().clone();
ui.label(format!(
"Current window: {}, Current rendering window: {}",
ctx.current_viewport(),
"Current rendering window: {}",
ctx.current_rendering_viewport()
));
});

View File

@@ -128,9 +128,6 @@ impl eframe::App for Application {
});
});
ctx.request_repaint_after(
Self::repaint_max_timeout(),
ctx.current_rendering_viewport(),
);
ctx.request_repaint_after(Self::repaint_max_timeout());
}
}