Hard crash on trying to resize stack to fit content
Posted: Mon Nov 28, 2022 5:19 pm
Hi all,
I seem to be doing some the that's causing a HARD crash. The code below works fine the first time I open a test stack, but if I close the and re-open it immediate crashes and exits LiveCode.
I know there are a lot of discussions about changing control dimensions/locations to fit the stack, but I have need of the opposite of this so that when I move from one card to another, the stack size changes to fit the content, and that the content should have equal margins from the edge of the stack all the way around.
My approach is to (in script) group all elements on the card into a temporary group, derive the rect of what the stack should be with default or given margins, resize/reposition the stack then ungroup the temporary group.
This all works well, but is very abrupt - I was hoping there would be some way of giving a smoother transition but unlock with visual effect seems to do nothing when switching to cards (I use Lock Screen instead of Lock Screen for visual effect, as the latter makes it *really* slow). Ideally I'd like to show a quick but smooth stretch in the top/left to bottom/right axis, but not found a way yet...
The problem is that this works well - but if I close and reopen the stack, it immediately and reproducibly crashes LC to crash hard and exit. If I relaunch LC and open the same stack again it works fine, but on closing and re-opening the test stack... BOOM!
I can't explain this...
My code resides in the stack script:
I attach the test stack - be aware that it will probably work fine to start with (just go to next card to test), but if you close and re-open the stack there should be an instantaneous crash - so please ensure your work is saved before testing this!
Many thanks for any suggestions (both for the crash and on how to smooth the widow size change if that's possible)
Stam
PS: As an aside, why is the rect of the stack so much larger than the rect of the card, but the height/width of the 2 are the same???
I seem to be doing some the that's causing a HARD crash. The code below works fine the first time I open a test stack, but if I close the and re-open it immediate crashes and exits LiveCode.
I know there are a lot of discussions about changing control dimensions/locations to fit the stack, but I have need of the opposite of this so that when I move from one card to another, the stack size changes to fit the content, and that the content should have equal margins from the edge of the stack all the way around.
My approach is to (in script) group all elements on the card into a temporary group, derive the rect of what the stack should be with default or given margins, resize/reposition the stack then ungroup the temporary group.
This all works well, but is very abrupt - I was hoping there would be some way of giving a smoother transition but unlock with visual effect seems to do nothing when switching to cards (I use Lock Screen instead of Lock Screen for visual effect, as the latter makes it *really* slow). Ideally I'd like to show a quick but smooth stretch in the top/left to bottom/right axis, but not found a way yet...
The problem is that this works well - but if I close and reopen the stack, it immediately and reproducibly crashes LC to crash hard and exit. If I relaunch LC and open the same stack again it works fine, but on closing and re-opening the test stack... BOOM!
I can't explain this...
My code resides in the stack script:
Code: Select all
## PURPOSE : resize stack to content when moving to another card
# NOTE: do not use with any cards containing background and/or shared groups, as these cannot be grouped
command autoResizeOnContent pMargins
# groups all elements into a large temporary group, calls autoResize then ungroups the temp group
local tControls, tGroupMembers, tGroupID
lock screen
repeat with x = 1 to the number of controls of this card
put the long name of control x of this card & return after tControls
end repeat
filter lines of tControls without regex pattern "\sgroup" // eliminates members of other groups
repeat for each line tLine in tControls
if there is a tLine then put the name of tLine & " and " after tGroupMembers
end repeat
delete the last word in tGroupMembers
do merge ("group [[tGroupMembers]]")
put it into tGroupID
autoResize pMargins
ungroup tGroupID
select empty
unlock screen with visual "stretch from top"
end autoResizeOnContent
command autoResize pMargins
# Resizes stack by the rect of the temporary group of all content in autoResizeOnContent
# default margin is 21 px (minus the 4 px of the group) = 17 px (leave blank to accept this)
# the groups have margin of 4, so subtract that from whatever margin you choose, or leave blank for default of 21 px (= 17 )
local tRect, tTopLeft, tWidth, tHeight, tGroup
put the topLeft of this stack into tTopLeft
if pMargins is empty then put 17 into pMargins
put the groupIDs of this card into tGroup
put the rect of group id tGroup into tRect
subtract pMargins from item 1 of tRect
subtract pMargins from item 2 of tRect
add pMargins to item 3 of tRect
add pMargins to item 4 of tRect
put item 3 of tRect - item 1 of tRect into tWidth
put item 4 of tRect - item 2 of tRect into tHeight
set the width of this stack to tWidth
set the height of this stack to tHeight
set the topLeft of this stack to tTopLeft
set the loc of group id tGroup to the loc of this card
end autoResize
on openCard
# if in stack script and if a card has an openCard handler, end this with 'pass openCard' or just add to card script directly
if the controlIDs of this card is not empty then
autoResizeOnContent
end if
end openCard
Many thanks for any suggestions (both for the crash and on how to smooth the widow size change if that's possible)
Stam
PS: As an aside, why is the rect of the stack so much larger than the rect of the card, but the height/width of the 2 are the same???