Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lf-araujo opened this issue Aug 19, 2017 · 10 comments
Open

Comments

@lf-araujo
Copy link

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)
}
@lf-araujo
Copy link
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?

@TomasLinhart
Copy link
Owner

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

@TomasLinhart
Copy link
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. 😄

@lf-araujo
Copy link
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?

@lf-araujo
Copy link
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)
}

@TomasLinhart
Copy link
Owner

I see I will look into it.

@lf-araujo
Copy link
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.

@lf-araujo
Copy link
Author

lf-araujo commented Sep 2, 2017

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.

@lf-araujo
Copy link
Author

lf-araujo commented Sep 2, 2017

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.

@lf-araujo
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants