-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
109 lines (86 loc) · 2.68 KB
/
list.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
package service
import (
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
"github.com/diamondburned/cchat-gtk/internal/ui/primitives/drag"
"github.com/diamondburned/cchat-gtk/internal/ui/service/session"
"github.com/diamondburned/cchat-gtk/internal/ui/service/session/server"
"github.com/gotk3/gotk3/gtk"
)
type ViewController interface {
ClearMessenger(*session.Row)
MessengerSelected(*session.Row, *server.ServerRow)
SessionSelected(*Service, *session.Row)
AuthenticateSession(*List, *Service)
OnSessionRemove(*Service, *session.Row)
OnSessionDisconnect(*Service, *session.Row)
}
// List is a list of services. Each service is a revealer that contains
// sessions.
type List struct {
*gtk.ScrolledWindow
// same methods as ListController
ViewController
ListBox *gtk.Box
Services []*Service // TODO: collision check
}
var _ ListController = (*List)(nil)
var listCSS = primitives.PrepareClassCSS("service-list", `
.service-list {
padding: 0;
background-color: @theme_bg_color;
/* background-color: mix(@theme_bg_color, @theme_fg_color, 0.03); */
}
`)
func NewList(vctl ViewController) *List {
svlist := &List{ViewController: vctl}
// List box of buttons.
svlist.ListBox, _ = gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
svlist.ListBox.Show()
svlist.ListBox.SetHAlign(gtk.ALIGN_START)
listCSS(svlist.ListBox)
svlist.ScrolledWindow, _ = gtk.ScrolledWindowNew(nil, nil)
svlist.ScrolledWindow.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_EXTERNAL)
svlist.ScrolledWindow.SetHExpand(false)
svlist.ScrolledWindow.Add(svlist.ListBox)
return svlist
}
func (sl *List) SetSizeRequest(w, h int) {
sl.ScrolledWindow.SetSizeRequest(w, h)
sl.ListBox.SetSizeRequest(w, h)
}
func (sl *List) AuthenticateSession(svc *Service) {
sl.ViewController.AuthenticateSession(sl, svc)
}
func (sl *List) OnSessionRemove(svc *Service, row *session.Row) {
sl.ViewController.OnSessionRemove(svc, row)
}
func (sl *List) OnSessionDisconnect(svc *Service, row *session.Row) {
sl.ViewController.OnSessionDisconnect(svc, row)
}
func (sl *List) AddService(svc cchat.Service) {
row := NewService(svc, sl)
row.Show()
sl.ListBox.Add(row)
sl.Services = append(sl.Services, row)
// Try and restore all sessions.
row.restoreAll()
// TODO: drag-and-drop?
}
func (sl *List) MoveService(targetID, movingID string) {
// Find the widgets.
var movingsv *Service
for _, svc := range sl.Services {
if svc.ID() == movingID {
movingsv = svc
}
}
// Not found, return.
if movingsv == nil {
return
}
// Get the location of where to move the widget to.
var targetix = drag.Find(sl.ListBox, targetID)
// Actually move the child.
sl.ListBox.ReorderChild(movingsv, targetix)
}