You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Help] How to use headerbar? How Gtk classes are translated into swift? #12
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)
}
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?
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.
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 , 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.
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!
Activity
lf-araujo commentedon Aug 19, 2017
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 commentedon Aug 19, 2017
I am away from a computer for a few days. Once I am back I will give you some information.
TomasLinhart commentedon Aug 21, 2017
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.
I hope, it helps. 😄
lf-araujo commentedon Aug 22, 2017
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 commentedon Aug 31, 2017
I have found a problem in the new code, the error is (I switched to Ubuntu):
The code I used was:
TomasLinhart commentedon Aug 31, 2017
I see I will look into it.
lf-araujo commentedon Sep 1, 2017
@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 commentedon 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, andHeaderBar. 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 commentedon Sep 2, 2017
Found the solution for that too, the contents of Packages.swift:
Source.
lf-araujo commentedon Apr 8, 2018
Dear Tomas,
You should probably re-release the version with HeaderBar, the sourcefile is not in the assets for 0.3.1.
Best,
Luis