~f4814n/frost

ref: 1eee8169fcf6bda919ba018c6742f7b2d5268f15 frost/view/roomlist/roomlist.go -rw-r--r-- 5.4 KiB
1eee8169Fabian Geiselhart platform: Colorful logs on linux 9 months ago
                                                                                
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package roomlist

import (
	"fmt"
	// "image"
	"unicode"

	"go.uber.org/zap"
	// "golang.org/x/exp/shiny/materialdesign/icons"

	"gioui.org/gesture"
	"gioui.org/layout"
	"gioui.org/text"
	"gioui.org/unit"
	// "gioui.org/widget"
	"gioui.org/widget/material"
	"git.sr.ht/~f4814n/frost"
	"git.sr.ht/~f4814n/frost/util"
	fwidget "git.sr.ht/~f4814n/frost/widget"
	"git.sr.ht/~f4814n/matrix"
	"git.sr.ht/~whereswaldon/materials"
	"golang.org/x/exp/shiny/materialdesign/icons"
)

type (
	g = layout.Context
	d = layout.Dimensions
)

type view struct {
	rx, tx chan frost.Event

	matrixEvents chan matrix.Event

	cli      *matrix.Client
	roomList *RoomList

	theme *material.Theme

	appBar *materials.AppBar

	logger *zap.Logger
}

func New(cli *matrix.Client, modalLayer *materials.ModalLayer, theme *material.Theme, logger *zap.Logger) frost.View {
	appBar := materials.NewAppBar(modalLayer)
	appBar.Title = "Frost"
	appBar.NavigationIcon = util.MustIcon(icons.NavigationMenu)

	return &view{
		cli:          cli,
		matrixEvents: make(chan matrix.Event, 100),
		roomList:     NewRoomList(logger, theme),
		theme:        theme,
		logger:       logger,
		appBar:       appBar,
	}
}

func (l *view) Run(rx, tx chan frost.Event) {
	l.logger.Info("Starting")

	l.rx, l.tx = rx, tx
	l.cli.Notify(l.matrixEvents)
}

func (l *view) Layout(gtx layout.Context) layout.Dimensions {
	l.update(gtx)

	return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
		layout.Rigid(fwidget.LayoutTheme(l.appBar.Layout, l.theme)),
		layout.Rigid(l.roomList.Layout),
	)
}

func (l *view) update(gtx g) {
	for _, room := range l.roomList.rooms {
		for _, e := range room.click.Events(gtx) {
			if e.Type == gesture.TypeClick {
				l.tx <- frost.ShowRoomEvent{
					Room: room.room,
				}
			}
		}
	}

	for _, event := range l.appBar.Events(gtx) {
		switch event.(type) {
		case materials.AppBarNavigationClicked:
			l.tx <- frost.ToggleNavigation{}
		}
	}

	for {
		select {
		case <-l.rx:
		case event := <-l.matrixEvents:
			l.roomList.NewEvent(event)
		default:
			return
		}
	}
}

func (l *view) Stop() {
}

// RoomList is a widget that displays a list of rooms
type RoomList struct {
	list   *layout.List
	logger *zap.Logger
	theme  *material.Theme
	rooms  []roomListElement
}

func NewRoomList(logger *zap.Logger, theme *material.Theme) *RoomList {
	return &RoomList{
		logger: logger,
		theme:  theme,
		list: &layout.List{
			Axis: layout.Vertical,
		},
	}
}

// NewEvent updates the room list when a new event arrives
func (w *RoomList) NewEvent(event matrix.Event) {
	var room matrix.Room

	switch event := event.(type) {
	case matrix.RoomEvent:
		room = event.Room
	case matrix.StateEvent:
		room = event.Room
	default:
		return
	}

	for i, elem := range w.rooms {
		if elem.room == room {
			w.logger.Debug("Adding room",
				zap.String("room", room.Displayname()),
			)
			w.rooms = append(w.rooms[:i], w.rooms[i+1:]...)
			break
		}
	}
	w.rooms = append([]roomListElement{NewRoomListElement(room, event, w.theme)}, w.rooms...)
}

// Layout implements the layout.Widget interface
func (w *RoomList) Layout(gtx g) d {
	return w.list.Layout(gtx, len(w.rooms), func(gtx g, index int) d {
		return w.rooms[index].Layout(gtx)
	})
}

// roomListElement is a element in the room list
type roomListElement struct {
	theme     *material.Theme
	room      matrix.Room
	lastEvent matrix.Event
	click     *gesture.Click
}

func NewRoomListElement(room matrix.Room, event matrix.Event, theme *material.Theme) roomListElement {
	return roomListElement{
		room:      room,
		lastEvent: event,
		theme:     theme,
		click:     new(gesture.Click),
	}
}

func (w *roomListElement) Layout(gtx g) d {
	return fwidget.IconListElement{Click: w.click, Icon: w.layoutIcon, Content: func(gtx g) d {
		return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
			layout.Rigid(w.layoutUpper),
			layout.Rigid(w.layoutLastEvent),
		)
	}}.Layout(gtx)
}

func (w *roomListElement) layoutIcon(gtx g) d {
	var initial string
	for _, c := range w.room.Displayname() {
		initial = string(unicode.ToUpper(c))
		break
	}

	return fwidget.InitialSign{Initial: initial, Theme: w.theme}.Layout(gtx)
}

func (w *roomListElement) layoutUpper(gtx g) d {
	bgtextcol := util.NRGB(0xbbbbbb)
	return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Baseline}.Layout(gtx,
		layout.Rigid(func(gtx g) d {
			lbl := material.H6(w.theme, w.room.Displayname())
			lbl.MaxLines = 1
			return lbl.Layout(gtx)
		}),
		layout.Flexed(1, func(gtx g) d {
			gtx.Constraints.Min.X = gtx.Constraints.Max.X
			in := layout.Inset{Left: unit.Dp(2)}
			return in.Layout(gtx, func(gtx g) d {
				lbl := material.Caption(w.theme, util.FormatTime(util.EventTime(w.lastEvent)))
				lbl.MaxLines = 1
				lbl.Color = bgtextcol
				lbl.Alignment = text.End
				return lbl.Layout(gtx)
			})
		}),
	)
}

func (w *roomListElement) layoutLastEvent(gtx g) d {
	bgtextcol := util.NRGB(0xbbbbbb)
	in := layout.Inset{Top: unit.Dp(6)}
	return in.Layout(gtx, func(gtx g) d {
		lbl := material.Body2(w.theme, formatEvent(w.lastEvent))
		lbl.Color = bgtextcol
		lbl.MaxLines = 1
		return lbl.Layout(gtx)
	})
}

func formatEvent(event matrix.Event) string {
	switch event := event.(type) {
	case matrix.RoomEvent:
		switch event.Type {
		case "m.room.message":
			if event.Content["msgtype"] == "m.text" {
				return fmt.Sprintf("%s: %s", event.Sender.Displayname(), event.Content["body"])
			}
			return fmt.Sprintf("Unsupported message type: %s", event.Content["msgtype"])
		}
	case matrix.StateEvent:
	}

	return fmt.Sprintf("%#v", event)
}