This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
preview.go
133 lines (101 loc) · 2.54 KB
/
preview.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
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
package extras
import (
"fmt"
"path"
"strings"
"github.com/diamondburned/gtkcord3/gtkcord/cache"
"github.com/diamondburned/gtkcord3/gtkcord/components/window"
"github.com/diamondburned/gtkcord3/gtkcord/gtkutils"
"github.com/diamondburned/gtkcord3/gtkcord/semaphore"
"github.com/diamondburned/gtkcord3/internal/log"
"github.com/diamondburned/handy"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"github.com/pkg/errors"
)
type PreviewDialog struct {
*handy.Dialog
Content *gtk.Box
Image *gtk.Image
OpenOriginal *gtk.Button
ImageView *gtk.ScrolledWindow
Proxy string
URL string
}
func SpawnPreviewDialog(proxy, imageURL string) {
// Trim the forms
proxy = strings.Split(proxy, "?")[0]
// Main dialog
w := window.Window.GetAllocatedWidth()
h := window.Window.GetAllocatedHeight()
w = int(float64(w) * 0.85)
h = int(float64(h) * 0.8)
d := handy.DialogNew(window.Window)
if !d.GetNarrow() {
d.SetDefaultSize(w, h)
}
// Hack for close button
d.Connect("response", func(_ *glib.Object, resp gtk.ResponseType) {
if resp == gtk.RESPONSE_DELETE_EVENT {
d.Hide()
d.Destroy()
}
})
// Header
header, _ := gtk.HeaderBarNew()
header.SetShowCloseButton(true)
bOriginal, _ := gtk.ButtonNewFromIconName(
"image-x-generic-symbolic",
gtk.ICON_SIZE_LARGE_TOOLBAR,
)
bOriginal.SetTooltipText("Open Original")
bOriginal.SetMarginStart(10)
bOriginal.SetHAlign(gtk.ALIGN_START)
header.PackStart(bOriginal)
header.SetTitle(path.Base(imageURL))
d.SetTitlebar(header)
// Content box: image
i, err := gtk.ImageNewFromIconName("image-loading", gtk.ICON_SIZE_DIALOG)
if err != nil {
log.Panicln("Icon image-loading not found:", err)
}
s, _ := gtk.ScrolledWindowNew(nil, nil)
s.Add(i)
s.SetVExpand(true)
c, _ := d.GetContentArea()
d.Remove(c)
d.Add(s)
d.ShowAll()
pd := PreviewDialog{
Dialog: d,
Content: c,
Image: i,
OpenOriginal: bOriginal,
ImageView: s,
Proxy: proxy,
URL: imageURL,
}
bOriginal.Connect("clicked", func() {
gtkutils.OpenURI(pd.URL)
})
// Calculate the sizee so that the image is just slightly (80%) smaller:
// w = w * 8 / 10
h = h * 9 / 10
go pd.Fetch(w, h)
d.Run()
}
func (pd *PreviewDialog) Fetch(w, h int) {
err := cache.SetImageAsync(pd.Proxy, pd.Image, w, h)
if err == nil {
return
}
err = errors.Wrap(err, "Failed to download the image")
log.Errorln(err)
errText := fmt.Sprintf(`<span color="red">%s</span>`, err)
semaphore.IdleMust(func() {
l, _ := gtk.LabelNew("")
l.SetMarkup(errText)
pd.Content.Remove(pd.Image)
pd.Content.Add(l)
})
}