News for hackers. Stuff that matters.
Re:SwiftUI for Mac 2024 by happytoexplain on Thursday August 22, 2024 @07:34AM (#41319071)
Swift UI makes simple UIs easier, moderately complex UIs harder, and very complex UIs impossible. The moment your UI is more complex than a stack of stacks, the elegance and comprehensibility falls apart compared to autolayout. It starts to feel like CSS all over again, using trial and error to try out combinations of elements and properties with unclear interactions.

The pattern it enforces is theoretically great, but you will get much more mileage out of learning to write in a "reactive" style manually (When state changes, update view to match. It doesn't need to literally be a function of state to make it a function of state. E.g. don't leave ifs without elses.)

A perfect combination would be a SwiftUI-style UI editor (sans bugs) to define UI, constraints included, but traditional layout management at runtime. Well, that or a ground-up redesign of SwiftUI's API.

Re:SwiftUI for Mac 2024 by rkangel on Thursday August 22, 2024 @07:56AM (#41319253)
I haven't used Swift UI, but I have used Elm, React (a bit), and Flutter (a fair amount). My understanding is that it's a similar model.

In those frameworks (particularly Flutter, which generally has less ceremony), I have found simple UIs easy, and complex UIs slightly harder to build, but then they're much more predictable (fewer surprising edge case bugs) and much much more maintainable.

Re:SwiftUI for Mac 2024 by yen223 on Thursday August 22, 2024 @08:25AM (#41319493)
The more I think about it, the more I think SwiftUI is inspired by Angular - two-way data binding and observables everywhere.

Two-way data binding makes for shorter demos, but it doesn't scale nearly as well as one-way data binding. I've found in SwiftUI the way state gets updated across components becomes very messy very quickly.

Jetpack Compose is definitely inspired by React, hooks and all. I much prefer working with Jetpack Compose for this reason.

Re:SwiftUI for Mac 2024 by mromanuk on Thursday August 22, 2024 @07:50AM (#41319201)
> Swift UI makes simple UIs easier, moderately complex UIs harder, and very complex UIs impossible. The moment your UI is more complex than a stack of stacks, the elegance and comprehensibly falls apart compared to autolayout. It starts to feel like CSS all over again, using trial and error to try out combinations of elements and properties with unclear interactions.

Sadly, this has been my experience. I built a moderately complex app, only to bump into more and more problems, and I find myself fighting the framework, building hacks, and implementing workarounds. When you venture into more advanced UIs, the leaky abstraction comes back to haunt you.

Re:SwiftUI for Mac 2024 by rockbruno on Thursday August 22, 2024 @07:50AM (#41319199)
Apple has historically not cared about mid-level companies and above, and SwiftUI is just another example of that. Everything they do is geared towards absolute beginners / very simple projects, even Xcode itself becomes basically useless once you reach a certain level of complexity. I don't understand why they do that.
Re:SwiftUI for Mac 2024 by ChrisMarshallNY on Thursday August 22, 2024 @08:32AM (#41319546)
> Apple has historically not cared about mid-level companies and above

I wouldn't say that. I've worked for a couple of marquee corporations that did a lot of work with Apple.

They do things like assign direct contacts to larger corps. They don't really rely on their "popular" culture docs to support bigger companies.

It's pretty natural to use their popular front as an "onboarding" system for smaller acts, with the plan to integrate them more tightly, if they get successful enough.

That said, their App Store is quite restrictive, and can be a big fat pain to deal with; even as a bigger corporation.

Quite doable, though. I've released over 20 apps to the iOS/Mac App stores, over the last dozen years.

Re:SwiftUI for Mac 2024 by iosguyryan on Thursday August 22, 2024 @12:06PM (#41321738)
Myopic take. Major tech companies adopted SwiftUI. Vision Pro is all SwiftUI and mixed declarative and imperative RealityKit.
Re:SwiftUI for Mac 2024 by rockbruno on Saturday August 31, 2024 @04:06AM (#41407266)
> Major tech companies adopted SwiftUI

Source? I haven't heard of any large company with complex projects fully adopting SwiftUI in their main product and not immediately regretting it. Successful adoption seems to be achieved only if the product is really simple or when using it in a very specific / isolated place of their main product (also simple). Everything else is UIKit because SwiftUI sucks at complicated logic.

Re:SwiftUI for Mac 2024 by Hamuko on Thursday August 22, 2024 @08:17AM (#41319426)
>Swift UI makes simple UIs easier, moderately complex UIs harder, and very complex UIs impossible.

Having tried to make a NavigationSplitView with multiple lists in the sidebar, I can fully agree with this sentiment.

Re:SwiftUI for Mac 2024 by robin_reala on Thursday August 22, 2024 @07:35AM (#41319077)
How is CSS like trial and error?
Re:SwiftUI for Mac 2024 by happytoexplain on Thursday August 22, 2024 @07:43AM (#41319141)
Because, compared to some modern layout systems, it takes longer (to put it lightly) to internalize the exact logic behind the implementation of all CSS properties and be able to simulate how they interact in complex UIs in your head.
Re:SwiftUI for Mac 2024 by mitemte on Thursday August 22, 2024 @07:55AM (#41319242)
Can you provide some examples of modern layout systems? I’d be curious to see what’s on offer.

I have experience with various iterations of UIKit’s auto layout, Yoga, as well as CSS and some Swift UI. I’ve found CSS to be considerably easier to work with, particularly when using flexbox and grid.

Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @08:06AM (#41319318)
Ok, then please implement the following in CSS:

   Outer
   +---------------------------------+
   |                                 |
   | Left        Middle        Right |
   |                                 |
   +---------------------------------+
Everything should be vertically centered. Left should stick to the "left" of the outer div, "middle" should be centered, "right" should stick to the right. Left/middle/right can have different widths and heights.

In AutoLayout this is achieved by 6 constraints:

- left.left = outer.left

- left.centerY = outer.centerY

- middle.centerX = outer.centerX

- middle.centerY = outer.centerY

- right.right = outer.right

- right.centerY = outer.centerY

I'd like to see how you implement it in CSS. (note: extra wrapper divs are not allowed)

Re:SwiftUI for Mac 2024 by qazxcvbnm on Thursday August 22, 2024 @08:20AM (#41319450)
Either put left, right as absolute (like the following) and leave middle in the flex, or do the opposite. One may decide which option to use based on whether the centre or the left/right is more semantically important in the layout.

We did have to use some CSS "tricks" (transform for absolute centring), but once you've learnt those then CSS is indeed quite sane.

  outer {
          flex-direction: row;
          justify-content: center;
          align-items: center; }
  left, right {
          position: absolute;
          top: 50%;
          transform: translateY(-50%); }
  left {
          left: 0; }
  right {
          right: 0; }
(This kind of layout, which is common in eg top nav bars, is slightly deceptive. robin_reala's solution does not conform to requirements that left and right can be different widths, which would cause middle to be not centred under simple justify-content: space-between.)
Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @09:57AM (#41320319)
Yes, this is the correct solution.

Which underlines the original point, that CSS is a hodgepodge of different ideas with no overarching system behind them. A simple task like this needed 3 different positioning systems:

1. flexbox for positioning the middle item

2. absolute positioning for left/right

3. and the translateY(-50%) trick for vertically positioning the absolute positioned items

Re:SwiftUI for Mac 2024 by qazxcvbnm on Thursday August 22, 2024 @11:34AM (#41321441)
Touché. Indeed, CSS forces one to recognise layouts that may not conform to a layout 'flow' in boxes.

In its defence, I suppose recognising this is important, as to make potential box overlaps explicit (which is typically unwanted).

CSS also handles a whole lot of things other than layout, which I'm not well informed of layout constraint systems being able to handle. Being a hodgepodge is an advantage, comparable to how its a great advantage that one can invoke any hodgepodge systems task from bash, as one single tool.

Re:SwiftUI for Mac 2024 by robin_reala on Thursday August 22, 2024 @08:19AM (#41319447)
Assuming your div has a class of outer it’s:

.outer { display: flex; justify-content: space-between; align-items: center; }

…and that’s it. (This is assuming your items are of equal width of course. It gets a little bit more complicated if not.)

Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @09:33AM (#41320099)
I've literally told you the items can be of arbitrary width and height. `space-between` won't keep the middle item centered.
Re:SwiftUI for Mac 2024 by mitemte on Thursday August 22, 2024 @08:55AM (#41319728)
Maybe something like this, which relies solely on flex box for layout.

    .outer { 
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .left {
      flex: 1;
      display: flex;
      justify-content: flex-start;
    }

    .middle {
      flex: 1;
      display: flex;
      justify-content: center;
    }

    .right {
      flex: 1;
      display: flex;
      justify-content: flex-end;
    }
Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @09:42AM (#41320184)
Wrong. `flex: 1` will cause the items to have a max-width of 1/3rd the container width. E.g. if the left and right items are small icons, and the middle one is a longer text, then the text will be forced to wrap into multiple lines as soon as it tries to grow beyond 1/3rd of the available space, even though there's tons of wasted space on the left/right.
Re:SwiftUI for Mac 2024 by emadda on Thursday August 22, 2024 @09:12AM (#41319877)
.parent { display: grid; grid-template-columns: 50px 200px 80px; justify-content: space-between; align-items: center; }

An interesting thing you can do with grid is align the tracks.

I wrote about this here:

https://cssprinciples.com/3/grid/#justify-and-align

Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @09:36AM (#41320131)
No. You're hardcoding the item widths in `grid-template-columns`. The left/middle/right items should be self-sizing. E.g. consider three divs with arbitrary text content.
Re:SwiftUI for Mac 2024 by emadda on Thursday August 22, 2024 @09:42AM (#41320180)
Grid allows minmax(a, b) to specify the bounds of tracks. Possible values can be the remaining free space in the parent (fr), or the size of the content in the child.

There is also fit-content:

E.g: grid-template-columns: fit-content(150px) …

Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @09:51AM (#41320263)
Sorry, but I don't understand what you're describing. Can you maybe show it in jsfiddle?
Re:SwiftUI for Mac 2024 by emadda on Thursday August 22, 2024 @09:54AM (#41320291)
I am just saying the columns of the grid can be dynamically sized depending on their contents.

Tracks is a grid term for rows or columns, as it works in both dimensions.

Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @10:03AM (#41320387)
I'd like you to demonstrate your approach with this jsfiddle: https://jsfiddle.net/nbdowrcm/

As you can see, the middle element is not centered.

Re:SwiftUI for Mac 2024 by emadda on Thursday August 22, 2024 @01:13PM (#41322322)
I see.

https://jsfiddle.net/Lah5g3vb/

Changes:

- each col is now 1fr (3 cols, 33% each)

- the child elements align inside the col using `justify-self: start | center | end`

Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @04:44PM (#41324249)
Not good, because now the elements are limited to 33% width of the container.

See: https://jsfiddle.net/e0un5Lxk/

The middle content is unnecessarily wrapped into two lines, even though there's plenty of space available.

Re:SwiftUI for Mac 2024 by emadda on Thursday August 22, 2024 @06:27PM (#41324836)
You can also use `minmax(min-content, max-content)` for the center col: (https://jsfiddle.net/y193p2b6/).

But this does not keep the center col exactly centered, but it does allow the content to take the free white space and then wrap when needed.

In your original post with Swift, what happens?

I am not familiar with auto layout so I do not know the exact behaviour vs CSS. Do you have a few screen shots of how it would handle different sized content?

Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @07:46PM (#41325203)
But now you just added `whitespace: no-wrap` to the middle element, with `overflow: visible`, to force it into a single line. That's hardly a generic solution, because it makes assumptions about the contained items... i.e. applying `whitespace: no-wrap` and `overflow: visible` will typically break the appearance of things.

Regarding AutoLayout: the six constraints from my original comment, don't say anything about the relationship between the left/middle/right items, so they will simply overlap if there isn't enough space. This can be remedied by adding two extra constraints:

1. left.right < middle.left

2. middle.right < right.left

It's just a bunch of equations. No weird tricks, no special cases, no hacks. You describe what you want with equations, and the solver spits out the solution.

Re:SwiftUI for Mac 2024 by emadda on Thursday August 22, 2024 @08:00PM (#41325254)
Yeh, I edited my reply, I realised what you meant afterwards.

Auto layout does sound neat.

Re:SwiftUI for Mac 2024 by emadda on Friday August 23, 2024 @08:31AM (#41328440)
Do you have an email? I am thinking of doing a blog post comparing css/auto layout, and would like your input?
Re:SwiftUI for Mac 2024 by meindnoch on Sunday August 25, 2024 @02:59AM (#41345069)
Re:SwiftUI for Mac 2024 by megaman821 on Thursday August 22, 2024 @10:29AM (#41320684)
In this specific example, I am pretty sure margin-right: auto on left and margin-left: auto on right in a flex-box does what you want.
Re:SwiftUI for Mac 2024 by meindnoch on Thursday August 22, 2024 @11:07AM (#41321151)
Wrong. The middle item will be misaligned from the center if "left" and "right" have different widths: https://jsfiddle.net/hxe8pcuf/
Re:SwiftUI for Mac 2024 by lylejantzi3rd on Thursday August 22, 2024 @11:44AM (#41321540)
Does anybody know how you'd solve this in tex (not latex, tex) or in some other constraints-based layout system like Cassowary?
Re:SwiftUI for Mac 2024 by pax_americana on Friday August 23, 2024 @01:24AM (#41326392)
Chrome just added CSS Anchor Positioning this year. The other browsers also expressed interest in implementing it, with Firefox having announced an Intent to Implement. The feature would likely be available on all major browsers sometime next year.

So here's how your example would work using it:

.outer { width: 100%; height: 400px; background: aliceblue; anchor-name: --outer; }

.left { width: 100px; height: 100px; background: white; position: absolute; position-anchor: --outer; left: anchor(left); align-self: anchor-center; }

.center { width: 150px; height: 200px; background: pink; position: absolute; position-anchor: --outer; justify-self: anchor-center; align-self: anchor-center; }

.right { width: 160px; height: 150px; background: gray; position: absolute; position-anchor: --outer; right: anchor(right); align-self: anchor-center; }

Source: https://developer.chrome.com/blog/anchor-positioning-api

Re:SwiftUI for Mac 2024 by meindnoch on Monday August 26, 2024 @08:43AM (#41356582)
Cool! Is it going to be the sixth or the seventh positioning system in CSS?
Re:SwiftUI for Mac 2024 by jitl on Thursday August 22, 2024 @08:48AM (#41319680)
I’m a lot happier with UIKit/AppKit plus SwiftUI than with just SwiftUI alone.

I wrote a couple of small sized apps trying to use only SwiftUI “the right way” to learn the framework and as you say whenever I wanted to do something specific with a lot of control I’d hit a wall with SwiftUI and have to go crazy with wild work arounds and trial-error throw spaghetti at the wall or just give up.

Now I’m writing another app and making heavy use of NSViewRepresentable use AppKit from SwiftUI, and NSHostingView to embed SwiftUI in AppKit views. I’m using mostly SeiftUI at the “leafs” of the view hierarchy, and AppKit towards the root; all my windows use a NSWindow subclass and/or a view controller.

Much happier with this!

Re:SwiftUI for Mac 2024 by JKCalhoun on Thursday August 22, 2024 @09:15AM (#41319907)
I like what you're describing, but still wonder if good ol' auto-layout for the leaves would have been equivalent and just as simple.
Re:SwiftUI for Mac 2024 by jitl on Thursday August 22, 2024 @12:16PM (#41321838)
I find creative visual styling of views like drawing paths/shapes, drop shadows, quick stacks/rows, grid panels, etc all much easier and faster with SwiftUI and SwiftUI #Preview.
Re:SwiftUI for Mac 2024 by Cthulhu_ on Friday August 23, 2024 @03:48AM (#41326987)
I suppose it's a compromise; the lower you go, the more control you have.

I used to work on an app where we used the UI builder at the time (I forgot the name) at first, but my colleague asserted that building the UI in code resulted in a faster app, because (according to him) parsing and rendering the XML output took longer than interpreting the code. I don't know if he was right, but a while later Apple introduced devices with different sizes (iirc the iphone 5 which was a little taller and the ipad) and doing all the layouting became impossible.

Anyway when I rejoined later it was a (I believe) healthy mix of the UI builder tool and custom code for some graphic / coded elements (think a fancy progress indicator). I suspect it'll be the same for SwiftUI.