about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJelle Besseling <jelle@pingiun.com>2019-08-04 16:09:48 +0200
committerDrew DeVault <sir@cmpwn.com>2019-08-07 15:10:24 +0900
commit507c90537c66289633af4b26abddcfd50a2a29d0 (patch)
tree10ac19bd89b0911ade88c6999d3c37544e6058a3
parent3650b72ca64b9fb6a7e7b058ad6ef67894c2866d (diff)
downloadaerc-507c90537c66289633af4b26abddcfd50a2a29d0.tar.gz
Implement next-message in msgview using account
This makes sure that the next-message command accepts the same arguments
in the account view and the msgview
-rw-r--r--commands/account/next.go48
-rw-r--r--commands/msgview/next.go17
2 files changed, 37 insertions, 28 deletions
diff --git a/commands/account/next.go b/commands/account/next.go
index f306b48..7b1f230 100644
--- a/commands/account/next.go
+++ b/commands/account/next.go
@@ -24,8 +24,20 @@ func (_ NextPrevMsg) Complete(aerc *widgets.Aerc, args []string) []string {
 }
 
 func (_ NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error {
+	var err, n, pct = ParseNextPrevMessage(args)
+	if err != nil {
+		return err
+	}
+	acct := aerc.SelectedAccount()
+	if acct == nil {
+		return errors.New("No account selected")
+	}
+	return ExecuteNextPrevMessage(args, acct, pct, n)
+}
+
+func ParseNextPrevMessage(args []string) (error, int, bool) {
 	if len(args) > 2 {
-		return nextPrevMessageUsage(args[0])
+		return nextPrevMessageUsage(args[0]), 0, false
 	}
 	var (
 		n   int = 1
@@ -39,30 +51,28 @@ func (_ NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error {
 		}
 		n, err = strconv.Atoi(args[1])
 		if err != nil {
-			return nextPrevMessageUsage(args[0])
+			return nextPrevMessageUsage(args[0]), 0, false
 		}
 	}
-	acct := aerc.SelectedAccount()
-	if acct == nil {
-		return errors.New("No account selected")
-	}
+	return nil, n, pct
+}
+
+func ExecuteNextPrevMessage(args []string, acct *widgets.AccountView, pct bool, n int) error {
 	if pct {
 		n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0))
 	}
-	for ; n > 0; n-- {
-		if args[0] == "prev-message" || args[0] == "prev" {
-			store := acct.Store()
-			if store != nil {
-				store.Prev()
-			}
-			acct.Messages().Scroll()
-		} else {
-			store := acct.Store()
-			if store != nil {
-				store.Next()
-			}
-			acct.Messages().Scroll()
+	if args[0] == "prev-message" || args[0] == "prev" {
+		store := acct.Store()
+		if store != nil {
+			store.NextPrev(-n)
+		}
+		acct.Messages().Scroll()
+	} else {
+		store := acct.Store()
+		if store != nil {
+			store.NextPrev(n)
 		}
+		acct.Messages().Scroll()
 	}
 	return nil
 }
diff --git a/commands/msgview/next.go b/commands/msgview/next.go
index 82fb12f..647aafb 100644
--- a/commands/msgview/next.go
+++ b/commands/msgview/next.go
@@ -1,8 +1,7 @@
 package msgview
 
 import (
-	"errors"
-
+	"git.sr.ht/~sircmpwn/aerc/commands/account"
 	"git.sr.ht/~sircmpwn/aerc/widgets"
 )
 
@@ -21,16 +20,16 @@ func (_ NextPrevMsg) Complete(aerc *widgets.Aerc, args []string) []string {
 }
 
 func (_ NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error {
+	err, n, pct := account.ParseNextPrevMessage(args)
+	if err != nil {
+		return err
+	}
 	mv, _ := aerc.SelectedTab().(*widgets.MessageViewer)
 	acct := mv.SelectedAccount()
 	store := mv.Store()
-	if acct == nil {
-		return errors.New("No account selected")
-	}
-	if args[0] == "prev-message" || args[0] == "prev" {
-		store.Prev()
-	} else {
-		store.Next()
+	err = account.ExecuteNextPrevMessage(args, acct, pct, n)
+	if err != nil {
+		return err
 	}
 	nextMsg := store.Selected()
 	if nextMsg == nil {
DeVault <sir@cmpwn.com> 2018-02-17 15:21:31 -0500 Add grid rendering, probably' href='/akspecs/aerc/commit/ui/grid.go?h=0.5.1&id=1892d73161a006182d7ef467e2bfc03c11587cb6'>1892d73 ^
8c8c21f ^
335db04 ^
8c8c21f ^

60b351b ^















f0791d4 ^









a0c2b1c ^
5feb7de ^


a0c2b1c ^






1892d73 ^




5feb7de ^



4675648 ^
335db04 ^

1892d73 ^

60b351b ^

1892d73 ^



1892d73 ^
60b351b ^



1892d73 ^








f0791d4 ^
1892d73 ^

60b351b ^
f0791d4 ^



60b351b ^
f0791d4 ^
1892d73 ^


f0791d4 ^





60b351b ^

1892d73 ^
60b351b ^
1892d73 ^


f0791d4 ^

1892d73 ^


60b351b ^
1892d73 ^
5685a17 ^
60b351b ^



5feb7de ^
4675648 ^
60b351b ^

5feb7de ^
8c8c21f ^

60b351b ^




60b351b ^
5feb7de ^
4675648 ^
5feb7de ^
8c8c21f ^
335db04 ^
60b351b ^

8c8c21f ^


5feb7de ^
4675648 ^
8c8c21f ^
4675648 ^
8c8c21f ^


5feb7de ^
60b351b ^
8c8c21f ^



5feb7de ^
4675648 ^
8c8c21f ^




5feb7de ^
8c8c21f ^


335db04 ^
5685a17 ^
8c8c21f ^
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

          


              
              
                     
 

                  
                     



                                 
                         



                             







                                                 
                      
                                           
                    


                                                                                   



                                          
                        

                  


                      



                   
                        
                                    

 















                                                      









                                                  
                                         


                                  






                                                     




                                      



                                  
                                         

                                                         

                                

                                                                                 



                                   
                                          



                                          








                                                             
                                                                            

                           
                             



                                                                
                                             
                                                   


                           





                                                                            

                                                                   
                         
                                             


                                                           

                                                            


                            
                                      
                           
                               



                                
                          
                                         

                                         
                            

 




                                                        
         
                         
                                             
                           
                                                       
                                

                               


                                               
                         
                                          
                                  
                                                                                


                             
                           
                               



                                                      
                          
                                        




                                             
                            


                                                                         
                                
                               
 
package ui

import (
	"fmt"
	"math"
	"sync"
	"sync/atomic"
)

type Grid struct {
	Invalidatable
	rows         []GridSpec
	rowLayout    []gridLayout
	columns      []GridSpec
	columnLayout []gridLayout
	invalid      bool

	// Protected by mutex
	cells []*GridCell
	mutex sync.RWMutex
}

const (
	SIZE_EXACT  = iota
	SIZE_WEIGHT = iota
)

// Specifies the layout of a single row or column
type GridSpec struct {
	// One of SIZE_EXACT or SIZE_WEIGHT
	Strategy int
	// If Strategy = SIZE_EXACT, this is the number of cells this row/col shall
	// occupy. If SIZE_WEIGHT, the space left after all exact rows/cols are
	// measured is distributed amonst the remainder weighted by this value.
	Size int
}

// Used to cache layout of each row/column
type gridLayout struct {
	Offset int
	Size   int
}

type GridCell struct {
	Row     int
	Column  int
	RowSpan int
	ColSpan int
	Content Drawable
	invalid atomic.Value // bool
}

func NewGrid() *Grid {
	return &Grid{invalid: true}
}

func (cell *GridCell) At(row, col int) *GridCell {
	cell.Row = row
	cell.Column = col
	return cell
}

func (cell *GridCell) Span(rows, cols int) *GridCell {
	cell.RowSpan = rows
	cell.ColSpan = cols
	return cell
}

func (grid *Grid) Rows(spec []GridSpec) *Grid {
	grid.rows = spec
	return grid
}

func (grid *Grid) Columns(spec []GridSpec) *Grid {
	grid.columns = spec
	return grid
}

func (grid *Grid) Children() []Drawable {
	grid.mutex.RLock()
	defer grid.mutex.RUnlock()

	children := make([]Drawable, len(grid.cells))
	for i, cell := range grid.cells {
		children[i] = cell.Content
	}
	return children
}

func (grid *Grid) Draw(ctx *Context) {
	invalid := grid.invalid
	if invalid {
		grid.reflow(ctx)
	}

	grid.mutex.RLock()
	defer grid.mutex.RUnlock()

	for _, cell := range grid.cells {
		cellInvalid := cell.invalid.Load().(bool)
		if !cellInvalid && !invalid {
			continue
		}
		rows := grid.rowLayout[cell.Row : cell.Row+cell.RowSpan]
		cols := grid.columnLayout[cell.Column : cell.Column+cell.ColSpan]
		x := cols[0].Offset
		y := rows[0].Offset
		width := 0
		height := 0
		for _, col := range cols {
			width += col.Size
		}
		for _, row := range rows {
			height += row.Size
		}
		subctx := ctx.Subcontext(x, y, width, height)
		cell.Content.Draw(subctx)
	}
}

func (grid *Grid) reflow(ctx *Context) {
	grid.rowLayout = nil
	grid.columnLayout = nil
	flow := func(specs *[]GridSpec, layouts *[]gridLayout, extent int) {
		exact := 0
		weight := 0
		nweights := 0
		for _, spec := range *specs {
			if spec.Strategy == SIZE_EXACT {
				exact += spec.Size
			} else if spec.Strategy == SIZE_WEIGHT {
				nweights += 1
				weight += spec.Size
			}
		}
		offset := 0
		for _, spec := range *specs {
			layout := gridLayout{Offset: offset}
			if spec.Strategy == SIZE_EXACT {
				layout.Size = spec.Size
			} else if spec.Strategy == SIZE_WEIGHT {
				size := float64(spec.Size) / float64(weight)
				size *= float64(extent - exact)
				layout.Size = int(math.Floor(size))
			}
			offset += layout.Size
			*layouts = append(*layouts, layout)
		}
	}
	flow(&grid.rows, &grid.rowLayout, ctx.Height())
	flow(&grid.columns, &grid.columnLayout, ctx.Width())
	grid.invalid = false
}

func (grid *Grid) invalidateLayout() {
	grid.invalid = true
	grid.DoInvalidate(grid)
}

func (grid *Grid) Invalidate() {
	grid.invalidateLayout()
	grid.mutex.RLock()
	for _, cell := range grid.cells {
		cell.Content.Invalidate()
	}
	grid.mutex.RUnlock()
}

func (grid *Grid) AddChild(content Drawable) *GridCell {
	cell := &GridCell{
		RowSpan: 1,
		ColSpan: 1,
		Content: content,
	}
	grid.mutex.Lock()
	grid.cells = append(grid.cells, cell)
	grid.mutex.Unlock()
	cell.Content.OnInvalidate(grid.cellInvalidated)
	cell.invalid.Store(true)
	grid.invalidateLayout()
	return cell
}

func (grid *Grid) RemoveChild(cell *GridCell) {
	grid.mutex.Lock()
	for i, _cell := range grid.cells {
		if _cell == cell {
			grid.cells = append(grid.cells[:i], grid.cells[i+1:]...)
			break
		}
	}
	grid.mutex.Unlock()
	grid.invalidateLayout()
}

func (grid *Grid) cellInvalidated(drawable Drawable) {
	var cell *GridCell
	grid.mutex.RLock()
	for _, cell = range grid.cells {
		if cell.Content == drawable {
			break
		}
		cell = nil
	}
	grid.mutex.RUnlock()
	if cell == nil {
		panic(fmt.Errorf("Attempted to invalidate unknown cell"))
	}
	cell.invalid.Store(true)
	grid.DoInvalidate(grid)
}