-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabpagelist.go
146 lines (116 loc) · 2.84 KB
/
tabpagelist.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
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
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"github.com/lxn/win"
)
type tabPageListObserver interface {
onInsertingPage(index int, page *TabPage) error
onInsertedPage(index int, page *TabPage) error
onRemovingPage(index int, page *TabPage) error
onRemovedPage(index int, page *TabPage) error
onClearingPages(pages []*TabPage) error
onClearedPages(pages []*TabPage) error
}
type TabPageList struct {
items []*TabPage
observer tabPageListObserver
}
func newTabPageList(observer tabPageListObserver) *TabPageList {
return &TabPageList{observer: observer}
}
func (l *TabPageList) Add(item *TabPage) error {
return l.Insert(len(l.items), item)
}
func (l *TabPageList) At(index int) *TabPage {
return l.items[index]
}
func (l *TabPageList) Clear() error {
observer := l.observer
if observer != nil {
if err := observer.onClearingPages(l.items); err != nil {
return err
}
}
oldItems := l.items
l.items = l.items[:0]
if observer != nil {
if err := observer.onClearedPages(oldItems); err != nil {
l.items = oldItems
return err
}
}
return nil
}
func (l *TabPageList) Index(item *TabPage) int {
for i, lvi := range l.items {
if lvi == item {
return i
}
}
return -1
}
func (l *TabPageList) Contains(item *TabPage) bool {
return l.Index(item) > -1
}
func (l *TabPageList) indexHandle(handle win.HWND) int {
for i, page := range l.items {
if page.Handle() == handle {
return i
}
}
return -1
}
func (l *TabPageList) containsHandle(handle win.HWND) bool {
return l.indexHandle(handle) > -1
}
func (l *TabPageList) insertIntoSlice(index int, item *TabPage) {
l.items = append(l.items, nil)
copy(l.items[index+1:], l.items[index:])
l.items[index] = item
}
func (l *TabPageList) Insert(index int, item *TabPage) error {
observer := l.observer
if observer != nil {
if err := observer.onInsertingPage(index, item); err != nil {
return err
}
}
l.insertIntoSlice(index, item)
if observer != nil {
if err := observer.onInsertedPage(index, item); err != nil {
l.items = append(l.items[:index], l.items[index+1:]...)
return err
}
}
return nil
}
func (l *TabPageList) Len() int {
return len(l.items)
}
func (l *TabPageList) Remove(item *TabPage) error {
index := l.Index(item)
if index == -1 {
return nil
}
return l.RemoveAt(index)
}
func (l *TabPageList) RemoveAt(index int) error {
observer := l.observer
item := l.items[index]
if observer != nil {
if err := observer.onRemovingPage(index, item); err != nil {
return err
}
}
l.items = append(l.items[:index], l.items[index+1:]...)
if observer != nil {
if err := observer.onRemovedPage(index, item); err != nil {
l.insertIntoSlice(index, item)
return err
}
}
return nil
}