mirror of
https://github.com/noctalia-dev/noctalia-shell.git
synced 2026-05-11 17:08:27 +08:00
Tray: fix traymenu positioning
This commit is contained in:
@@ -47,6 +47,16 @@ PopupWindow {
|
||||
|
||||
// Use the content height of the Flickable for implicit height
|
||||
implicitHeight: Math.min(screen?.height * 0.9, flickable.contentHeight + (Style.marginS * 2))
|
||||
|
||||
// When implicitHeight changes (menu content loads), force anchor recalculation
|
||||
onImplicitHeightChanged: {
|
||||
if (visible && anchorItem) {
|
||||
Qt.callLater(() => {
|
||||
anchor.updateAnchor();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
visible: false
|
||||
color: "transparent"
|
||||
anchor.item: anchorItem
|
||||
@@ -63,21 +73,25 @@ PopupWindow {
|
||||
menuScreenX = windowXOnScreen + posInPopup.x + baseX;
|
||||
} else {
|
||||
const anchorGlobalPos = anchorItem.mapToItem(null, 0, 0);
|
||||
menuScreenX = anchorGlobalPos.x + baseX;
|
||||
const anchorScreenX = anchorGlobalPos.x;
|
||||
menuScreenX = anchorScreenX + baseX;
|
||||
}
|
||||
|
||||
const menuRight = menuScreenX + implicitWidth;
|
||||
const screenRight = screen.width;
|
||||
const menuLeft = menuScreenX;
|
||||
|
||||
// Adjust if menu would clip on the right
|
||||
if (menuRight > screenRight) {
|
||||
// Only adjust if menu would clip off screen boundaries
|
||||
// Don't adjust if the positioning is intentional (e.g., negative offset for right bar)
|
||||
if (menuRight > screenRight && menuLeft < screenRight) {
|
||||
// Clipping on right edge - shift left
|
||||
const overflow = menuRight - screenRight;
|
||||
return baseX - overflow - Style.marginM;
|
||||
} else if (menuLeft < 0 && menuRight > 0) {
|
||||
// Clipping on left edge - shift right
|
||||
return baseX - menuLeft + Style.marginM;
|
||||
}
|
||||
// Adjust if menu would clip on the left
|
||||
if (menuScreenX < 0) {
|
||||
return baseX - menuScreenX + Style.marginM;
|
||||
}
|
||||
|
||||
return baseX;
|
||||
}
|
||||
return anchorX;
|
||||
@@ -86,52 +100,56 @@ PopupWindow {
|
||||
if (anchorItem && screen) {
|
||||
const barPosition = Settings.getBarPositionForScreen(root.screen?.name);
|
||||
|
||||
// Calculate base Y offset (relative to anchor item)
|
||||
let baseY = anchorY;
|
||||
if (!isSubMenu && barPosition === "bottom") {
|
||||
// For bottom bar, position menu above the anchor with margin (adjusted to match widget menu positioning)
|
||||
|
||||
// Only apply bottom bar special positioning if:
|
||||
// 1. Not a submenu
|
||||
// 2. Bar is at bottom
|
||||
// 3. anchorY is not already negative (if negative, it's pre-calculated from drawer)
|
||||
const shouldApplyBottomBarLogic = !isSubMenu && barPosition === "bottom" && anchorY >= 0;
|
||||
|
||||
if (shouldApplyBottomBarLogic) {
|
||||
// For bottom bar from the bar itself, position menu above the anchor with margin
|
||||
baseY = -(implicitHeight + Style.marginL + 2);
|
||||
}
|
||||
|
||||
// Calculate position relative to current screen (not global coordinates)
|
||||
let menuScreenY;
|
||||
if (isSubMenu && anchorItem.Window && anchorItem.Window.window) {
|
||||
// Submenu: anchor is inside parent PopupWindow
|
||||
const posInPopup = anchorItem.mapToItem(null, 0, 0);
|
||||
const parentWindow = anchorItem.Window.window;
|
||||
// Convert global window Y to screen-relative Y by subtracting screen offset
|
||||
const windowYOnScreen = parentWindow.y - screen.y;
|
||||
menuScreenY = windowYOnScreen + posInPopup.y + baseY;
|
||||
} else if (!isSubMenu && barPosition === "bottom") {
|
||||
// Bottom bar main menu: subtract baseY to position above anchor
|
||||
const anchorGlobalPos = anchorItem.mapToItem(null, 0, 0);
|
||||
menuScreenY = anchorGlobalPos.y - baseY;
|
||||
} else {
|
||||
// Main menu for other positions: add baseY
|
||||
const anchorGlobalPos = anchorItem.mapToItem(null, 0, 0);
|
||||
menuScreenY = anchorGlobalPos.y + baseY;
|
||||
// Use a robust way to get screen coordinates
|
||||
const posInWindow = anchorItem.mapToItem(null, 0, 0);
|
||||
const parentWindow = anchorItem.Window.window;
|
||||
|
||||
// Calculate screen-relative Y of the window
|
||||
let windowYOnScreen = (parentWindow && screen) ? (parentWindow.y - screen.y) : 0;
|
||||
|
||||
// If window reported 0 but bar is at bottom, assume it's at screen bottom
|
||||
if (windowYOnScreen === 0 && barPosition === "bottom" && screen) {
|
||||
windowYOnScreen = screen.height - (parentWindow ? parentWindow.height : Style.getBarHeightForScreen(screen.name));
|
||||
}
|
||||
|
||||
const menuBottom = menuScreenY + implicitHeight;
|
||||
const screenBottom = screen.height;
|
||||
// Calculate the screen Y of the menu top
|
||||
// Use a small guess for height if implicitHeight is 0 to avoid covering the bar on the first frame
|
||||
const effectiveHeight = implicitHeight > 0 ? implicitHeight : 200;
|
||||
const effectiveBaseY = shouldApplyBottomBarLogic ? -(effectiveHeight + Style.marginL + 2) : baseY;
|
||||
|
||||
// Adjust baseY if menu would clip
|
||||
if (menuBottom > screenBottom) {
|
||||
// Clip at bottom - shift up by the overflow amount
|
||||
const overflow = menuBottom - screenBottom;
|
||||
if (!isSubMenu && barPosition === "bottom") {
|
||||
return baseY + overflow + Style.marginM;
|
||||
}
|
||||
return baseY - overflow - Style.marginM;
|
||||
const menuScreenY = windowYOnScreen + posInWindow.y + effectiveBaseY;
|
||||
const menuBottom = menuScreenY + (implicitHeight > 0 ? implicitHeight : effectiveHeight);
|
||||
const screenHeight = screen ? screen.height : 1080;
|
||||
|
||||
// Adjust the final baseY (the actual value returned to anchor.rect.y)
|
||||
let finalBaseY = shouldApplyBottomBarLogic ? -(implicitHeight + Style.marginL + 2) : baseY;
|
||||
|
||||
// Adjust if menu would clip off the bottom
|
||||
if (menuBottom > screenHeight) {
|
||||
const overflow = menuBottom - screenHeight;
|
||||
finalBaseY -= (overflow + Style.marginM);
|
||||
}
|
||||
|
||||
// Adjust if menu would clip off the top
|
||||
// menuScreenY < 0 means it's above the screen edge
|
||||
if (menuScreenY < 0) {
|
||||
// Clip at top - shift down
|
||||
if (!isSubMenu && barPosition === "bottom") {
|
||||
return baseY + menuScreenY - Style.marginM;
|
||||
}
|
||||
return baseY - menuScreenY + Style.marginM;
|
||||
finalBaseY -= (menuScreenY - Style.marginM);
|
||||
}
|
||||
return baseY;
|
||||
|
||||
return finalBaseY;
|
||||
}
|
||||
|
||||
// Fallback if no anchor/screen
|
||||
|
||||
@@ -447,23 +447,30 @@ Item {
|
||||
}
|
||||
|
||||
if (modelData.hasMenu && modelData.menu && trayMenu && trayMenu.item) {
|
||||
// Position menu based on bar position, using tooltipAnchor for proper positioning
|
||||
let menuX, menuY;
|
||||
if (barPosition === "left") {
|
||||
// For left bar: position menu to the right of the visual area
|
||||
menuX = tooltipAnchor.width + Style.marginM;
|
||||
menuY = 0;
|
||||
} else if (barPosition === "right") {
|
||||
// For right bar: position menu to the left of the visual area
|
||||
menuX = -trayMenu.item.width - Style.marginM;
|
||||
menuY = 0;
|
||||
} else {
|
||||
// For horizontal bars: center horizontally and position below visual area
|
||||
menuX = (tooltipAnchor.width / 2) - (trayMenu.item.width / 2);
|
||||
menuY = tooltipAnchor.height + Style.marginS;
|
||||
}
|
||||
// Calculate menu position after ensuring menu is loaded
|
||||
const calculateAndShow = () => {
|
||||
// Position menu based on bar position, using tooltipAnchor for proper positioning
|
||||
// Increased spacing for better alignment with other context menus
|
||||
let menuX, menuY;
|
||||
if (barPosition === "left") {
|
||||
// For left bar: position menu to the right of the visual area
|
||||
menuX = tooltipAnchor.width + Style.marginL;
|
||||
menuY = 0;
|
||||
} else if (barPosition === "right") {
|
||||
// For right bar: position menu to the left of the visual area
|
||||
menuX = -trayMenu.item.implicitWidth - Style.marginL;
|
||||
menuY = 0;
|
||||
} else {
|
||||
// For horizontal bars: center horizontally and position below visual area
|
||||
menuX = (tooltipAnchor.width / 2) - (trayMenu.item.implicitWidth / 2);
|
||||
menuY = tooltipAnchor.height + Style.marginM;
|
||||
}
|
||||
|
||||
PanelService.showTrayMenu(root.screen, modelData, trayMenu.item, tooltipAnchor, menuX, menuY, root.section, root.sectionWidgetIndex);
|
||||
PanelService.showTrayMenu(root.screen, modelData, trayMenu.item, tooltipAnchor, menuX, menuY, root.section, root.sectionWidgetIndex);
|
||||
};
|
||||
|
||||
// Use Qt.callLater to ensure menu dimensions are calculated
|
||||
Qt.callLater(calculateAndShow);
|
||||
} else {
|
||||
Logger.d("Tray", "No menu available for", modelData.id, "or trayMenu not set");
|
||||
}
|
||||
|
||||
@@ -241,17 +241,24 @@ SmartPanel {
|
||||
|
||||
if (modelData.hasMenu && modelData.menu && panelContent.trayMenu && panelContent.trayMenu.item) {
|
||||
const barPosition = Settings.getBarPositionForScreen(root.screen?.name);
|
||||
// Increased spacing for better alignment with other context menus
|
||||
let menuX, menuY;
|
||||
|
||||
if (barPosition === "left") {
|
||||
menuX = trayIcon.width + Style.marginM;
|
||||
menuX = trayIcon.width + Style.marginL;
|
||||
menuY = 0;
|
||||
} else if (barPosition === "right") {
|
||||
menuX = -panelContent.trayMenu.item.width - Style.marginM;
|
||||
menuX = -panelContent.trayMenu.item.width - Style.marginL;
|
||||
menuY = 0;
|
||||
} else {
|
||||
} else if (barPosition === "bottom") {
|
||||
// For bottom bar: let TrayMenu handle positioning by passing anchorY >= 0
|
||||
// TrayMenu will position above the anchor item
|
||||
menuX = (trayIcon.width / 2) - (panelContent.trayMenu.item.width / 2);
|
||||
menuY = trayIcon.height + Style.marginS;
|
||||
menuY = trayIcon.height + Style.marginL;
|
||||
} else {
|
||||
// For top bar: position menu below the icon with more spacing
|
||||
menuX = (trayIcon.width / 2) - (panelContent.trayMenu.item.width / 2);
|
||||
menuY = trayIcon.height + Style.marginL;
|
||||
}
|
||||
|
||||
PanelService.showTrayMenu(root.screen, modelData, panelContent.trayMenu.item, trayIcon, menuX, menuY, root.widgetSection, root.widgetIndex);
|
||||
|
||||
Reference in New Issue
Block a user