it seems ridiculous that we have to embed an entire browser, meant for internet web browsing, just to create a cross-platform UI with moderate ease.

Why are native or semi-native UI frameworks lagging so far behind? am I wrong in thinking this? are there easier, declarative frameworks for creating semi-native UIs on desktop that don’t look like windows 1998?

  • abhibeckert@lemmy.world
    link
    fedilink
    arrow-up
    7
    ·
    edit-2
    1 year ago

    Quartz (usually referred to as Core Graphics) isn’t recommended anymore on Macs.

    Developers should be using SwiftUI now, which is a completely different approach:

    class HelloWorldView: NSView {
        override func draw(_ dirtyRect: NSRect) {
            super.draw(dirtyRect)
    
            // Drawing code here.
            guard let context = NSGraphicsContext.current?.cgContext else { return }
    
            // Set text attributes
            let attributes: [NSAttributedString.Key: Any] = [
                .font: NSFont.systemFont(ofSize: 24),
                .foregroundColor: NSColor.black
            ]
    
            // Create the string
            let string = NSAttributedString(string: "Hello World", attributes: attributes)
    
            // Draw the string
            string.draw(at: CGPoint(x: 20, y: 20))
        }
    }
    

    Here’s the same thing with SwiftUI:

    struct HelloWorldView: View {
        var body: some View {
            Text("Hello World")
                .font(.system(size: 24))
                .foregroundColor(.black)
                .padding()
        }
    }
    
    • seeaya@lemmy.world
      link
      fedilink
      arrow-up
      6
      ·
      1 year ago

      Quartz is a layer beneath SwiftUI or AppKit. SwiftUI is still using Quartz under the hood. The way you use Quartz directly from SwiftUI vs AppKit is a bit different, though still fairly similar. A more fair comparison of the SwiftUI code would be:

      struct HelloWorldView: View {
        var body: some View {
          Canvas { context, _ in
            context.draw(
              Text("HelloWorld")
                .font(.system(size: 24))
                .foregroundColor(.black),
              at: CGPoint(x: 20, y: 20)
            )
          }
        }
      }
      

      Alternatively an AppKit solution (not using Quartz directly) would be something like:

      class HelloWorldView: NSView {
        override init(frame frameRect: NSRect) {
          super.init(frame: frameRect)
          let text = NSAttributedString(
            string: “Hello World”,
            attributes: [.font: NSFont.systemFont(ofSize: 24), .foregroundColor: NSColor.black]
          )
          let label = NSTextField(labelWithAttributedString: text)
          addSubview(label)
        }
      
        required init?(coder: NSCoder) {
          fatalError()
        }
      }
      

      In either AppKit or SwiftUI, you can access Quartz directly to implement custom views. However, most of the time the UI code you write in either SwiftUI or AppKit won’t call Quartz directly at all, but will instead be composed of built-in views, like (NS)TextField or (NS)Button. Under the hood, SwiftUI is mainly just using the AppKit components at the moment, but provides a significantly nicer way to use them (especially in regards to layout and data synchronization).