Gridview cells changing size after being scrolled

,

Program: Visual Studio
OS: Windows 11

I am noticing that after scrolling in a gridview, the cells appear to be changing size by 1 pixel? This then visually shifts them on the screen, which is driving my OCD nuts.

Here is how the cell looks when I load this screen:

2024-12-06 01_27_23-Playdate Simulator

This is the cell after scrolling down by 1 cell:

2024-12-06 01_27_47-Playdate Simulator

By looking at the shape of the pixels of the rounded rect at the bottom corners, you can see the size is changing somewhere.

Is this a bug, or expected because of how I have it coded?

In the update loop I use the needsDisplay property and have it call the Screen code again so it just redraws the screen so we see the updated gridview element.

I honestly prefer the shape and look in the second picture. So what am I doing wrong?

Thank you for your time and consideration,

Gridview Coding
-- VARIABLES FOR POSITION SELECTOR GRIDVIEW
local positionSelectorGridview = pd.ui.gridview.new(0, 26)
local positionCount = #selectedSpread.PositionCoordinates  -- Count of positions in the selected spread

-- Set up the position selector gridview
positionSelectorGridview:setNumberOfRows(positionCount)
positionSelectorGridview:setCellPadding(2, 2, 2, 2)
positionSelectorGridview.scrollCellsToCenter = true

-- Function to draw each row of the gridview
function positionSelectorGridview:drawCell(section, row, column, selected, x, y, width, height)
    gfx.setFont(garnet_16)
    local fontHeight = garnet_16:getHeight()

    -- Set up the position label, e.g., "Pos. 1", "Pos. 2", etc.
    local positionText = "Pos. " .. row

    -- Fill the background with white
    gfx.setColor(gfx.kColorWhite)
    gfx.fillRect(x, y, width, height)

    -- Handle drawing the selected state
    if selected then
        gfx.setColor(gfx.kColorBlack)
        gfx.fillRoundRect(x, y, width, height, 4)
        gfx.setImageDrawMode(gfx.kDrawModeFillWhite)
    else
        gfx.setImageDrawMode(gfx.kDrawModeCopy)
    end

    -- Draw the position text in the center of the cell
    gfx.setColor(gfx.kColorBlack)
    gfx.drawTextInRect(positionText, x, y + (height / 2 - fontHeight / 2), width, fontHeight, nil, nil, kTextAlignment.center)
end
Screen Coding
-- FUNCTIONS FOR READING REVIEWS
local function reviewReadingScreen()
    local combinedImage = gfx.image.new(400, 240)

    local backgroundImage = gfx.image.new(SelectedBackground)
    if backgroundImage then
        local bgOffsetX, bgOffsetY = calculateOffsets(backgroundImage, 400, 240)

        gfx.pushContext(combinedImage)
            backgroundImage:draw(bgOffsetX, bgOffsetY)
        gfx.popContext()
    else
        print("Error: Background image not found.")
    end

    local reviewImage = gfx.image.new("images/Misc/Reading_Review")
    if reviewImage then
        local reviewOffsetX, reviewOffsetY = calculateOffsets(reviewImage, 400, 240)

        gfx.pushContext(combinedImage)
            reviewImage:draw(reviewOffsetX, reviewOffsetY)
            positionSelectorGridview:drawInRect(156, 12, 88, 26)
        gfx.popContext()
    else
        print("Error: Review image not found.")
    end

    -- If Sprite already exists, remove it to replace with updated one
    if ReviewReadingSprite then
        ReviewReadingSprite:remove()
    end

    ReviewReadingSprite = gfx.sprite.new(combinedImage)
    ReviewReadingSprite:setZIndex(2)
    ReviewReadingSprite:moveTo(200, 120)
    ReviewReadingSprite:add()

    gfx.sprite.update()
end

Someone on the discord had an issue with grid cells not having the expected size and iirc the cause was that the drawCell function passed in the cell’s width and height based on what’s viewable on screen, not based on its cell size. Not sure if that’s the case here

1 Like

I tried searching through the discord and couldn't find the mention to that situation. I admit that I'm probably searching incorrectly since I'm not finding much of anything with my searches haha.

So I tried breaking down what you mentioned to see if I could understand it better.

I changed the initialization height to 20 so it would be smaller than the height in the drawInRect call (26) and that seems to have resolved that. So making them the same height is just a weird interaction.

Now, I'm seeing something else, so it appears there were actually two different issues at play.

When scrolling, the cells show at different heights. The first cell is perfectly positioned, but the rest of the list except for the end are a pixel higher. The last entry in the list is a pixel lower than the first.

So, I'm going to guess this interaction is based on what is being displayed on the screen since I have the scrollCellsToCenter set to true? So would that be the height/width values provided in the drawInRect action?

Just answered my own question with further tweaking of numbers haha.

So yes, for my case, I needed to set the height of the drawInRect to 24 and now they are all perfectly steady when scrolling.

Thank you Gamma, as always, for helping and stimulating thoughts to get me to a solution!

1 Like

I also tried this morning and couldn’t find it, discord search sucks.

But I searched again and found it with “has:image wrap”! It was from @fletchmakes

IMG_2228
Happy to help, glad you found a solution!

1 Like