11package components
22
33import (
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+
1624const (
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+
2048func 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+ }
0 commit comments