Skip to content

Commit 726e3f2

Browse files
committed
Fixes #474
1 parent a0ea73a commit 726e3f2

File tree

5 files changed

+104
-33
lines changed

5 files changed

+104
-33
lines changed

src/Myra/Graphics2D/UI/Selectors/ListView.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ protected internal override void CopyFrom(Widget w)
550550
ListBoxStyle = listView.ListBoxStyle;
551551
SelectionMode = listView.SelectionMode;
552552

553-
foreach(var child in listView.Widgets)
553+
foreach (var child in listView.Widgets)
554554
{
555555
Widgets.Add(child.Clone());
556556
}

src/Myra/Graphics2D/UI/Selectors/ListViewButton.cs

+28-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
{
33
internal class ListViewButton : ToggleButton
44
{
5+
public Widget ButtonsContainer { get; set; }
6+
public Widget TopParent => ButtonsContainer ?? Parent;
7+
8+
59
public ListViewButton() : base(null)
610
{
711
}
@@ -17,14 +21,23 @@ public override bool IsPressed
1721
// If this is last pressed button
1822
// Don't allow it to be unpressed
1923
var allow = false;
20-
foreach (var child in Parent.ChildrenCopy)
24+
foreach (var child in TopParent.ChildrenCopy)
2125
{
2226
var asListViewButton = child as ListViewButton;
23-
if (asListViewButton == null || asListViewButton == this)
27+
if (asListViewButton == this)
2428
{
2529
continue;
2630
}
2731

32+
if (asListViewButton == null)
33+
{
34+
asListViewButton = child.FindChild<ListViewButton>();
35+
if (asListViewButton == null || asListViewButton == this)
36+
{
37+
continue;
38+
}
39+
}
40+
2841
if (asListViewButton.IsPressed)
2942
{
3043
allow = true;
@@ -52,15 +65,24 @@ public override void OnPressedChanged()
5265
}
5366

5467
// Release other pressed radio buttons
55-
foreach (var child in Parent.ChildrenCopy)
68+
foreach (var child in TopParent.ChildrenCopy)
5669
{
57-
var asRadio = child as ListViewButton;
58-
if (asRadio == null || asRadio == this)
70+
var asListViewButton = child as ListViewButton;
71+
if (asListViewButton == this)
5972
{
6073
continue;
6174
}
6275

63-
asRadio.IsPressed = false;
76+
if (asListViewButton == null)
77+
{
78+
asListViewButton = child.FindChild<ListViewButton>();
79+
if (asListViewButton == null || asListViewButton == this)
80+
{
81+
continue;
82+
}
83+
}
84+
85+
asListViewButton.IsPressed = false;
6486
}
6587
}
6688
}

src/Myra/Graphics2D/UI/Selectors/TabControl.cs

+66-6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public TabSelectorPosition TabSelectorPosition
7373
}
7474
}
7575

76+
[Category("Behavior")]
77+
public bool CloseableTabs { get; set; }
78+
7679
public TabControl(string styleName = Stylesheet.DefaultStyleName) : base(new Grid())
7780
{
7881
HorizontalAlignment = HorizontalAlignment.Left;
@@ -227,17 +230,48 @@ protected override void InsertItem(TabItem item, int index)
227230
HorizontalAlignment = HorizontalAlignment.Stretch,
228231
VerticalAlignment = VerticalAlignment.Stretch,
229232
Height = item.Height,
230-
Content = panel
233+
Content = panel,
234+
ButtonsContainer = _gridButtons
231235
};
232236

233237
button.ApplyButtonStyle(TabControlStyle.TabItemStyle);
234238

235239
button.Click += ButtonOnClick;
236240

237-
_gridButtons.Widgets.Insert(index, button);
238-
239241
item.Button = button;
240242

243+
if (!CloseableTabs)
244+
{
245+
_gridButtons.Widgets.Insert(index, button);
246+
} else
247+
{
248+
var topItemPanel = new HorizontalStackPanel();
249+
topItemPanel.Widgets.Add(button);
250+
StackPanel.SetProportionType(button, ProportionType.Fill);
251+
252+
var closeButton = new Button
253+
{
254+
Content = new Image(),
255+
HorizontalAlignment = HorizontalAlignment.Right
256+
};
257+
258+
closeButton.Click += (s, e) => RemoveItem(item);
259+
260+
var style = TabControlStyle;
261+
if (style.CloseButtonStyle != null)
262+
{
263+
closeButton.ApplyButtonStyle(style.CloseButtonStyle);
264+
if (style.CloseButtonStyle.ImageStyle != null)
265+
{
266+
var closeImage = (Image)closeButton.Content;
267+
closeImage.ApplyPressableImageStyle(style.CloseButtonStyle.ImageStyle);
268+
}
269+
}
270+
271+
topItemPanel.Widgets.Add(closeButton);
272+
_gridButtons.Widgets.Insert(index, topItemPanel);
273+
}
274+
241275
UpdateButtonsGrid();
242276

243277
if (Items.Count == 1)
@@ -247,11 +281,32 @@ protected override void InsertItem(TabItem item, int index)
247281
}
248282
}
249283

284+
private int GetButtonIndex(ListViewButton button)
285+
{
286+
var index = -1;
287+
for (var i = 0; i < _gridButtons.Widgets.Count; ++i)
288+
{
289+
var widget = _gridButtons.Widgets[i];
290+
if (widget == button || widget.FindChild<ListViewButton>() == button)
291+
{
292+
index = i;
293+
break;
294+
}
295+
}
296+
297+
return index;
298+
}
299+
250300
protected override void RemoveItem(TabItem item)
251301
{
252302
item.Changed -= ItemOnChanged;
253303

254-
var index = _gridButtons.Widgets.IndexOf(item.Button);
304+
var index = GetButtonIndex(item.Button);
305+
if (index < 0)
306+
{
307+
return;
308+
}
309+
255310
_gridButtons.Widgets.RemoveAt(index);
256311

257312
if (SelectedItem == item)
@@ -287,8 +342,13 @@ protected override void Reset()
287342

288343
private void ButtonOnClick(object sender, EventArgs eventArgs)
289344
{
290-
var item = (ListViewButton)sender;
291-
var index = _gridButtons.Widgets.IndexOf(item);
345+
var button = (ListViewButton)sender;
346+
var index = GetButtonIndex(button);
347+
if (index < 0)
348+
{
349+
return;
350+
}
351+
292352
SelectedIndex = index;
293353
}
294354

src/Myra/Graphics2D/UI/Styles/TabControlStyle.cs

+6-20
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,16 @@
22
{
33
public class TabControlStyle : WidgetStyle
44
{
5-
public ImageTextButtonStyle TabItemStyle
6-
{
7-
get; set;
8-
}
5+
public ImageTextButtonStyle TabItemStyle { get; set; }
96

10-
public WidgetStyle ContentStyle
11-
{
12-
get; set;
13-
}
7+
public WidgetStyle ContentStyle { get; set; }
148

15-
public int ButtonSpacing
16-
{
17-
get; set;
18-
}
9+
public int ButtonSpacing { get; set; }
1910

20-
public int HeaderSpacing
21-
{
22-
get; set;
23-
}
11+
public int HeaderSpacing { get; set; }
2412

25-
public TabSelectorPosition TabSelectorPosition
26-
{
27-
get; set;
28-
}
13+
public TabSelectorPosition TabSelectorPosition { get; set; }
14+
public ImageButtonStyle CloseButtonStyle { get; set; }
2915

3016
public TabControlStyle()
3117
{

src/Myra/Resources/default_ui_skin.xmms

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@
9898
<LabelStyle Font="default-font" TextColor="white" DisabledTextColor="gray" />
9999
</TabItemStyle>
100100
<ContentStyle Background="window" Padding="5" />
101+
<CloseButtonStyle OverBackground="button-over" Padding="5, 0" PressedBackground="button-red">
102+
<ImageStyle Image="icon-close" />
103+
</CloseButtonStyle>
101104
</TabControlStyle>
102105
</TabControlStyles>
103106
<TreeStyles>

0 commit comments

Comments
 (0)