Skip to content

[Help] How to use headerbar? How Gtk classes are translated into swift? #12

Open

Description

@lf-araujo

Dear Tomas,

Thank you for this bindings! This is super useful.

I have a little experience in Vala, but on trying to use some elements from gtk I get errors. Specifically I am trying to use headerbars. How do this class translates into Swift? What am I doing wrong in the code below?

import SwiftGtk

let app = Application(applicationId: "com.example.application")

app.run { window in
    window.defaultSize = Size(width: 400, height: 400)
    window.resizable = true
    let headerbar = Headerbar()
    headerbar.set_title("My test")


    let button = Button(label: "Press Me")
    button.clicked = { _ in
        let newWindow = Window(windowType: .topLevel)
        newWindow.title = "Just a window"
        newWindow.defaultSize = Size(width: 200, height: 200)
        let labelPressed = Label(text: "Oh, you pressed the button.")
        newWindow.add(labelPressed)

        newWindow.showAll()
    }
    window.set_titlebar(headerbar)
    window.add(button)
}

Activity

  1. lf-araujo commented on Aug 19, 2017

    @lf-araujo
    Author

    A better inspection of the source code and I found that these classes are implemented in the Sources directory. How would you recommend to approach creating a new one? Where you get the API or the methods that one should implement in the class source? I suppose those are the C Gtk methods, right?

  2. TomasLinhart commented on Aug 19, 2017

    @TomasLinhart
    Owner

    I am away from a computer for a few days. Once I am back I will give you some information.

  3. TomasLinhart commented on Aug 21, 2017

    @TomasLinhart
    Owner

    So...

    First, you need to find documentation for API that you want to use. In your case it is https://developer.gnome.org/gtk3/stable/GtkHeaderBar.html Here you see all properties and methods that are available. To use them you need to create a new class and properties and methods for everything that you find the documentation.

    I went ahead and created the binding for you so you can see how you would do it,
    1801c74.

    And you can use it in the following way.

    let headerBar = HeaderBar()
    headerBar.title = "Hello World"
    headerBar.subtitle = "oh yea"
    headerBar.showCloseButton = true
    headerBar.spacing = 50
    
    let label = Label(text: "1")
    
    headerBar.packStart(child: label)
    headerBar.packStart(child: Label(text: "2"))
    
    headerBar.remove(label)
    
    window.titlebar = headerBar
    window.hideTitlebarWhenMaximized = true

    I hope, it helps. 😄

  4. lf-araujo commented on Aug 22, 2017

    @lf-araujo
    Author

    Thank you very much! I will contribute as soon as possible. Why don't you add this instruction into the README so others can get right at it?

  5. lf-araujo commented on Aug 31, 2017

    @lf-araujo
    Author

    I have found a problem in the new code, the error is (I switched to Ubuntu):

    /home/luis/Dropbox/Documentos/Coding/Swift/SwiftGtk/.build/checkouts/SwiftGtk--2571330441700638662/Sources/HeaderBar.swift:15:63: error: value of type 'UnsafePointer<gchar>' (aka 'UnsafePointer<Int8>') has no member 'toString'
            get { return gtk_header_bar_get_title(castedPointer())?.toString() }
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ ~~~~~~~~
    /home/luis/Dropbox/Documentos/Coding/Swift/SwiftGtk/.build/checkouts/SwiftGtk--2571330441700638662/Sources/HeaderBar.swift:20:66: error: value of type 'UnsafePointer<gchar>' (aka 'UnsafePointer<Int8>') has no member 'toString'
            get { return gtk_header_bar_get_subtitle(castedPointer())?.toString() }
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ ~~~~~~~~
    /home/luis/Dropbox/Documentos/Coding/Swift/SwiftGtk/.build/checkouts/SwiftGtk--2571330441700638662/Sources/HeaderBar.swift:41:75: error: value of type 'UnsafePointer<gchar>' (aka 'UnsafePointer<Int8>') has no member 'toString'
            get { return gtk_header_bar_get_decoration_layout(castedPointer())?.toString() }
    

    The code I used was:

    import SwiftGtk
    
    let app = Application(applicationId: "com.example.application")
    
    app.run { window in
        window.defaultSize = Size(width: 400, height: 400)
        window.resizable = true
        let headerBar = HeaderBar()
        headerBar.title = "Hello World"
        headerBar.subtitle = "oh yea"
        headerBar.showCloseButton = true
        headerBar.spacing = 50
    
        let label = Label(text: "1")
    
        headerBar.packStart(child: label)
        headerBar.packStart(child: Label(text: "2"))
    
        headerBar.remove(label)
    
        window.titlebar = headerBar
        window.hideTitlebarWhenMaximized = true
    
    
        let button = Button(label: "Press Me")
        button.clicked = { _ in
            let newWindow = Window(windowType: .topLevel)
            newWindow.title = "Just a window"
            newWindow.defaultSize = Size(width: 200, height: 200)
            let labelPressed = Label(text: "Oh, you pressed the button.")
            newWindow.add(labelPressed)
    
            newWindow.showAll()
        }
    
    
        window.add(button)
    }
    
  6. TomasLinhart commented on Aug 31, 2017

    @TomasLinhart
    Owner

    I see I will look into it.

  7. lf-araujo commented on Sep 1, 2017

    @lf-araujo
    Author

    @TomasLinhart , nevermind, for some reason swift builder is not pulling the new files. I added UnsafePointerGChar and Window.swift; the issue was resolved. Sorry for the bother.

  8. lf-araujo commented on Sep 2, 2017

    @lf-araujo
    Author

    Not related to your package, but I am doing something wrong. Perhaps @bmdelacruz can also help.

    Any changes to main.swift requires .build folder to be deleted and the addition to the new files Window, UnsafePointerGChar, and HeaderBar. How can I instruct Swift build to detect the changes into main.swift without having to start over every run? It is probably something easy, but I cant find it!

    Thank you.

  9. lf-araujo commented on Sep 2, 2017

    @lf-araujo
    Author

    Found the solution for that too, the contents of Packages.swift:

    import PackageDescription
    
    let package = Package(
        name: "SwiftGtkApplication",
        dependencies: [
            .Package(url: "https://github.com/TomasLinhart/SwiftGtk", majorVersion: 1)
        ]
    )
    

    Source.

  10. lf-araujo commented on Apr 8, 2018

    @lf-araujo
    Author

    Dear Tomas,

    You should probably re-release the version with HeaderBar, the sourcefile is not in the assets for 0.3.1.

    Best,

    Luis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions