Categories
Software

SwiftUI Being a bit Quirky

The Ecstasy and the Agony of Making Software

I recently wanted to create some content that would sometimes display in an HStack and sometimes in a VStack. The content was the same in both cases, so I wanted to avoid doing:

if goVertical {
    VStack {
        thing1
        thing2
    }
} else {
    HStack {
        thing1
        thing2
    }
}

Fortunately, I was able to use a ViewBuilder to contain the contents of the stacks.

@ViewBuilder var stackContents: some View {
    thing1
    thing2
}

...

if goVertical {
    VStack {
        contents
            .padding(.horizontal)
    }
} else {
    HStack {
        contents
    }
}

So far so good. (Aside: Am I the only one who didn’t know @ViewBuilder can be used to return a list of SwiftUI views?!) However I eventually got myself into a bit of trouble with my stack contents in an interesting way. Here are the details.

First you need to know that thing1 is an image and thing 2 is a VStack of sliders, steppers and such. Here’s how the view looks in portrait and landscape.

BUT… since I will be adding more sliders, steppers and such, I moved them into a ScrollView. But (as you can see on the right) when I did this, The ScrollView took more height than it needed and short changed the Image.

My first thought was in use a layoutPriority modifier on the image, but this had no effect, so I removed it. Next, I tried adding background modifiers to the VStack contents, and that proved to be very illuminating. (see below)

Here is the code with the background modifiers:

        VStack {
            contents
                .background(.red)
                .padding(.horizontal)
                .background(.blue)
        }
        .background(.yellow)

Applying horizontal padding to contents did not apply it once, it (presumably) iterated through all the items in contents and applied the padding to each item in contents.

I moved the padding (and background modifiers to apply to the VStack, instead of contents

        VStack {
            contents
        }
        .background(.red)
        .padding(.horizontal)
        .background(.blue)

Great, this looks more like what I was expecting with the padding. But the image is still narrower.

So I re-added the layoutPriority modifier to the image, and that did the trick. I still don’t understand why the ScrollView ‘steals’ vertical space from the Image. (And maybe one day I’ll better understand what causes me to anthropomorphize SW modules like layout engines.)

The interesting thing for me in this issue was how doing something incorrectly (ie applying the horizontal padding to the contents of the VStack, rather than to the VStack) worked correctly at first, but when the butterfly flapped its wings (ie one of the elements in contents was wrapped in a ScrollView) the wheels fell off.

Encountering and eventually overcoming this type of problem is a big part of the ecstasy and the agony of writing software.

Categories
Haiku

Haiku for an Old House

Tearing down the house
Big yellow excavator
Eating bite by bite

One house to the west
A new foundation in place
Waiting for the top

Gonna miss the house
Next door. Small quaint and frumpy
Great people in there

Categories
Haiku

Rubiks Cube Haiku

Turn the yellow face
Now white is messed up. Next time
Unpeel the stickers

Categories
Haiku Uncategorized

Random Full Time Employment Haiku

You and me belong
To the Sun. Everyone does,
But you more than most.

Marge thought through laughing
Ivy is more circumspect
Syllables matter

Categories
Books

Ice and Death Haiku

Squishy sidewalk ice
Slippery as hell, very hard
Glued to the concrete

Not near our house
Not my problem, even though we
Would all fall quickly

I get the mattock
To attack it, whack by whack
Big chunks break apart

Kick the chunks and slush
Off to the sides, clear the path
Unslippery again

Admiring my work
Every time I go that way
Walking is easy

What if I am gone?
This clear patch of path could be
My big legacy

Categories
Haiku

Winter Haiku

A spruce Christmas tree
Throwing shimmering warm light
Guarding wrapped gifts

Below zero days
Turn the lake to glass, ready
For many skaters

Poinsettia sitting
By the window, watching the
Cold walkers cruise by

Weather report says
Mixed rain and snow. One snowflake
Gives reason to hope

Categories
Haiku

HodgePodge Haiku

Eating pineapple
Listening to gentle waves
Lapping at the shore

It’s below zero
The sun blinds in the blue sky
I must wear my toque

Sometimes I’m afraid
But I seldom know what of
I’m nervous Nellie

Categories
Haiku

In the Moment Haiku

Light rain falling now
So cold, it might turn to snow
Bring on a blizzard

Dislocating shoulder
Painful when out, then tender
Wish it would stay in

Walking recharges
It’s good for your soul and lungs
and also the soles

Categories
Haiku

Water Haiku

Calm water at dawn
The canoe glides on the lake
A duck loudly quacks

Just before midnight
The swimmers arrive and undress
Cold water loud shouts

The long sandy beach
Pounded by waves. One person
contemplates the scene

Scorching midday heat
Nowhere to hide, except in
The green mountain lake

Categories
Happy

Proposal: Emotions as Verbs

Who do I talk to about this? Is there an Office For the Classification of English Words? Maybe a form to fill out? Or just a free form petition?

I am somebody for whom emotions do not come easily. I am loathe to say ‘I am angry.’ Hmm, upon reflection that is a massive understatement. I am incapable of saying (and meaning) ‘I am angry.’

I am Canadian. I am subject to the laws of gravity. I don’t expect either of these to things to change in the foreseeable future. Am and being come with ominous permanence that scares me. Once I say I am angry, I will be an Angry Canadian forever attracted to the planet Earth.

On the other hand I ran to the store suggests an action that has a beginning and an end. It has both intentionality and impermanence.

I realize there are a few potential holes in my thesis.

I feel angry solves the permanence problem. But in the heat of the moment most people are going to keep things simple and basic.

Bob: I’m angry!
Sally: Don’t you mean you feel angry?
Bob: NO! I AM ANGRY!

Many emotion and feeling words are already verbs. Very true, all the more reason to convert all of them! Interestingly, there are many emotion words that have already been verbed, in the opposite direction. Anger being a prime example. Consider the sentence Bob angers Steve. Bob is undertaking some action that is causing Steve to feel the emotion anger. This is crazy! It’s hard enough to control our own emotions, but our language gives us the power to control the emotions of others? wow!

Put another way, anger is a transitive verb; it requires an object. Who or what did you anger? What about making it an intransitive verb. I anger, therefore I am?

Using intransitive verbs for emotions would cause a couple of changes. First it would treat emotional states as things with beginnings and ends. The sun shines from sunrise until sunset. It also provides a sense of ownership.

Sally: Why did you punch a hole in the wall?
Bob: I was angry.

There is no ownership. Some mysterious state of anger descended on Bob and took control of him. Note the difference between: I drank vs I was drunk. One incorporates responsibility, the other abdicates responsibility.

Here is a table with many emotion words divided into three groups. One group includes emotions that already have the a handy verb version. The next group contains the emotion words that don’t have a verb version. The final group lists all the emotions that have a transitive verb form, and are used to impose emotional states on other people or things.

VerbedEnvious, fearful, raging, critical, resentment, hate, mournful, weepy, doubtful, hesitant, decisive, tense, marvellous, love
UnverbedLivid, mad, furious, bitter, sorrow, dejected, desperate, (un)certain, shy, (un)sure, pessimistic, indecisive, lost, glad, content, joyful, grateful, optimistic, loved, confident, sure, unique, certain, bold, tenacious, ambition,
BackwardsAngered, annoyed, irritated, agitated, disgusted, outraged, upset, crushed, depressed, frustrated, perplexed, embarrassed, pleased, amused, delighted, charmed, empowered
Common emotion words grouped by their flavours of verbness

Another flaw in my argument is examples like I am hungry. The verb to be is not inherently permanent. While this is true, I think the amount of permanence is tied to the specific adjective. We all agree that hunger is transient. Perhaps there is a sliding scale of this permanence, but to me I feel a certain open endedness with angry that I don’t feel with hungry.

Lastly, I would be remiss if I talked about emotions as verbs if I didn’t give a shout out to the classic couples help book Love is a Verb. (also apparently a movie and a song. who knew?)