mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 06:10:06 -04:00
Fixed color edit popup going outside the screen (#2270)
* Fixed color edit popup going outside the screen * Added option to constrain areas * Constrain color picker area * Constrain popups * Updated changelog
This commit is contained in:
@@ -46,6 +46,7 @@ pub struct Area {
|
||||
movable: bool,
|
||||
interactable: bool,
|
||||
enabled: bool,
|
||||
constrain: bool,
|
||||
order: Order,
|
||||
default_pos: Option<Pos2>,
|
||||
anchor: Option<(Align2, Vec2)>,
|
||||
@@ -59,6 +60,7 @@ impl Area {
|
||||
id: id.into(),
|
||||
movable: true,
|
||||
interactable: true,
|
||||
constrain: false,
|
||||
enabled: true,
|
||||
order: Order::Middle,
|
||||
default_pos: None,
|
||||
@@ -120,6 +122,12 @@ impl Area {
|
||||
self
|
||||
}
|
||||
|
||||
/// Constrains this area to the screen bounds.
|
||||
pub fn constrain(mut self, constrain: bool) -> Self {
|
||||
self.constrain = constrain;
|
||||
self
|
||||
}
|
||||
|
||||
/// Positions the window and prevents it from being moved
|
||||
pub fn fixed_pos(mut self, fixed_pos: impl Into<Pos2>) -> Self {
|
||||
self.new_pos = Some(fixed_pos.into());
|
||||
@@ -202,6 +210,7 @@ impl Area {
|
||||
new_pos,
|
||||
anchor,
|
||||
drag_bounds,
|
||||
constrain,
|
||||
} = self;
|
||||
|
||||
let layer_id = LayerId::new(order, id);
|
||||
@@ -274,6 +283,12 @@ impl Area {
|
||||
|
||||
state.pos = ctx.round_pos_to_pixels(state.pos);
|
||||
|
||||
if constrain {
|
||||
state.pos = ctx
|
||||
.constrain_window_rect_to_area(state.rect(), drag_bounds)
|
||||
.min;
|
||||
}
|
||||
|
||||
Prepared {
|
||||
layer_id,
|
||||
state,
|
||||
|
||||
@@ -325,6 +325,7 @@ pub fn popup_below_widget<R>(
|
||||
if ui.memory().is_popup_open(popup_id) {
|
||||
let inner = Area::new(popup_id)
|
||||
.order(Order::Foreground)
|
||||
.constrain(true)
|
||||
.fixed_pos(widget_response.rect.left_bottom())
|
||||
.show(ui.ctx(), |ui| {
|
||||
// Note: we use a separate clip-rect for this area, so the popup can be outside the parent.
|
||||
|
||||
@@ -350,13 +350,17 @@ pub fn color_edit_button_hsva(ui: &mut Ui, hsva: &mut Hsva, alpha: Alpha) -> Res
|
||||
if button_response.clicked() {
|
||||
ui.memory().toggle_popup(popup_id);
|
||||
}
|
||||
|
||||
const COLOR_SLIDER_WIDTH: f32 = 210.0;
|
||||
|
||||
// TODO(emilk): make it easier to show a temporary popup that closes when you click outside it
|
||||
if ui.memory().is_popup_open(popup_id) {
|
||||
let area_response = Area::new(popup_id)
|
||||
.order(Order::Foreground)
|
||||
.fixed_pos(button_response.rect.max)
|
||||
.constrain(true)
|
||||
.show(ui.ctx(), |ui| {
|
||||
ui.spacing_mut().slider_width = 210.0;
|
||||
ui.spacing_mut().slider_width = COLOR_SLIDER_WIDTH;
|
||||
Frame::popup(ui.style()).show(ui, |ui| {
|
||||
if color_picker_hsva_2d(ui, hsva, alpha) {
|
||||
button_response.mark_changed();
|
||||
|
||||
Reference in New Issue
Block a user