-
Notifications
You must be signed in to change notification settings - Fork 38
/
dialog.go
84 lines (69 loc) · 1.91 KB
/
dialog.go
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
package quickswitcher
import (
"context"
"github.com/diamondburned/gotk4-adwaita/pkg/adw"
"github.com/diamondburned/gotk4/pkg/gdk/v4"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/diamondburned/gotkit/app"
"github.com/diamondburned/gotkit/gtkutil/cssutil"
)
// Dialog is a Quick Switcher dialog.
type Dialog struct {
*adw.ApplicationWindow
QuickSwitcher *QuickSwitcher
}
// ShowDialog shows a new Quick Switcher dialog.
func ShowDialog(ctx context.Context) {
d := NewDialog(ctx)
d.Show()
}
var dialogCSS = cssutil.Applier("quickswitcher-dialog", "")
// NewDialog creates a new Quick Switcher dialog.
func NewDialog(ctx context.Context) *Dialog {
qs := NewQuickSwitcher(ctx)
qs.Box.Remove(qs.search) // jank
qs.search.SetHExpand(true)
win := app.GTKWindowFromContext(ctx)
app := app.FromContext(ctx)
header := adw.NewHeaderBar()
header.SetTitleWidget(qs.search)
toolbarView := adw.NewToolbarView()
toolbarView.SetTopBarStyle(adw.ToolbarFlat)
toolbarView.AddTopBar(header)
toolbarView.SetContent(qs)
d := Dialog{QuickSwitcher: qs}
d.ApplicationWindow = adw.NewApplicationWindow(app.Application)
d.SetTransientFor(win)
d.SetDefaultSize(375, 275)
d.SetModal(true)
d.SetDestroyWithParent(true)
d.SetTitle(app.SuffixedTitle("Quick Switcher"))
d.SetContent(toolbarView)
d.ConnectShow(func() {
qs.Clear()
qs.search.GrabFocus()
})
dialogCSS(d)
// SetDestroyWithParent doesn't work for some reason, so we have to manually
// destroy the QuickSwitcher on transient window destroy.
win.ConnectCloseRequest(func() bool {
d.Destroy()
return false
})
qs.ConnectChosen(func() {
d.Close()
})
esc := gtk.NewEventControllerKey()
esc.SetName("dialog-escape")
esc.ConnectKeyPressed(func(val, _ uint, state gdk.ModifierType) bool {
switch val {
case gdk.KEY_Escape:
d.Close()
return true
}
return false
})
qs.search.SetKeyCaptureWidget(d)
qs.search.AddController(esc)
return &d
}