summaryrefslogtreecommitdiff
path: root/src/ui/util.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-09-26 08:31:15 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-09-26 08:31:15 +0300
commit5843195683f56797d0f637205851f69289047343 (patch)
treec7e542183133f56aecf7e66344d45c65c2d21a77 /src/ui/util.c
parent549b1bda138dbfcc969cad572df7739d6821d02d (diff)
Fixed context menu centering with popup windows
Diffstat (limited to 'src/ui/util.c')
-rw-r--r--src/ui/util.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/ui/util.c b/src/ui/util.c
index 244eb24d..872a91ab 100644
--- a/src/ui/util.c
+++ b/src/ui/util.c
@@ -855,7 +855,7 @@ iWidget *makeMenu_Widget(iWidget *parent, const iMenuItem *items, size_t n) {
855} 855}
856 856
857void openMenu_Widget(iWidget *d, iInt2 windowCoord) { 857void openMenu_Widget(iWidget *d, iInt2 windowCoord) {
858 openMenuFlags_Widget(d, windowCoord, iTrue); 858 openMenuFlags_Widget(d, windowCoord, postCommands_MenuOpenFlags);
859} 859}
860 860
861static void updateMenuItemFonts_Widget_(iWidget *d) { 861static void updateMenuItemFonts_Widget_(iWidget *d) {
@@ -1002,7 +1002,8 @@ void releaseNativeMenu_Widget(iWidget *d) {
1002#endif 1002#endif
1003} 1003}
1004 1004
1005void openMenuFlags_Widget(iWidget *d, iInt2 windowCoord, iBool postCommands) { 1005void openMenuFlags_Widget(iWidget *d, iInt2 windowCoord, int menuOpenFlags) {
1006 const iBool postCommands = (menuOpenFlags & postCommands_MenuOpenFlags) != 0;
1006#if defined (iHaveNativeContextMenus) 1007#if defined (iHaveNativeContextMenus)
1007 const iArray *items = userData_Object(d); 1008 const iArray *items = userData_Object(d);
1008 iAssert(flags_Widget(d) & nativeMenu_WidgetFlag); 1009 iAssert(flags_Widget(d) & nativeMenu_WidgetFlag);
@@ -1020,6 +1021,7 @@ void openMenuFlags_Widget(iWidget *d, iInt2 windowCoord, iBool postCommands) {
1020 processEvents_App(postedEventsOnly_AppEventMode); 1021 processEvents_App(postedEventsOnly_AppEventMode);
1021 setFlags_Widget(d, hidden_WidgetFlag, iFalse); 1022 setFlags_Widget(d, hidden_WidgetFlag, iFalse);
1022 setFlags_Widget(d, commandOnMouseMiss_WidgetFlag, iTrue); 1023 setFlags_Widget(d, commandOnMouseMiss_WidgetFlag, iTrue);
1024 setFlags_Widget(findChild_Widget(d, "menu.cancel"), disabled_WidgetFlag, iFalse);
1023 if (isUsingMenuPopupWindows_()) { 1025 if (isUsingMenuPopupWindows_()) {
1024 if (postCommands) { 1026 if (postCommands) {
1025 postCommand_Widget(d, "menu.opened"); 1027 postCommand_Widget(d, "menu.opened");
@@ -1030,12 +1032,21 @@ void openMenuFlags_Widget(iWidget *d, iInt2 windowCoord, iBool postCommands) {
1030 setUserData_Object(d, parent_Widget(d)); 1032 setUserData_Object(d, parent_Widget(d));
1031 iAssert(userData_Object(d)); 1033 iAssert(userData_Object(d));
1032 removeChild_Widget(parent_Widget(d), d); /* we'll borrow the widget for a while */ 1034 removeChild_Widget(parent_Widget(d), d); /* we'll borrow the widget for a while */
1035 SDL_Window *sdlWin = get_Window()->win;
1036 iInt2 winPos;
1037 SDL_GetWindowPosition(sdlWin, &winPos.x, &winPos.y);
1038// const iRect winRect =
1033 const float pixelRatio = get_Window()->pixelRatio; 1039 const float pixelRatio = get_Window()->pixelRatio;
1034 iInt2 menuPos = add_I2(get_MainWindow()->place.normalRect.pos, 1040 iInt2 menuPos = add_I2(winPos,
1035 divf_I2(sub_I2(windowCoord, divi_I2(gap2_UI, 2)), pixelRatio)); 1041 divf_I2(sub_I2(windowCoord, divi_I2(gap2_UI, 2)), pixelRatio));
1036 arrange_Widget(d); 1042 arrange_Widget(d);
1037 /* Check display bounds. */ { 1043 /* Check display bounds. */ {
1038 const iInt2 menuSize = divf_I2(d->rect.size, pixelRatio); 1044 const iInt2 menuSize = divf_I2(d->rect.size, pixelRatio);
1045 if (menuOpenFlags & center_MenuOpenFlags) {
1046 iInt2 winSize;
1047 SDL_GetWindowSize(sdlWin, &winSize.x, &winSize.y);
1048 menuPos = sub_I2(add_I2(winPos, divi_I2(winSize, 2)), divi_I2(menuSize, 2));
1049 }
1039 SDL_Rect displayRect; 1050 SDL_Rect displayRect;
1040 SDL_GetDisplayBounds(SDL_GetWindowDisplayIndex(get_Window()->win), &displayRect); 1051 SDL_GetDisplayBounds(SDL_GetWindowDisplayIndex(get_Window()->win), &displayRect);
1041 menuPos.x = iMin(menuPos.x, displayRect.x + displayRect.w - menuSize.x); 1052 menuPos.x = iMin(menuPos.x, displayRect.x + displayRect.w - menuSize.x);
@@ -1051,7 +1062,6 @@ void openMenuFlags_Widget(iWidget *d, iInt2 windowCoord, iBool postCommands) {
1051 return; 1062 return;
1052 } 1063 }
1053 raise_Widget(d); 1064 raise_Widget(d);
1054 setFlags_Widget(findChild_Widget(d, "menu.cancel"), disabled_WidgetFlag, iFalse);
1055 if (isPortraitPhone) { 1065 if (isPortraitPhone) {
1056 setFlags_Widget(d, arrangeWidth_WidgetFlag | resizeChildrenToWidestChild_WidgetFlag, iFalse); 1066 setFlags_Widget(d, arrangeWidth_WidgetFlag | resizeChildrenToWidestChild_WidgetFlag, iFalse);
1057 setFlags_Widget(d, resizeWidthOfChildren_WidgetFlag | drawBackgroundToBottom_WidgetFlag, iTrue); 1067 setFlags_Widget(d, resizeWidthOfChildren_WidgetFlag | drawBackgroundToBottom_WidgetFlag, iTrue);
@@ -1106,7 +1116,7 @@ void openMenuFlags_Widget(iWidget *d, iInt2 windowCoord, iBool postCommands) {
1106 postCommand_Widget(d, "menu.opened"); 1116 postCommand_Widget(d, "menu.opened");
1107 } 1117 }
1108 setupMenuTransition_Mobile(d, iTrue); 1118 setupMenuTransition_Mobile(d, iTrue);
1109#endif 1119#endif
1110} 1120}
1111 1121
1112void closeMenu_Widget(iWidget *d) { 1122void closeMenu_Widget(iWidget *d) {