Skip to content

Commit

Permalink
Merge pull request #186 from sdgoihew/context-menu-tuning
Browse files Browse the repository at this point in the history
ContextMenu respects window borders and resizing
  • Loading branch information
Andrew Wheeler(Genusis) authored Sep 26, 2023
2 parents 132a84d + 34f32a7 commit fe20800
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 fe20800

Please sign in to comment.