So I have this simple egui app written in Rust and I want to drag my window around the screen when I left click my mouse over the window and drag.
This is my window:
fn main() {
let nativeoption = eframe::NativeOptions {
always_on_top: false,
maximized: false,
decorated: true,
fullscreen: false,
drag_and_drop_support: true,
initial_window_size: Option::from(egui::Vec2::new(300f32, 300f32)),
min_window_size: None,
max_window_size: None,
resizable: true,
..Default::default()
};
eframe::run_native(
"my egui app",
nativeoption,
Box::new(|cc| Box::new(MyEguiApp::new(cc))),
)
.expect("TODO: panic message");
}
#[derive(Default)]
struct MyEguiApp {
name: String,
}
impl MyEguiApp {
fn new(cc: &eframe::CreationContext<'_>) -> Self {
MyEguiApp {
name: String::new(),
};
Self::default()
}
}
impl eframe::App for MyEguiApp {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("hello welcome to egui");
ui.separator();
ui.horizontal(|ui| {
ui.label("Enter your name");
let res = ui.text_edit_singleline(&mut self.name);
});
ui.label(format!("my name is {}", &self.name));
});
}
}
I want to be able to drag this anywhere using my mouse/cursor.
You can have portions of your UI drag the window around by adding a drag
sense and calling frame.drag_window()
. Here's an example with your heading:
// use more verbose definition so we can add `.sense()`
let res = Label::new(RichText::new("hello welcome to egui").heading())
.sense(Sense::drag())
.ui(ui);
// if heading is dragged, drag the whole window
if res.dragged() {
frame.drag_window();
}
If you do want the whole window to be drag-able, try this:
if ui.interact(ui.max_rect(), Id::new("window-drag"), Sense::drag()).dragged() {
frame.drag_window();
}