Send the Stack to the Center of the screen.

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

trailboss
Posts: 121
Joined: Sat Dec 13, 2008 4:55 pm

Send the Stack to the Center of the screen.

Post by trailboss » Mon Dec 11, 2023 8:30 pm

I can't seem to get the stack centered on the screen. I put send centerme in the openstack with
on centerMe
set the loc of this stack to the screenLoc
end centerMe

And nothing changes. I can drag the stack to the center of the screen but when I Command M and type send centerme, it jumps up and to the left.
What am I doing wrong?
Thanks in advance.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Send the Stack to the Center of the screen.

Post by dunbarx » Mon Dec 11, 2023 10:05 pm

Hi.
What am I doing wrong?
I cannot imagine. Should work just fine.

Close LC and then re-open. Make a brand new stack and try that line of code from the message box. This will determine if the problem is in your stack, or the universe has finally flipped.

Please put code into the code tags.

Craig

trailboss
Posts: 121
Joined: Sat Dec 13, 2008 4:55 pm

Re: Send the Stack to the Center of the screen.

Post by trailboss » Mon Dec 11, 2023 10:21 pm

Oh, I have this in the preopenstack: set the scaleFactor of this stack to .7

trailboss
Posts: 121
Joined: Sat Dec 13, 2008 4:55 pm

Re: Send the Stack to the Center of the screen.

Post by trailboss » Mon Dec 11, 2023 10:56 pm

If you use that scalefactor and set it to .7 centering will send it up and left. You can't get rid of it once it's been saved. You must put in .1 to change it.. Erase the .1 scalefactor and the stack REVERTS to .7 even though it's not part of the code anymore. Forever corrupted it seems to me but I'd love to be wrong.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Send the Stack to the Center of the screen.

Post by stam » Tue Dec 12, 2023 12:37 am

trailboss wrote:
Mon Dec 11, 2023 10:56 pm
If you use that scalefactor and set it to .7 centering will send it up and left. You can't get rid of it once it's been saved. You must put in .1 to change it.. Erase the .1 scalefactor and the stack REVERTS to .7 even though it's not part of the code anymore. Forever corrupted it seems to me but I'd love to be wrong.
Not quite sure what you mean.Take a deep breath ;)
You obviously cannot leave an empty scaleFactor - if you do, it will just put in the last value it had when you re-open the property inspector. However if you change it, it should stay with the new value. If that's not happening, perhaps there is something in the code that is changing this - in that case it would be worth searching the entire stack for 'scaleFactor' ;)

Back to your original question:
trailboss wrote:
Mon Dec 11, 2023 8:30 pm
I can't seem to get the stack centered on the screen. I put send centerme in the openstack with

Code: Select all

on centerMe
   set the loc of this stack to the screenLoc
end centerMe
And nothing changes. I can drag the stack to the center of the screen but when I Command M and type send centerme, it jumps up and to the left.
What am I doing wrong?
Thanks in advance.
Yes that seems weird but is actually expected and reproducible.
When scaling a stack, anything outside the scaled stack, including the screenRect and screenLoc remain unchanged - however in the microcosm of the stack everything has shrunk by (1-scaleFactor). So as far as the microcosm of the stack is concerned the screenRect should have grown proportionally as well, but of course it hasn't.

So what the scaled stack thinks is the screenLoc and what is actually the screenLoc are quite different, hence the misalignment... so the numbers from screenLoc need to be adjusted.

This works:

Code: Select all

on centerMe
   set the loc of this stack to \
       item 1 of the screenLoc / the scaleFactor of this stack,\
       item 2 of the screenLoc / the scaleFactor of this stack
end centerMe
This works for both scaled and unscaled stacks.
Stam


PS: Using scaleFactor is easy but it's a quick'n'dirty hack that comes with issues like this (what the dimensions of the scaled stack are are significantly different to dimensions of things outside your stack as these aren't scaled). The much better option, if you want to dedicate time to it, is to create an adaptive interface that moves/resizes the various objects to fit differing screen sizes, like I mentioned in my previous post. But that too can be a chore to set up, so only worth doing if it's worth doing ;)

trailboss
Posts: 121
Joined: Sat Dec 13, 2008 4:55 pm

Re: Send the Stack to the Center of the screen.

Post by trailboss » Tue Dec 12, 2023 1:53 am

I guess my only problem is using scalefactor. It's so easy to reduce the size of your project on the screen but when you use it, it can't be centered.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Send the Stack to the Center of the screen.

Post by SparkOut » Tue Dec 12, 2023 3:03 am

trailboss wrote:
Tue Dec 12, 2023 1:53 am
I guess my only problem is using scalefactor. It's so easy to reduce the size of your project on the screen but when you use it, it can't be centered.
Yes it can, as per stam's detailed explanation, the solution is a single line of script, that he provided above. (Admittedly a longish line, but split for easy reading,)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Send the Stack to the Center of the screen.

Post by richmond62 » Tue Dec 12, 2023 9:11 am

After about 22 with RR/LC I am extremely glad to find out about scaleFactor . . .

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Send the Stack to the Center of the screen.

Post by stam » Tue Dec 12, 2023 9:52 am

Just remember: scaleFactor is an excellent example in relativity ;)

Taking the example above:

For an observer exterior to the stack, the stack appears to shrink by 30%, I.e. 100*(1-scaleFactor)% (or, that for this stack, 1 is now equal to 0.7, or 1*scaleFactor).

However for an observer inside the stack, 1 still equals 1 but the external world (ie the actual desktop/screen) has grown by 70%, or 100*scaleFactor% (or that external to the stack, 1 is now equal to 1.43, or 1/scaleFactor).

But otherwise it’s easy to use if you don’t mind everything shrinking/expanding!

I have certainly used this for time critical stuff but usually prefer to not scale the stack’s universe ;)

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Send the Stack to the Center of the screen.

Post by mrcoollion » Tue Dec 12, 2023 3:33 pm

Here is an example what i made to position my stack left, right or in the center.
This code is in a group that hold three radio buttons named LeftTop,MidTop and RightTop.
To do some coding exercises, and figure out how it works. It pretty simple.

Regards,

Paul

Code: Select all

on mouseup
   put the screenRects into tScreenRects
   put the effective working screenRect into tScreenRect
   put item 3 of tScreenRect into tScreenWidth
   put item 4 of tScreenRect into tScreenHight
   put the rectangle of this stack into tStackRect
   put the width of this stack into tStackWidth
   put the height of this stack into tStackHight
   put item 1 of tScreenRect into tMinScreen_Left
   --
   put tScreenWidth-tStackWidth into tSetLoc_Stack_Right
   put tMinScreen_Left into tSetLoc_Stack_Left
   put round((tScreenWidth/2)-(tStackWidth/2)) into tSetLoc_Stack_Mid
   --
   put the the short name of target into tChoice
   switch tChoice
      case "LeftTop"
         set the topleft of this stack to tSetLoc_Stack_Left,30
         break
      case "MidTop"
         set the topleft of this stack to tSetLoc_Stack_Mid,30
         break
      case "RightTop"
         set the topleft of this stack to tSetLoc_Stack_Right,30
         break
   end switch
end mouseup

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Send the Stack to the Center of the screen.

Post by stam » Tue Dec 12, 2023 5:50 pm

That’s great!

But this probably won’t work with scaled stacks as from the stack’s point of view, the screenrect has changed, but in reality it hasn’t because the screen is not scaled accordingly… (I say this only briefly examining your code and not being able to test as my day job is nothing to do with coding…)

The issue encountered in this thread arose because the stack’s scaleFactor was set at 0.7… see my posts above…

I can’t test as am at work but you can probably cater for the stack’s scaleFactor and make your handler more bulletproof…

Stam

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Send the Stack to the Center of the screen.

Post by richmond62 » Tue Dec 12, 2023 8:01 pm

This sounds lika a headache.

Having scaled a stack to fit a screen I would concern myself with the topleft corner of the stack.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Send the Stack to the Center of the screen.

Post by stam » Tue Dec 12, 2023 8:41 pm

richmond62 wrote:
Tue Dec 12, 2023 8:01 pm
This sounds lika a headache.

Having scaled a stack to fit a screen I would concern myself with the topleft corner of the stack.
You'd think eh? But no... try this and see what happens on MacOS...

Code: Select all

set the topLeft of this stack to item 2 of the screenRect, item 1 of the screenRect
In case you're not on MacOS, the top of the screen is occupied by the MenuBar and will cover the top part of the stack which has been aligned to the top of the screen, not the bottom of the Apple MenuBar, rendering the stack unmovable by users.

In Paul’s example above the same thing happens even though he uses the effective working screenRect - I hadn’t seen this before and it almost works (it locates the top of the Dock, but fails on the menubar, I presume because it’s buggy), so learned something new today ;)

The MenuBar on MacOS has got bigger with Sonoma, but in truth it's been gradually growing for a while ;). It also apparently depends on what hardware you're running (intel vs ARM), which OS you're running and what your resolution is, because it actually changes the menubar height with resolution... On my system (M2 MBP 16', running Sonoma at default resolution), the menubar height is 28 px, so the top of the usable screen on my MacOS system is

Code: Select all

item 2 of the screenRect + 28
Then we have the scaling issue - like I mentioned above scaling messes with how the scaled stack sees the screenRect - if referencing any screen element from within the scaled stack, this needs to be adjusted for the scaling.

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Send the Stack to the Center of the screen.

Post by stam » Wed Dec 13, 2023 8:50 am

mrcoollion wrote:
Tue Dec 12, 2023 3:33 pm
Here is an example what i made to position my stack left, right or in the center.
This code is in a group that hold three radio buttons named LeftTop,MidTop and RightTop.
On testing, scaling does unfortunately mess up this handler - but the fix is simple: screen-dependent elements need to be divided by the scaleFactor of the stack.

In the code below, each line where this happens are followed by '##' to show where the adjustments are - just 3 changes are needed (the top, the height and the width of the screen).

EDIT: the keyword 'effective' does not add anything; the working screenRect shows gives the space between the taskbar (win) and Dock (MacOS) and the top of the screen (exclduing the MacOS menubar). The thing to keep in mind is if using the screenRect to position or resize a stack, you have to take into account the 28 px titlebar of the window, as the stack's rect is only the content area, and does not include the titlebar. The remainder of this post has been altered accordingly.

The code below should work with any scaled or unscaled stack,. As per Paul's example, this is the script for a group of 3 radioButtons called 'leftTop', 'midTop' and 'rightTop' respectively:

Code: Select all

on mouseup
    local tScreenRect, tScreenWidth, tScreenHeight, tStackWidth, tStackHeight
    local tLeft, tRight, tMid, tFactor, tTop
    
    put the scaleFactor of this stack into tFactor
    put the working screenRect into tScreenRect
    
    add 28 to item 2 of tScreenRect -- the height of the stack's titlebar
    put item 2 of tScreenRect / tFactor into tTop ## top of usable screen adjusted for scaleFactor ##
    
    put item 3 of tScreenRect / tFactor into tScreenWidth  ## adjust the width of the screen for scaleFactor ##
    put (item 4 of tScreenRect - item 2 of tScreenRect) / tFactor into tScreenHeight  ## for the stack's titlebar, adjusted for scaleFactor ##
    put the width of this stack into tStackWidth
    put the height of this stack into tStackHeight
    
    put item 1 of tScreenRect into tLeft
    put tScreenWidth-tStackWidth into tRight
    put round((tScreenWidth/2)-(tStackWidth/2)) into tMid
    --
    switch the short name of the target
        case "LeftTop"
            set the topleft of this stack to tLeft, tTop
            break
        case "MidTop"
            set the topleft of this stack to tMid, tTop
            break
        case "RightTop"
            set the topleft of this stack to tRight, tTop
            break
    end switch
end mouseup
The slippage of revMenuBar I mentioned before is not related and is fixed with Bernd's solution...

Stam
Last edited by stam on Thu Dec 14, 2023 3:07 am, edited 2 times in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9389
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Send the Stack to the Center of the screen.

Post by richmond62 » Wed Dec 13, 2023 10:54 am

I wonder WHAT exactly the effective screenRect 'sees' and why it doesn't on the 3 desktop systems LC deploys on: and, after all, with Linux what with KDE, GNOME, XFCE, LXDE, and so on, I don't actually see HOW LC can account for docks, panels, taskbars, menu bars, cocktail bars, and so on.

AND (using, for the sake of argument, MacOS), does LC really detect the fatness of the MacOS menu bra, or does it just have a number of pixels hard-coded into it for the top bit of effective screenrect?

If you look up effective in the Dictionary, screenRect is NOT among the things listed to which it can be applied.

AND, to really screw things up:

Code: Select all

put the effective working screenRect
produces "0,25,2560,1346" for my 27" retina iMac, while a screenshot of the same monitor displays at 5120,2880

Code: Select all

put the screenRect
returns half of that, unexpectedly.

And that 1346 (item 4 of the effective working screenRect) does NOT take the MacOS Dock into account, so is not really very useful at all.

Putting this:

Code: Select all

on mouseUp
   set the itemDelimiter to ","
   put item 4 of the effective working screenRect into UD
   put item 3 of the screenRect into LR
   set the height of Stack "screen Stretch" to UD
   set the width of Stack "screen Stretch" to LR
   set the loc of stack "screen Stretch" to the screenLoc
end mouseUp
in a button makes the stack 2560 x 1346, put the top of the stack just below the MacOS Monterey menu bar, and HALFWAY behind the MacOS Dock:
-
Screenshot 2023-12-13 at 12.15.01.png
-
Which is NBG.
-
And I haven't even started playing around with scaleFactor yet. :?

I am going to take a break and imagine what would happen on a Linux desktop with top, bottom, and left-side XFCE panels all of differing thicknesses: but I am not going to go and try that out as I already feel sufficiently lumpy about effective working screenWreck on MacOS not to bother.

Post Reply

Return to “Talking LiveCode”