Skip to content

Commit

Permalink
ContextMenu respects window borders and resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 25, 2023
1 parent 132a84d commit 34f32a7
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/native/overlay/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use iced_widget::core::{
mouse::{self, Cursor},
overlay, renderer, touch,
widget::tree::Tree,
Clipboard, Color, Element, Event, Layout, Point, Rectangle, Shell, Size,
window, Clipboard, Color, Element, Event, Layout, Point, Rectangle, Shell, Size,
};

/// The overlay of the [`ContextMenu`](crate::native::ContextMenu).
Expand Down Expand Up @@ -75,6 +75,16 @@ where
let max_size = limits.max();

let mut content = self.content.as_widget().layout(renderer, &limits);

// Try to stay inside borders
let mut position = position;
if position.x + content.size().width > bounds.width {
position.x = f32::max(0.0, position.x - content.size().width);
}
if position.y + content.size().height > bounds.height {
position.y = f32::max(0.0, position.y - content.size().height);
}

content.move_to(position);

Node::with_children(max_size, vec![content])
Expand Down Expand Up @@ -166,6 +176,11 @@ where
}
}

Event::Window(window::Event::Resized { .. }) => {
self.state.show = false;
Status::Captured
}

_ => Status::Ignored,
};

Expand Down

0 comments on commit 34f32a7

Please sign in to comment.