Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixed color edit popup going outside the screen #2270

Merged
merged 5 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG
* Added `egui::gui_zoom` module with helpers for scaling the whole GUI of an app ([#2239](https://github.com/emilk/egui/pull/2239)).
* You can now put one interactive widget on top of another, and only one will get interaction at a time ([#2244](https://github.com/emilk/egui/pull/2244)).
* Add `ui.centered`.
* Added `Area::constrain` which constrains area to the screen bounds. ([#2270](https://github.com/emilk/egui/pull/2270)).

### Changed 🔧
* Panels always have a separator line, but no stroke on other sides. Their spacing has also changed slightly ([#2261](https://github.com/emilk/egui/pull/2261)).
Expand All @@ -26,6 +27,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG
* ⚠️ BREAKING: Fix text being too small ([#2069](https://github.com/emilk/egui/pull/2069)).
* Improved text rendering ([#2071](https://github.com/emilk/egui/pull/2071)).
* Less jitter when calling `Context::set_pixels_per_point` ([#2239](https://github.com/emilk/egui/pull/2239)).
* Fixed popups and color edit going outside the screen.


## 0.19.0 - 2022-08-20
Expand Down
15 changes: 15 additions & 0 deletions crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct Area {
movable: bool,
interactable: bool,
enabled: bool,
constrain: bool,
order: Order,
default_pos: Option<Pos2>,
anchor: Option<(Align2, Vec2)>,
Expand All @@ -61,6 +62,7 @@ impl Area {
id: Id::new(id_source),
movable: true,
interactable: true,
constrain: false,
enabled: true,
order: Order::Middle,
default_pos: None,
Expand Down Expand Up @@ -122,6 +124,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());
Expand Down Expand Up @@ -204,6 +212,7 @@ impl Area {
new_pos,
anchor,
drag_bounds,
constrain,
} = self;

let layer_id = LayerId::new(order, id);
Expand Down Expand Up @@ -276,6 +285,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,
Expand Down
1 change: 1 addition & 0 deletions crates/egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,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.
Expand Down
6 changes: 5 additions & 1 deletion crates/egui/src/widgets/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down