Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NotificatinsBox.go « uiWidgets - github.com/Z-Bolt/OctoScreen.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 802c2d0464dc86a5efae0dadfde00c4fbe2abf61 (plain)
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
package uiWidgets

import (
	"fmt"
	"time"

	"github.com/gotk3/gotk3/gdk"
	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
	"github.com/sirupsen/logrus"
	"github.com/Z-Bolt/OctoScreen/utils"
)

type NotificationsBox struct {
	*gtk.Box
}

func NewNotificationsBox() *NotificationsBox {
	base := utils.MustBox(gtk.ORIENTATION_VERTICAL, 5)
	base.SetVAlign(gtk.ALIGN_START)
	base.SetHAlign(gtk.ALIGN_CENTER)
	base.SetHExpand(true)

	instance := &NotificationsBox {
		Box: base,
	}
	notificationsHook := NewNotificationsHook(instance)
	logrus.AddHook(notificationsHook)

	return instance
}

func (this *NotificationsBox) Show(style, msg string, durration time.Duration) {
	defer this.Box.ShowAll()

	eventBox := this.newEventBox(style, msg)
	this.Box.Add(eventBox)

	go func() {
		time.Sleep(durration)
		glib.IdleAdd(eventBox.Destroy)
	}()
}

func (this *NotificationsBox) newEventBox(style, msg string) *gtk.EventBox {
	label := utils.MustLabel("")
	label.SetMarkup(fmt.Sprintf("<b>%s</b>", msg))
	label.SetLineWrap(true)

	ctx, _ := label.GetStyleContext()
	ctx.AddClass("notification")
	ctx.AddClass(style)

	eventBox, _ := gtk.EventBoxNew()
	eventBox.Add(label)
	eventBox.SetEvents(int(gdk.BUTTON_PRESS_MASK))
	eventBox.Connect("button-press-event", eventBox.Destroy)

	return eventBox
}