Update: Removing and re-adding the header bar as done in this code results in the warning
Gtk-WARNING **: gtk_window_set_titlebar() called on a realized window
This can safely be ignored, but if it bothers you can silance the warning, or, maybe better yet would be to create two header bars. One as the window title bar (which will be hidden when the window is fullscreen), and another that is packed at the top of the window, which will be shown only when the window is fulscreen. This seems to be the way it is done in Gedit, and while it means you will have two header bars for the window, only one is ever shown at once.

In addition to to the standard issue window controls, I wanted a fullscreen toggle button in the title bar of hazzy, so I sub-classed Gtk.HeaderBar and added one. I matched all the CSS style classed so it should render properly with different themes.

Results

Custom HeaderBar with Adwaita theme
Custom HeaderBar with dark theme

Code

transparent_window.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python

#  Copyright (c) 2017 Kurt Jacobson
#  License: https://kcj.mit-license.org/@2017

import os
import gi

gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')

from gi.repository import Gtk
from gi.repository import Gdk

# GtkHeaderBar source, useful for finding style class names etc.
#    https://git.gnome.org/browse/gtk+/tree/gtk/gtkheaderbar.c?h=3.22.19#n394

ICONSIZE = Gtk.IconSize.MENU

class HeaderBar(Gtk.HeaderBar):
    def __init__(self, window, *args, **kwargs):
        Gtk.HeaderBar.__init__(self, *args, **kwargs)

        self.window = window
        self.window_box = window.get_child()

        self.window_size = None
        self.window_position = None

        # Track window state
        self.window_fullscreen = False
        self.window_maximized = False
        self.window.connect('window-state-event', self.on_window_state_event)
        self.window.connect('map-event', self.on_map)

        GetIcon = Gtk.Image.new_from_icon_name

        # Close icon
        self.close_icon = GetIcon('window-close-symbolic', ICONSIZE)
        self.close_icon.show()

        # Maximize icon
        self.maximize_icon = GetIcon('window-maximize-symbolic', ICONSIZE)
        self.maximize_icon.show()

        # Restore (unaximize) icon
        self.restore_icon = GetIcon('window-restore-symbolic', ICONSIZE)
        self.restore_icon.show()

        # Minimize icon
        self.minimize_icon = GetIcon('window-minimize-symbolic', ICONSIZE)
        self.minimize_icon.show()

        # Fullscreen icon
        self.fullscreen_icon = GetIcon('view-fullscreen-symbolic', ICONSIZE)
        self.fullscreen_icon.show()

        # Unfullscreen icon
        self.unfullscreen_icon = GetIcon('view-restore-symbolic', ICONSIZE)
        self.unfullscreen_icon.show()

        # Control button box, set style class to match that the default box
        self.button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        style = self.button_box.get_style_context()
        style.add_class('right')

        # Add to the right side of the titlebar
        self.pack_end(self.button_box)

        # Add the separator
        self.separator = Gtk.Separator(orientation=Gtk.Orientation.VERTICAL)
        self.button_box.pack_start(self.separator, False, False, 3)
        self.separator.get_style_context().add_class('titlebutton')

        # Fullscreen button
        self.fullscreen_btn = Gtk.Button()
        self.fullscreen_btn.add(self.fullscreen_icon)

        # Add same style classes as used by default controls
        self.fullscreen_btn.get_style_context().add_class('titlebutton')
        self.fullscreen_btn.get_style_context().add_class('fullscreen')
        self.button_box.pack_start(self.fullscreen_btn, False, False, 3)
        self.fullscreen_btn.connect('clicked', self.on_fullscreen_clicked)


        # Minimize button
        self.minimize_btn = Gtk.Button()
        self.minimize_btn.add(self.minimize_icon)

        # Add same style classes as used by default controls
        self.minimize_btn.get_style_context().add_class('titlebutton')
        self.minimize_btn.get_style_context().add_class('minimize')
        self.button_box.pack_start(self.minimize_btn, False, False, 3)
        self.minimize_btn.connect('clicked', self.on_minimize_clicked)


        # Maximize button
        self.maximize_btn = Gtk.Button()
        self.maximize_btn.add(self.maximize_icon)

        # Add same style classes as used by default controls
        self.maximize_btn.get_style_context().add_class('titlebutton')
        self.maximize_btn.get_style_context().add_class('maximize')
        self.button_box.pack_start(self.maximize_btn, False, False, 3)
        self.maximize_btn.connect('clicked', self.on_maximize_clicked)


        # Close button
        self.close_btn = Gtk.Button()
        self.close_btn.add(self.close_icon)

        # Add same style classes as used by default controls
        self.close_btn.get_style_context().add_class('titlebutton')
        self.close_btn.get_style_context().add_class('close')
        self.button_box.pack_start(self.close_btn, False, False, 3)
        self.close_btn.connect('clicked', self.on_close_clicked)

    def on_map(self, widget, event):

        # Prevent error on start up
        if not self.window_size or not self.window_position:
            return

        # Restore size from before fullscreen
        w, h = self.window_size
        self.window.resize(w, h)

        # Restore position
        x, y = self.window_position
        self.window.move(x, y)

    def on_close_clicked(self, widget):
        self.window.close()

    def on_maximize_clicked(self, widget):
        if self.window_maximized:
            self.window.unmaximize()
        else:
            self.window.maximize()

    def on_minimize_clicked(self, widget):
        self.window.iconify()

    def on_fullscreen_clicked(self, widget):
        if self.window_fullscreen:
            self.window.unfullscreen()
        else:
            # Get the current size and post so can restore latter
            self.window_size = self.window.get_size()
            self.window_position = self.window.get_position()
            self.window.fullscreen()

    def on_maximized_state_changed(self, maximized):
        # Remove whatever icon is present
        image = self.maximize_btn.get_child()
        self.maximize_btn.remove(image)

        if maximized:
            # Change to the restore icon
            self.maximize_btn.add(self.restore_icon)
        else:
            # Change back to the maximize icon
            self.maximize_btn.add(self.maximize_icon)

    def on_fullscreen_state_changed(self, fullscreen):
        # Remove whatever icon is present
        image = self.fullscreen_btn.get_child()
        self.fullscreen_btn.remove(image)

        if fullscreen:
            # Remove the headerbar from the titlebar and add to top of window
            self.window.remove(self)
            self.window_box.pack_start(self, False, False, 0)

            # Will end up ad the bottom, so move to top
            self.window_box.reorder_child(self, 0)

            # Change to un-fullscreen icon
            self.fullscreen_btn.add(self.unfullscreen_icon)

            # Maximizing a fullscreen window does nothing, so hide the button
            self.maximize_btn.hide()

        else:

            # Remove headerbar from box and put back in titlebar
            self.window_box.remove(self)

            # Calling `set_titlebar` on a realized window causes this (harmless) warning
            #    Gtk-WARNING **: gtk_window_set_titlebar() called on a realized window
            self.window.set_titlebar(self)

            # Change the icon back
            self.fullscreen_btn.add(self.fullscreen_icon)

            # Re-show the maximize button
            self.maximize_btn.show()

    def on_window_state_event(self, widget, event):
        # Listen to state event and track window state
        if self.window_fullscreen != bool(event.new_window_state & Gdk.WindowState.FULLSCREEN):
            self.window_fullscreen = bool(event.new_window_state & Gdk.WindowState.FULLSCREEN)
            self.on_fullscreen_state_changed(self.window_fullscreen)
        if self.window_maximized != bool(event.new_window_state & Gdk.WindowState.MAXIMIZED):
            self.window_maximized = bool(event.new_window_state & Gdk.WindowState.MAXIMIZED)
            self.on_maximized_state_changed(self.window_maximized)


def main():
    window = Gtk.Window()

    box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
    window.add(box)

    title = "Custom HeaderBar"
    subtitle = "with fullscreen toggle button"
    header_bar = HeaderBar(window, title=title, subtitle=subtitle)
    window.set_titlebar(header_bar)

    window.set_default_size(600, 300)
    window.connect('destroy', Gtk.main_quit)

    window.show_all()
    Gtk.main()

if __name__ == "__main__":
main()

The example file can be downloaded from the GitHub Gist located here.