Skip to content

Commit 6014d2a

Browse files
committed
feat: Port ErrorDialog to use subclassing
Signed-off-by: Felicitas Pojtinger <[email protected]>
1 parent 061dbb1 commit 6014d2a

File tree

8 files changed

+95
-19
lines changed

8 files changed

+95
-19
lines changed

assets/resources/error.blp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using Gtk 4.0;
22
using Adw 1;
33

4-
Adw.AlertDialog error-dialog {
4+
template $ErrorDialog: Adw.AlertDialog {
55
heading: _("A Fatal Error Occured");
6-
default-response: "save";
7-
close-response: "report";
6+
default-response: "close";
7+
close-response: "close";
88

99
responses [
1010
report: _("Report Error"),
Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,59 @@
11
package components
22

33
import (
4-
. "github.com/pojntfx/go-gettext/pkg/i18n"
5-
64
"context"
75
"os"
6+
"runtime"
7+
"unsafe"
8+
9+
. "github.com/pojntfx/go-gettext/pkg/i18n"
810

911
"github.com/jwijenbergh/puregotk/v4/adw"
12+
"github.com/jwijenbergh/puregotk/v4/glib"
13+
"github.com/jwijenbergh/puregotk/v4/gobject"
1014
"github.com/jwijenbergh/puregotk/v4/gtk"
1115
"github.com/pojntfx/multiplex/assets/resources"
1216
"github.com/rs/zerolog/log"
1317
"github.com/rymdport/portal/openuri"
1418
)
1519

20+
var (
21+
gTypeErrorDialog gobject.Type
22+
)
23+
1624
const (
1725
issuesURL = "https://github.com/pojntfx/multiplex/issues"
1826
)
1927

28+
type ErrorDialog struct {
29+
adw.AlertDialog
30+
31+
responseCallback func(response string)
32+
}
33+
34+
func NewErrorDialog() ErrorDialog {
35+
obj := gobject.NewObject(gTypeErrorDialog, "css-name")
36+
37+
var v ErrorDialog
38+
obj.Cast(&v)
39+
40+
return v
41+
}
42+
43+
func (e *ErrorDialog) SetResponseCallback(callback func(response string)) {
44+
errD := (*ErrorDialog)(unsafe.Pointer(e.GetData(dataKeyGoInstance)))
45+
errD.responseCallback = callback
46+
}
47+
2048
func OpenErrorDialog(ctx context.Context, window *adw.ApplicationWindow, err error) {
2149
log.Error().
2250
Err(err).
2351
Msg(L("Could not continue due to a fatal error"))
2452

25-
errorBuilder := gtk.NewBuilderFromResource(resources.ResourceErrorPath)
26-
defer errorBuilder.Unref()
27-
var errorDialog adw.AlertDialog
28-
errorBuilder.GetObject("error-dialog").Cast(&errorDialog)
29-
defer errorDialog.Unref()
30-
53+
errorDialog := NewErrorDialog()
3154
errorDialog.SetBody(err.Error())
3255

33-
responseCallback := func(dialog adw.AlertDialog, response string) {
56+
responseCallback := func(response string) {
3457
switch response {
3558
case "report":
3659
_ = openuri.OpenURI("", issuesURL, nil)
@@ -45,7 +68,60 @@ func OpenErrorDialog(ctx context.Context, window *adw.ApplicationWindow, err err
4568
os.Exit(1)
4669
}
4770
}
48-
errorDialog.ConnectResponse(&responseCallback)
71+
errorDialog.SetResponseCallback(responseCallback)
4972

5073
errorDialog.Present(&window.Widget)
5174
}
75+
76+
func init() {
77+
var classInit gobject.ClassInitFunc = func(tc *gobject.TypeClass, u uintptr) {
78+
typeClass := (*gtk.WidgetClass)(unsafe.Pointer(tc))
79+
typeClass.SetTemplateFromResource(resources.ResourceErrorPath)
80+
81+
objClass := (*gobject.ObjectClass)(unsafe.Pointer(tc))
82+
83+
objClass.OverrideConstructed(func(o *gobject.Object) {
84+
parentObjClass := (*gobject.ObjectClass)(unsafe.Pointer(tc.PeekParent()))
85+
parentObjClass.GetConstructed()(o)
86+
87+
var parent adw.AlertDialog
88+
o.Cast(&parent)
89+
90+
parent.InitTemplate()
91+
92+
e := &ErrorDialog{
93+
AlertDialog: parent,
94+
}
95+
96+
responseCallback := func(dialog adw.AlertDialog, response string) {
97+
if e.responseCallback != nil {
98+
e.responseCallback(response)
99+
}
100+
}
101+
parent.ConnectResponse(&responseCallback)
102+
103+
var pinner runtime.Pinner
104+
pinner.Pin(e)
105+
106+
var cleanupCallback glib.DestroyNotify = func(data uintptr) {
107+
pinner.Unpin()
108+
}
109+
o.SetDataFull(dataKeyGoInstance, uintptr(unsafe.Pointer(e)), &cleanupCallback)
110+
})
111+
}
112+
113+
var instanceInit gobject.InstanceInitFunc = func(ti *gobject.TypeInstance, tc *gobject.TypeClass) {}
114+
115+
var parentQuery gobject.TypeQuery
116+
gobject.NewTypeQuery(adw.AlertDialogGLibType(), &parentQuery)
117+
118+
gTypeErrorDialog = gobject.TypeRegisterStaticSimple(
119+
parentQuery.Type,
120+
"ErrorDialog",
121+
parentQuery.ClassSize,
122+
&classInit,
123+
parentQuery.InstanceSize+uint(unsafe.Sizeof(ErrorDialog{}))+uint(unsafe.Sizeof(&ErrorDialog{}))+uint(unsafe.Sizeof(adw.AlertDialog{})), // TODO: Figure out why we need the extra `adw.AlertDialog` here
124+
&instanceInit,
125+
0,
126+
)
127+
}

po/de/LC_MESSAGES/default.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ msgstr "Unbenannte Spur"
8181
msgid "Manually added"
8282
msgstr "Manuell hinzugefügt"
8383

84-
#: ../internal/components/error_dialog.go:23
84+
#: ../internal/components/error_dialog.go:51
8585
msgid "Could not continue due to a fatal error"
8686
msgstr ""
8787
"Aufgrund eines schwerwiegenden Fehlers konnte nicht fortgefahren werden"

po/default.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ msgstr ""
7575
msgid "Manually added"
7676
msgstr ""
7777

78-
#: ../internal/components/error_dialog.go:23
78+
#: ../internal/components/error_dialog.go:51
7979
msgid "Could not continue due to a fatal error"
8080
msgstr ""
8181

po/en_GB/LC_MESSAGES/default.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ msgstr ""
8181
msgid "Manually added"
8282
msgstr ""
8383

84-
#: ../internal/components/error_dialog.go:23
84+
#: ../internal/components/error_dialog.go:51
8585
msgid "Could not continue due to a fatal error"
8686
msgstr ""
8787

po/en_US/LC_MESSAGES/default.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ msgstr ""
8181
msgid "Manually added"
8282
msgstr ""
8383

84-
#: ../internal/components/error_dialog.go:23
84+
#: ../internal/components/error_dialog.go:51
8585
msgid "Could not continue due to a fatal error"
8686
msgstr ""
8787

po/fr/LC_MESSAGES/default.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ msgstr ""
8181
msgid "Manually added"
8282
msgstr ""
8383

84-
#: ../internal/components/error_dialog.go:23
84+
#: ../internal/components/error_dialog.go:51
8585
msgid "Could not continue due to a fatal error"
8686
msgstr ""
8787

po/fr_CA/LC_MESSAGES/default.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ msgstr ""
8181
msgid "Manually added"
8282
msgstr ""
8383

84-
#: ../internal/components/error_dialog.go:23
84+
#: ../internal/components/error_dialog.go:51
8585
msgid "Could not continue due to a fatal error"
8686
msgstr ""
8787

0 commit comments

Comments
 (0)