forked from richardwilkes/unison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
59 lines (52 loc) · 1.58 KB
/
group.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
// Copyright ©2021-2022 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import "github.com/richardwilkes/toolbox/collection/slice"
// Group is used to ensure only one panel in a group is selected at a time.
type Group struct {
panel []*GroupPanel
}
// NewGroup creates a new group for the specified set of panels. Each panel is removed from any other group it may be in
// and placed in the newly created one.
func NewGroup(panel ...*GroupPanel) *Group {
sg := &Group{panel: panel}
for _, one := range panel {
sg.Add(one)
}
return sg
}
// Add a panel to the group, removing it from any group it may have previously been associated with.
func (sg *Group) Add(panel *GroupPanel) {
if panel.group != nil {
panel.group.Remove(panel)
}
panel.group = sg
sg.panel = append(sg.panel, panel)
}
// Remove a panel from the group.
func (sg *Group) Remove(panel *GroupPanel) {
if sg == panel.group {
for i, one := range sg.panel {
if !one.Is(panel) {
continue
}
sg.panel = slice.ZeroedDelete(sg.panel, i, i+1)
panel.group = nil
break
}
}
}
// Select a panel, deselecting all others in the group.
func (sg *Group) Select(panel *GroupPanel) {
if panel.group == sg {
for _, one := range sg.panel {
one.setSelected(one.Is(panel))
}
}
}