1
1
using System . ComponentModel ;
2
2
using Myra . Graphics2D . UI . Styles ;
3
3
using System ;
4
- using System . Collections . ObjectModel ;
5
4
using System . Xml . Serialization ;
5
+ using Microsoft . Xna . Framework ;
6
6
7
7
namespace Myra . Graphics2D . UI
8
8
{
@@ -19,13 +19,15 @@ public class TabControl : Selector<Grid, TabItem>
19
19
private Grid _gridButtons ;
20
20
private Panel _panelContent ;
21
21
private TabSelectorPosition _selectorPosition ;
22
- private ObservableCollection < Proportion > _buttonProportions ;
23
- private ObservableCollection < Proportion > _contentProportions ;
24
22
25
23
[ Browsable ( false ) ]
26
24
[ XmlIgnore ]
27
25
public TabControlStyle TabControlStyle { get ; set ; }
28
26
27
+ [ Browsable ( false ) ]
28
+ [ XmlIgnore ]
29
+ public override SelectionMode SelectionMode { get => base . SelectionMode ; set => base . SelectionMode = value ; }
30
+
29
31
[ DefaultValue ( HorizontalAlignment . Left ) ]
30
32
public override HorizontalAlignment HorizontalAlignment
31
33
{
@@ -62,94 +64,13 @@ public TabSelectorPosition TabSelectorPosition
62
64
}
63
65
set
64
66
{
65
- if ( _selectorPosition != value )
67
+ if ( value == _selectorPosition )
66
68
{
67
- // Content proportions and widgets need to be reversed if switching between
68
- // right/bottom and top/left
69
- bool newValueIsTopOrLeft = value == TabSelectorPosition . Top ||
70
- value == TabSelectorPosition . Left ;
71
- bool oldValueWasBottomOrRight = _selectorPosition == TabSelectorPosition . Bottom ||
72
- _selectorPosition == TabSelectorPosition . Right ;
73
-
74
- bool newValueIsTopOrBottom = value == TabSelectorPosition . Top ||
75
- value == TabSelectorPosition . Bottom ;
76
- bool oldValueWasTopOrBottom = _selectorPosition == TabSelectorPosition . Top ||
77
- _selectorPosition == TabSelectorPosition . Bottom ;
78
-
79
- bool transposeContent = newValueIsTopOrLeft == oldValueWasBottomOrRight ;
80
- ObservableCollection < Proportion > newButtonProportions ;
81
- ObservableCollection < Proportion > newContentProportions ;
82
-
83
- if ( newValueIsTopOrBottom )
84
- {
85
- newButtonProportions = _gridButtons . ColumnsProportions ;
86
- newContentProportions = InternalChild . RowsProportions ;
87
- }
88
- else
89
- {
90
- newButtonProportions = _gridButtons . RowsProportions ;
91
- newContentProportions = InternalChild . ColumnsProportions ;
92
- }
93
-
94
- Grid . SetColumn ( _panelContent , value == TabSelectorPosition . Left ? 1 : 0 ) ;
95
- Grid . SetRow ( _panelContent , value == TabSelectorPosition . Top ? 1 : 0 ) ;
96
-
97
- Grid . SetColumn ( _gridButtons , value == TabSelectorPosition . Right ? 1 : 0 ) ;
98
- Grid . SetRow ( _gridButtons , value == TabSelectorPosition . Bottom ? 1 : 0 ) ;
99
-
100
- if ( newButtonProportions != _buttonProportions )
101
- {
102
- for ( int i = 0 ; i < _buttonProportions . Count ; i ++ )
103
- {
104
- newButtonProportions . Add ( _buttonProportions [ i ] ) ;
105
- }
106
-
107
- _buttonProportions . Clear ( ) ;
108
- _buttonProportions = newButtonProportions ;
109
- }
110
-
111
- if ( newContentProportions != _contentProportions )
112
- {
113
- for ( int i = 0 ; i < _contentProportions . Count ; i ++ )
114
- {
115
- newContentProportions . Add ( _contentProportions [ i ] ) ;
116
- }
117
-
118
- _contentProportions . Clear ( ) ;
119
- _contentProportions = newContentProportions ;
120
- }
121
-
122
- if ( transposeContent )
123
- {
124
- for ( int i = 0 ; i < InternalChild . Widgets . Count ; i ++ )
125
- {
126
- _contentProportions . Move ( _contentProportions . Count - 1 , i ) ;
127
- InternalChild . Widgets . Move ( InternalChild . Widgets . Count - 1 , i ) ;
128
- }
129
- }
130
-
131
- for ( int i = 0 ; i < InternalChild . Widgets . Count ; i ++ )
132
- {
133
- Widget w = InternalChild . Widgets [ i ] ;
134
- if ( newValueIsTopOrBottom )
135
- {
136
- Grid . SetColumn ( w , 0 ) ;
137
- Grid . SetRow ( w , i ) ;
138
- }
139
- else
140
- {
141
- Grid . SetColumn ( w , i ) ;
142
- Grid . SetRow ( w , 0 ) ;
143
- }
144
- }
145
-
146
- _selectorPosition = value ;
147
-
148
- if ( newValueIsTopOrBottom != oldValueWasTopOrBottom )
149
- {
150
- UpdateGridPositions ( ) ;
151
- }
69
+ return ;
152
70
}
71
+
72
+ _selectorPosition = value ;
73
+ UpdateSelectorPosition ( ) ;
153
74
}
154
75
}
155
76
@@ -160,19 +81,19 @@ public TabControl(string styleName = Stylesheet.DefaultStyleName) : base(new Gri
160
81
161
82
_gridButtons = new Grid ( ) ;
162
83
_panelContent = new Panel ( ) ;
163
- Grid . SetRow ( _panelContent , 1 ) ;
164
84
165
- // Default to Top selector position:
166
85
_selectorPosition = TabSelectorPosition . Top ;
167
- _buttonProportions = _gridButtons . ColumnsProportions ;
168
- _contentProportions = InternalChild . RowsProportions ;
86
+ _gridButtons . DefaultColumnProportion = Proportion . Auto ;
87
+ _gridButtons . DefaultRowProportion = Proportion . Auto ;
88
+
89
+ InternalChild . DefaultColumnProportion = Proportion . Fill ;
90
+ InternalChild . DefaultRowProportion = Proportion . Fill ;
169
91
170
- // button, then content
171
- _contentProportions . Add ( new Proportion ( ) ) ;
172
- _contentProportions . Add ( new Proportion ( ProportionType . Fill ) ) ;
173
92
InternalChild . Widgets . Add ( _gridButtons ) ;
174
93
InternalChild . Widgets . Add ( _panelContent ) ;
175
94
95
+ UpdateSelectorPosition ( ) ;
96
+
176
97
ClipToBounds = true ;
177
98
178
99
SetStyle ( styleName ) ;
@@ -194,11 +115,72 @@ private void ItemOnChanged(object sender, EventArgs eventArgs)
194
115
InvalidateMeasure ( ) ;
195
116
}
196
117
197
- private void UpdateGridPositions ( )
118
+ private void UpdateSelectorPosition ( )
119
+ {
120
+ switch ( _selectorPosition )
121
+ {
122
+ case TabSelectorPosition . Top :
123
+ Grid . SetColumn ( _gridButtons , 0 ) ;
124
+ Grid . SetRow ( _gridButtons , 0 ) ;
125
+
126
+ Grid . SetColumn ( _panelContent , 0 ) ;
127
+ Grid . SetRow ( _panelContent , 1 ) ;
128
+
129
+ InternalChild . ColumnsProportions . Clear ( ) ;
130
+ InternalChild . RowsProportions . Clear ( ) ;
131
+ InternalChild . RowsProportions . Add ( Proportion . Auto ) ;
132
+ InternalChild . RowsProportions . Add ( Proportion . Fill ) ;
133
+ break ;
134
+
135
+ case TabSelectorPosition . Right :
136
+ Grid . SetColumn ( _gridButtons , 1 ) ;
137
+ Grid . SetRow ( _gridButtons , 0 ) ;
138
+
139
+ Grid . SetColumn ( _panelContent , 0 ) ;
140
+ Grid . SetRow ( _panelContent , 0 ) ;
141
+
142
+ InternalChild . ColumnsProportions . Clear ( ) ;
143
+ InternalChild . ColumnsProportions . Add ( Proportion . Fill ) ;
144
+ InternalChild . ColumnsProportions . Add ( Proportion . Auto ) ;
145
+ InternalChild . RowsProportions . Clear ( ) ;
146
+ break ;
147
+
148
+ case TabSelectorPosition . Bottom :
149
+ Grid . SetColumn ( _gridButtons , 0 ) ;
150
+ Grid . SetRow ( _gridButtons , 1 ) ;
151
+
152
+ Grid . SetColumn ( _panelContent , 0 ) ;
153
+ Grid . SetRow ( _panelContent , 0 ) ;
154
+
155
+
156
+ InternalChild . ColumnsProportions . Clear ( ) ;
157
+ InternalChild . RowsProportions . Clear ( ) ;
158
+ InternalChild . RowsProportions . Add ( Proportion . Fill ) ;
159
+ InternalChild . RowsProportions . Add ( Proportion . Auto ) ;
160
+ break ;
161
+
162
+ case TabSelectorPosition . Left :
163
+ Grid . SetColumn ( _gridButtons , 0 ) ;
164
+ Grid . SetRow ( _gridButtons , 0 ) ;
165
+
166
+ Grid . SetColumn ( _panelContent , 1 ) ;
167
+ Grid . SetRow ( _panelContent , 0 ) ;
168
+
169
+ InternalChild . ColumnsProportions . Clear ( ) ;
170
+ InternalChild . ColumnsProportions . Add ( Proportion . Auto ) ;
171
+ InternalChild . ColumnsProportions . Add ( Proportion . Fill ) ;
172
+ InternalChild . RowsProportions . Clear ( ) ;
173
+ break ;
174
+ }
175
+
176
+ UpdateButtonsGrid ( ) ;
177
+ }
178
+
179
+ private void UpdateButtonsGrid ( )
198
180
{
199
181
bool tabSelectorIsLeftOrRight = TabSelectorPosition == TabSelectorPosition . Left ||
200
182
TabSelectorPosition == TabSelectorPosition . Right ;
201
- for ( var i = 0 ; i < Items . Count ; ++ i )
183
+ for ( var i = 0 ; i < _gridButtons . Widgets . Count ; ++ i )
202
184
{
203
185
var widget = _gridButtons . Widgets [ i ] ;
204
186
if ( tabSelectorIsLeftOrRight )
@@ -234,7 +216,7 @@ protected override void InsertItem(TabItem item, int index)
234
216
var panel = new HorizontalStackPanel
235
217
{
236
218
Spacing = item . ImageTextSpacing ,
237
- VerticalAlignment = item . ContentVerticalAlignment
219
+ VerticalAlignment = VerticalAlignment . Stretch
238
220
} ;
239
221
240
222
panel . Widgets . Add ( image ) ;
@@ -253,12 +235,11 @@ protected override void InsertItem(TabItem item, int index)
253
235
254
236
button . Click += ButtonOnClick ;
255
237
256
- _buttonProportions . Insert ( index , new Proportion ( ProportionType . Auto ) ) ;
257
238
_gridButtons . Widgets . Insert ( index , button ) ;
258
239
259
240
item . Button = button ;
260
241
261
- UpdateGridPositions ( ) ;
242
+ UpdateButtonsGrid ( ) ;
262
243
263
244
if ( Items . Count == 1 )
264
245
{
@@ -272,15 +253,14 @@ protected override void RemoveItem(TabItem item)
272
253
item . Changed -= ItemOnChanged ;
273
254
274
255
var index = _gridButtons . Widgets . IndexOf ( item . Button ) ;
275
- _buttonProportions . RemoveAt ( index ) ;
276
256
_gridButtons . Widgets . RemoveAt ( index ) ;
277
257
278
258
if ( SelectedItem == item )
279
259
{
280
260
SelectedItem = null ;
281
261
}
282
262
283
- UpdateGridPositions ( ) ;
263
+ UpdateButtonsGrid ( ) ;
284
264
}
285
265
286
266
private void UpdateContent ( )
0 commit comments