Gui/Control objects position/size properties after creation

Propose new features and changes
User avatar
RaptorX
Posts: 371
Joined: 06 Dec 2014, 14:27
Contact:

Gui/Control objects position/size properties after creation

Post by RaptorX » 06 Jun 2023, 12:09

When creating GUIs, I usually create some logic to position the controls relative to one-another.
for example I want this button to dynamically move if I change the height of the listview:
Spoiler

This code achieves that

Code: Select all

MyGui:= Gui(,'Category Manager')
lvCategory := MyGui.AddListView('vcategory_list w600 r25', ['Category', 'Type'])
MyGui.AddButton('w75 ym', 'Add')
MyGui.AddButton('w75 xp', 'Delete')

lvCategory.GetPos(,,,&height)
yloc := height - 25 * 3
MyGui.AddButton('y+' yloc ' w75', 'Close').OnEvent('Click', (*)=>MyGui.Hide())
MyGui.Show()
Unfortunately I we are forced to use ctrl.GetPos() to get the current pos/size information of a control.
I would love to simply be able to refer to ctrl.x, ctrl.y, ctrl.width and ctrl.height!
It think it should be the same for GUI objects.

It would help making the creation of responsive GUIs a little bit more intuitive.

Of course I know that we can use the Section option to save the positions of controls for later use, but there is no ws or hs AND you can only have one saved section at a time... so there are situations in which if you have a very complex gui you cant have several sections saved.

I should create another wish post with named sections for different saved positions haha :D
Projects:
AHK-ToolKit

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: Gui/Control objects position/size properties after creation

Post by lexikos » 29 Jun 2023, 20:35

I have had similar thoughts. I think the reasons I did not go that way were:
  • For the Gui, retrieving the position each time a property is invoked gives the risk that the components will be out of sync, with the window having moved between calls. (I'm not really sure what practical impact this has. For controls, it is basically a non-issue since they are less likely to be moved by something external.)
  • It gives an impression of inefficiency, retrieving the position multiple times. (This is untested and may be untrue, as retrieving the window's position via the API probably has negligible cost.)
Part of the reason I do not make small additions to the GUI is that I think the current paradigm is ill-suited to dynamic or scalable GUIs, and a more fundamental shift would be better.

I may accept a pull request for this (to the alpha branch).

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Gui/Control objects position/size properties after creation

Post by kczx3 » 30 Jun 2023, 07:32

You seem more inclined to do this than in the past. I do recall discussions in the past and I recall your main concern was performance and that retrieving the width and height would ultimately result in multiple API calls to get the same information. I’ll have to find the conversation to link it here for clarity

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Gui/Control objects position/size properties after creation

Post by kczx3 » 30 Jun 2023, 07:43

Found it. Your first post in this thread has good information.

viewtopic.php?f=37&t=78003&p=342080&hilit=Getpos#p342080

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: Gui/Control objects position/size properties after creation

Post by lexikos » 30 Jun 2023, 19:30

kczx3 wrote:
30 Jun 2023, 07:32
You seem more inclined to do this than in the past.
Most of that older topic relates to GuiPos() vs Pos returning an object, and tangential arguments. As far as I can see, the only part of that discussion which directly addressed separate properties is where I suggested "Perhaps separate properties would be faster, more convenient and less problematic" and demonstrated an implementation and usage identical to what RaptorX is requesting.
I recall your main concern was performance
I literally wrote "I am not concerned about performance" in that topic.
and that retrieving the width and height would ultimately result in multiple API calls to get the same information.
For something like g.Pos.X + g.Pos.W, the inefficiency is not just the multiple API calls, but also multiple times querying the Pos property and multiple times creating an object with all four properties. It would be a lie to say that the perception of encouraging inefficiency didn't affect my decision (even if I'm not actually concerned about real-world performance), but it wasn't my main concern.

Calling the API multiple times to get the same information is all well and good, but multiple API calls means that you may not be getting the same information, as a window can be moved externally.

Now that I think about it, something like ctrl.X + ctrl.Width is unlikely to get out of sync values since there's no message-checking in between the two property calls. That would normally occur between lines, or when certain functions are sleeping/waiting. It would be different if the window belonged to another thread or process.

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Gui/Control objects position/size properties after creation

Post by kczx3 » 30 Jun 2023, 21:11

Alas, I mistook apples for oranges. Still, the discussion and topic is about fruit. I’m supportive of RaptorX’s wish.

User avatar
RaptorX
Posts: 371
Joined: 06 Dec 2014, 14:27
Contact:

Re: Gui/Control objects position/size properties after creation

Post by RaptorX » 01 Jul 2023, 08:48

After your replies I see how the GUI object is a bit more complex and in my code rarely do I need to get the current window width/height unless there is a resize event which then I obviously handle with the proper Event mechanism which already gives me all I need.

I do understand being hesitant to modify GUI creation too much in favor of potentially having an overall better approach down the line.
lexikos wrote:
30 Jun 2023, 19:30
Now that I think about it, something like ctrl.X + ctrl.Width is unlikely to get out of sync values since there's no message-checking in between the two property calls. That would normally occur between lines, or when certain functions are sleeping/waiting. It would be different if the window belonged to another thread or process.
From an outsider's perspective lvCategory.GetPos(,,,&height) and lvCategory.height seem to pretty much do the same thing, im getting the current width/height values whenever I care about them, is just that one of them seems less clunky than the other.
Projects:
AHK-ToolKit

lexikos
Posts: 9553
Joined: 30 Sep 2013, 04:07
Contact:

Re: Gui/Control objects position/size properties after creation

Post by lexikos » 02 Jul 2023, 01:36

Cases where you really are only retrieving one of the components are exactly what the properties would be meant for - and safe to use for - but they would also be misused for cases where doing so may carry a risk of unexpected results due to the window moving. The same can happen with GetPos, but an author is less likely to use lvCategory.GetPos(,,,&height) immediately with lvCategory.GetPos(,,&width) when it's less effort to get both values with one call.

Post Reply

Return to “Wish List”