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

Window: move only by dragging title bar (#8183)

* Part of https://github.com/emilk/egui/issues/8180

So far, you've been able to move any `egui::Window` by dragging anywhere
on it. This makes sense on touch screens with thick fingers, but less so
on non-touch-screens.

With this PR, you can now control it with a new enum `WindowDrag`
This commit is contained in:
Emil Ernerfeldt
2026-05-22 12:39:43 +02:00
committed by GitHub
parent 27373b06d0
commit bea47a2ce7
7 changed files with 390 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
use egui::{UiKind, Vec2b};
use egui::{UiKind, Vec2b, WindowDrag};
#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
@@ -7,6 +7,7 @@ pub struct WindowOptions {
title_bar: bool,
closable: bool,
collapsible: bool,
movable: bool,
resizable: bool,
constrain: bool,
scroll2: Vec2b,
@@ -15,6 +16,8 @@ pub struct WindowOptions {
anchored: bool,
anchor: egui::Align2,
anchor_offset: egui::Vec2,
drag_area: WindowDrag,
}
impl Default for WindowOptions {
@@ -24,6 +27,7 @@ impl Default for WindowOptions {
title_bar: true,
closable: true,
collapsible: true,
movable: true,
resizable: true,
constrain: true,
scroll2: Vec2b::TRUE,
@@ -31,6 +35,7 @@ impl Default for WindowOptions {
anchored: false,
anchor: egui::Align2::RIGHT_TOP,
anchor_offset: egui::Vec2::ZERO,
drag_area: WindowDrag::default(),
}
}
}
@@ -46,6 +51,7 @@ impl crate::Demo for WindowOptions {
title_bar,
closable,
collapsible,
movable,
resizable,
constrain,
scroll2,
@@ -53,6 +59,7 @@ impl crate::Demo for WindowOptions {
anchored,
anchor,
anchor_offset,
drag_area,
} = self.clone();
let enabled = ui.input(|i| i.time) - disabled_time > 2.0;
@@ -66,7 +73,9 @@ impl crate::Demo for WindowOptions {
.resizable(resizable)
.constrain(constrain)
.collapsible(collapsible)
.movable(movable)
.title_bar(title_bar)
.drag_area(drag_area)
.scroll(scroll2)
.constrain_to(ui.available_rect_before_wrap())
.enabled(enabled);
@@ -87,6 +96,7 @@ impl crate::View for WindowOptions {
title_bar,
closable,
collapsible,
movable,
resizable,
constrain,
scroll2,
@@ -94,6 +104,7 @@ impl crate::View for WindowOptions {
anchored,
anchor,
anchor_offset,
drag_area,
} = self;
ui.horizontal(|ui| {
ui.label("title:");
@@ -106,6 +117,8 @@ impl crate::View for WindowOptions {
ui.checkbox(title_bar, "title_bar");
ui.checkbox(closable, "closable");
ui.checkbox(collapsible, "collapsible");
ui.checkbox(movable, "movable")
.on_hover_text("Can the window be moved by dragging?");
ui.checkbox(resizable, "resizable");
ui.checkbox(constrain, "constrain")
.on_hover_text("Constrain window to the screen");
@@ -140,6 +153,19 @@ impl crate::View for WindowOptions {
});
});
ui.horizontal(|ui| {
ui.label("Drag to move:")
.on_hover_text("Where the user can grab the window to move it");
ui.selectable_value(drag_area, WindowDrag::Off, "Off")
.on_hover_text("The window cannot be dragged to move it (same as movable = false)");
ui.selectable_value(drag_area, WindowDrag::OnTouch, "OnTouch")
.on_hover_text("Anywhere on touch screens, title-bar only otherwise (default)");
ui.selectable_value(drag_area, WindowDrag::TitleBar, "TitleBar")
.on_hover_text("Only the title bar moves the window");
ui.selectable_value(drag_area, WindowDrag::Anywhere, "Anywhere")
.on_hover_text("Drag anywhere on the window to move it");
});
ui.separator();
let on_top = Some(ui.layer_id()) == ui.ctx().top_layer_id();
ui.label(format!("This window is on top: {on_top}."));

View File

@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ab0a8201038b2b066c5aff1fd1a35bc7aed95e74cf50c293eee4a76d66623822
size 35171
oid sha256:d27392f625445e82285c8eaeb532e11dcb02030f24c7a769356cb4c23d7c2067
size 41516