Skip to content

Commit

Permalink
Fixes #474
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Sep 19, 2024
1 parent a0ea73a commit 726e3f2
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Myra/Graphics2D/UI/Selectors/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ protected internal override void CopyFrom(Widget w)
ListBoxStyle = listView.ListBoxStyle;
SelectionMode = listView.SelectionMode;

foreach(var child in listView.Widgets)
foreach (var child in listView.Widgets)
{
Widgets.Add(child.Clone());
}
Expand Down
34 changes: 28 additions & 6 deletions src/Myra/Graphics2D/UI/Selectors/ListViewButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{
internal class ListViewButton : ToggleButton
{
public Widget ButtonsContainer { get; set; }
public Widget TopParent => ButtonsContainer ?? Parent;


public ListViewButton() : base(null)
{
}
Expand All @@ -17,14 +21,23 @@ public override bool IsPressed
// If this is last pressed button
// Don't allow it to be unpressed
var allow = false;
foreach (var child in Parent.ChildrenCopy)
foreach (var child in TopParent.ChildrenCopy)
{
var asListViewButton = child as ListViewButton;
if (asListViewButton == null || asListViewButton == this)
if (asListViewButton == this)
{
continue;
}

if (asListViewButton == null)
{
asListViewButton = child.FindChild<ListViewButton>();
if (asListViewButton == null || asListViewButton == this)
{
continue;
}
}

if (asListViewButton.IsPressed)
{
allow = true;
Expand Down Expand Up @@ -52,15 +65,24 @@ public override void OnPressedChanged()
}

// Release other pressed radio buttons
foreach (var child in Parent.ChildrenCopy)
foreach (var child in TopParent.ChildrenCopy)
{
var asRadio = child as ListViewButton;
if (asRadio == null || asRadio == this)
var asListViewButton = child as ListViewButton;
if (asListViewButton == this)
{
continue;
}

asRadio.IsPressed = false;
if (asListViewButton == null)
{
asListViewButton = child.FindChild<ListViewButton>();
if (asListViewButton == null || asListViewButton == this)
{
continue;
}
}

asListViewButton.IsPressed = false;
}
}
}
Expand Down
72 changes: 66 additions & 6 deletions src/Myra/Graphics2D/UI/Selectors/TabControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public TabSelectorPosition TabSelectorPosition
}
}

[Category("Behavior")]
public bool CloseableTabs { get; set; }

public TabControl(string styleName = Stylesheet.DefaultStyleName) : base(new Grid())
{
HorizontalAlignment = HorizontalAlignment.Left;
Expand Down Expand Up @@ -227,17 +230,48 @@ protected override void InsertItem(TabItem item, int index)
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
Height = item.Height,
Content = panel
Content = panel,
ButtonsContainer = _gridButtons
};

button.ApplyButtonStyle(TabControlStyle.TabItemStyle);

button.Click += ButtonOnClick;

_gridButtons.Widgets.Insert(index, button);

item.Button = button;

if (!CloseableTabs)
{
_gridButtons.Widgets.Insert(index, button);
} else
{
var topItemPanel = new HorizontalStackPanel();
topItemPanel.Widgets.Add(button);
StackPanel.SetProportionType(button, ProportionType.Fill);

var closeButton = new Button
{
Content = new Image(),
HorizontalAlignment = HorizontalAlignment.Right
};

closeButton.Click += (s, e) => RemoveItem(item);

var style = TabControlStyle;
if (style.CloseButtonStyle != null)
{
closeButton.ApplyButtonStyle(style.CloseButtonStyle);
if (style.CloseButtonStyle.ImageStyle != null)
{
var closeImage = (Image)closeButton.Content;
closeImage.ApplyPressableImageStyle(style.CloseButtonStyle.ImageStyle);
}
}

topItemPanel.Widgets.Add(closeButton);
_gridButtons.Widgets.Insert(index, topItemPanel);
}

UpdateButtonsGrid();

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

private int GetButtonIndex(ListViewButton button)
{
var index = -1;
for (var i = 0; i < _gridButtons.Widgets.Count; ++i)
{
var widget = _gridButtons.Widgets[i];
if (widget == button || widget.FindChild<ListViewButton>() == button)
{
index = i;
break;
}
}

return index;
}

protected override void RemoveItem(TabItem item)
{
item.Changed -= ItemOnChanged;

var index = _gridButtons.Widgets.IndexOf(item.Button);
var index = GetButtonIndex(item.Button);
if (index < 0)
{
return;
}

_gridButtons.Widgets.RemoveAt(index);

if (SelectedItem == item)
Expand Down Expand Up @@ -287,8 +342,13 @@ protected override void Reset()

private void ButtonOnClick(object sender, EventArgs eventArgs)
{
var item = (ListViewButton)sender;
var index = _gridButtons.Widgets.IndexOf(item);
var button = (ListViewButton)sender;
var index = GetButtonIndex(button);
if (index < 0)
{
return;
}

SelectedIndex = index;
}

Expand Down
26 changes: 6 additions & 20 deletions src/Myra/Graphics2D/UI/Styles/TabControlStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,16 @@
{
public class TabControlStyle : WidgetStyle
{
public ImageTextButtonStyle TabItemStyle
{
get; set;
}
public ImageTextButtonStyle TabItemStyle { get; set; }

public WidgetStyle ContentStyle
{
get; set;
}
public WidgetStyle ContentStyle { get; set; }

public int ButtonSpacing
{
get; set;
}
public int ButtonSpacing { get; set; }

public int HeaderSpacing
{
get; set;
}
public int HeaderSpacing { get; set; }

public TabSelectorPosition TabSelectorPosition
{
get; set;
}
public TabSelectorPosition TabSelectorPosition { get; set; }
public ImageButtonStyle CloseButtonStyle { get; set; }

public TabControlStyle()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Myra/Resources/default_ui_skin.xmms
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@
<LabelStyle Font="default-font" TextColor="white" DisabledTextColor="gray" />
</TabItemStyle>
<ContentStyle Background="window" Padding="5" />
<CloseButtonStyle OverBackground="button-over" Padding="5, 0" PressedBackground="button-red">
<ImageStyle Image="icon-close" />
</CloseButtonStyle>
</TabControlStyle>
</TabControlStyles>
<TreeStyles>
Expand Down

0 comments on commit 726e3f2

Please sign in to comment.