Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

xdg: Add get_anchor_point method to PositionerState #1630

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
42 changes: 27 additions & 15 deletions src/wayland/shell/xdg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,32 @@ impl PositionerState {
}
}

/// Get the anchor point for a popup as defined by this positioner.
///
/// Defined by `xdg_positioner.set_anchor_rect` and
/// `xdg_positioner.set_anchor`.
pub fn get_anchor_point(&self) -> Point<i32, Logical> {
let mut point = self.anchor_rect.loc;

point.y += if self.anchor_has_edge(xdg_positioner::Anchor::Top) {
0
} else if self.anchor_has_edge(xdg_positioner::Anchor::Bottom) {
self.anchor_rect.size.h
} else {
self.anchor_rect.size.h / 2
};

point.x += if self.anchor_has_edge(xdg_positioner::Anchor::Left) {
0
} else if self.anchor_has_edge(xdg_positioner::Anchor::Right) {
self.anchor_rect.size.w
} else {
self.anchor_rect.size.w / 2
};

point
}

pub(crate) fn gravity_has_edge(&self, edge: xdg_positioner::Gravity) -> bool {
match edge {
xdg_positioner::Gravity::Top => {
Expand Down Expand Up @@ -627,21 +653,7 @@ impl PositionerState {
// 'bottom_right'), the anchor point will be at the specified corner;
// otherwise, the derived anchor point will be centered on the specified
// edge, or in the center of the anchor rectangle if no edge is specified.
if self.anchor_has_edge(xdg_positioner::Anchor::Top) {
geometry.loc.y += self.anchor_rect.loc.y;
} else if self.anchor_has_edge(xdg_positioner::Anchor::Bottom) {
geometry.loc.y += self.anchor_rect.loc.y + self.anchor_rect.size.h;
} else {
geometry.loc.y += self.anchor_rect.loc.y + self.anchor_rect.size.h / 2;
}

if self.anchor_has_edge(xdg_positioner::Anchor::Left) {
geometry.loc.x += self.anchor_rect.loc.x;
} else if self.anchor_has_edge(xdg_positioner::Anchor::Right) {
geometry.loc.x += self.anchor_rect.loc.x + self.anchor_rect.size.w;
} else {
geometry.loc.x += self.anchor_rect.loc.x + self.anchor_rect.size.w / 2;
}
geometry.loc += self.get_anchor_point();

// Defines in what direction a surface should be positioned, relative to
// the anchor point of the parent surface. If a corner gravity is
Expand Down
Loading