Calculate position of dropped menu EXACTLY below the icon

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
limajose79
Posts: 2
Joined: 30 Mar 2023 22:29

Calculate position of dropped menu EXACTLY below the icon

Post by limajose79 »

Hi, I need some help in this...

Based on my window configuration, the XYplorer window sits at x=192px... anyway...
When we click the icon, basicaly the menu drops right below the mouse pointer, so I'm trying to change the position, for just below the icon (Toolbar).
I'm trying to develop a equation, that if we click (like range) from the middle to the end of the icon, the code compensates the overflow (even if it was more 14px...)...
or the contrary... from the the middle, back where the icon starts (like, the first 16px..)

Noob skills aside... 8) and write some code for the popupnested() menu:

Code: Select all

  $homemenu = <<<>>>
<bla bla bla>
>>>;

  $szScreen = 1920;
  $szIcon = 32;

  // CTB Position "X"
  if ($xValue) {
    $xPosition = get("cursorpos", x);
    $valuePositionX = $szScreen / $xPosition;
    $xDiff = $szIcon / $valuePositionX;
  }
  $xValue = $xPosition - $xDiff;

  // CTB Position "Y"
  if ($yValue) {
    $yPosition = get("cursorpos", y);
    if ($buttonSet) {
      if (buttonset() == 1) {
        $buttonSet = 93;
      }
      elseif (buttonset() == 2) {
        $buttonSet = 58;
      }
    }
  }
  $yValue = ($yPosition - $yPosition) + $buttonSet;

  $command = trim(popupnested($homemenu,$xValue,$yValue, 7:="|"));

  if ($command != '') { load $command,, s; }
  
Like this:
Image
https://ibb.co/sK5rmcJ

NOT this:
Image
https://ibb.co/0FL8NfT

Hope get some help!

Thanks!
Lima79.

highend
Posts: 14571
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Calculate position of dropped menu EXACTLY below the icon

Post by highend »

You'll need to measure the widths of the special icons (read the code)...

Code: Select all

    $homemenu = <<<>>>
hello
world
>>>;

    // We only need "special" icons here
    // "-" in toolbar() is the separator
    // Icons with the same width can be in a single line (","-separated)
    $specialWidths = <<<>>>
-=8
tbs=10
menu=14
back,fore,undo,redo=36
    >>>;
    $normalWidth = 32;

    $tb     = controlposition("TB"); // 86|75|2458|33
    $tbX    = gettoken($tb, 1, "|"); // 86
    $tbY    = gettoken($tb, 2, "|"); // 75
    $tbH    = gettoken($tb, 4, "|"); // 33
    $tbYMax = $tbY + $tbH;           // 75 + 33 = 108

    $cursor  = get("cursorpos"); // 425;237
    $cursorX = gettoken($cursor, 1, ";"); // 425
    $cursorY = gettoken($cursor, 2, ";"); // 237

    $iconPos = posatpos();
    $toolbar = toolbar();
    $screen  = get("Screen", "width");


    // Check click boundaries
    if ($cursorY >= $tbY && $cursorY <= $tbYMax) {
        // Get all icons _before_ the one that was clicked
        $icons = gettoken($toolbar, $iconPos - 1, ",", , 1); // tbs,menu,back,fore,-

        // Try to calculate the width of all of them...
        $iconWidths = 0;
        foreach($icon, $icons, ",", "e") {
            // tbs=10 or -=8, etc.
            $width = regexmatches($specialWidths, "(^|,).*?" . $icon . ".*?=\d+");
            $width = gettoken($width, 2, "=", "t");

            // Use standard size if it's not a special width
            if (!$width) { $width = $normalWidth; }

            // Add up
            $iconWidths += $width;
        }
        // All widths were relative to XY, bring it to screen size
        // You'd need a cli tool to get the XY window size (the X position)
        // In your case you could just add your 192 px
        $iconWidths += 96;

        // Calculate the click boundaries again and use the $iconWidths
        // variable (+ padding eventually)
        // ...

        $sel = popupmenu($homemenu, $iconWidths, $tbYMax);
        // ...
    }
One of my scripts helped you out? Please donate via Paypal

jupe
Posts: 3292
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: Calculate position of dropped menu EXACTLY below the icon

Post by jupe »

In your screenshot you are using multirow toolbar, so I doubt highends script will work properly without mods, I assume it will produce strange placement, also under other conditions too, such as if you are using BTBs or text btns etc,

If I were you I would just calculate the Y axis and estimate the X off of cursorpos, here is a hack job implementation I threw together if you are interested.

Code: Select all

  $i  = 1;
  $tb = toolbar();
  foreach($row, $tb,, e) {
    if (gettokenindex("ctb<get trigger ctbindex>", $row, ",", "")) { break; }
    $i++;
  }
  $r  = gettoken($tb, "count", "|");
  $tb = controlposition("TB");
  $h  = gettoken($tb, 4, "|") / $r;
  $y  = gettoken($tb, 2, "|") + $h * $i;
  popupmenu("a|b|c", <get cursorpos x>-($h/2), $y);
Of course if your menu captions are longer than available screen space the menu will be out of alignment still on the X axis unless you truncate the captions.

limajose79
Posts: 2
Joined: 30 Mar 2023 22:29

Re: Calculate position of dropped menu EXACTLY below the icon

Post by limajose79 »

Thanks highend and jupe!!! Very nice ideas.
I'm doing some tests with both codes...
I'll back soon with some results...

admin
Site Admin
Posts: 64915
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Calculate position of dropped menu EXACTLY below the icon

Post by admin »

In the next beta you can pass a toolbar button key to popupmenu/popupnested, and XY handles the rest.

admin
Site Admin
Posts: 64915
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Calculate position of dropped menu EXACTLY below the icon

Post by admin »

So, everybody happy now?

Post Reply