commit eada435

delthas  ·  2024-07-17 10:54:23 +0000 UTC
parent c1a3108
Only consider messages read when the terminal is focused

Thanks to terminal focus events.

Fixes: https://todo.sr.ht/~delthas/senpai/166
3 files changed,  +38, -10
M app.go
M app.go
+12, -6
 1@@ -263,10 +263,12 @@ func (app *App) eventLoop() {
 2 		}
 3 
 4 		if !app.pasting {
 5-			if netID, buffer, timestamp := app.win.UpdateRead(); buffer != "" {
 6-				s := app.sessions[netID]
 7-				if s != nil {
 8-					s.ReadSet(buffer, timestamp)
 9+			if app.win.Focused() {
10+				if netID, buffer, timestamp := app.win.UpdateRead(); buffer != "" {
11+					s := app.sessions[netID]
12+					if s != nil {
13+						s.ReadSet(buffer, timestamp)
14+					}
15 				}
16 			}
17 			app.maybeRequestHistory()
18@@ -493,6 +495,10 @@ func (app *App) handleUIEvent(ev interface{}) bool {
19 		app.handleMouseEvent(ev)
20 	case vaxis.Key:
21 		app.handleKeyEvent(ev)
22+	case vaxis.FocusIn:
23+		app.win.SetFocused(true)
24+	case vaxis.FocusOut:
25+		app.win.SetFocused(false)
26 	case *ui.NotifyEvent:
27 		app.win.JumpBufferNetwork(ev.NetID, ev.Buffer)
28 	case statusLine:
29@@ -1127,7 +1133,7 @@ func (app *App) handleIRCEvent(netID string, ev interface{}) {
30 		app.win.AddLine(netID, buffer, line)
31 		if line.Notify == ui.NotifyHighlight {
32 			curNetID, curBuffer := app.win.CurrentBuffer()
33-			current := curNetID == netID && curBuffer == buffer
34+			current := app.win.Focused() && curNetID == netID && curBuffer == buffer
35 			app.notifyHighlight(buffer, ev.User, line.Body.String(), current)
36 		}
37 		if !s.IsChannel(msg.Params[0]) && !s.IsMe(ev.User) {
38@@ -1340,7 +1346,7 @@ func (app *App) isHighlight(s *irc.Session, content string) bool {
39 // notifyHighlight executes the script at "on-highlight-path" according to the given
40 // message context.
41 func (app *App) notifyHighlight(buffer, nick, content string, current bool) {
42-	if app.cfg.OnHighlightBeep {
43+	if !current && app.cfg.OnHighlightBeep {
44 		app.win.Beep()
45 	}
46 
+16, -3
 1@@ -229,6 +229,7 @@ type BufferList struct {
 2 	overlay *buffer
 3 	current int
 4 	clicked int
 5+	focused bool
 6 
 7 	tlInnerWidth int
 8 	tlHeight     int
 9@@ -245,6 +246,7 @@ func NewBufferList(ui *UI) BufferList {
10 		ui:      ui,
11 		list:    []buffer{},
12 		clicked: -1,
13+		focused: true,
14 	}
15 }
16 
17@@ -465,10 +467,10 @@ func (bs *BufferList) AddLine(netID, title string, line Line) {
18 		}
19 	}
20 
21-	if line.Notify != NotifyNone && b != current {
22+	if line.Notify != NotifyNone && (!bs.focused || b != current) {
23 		b.unread = true
24 	}
25-	if line.Notify == NotifyHighlight && b != current {
26+	if line.Notify == NotifyHighlight && (!bs.focused || b != current) {
27 		b.highlights++
28 	}
29 	if b == current && b.unreadSkip == optionalUnset && len(b.lines) > 0 {
30@@ -485,7 +487,7 @@ func (bs *BufferList) AddLines(netID, title string, before, after []Line) {
31 	if b == nil {
32 		return
33 	}
34-	updateRead := b != bs.cur() && !b.read.IsZero()
35+	updateRead := (!bs.focused || b != bs.cur()) && !b.read.IsZero()
36 
37 	lines := make([]Line, 0, len(before)+len(b.lines)+len(after))
38 	for _, buf := range []*[]Line{&before, &b.lines, &after} {
39@@ -525,6 +527,17 @@ func (bs *BufferList) AddLines(netID, title string, before, after []Line) {
40 	}
41 }
42 
43+func (bs *BufferList) Focused() bool {
44+	return bs.focused
45+}
46+
47+func (bs *BufferList) SetFocused(focused bool) {
48+	bs.focused = focused
49+	if focused {
50+		bs.clearRead(bs.current)
51+	}
52+}
53+
54 func (bs *BufferList) SetTopic(netID, title string, topic string) {
55 	_, b := bs.at(netID, title)
56 	if b == nil {
+10, -1
 1@@ -369,7 +369,8 @@ func (ui *UI) AddLine(netID, buffer string, line Line) {
 2 
 3 	curNetID, curBuffer := ui.bs.Current()
 4 	_, b := ui.bs.at(netID, buffer)
 5-	if b != nil && line.Notify == NotifyHighlight && (curNetID != netID || curBuffer != buffer) {
 6+	focused := ui.bs.focused && curNetID == netID && curBuffer == buffer
 7+	if b != nil && line.Notify == NotifyHighlight && !focused {
 8 		var header string
 9 		if buffer != line.Head {
10 			header = fmt.Sprintf("%s — %s", buffer, line.Head)
11@@ -432,6 +433,14 @@ func (ui *UI) JumpBufferNetwork(netID, buffer string) bool {
12 	return false
13 }
14 
15+func (ui *UI) Focused() bool {
16+	return ui.bs.Focused()
17+}
18+
19+func (ui *UI) SetFocused(focused bool) {
20+	ui.bs.SetFocused(focused)
21+}
22+
23 func (ui *UI) SetTopic(netID, buffer string, topic string) {
24 	ui.bs.SetTopic(netID, buffer, topic)
25 }