Skip to content

Commit

Permalink
TabControl refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Sep 19, 2024
1 parent f993c04 commit 1bb37ee
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 122 deletions.
186 changes: 83 additions & 103 deletions src/Myra/Graphics2D/UI/Selectors/TabControl.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.ComponentModel;
using Myra.Graphics2D.UI.Styles;
using System;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;

Check failure on line 5 in src/Myra/Graphics2D/UI/Selectors/TabControl.cs

View workflow job for this annotation

GitHub Actions / BuildAndPublish

The type or namespace name 'Xna' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

Check failure on line 5 in src/Myra/Graphics2D/UI/Selectors/TabControl.cs

View workflow job for this annotation

GitHub Actions / BuildAndPublish

The type or namespace name 'Xna' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

namespace Myra.Graphics2D.UI
{
Expand All @@ -19,13 +19,15 @@ public class TabControl : Selector<Grid, TabItem>
private Grid _gridButtons;
private Panel _panelContent;
private TabSelectorPosition _selectorPosition;
private ObservableCollection<Proportion> _buttonProportions;
private ObservableCollection<Proportion> _contentProportions;

[Browsable(false)]
[XmlIgnore]
public TabControlStyle TabControlStyle { get; set; }

[Browsable(false)]
[XmlIgnore]
public override SelectionMode SelectionMode { get => base.SelectionMode; set => base.SelectionMode = value; }

[DefaultValue(HorizontalAlignment.Left)]
public override HorizontalAlignment HorizontalAlignment
{
Expand Down Expand Up @@ -62,94 +64,13 @@ public TabSelectorPosition TabSelectorPosition
}
set
{
if (_selectorPosition != value)
if (value == _selectorPosition)
{
// Content proportions and widgets need to be reversed if switching between
// right/bottom and top/left
bool newValueIsTopOrLeft = value == TabSelectorPosition.Top ||
value == TabSelectorPosition.Left;
bool oldValueWasBottomOrRight = _selectorPosition == TabSelectorPosition.Bottom ||
_selectorPosition == TabSelectorPosition.Right;

bool newValueIsTopOrBottom = value == TabSelectorPosition.Top ||
value == TabSelectorPosition.Bottom;
bool oldValueWasTopOrBottom = _selectorPosition == TabSelectorPosition.Top ||
_selectorPosition == TabSelectorPosition.Bottom;

bool transposeContent = newValueIsTopOrLeft == oldValueWasBottomOrRight;
ObservableCollection<Proportion> newButtonProportions;
ObservableCollection<Proportion> newContentProportions;

if (newValueIsTopOrBottom)
{
newButtonProportions = _gridButtons.ColumnsProportions;
newContentProportions = InternalChild.RowsProportions;
}
else
{
newButtonProportions = _gridButtons.RowsProportions;
newContentProportions = InternalChild.ColumnsProportions;
}

Grid.SetColumn(_panelContent, value == TabSelectorPosition.Left ? 1 : 0);
Grid.SetRow(_panelContent, value == TabSelectorPosition.Top ? 1 : 0);

Grid.SetColumn(_gridButtons, value == TabSelectorPosition.Right ? 1 : 0);
Grid.SetRow(_gridButtons, value == TabSelectorPosition.Bottom ? 1 : 0);

if (newButtonProportions != _buttonProportions)
{
for (int i = 0; i < _buttonProportions.Count; i++)
{
newButtonProportions.Add(_buttonProportions[i]);
}

_buttonProportions.Clear();
_buttonProportions = newButtonProportions;
}

if (newContentProportions != _contentProportions)
{
for (int i = 0; i < _contentProportions.Count; i++)
{
newContentProportions.Add(_contentProportions[i]);
}

_contentProportions.Clear();
_contentProportions = newContentProportions;
}

if (transposeContent)
{
for (int i = 0; i < InternalChild.Widgets.Count; i++)
{
_contentProportions.Move(_contentProportions.Count - 1, i);
InternalChild.Widgets.Move(InternalChild.Widgets.Count - 1, i);
}
}

for (int i = 0; i < InternalChild.Widgets.Count; i++)
{
Widget w = InternalChild.Widgets[i];
if (newValueIsTopOrBottom)
{
Grid.SetColumn(w, 0);
Grid.SetRow(w, i);
}
else
{
Grid.SetColumn(w, i);
Grid.SetRow(w, 0);
}
}

_selectorPosition = value;

if (newValueIsTopOrBottom != oldValueWasTopOrBottom)
{
UpdateGridPositions();
}
return;
}

_selectorPosition = value;
UpdateSelectorPosition();
}
}

Expand All @@ -160,19 +81,19 @@ public TabControl(string styleName = Stylesheet.DefaultStyleName) : base(new Gri

_gridButtons = new Grid();
_panelContent = new Panel();
Grid.SetRow(_panelContent, 1);

// Default to Top selector position:
_selectorPosition = TabSelectorPosition.Top;
_buttonProportions = _gridButtons.ColumnsProportions;
_contentProportions = InternalChild.RowsProportions;
_gridButtons.DefaultColumnProportion = Proportion.Auto;
_gridButtons.DefaultRowProportion = Proportion.Auto;

InternalChild.DefaultColumnProportion = Proportion.Fill;
InternalChild.DefaultRowProportion = Proportion.Fill;

// button, then content
_contentProportions.Add(new Proportion());
_contentProportions.Add(new Proportion(ProportionType.Fill));
InternalChild.Widgets.Add(_gridButtons);
InternalChild.Widgets.Add(_panelContent);

UpdateSelectorPosition();

ClipToBounds = true;

SetStyle(styleName);
Expand All @@ -194,11 +115,72 @@ private void ItemOnChanged(object sender, EventArgs eventArgs)
InvalidateMeasure();
}

private void UpdateGridPositions()
private void UpdateSelectorPosition()
{
switch (_selectorPosition)
{
case TabSelectorPosition.Top:
Grid.SetColumn(_gridButtons, 0);
Grid.SetRow(_gridButtons, 0);

Grid.SetColumn(_panelContent, 0);
Grid.SetRow(_panelContent, 1);

InternalChild.ColumnsProportions.Clear();
InternalChild.RowsProportions.Clear();
InternalChild.RowsProportions.Add(Proportion.Auto);
InternalChild.RowsProportions.Add(Proportion.Fill);
break;

case TabSelectorPosition.Right:
Grid.SetColumn(_gridButtons, 1);
Grid.SetRow(_gridButtons, 0);

Grid.SetColumn(_panelContent, 0);
Grid.SetRow(_panelContent, 0);

InternalChild.ColumnsProportions.Clear();
InternalChild.ColumnsProportions.Add(Proportion.Fill);
InternalChild.ColumnsProportions.Add(Proportion.Auto);
InternalChild.RowsProportions.Clear();
break;

case TabSelectorPosition.Bottom:
Grid.SetColumn(_gridButtons, 0);
Grid.SetRow(_gridButtons, 1);

Grid.SetColumn(_panelContent, 0);
Grid.SetRow(_panelContent, 0);


InternalChild.ColumnsProportions.Clear();
InternalChild.RowsProportions.Clear();
InternalChild.RowsProportions.Add(Proportion.Fill);
InternalChild.RowsProportions.Add(Proportion.Auto);
break;

case TabSelectorPosition.Left:
Grid.SetColumn(_gridButtons, 0);
Grid.SetRow(_gridButtons, 0);

Grid.SetColumn(_panelContent, 1);
Grid.SetRow(_panelContent, 0);

InternalChild.ColumnsProportions.Clear();
InternalChild.ColumnsProportions.Add(Proportion.Auto);
InternalChild.ColumnsProportions.Add(Proportion.Fill);
InternalChild.RowsProportions.Clear();
break;
}

UpdateButtonsGrid();
}

private void UpdateButtonsGrid()
{
bool tabSelectorIsLeftOrRight = TabSelectorPosition == TabSelectorPosition.Left ||
TabSelectorPosition == TabSelectorPosition.Right;
for (var i = 0; i < Items.Count; ++i)
for (var i = 0; i < _gridButtons.Widgets.Count; ++i)
{
var widget = _gridButtons.Widgets[i];
if (tabSelectorIsLeftOrRight)
Expand Down Expand Up @@ -234,7 +216,7 @@ protected override void InsertItem(TabItem item, int index)
var panel = new HorizontalStackPanel
{
Spacing = item.ImageTextSpacing,
VerticalAlignment = item.ContentVerticalAlignment
VerticalAlignment = VerticalAlignment.Stretch
};

panel.Widgets.Add(image);
Expand All @@ -253,12 +235,11 @@ protected override void InsertItem(TabItem item, int index)

button.Click += ButtonOnClick;

_buttonProportions.Insert(index, new Proportion(ProportionType.Auto));
_gridButtons.Widgets.Insert(index, button);

item.Button = button;

UpdateGridPositions();
UpdateButtonsGrid();

if (Items.Count == 1)
{
Expand All @@ -272,15 +253,14 @@ protected override void RemoveItem(TabItem item)
item.Changed -= ItemOnChanged;

var index = _gridButtons.Widgets.IndexOf(item.Button);
_buttonProportions.RemoveAt(index);
_gridButtons.Widgets.RemoveAt(index);

if (SelectedItem == item)
{
SelectedItem = null;
}

UpdateGridPositions();
UpdateButtonsGrid();
}

private void UpdateContent()
Expand Down
7 changes: 0 additions & 7 deletions src/Myra/Graphics2D/UI/Selectors/TabItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@ public Widget Content
[XmlIgnore]
public int ImageTextSpacing { get; set; }

[DefaultValue(VerticalAlignment.Stretch)]

public VerticalAlignment ContentVerticalAlignment
{
get; set;
} = VerticalAlignment.Stretch;

[DefaultValue(null)]
public int? Height { get; set; }

Expand Down
32 changes: 20 additions & 12 deletions src/MyraPad/AsyncTasksQueue.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using Myra.Graphics2D.UI;
using System;
using System.Collections.Concurrent;
using System.Threading;

namespace MyraPad
{
internal class AsyncTasksQueue
{
private bool _running = true;
private string _projectXml;
private string _objectXml;
private readonly ConcurrentQueue<string> _projectXmls = new ConcurrentQueue<string>();
private readonly ConcurrentQueue<string> _objectXmls = new ConcurrentQueue<string>();

private readonly AutoResetEvent _refreshProjectEvent = new AutoResetEvent(false);
private readonly AutoResetEvent _waitForQuitEvent = new AutoResetEvent(false);
Expand All @@ -20,13 +21,13 @@ public AsyncTasksQueue()

public void QueueLoadProject(string xml)
{
_projectXml = xml;
_projectXmls.Enqueue(xml);
_refreshProjectEvent.Set();
}

public void QueueLoadObject(string xml)
{
_objectXml = xml;
_objectXmls.Enqueue(xml);
_refreshProjectEvent.Set();
}

Expand All @@ -43,31 +44,40 @@ private void RefreshProc(object state)
{
_refreshProjectEvent.WaitOne();

if (!string.IsNullOrEmpty(_projectXml))
// Get last project xml
var projectXml = string.Empty;
while (_projectXmls.Count > 0)
{
_projectXmls.TryDequeue(out projectXml);
}
if (!string.IsNullOrEmpty(projectXml))
{
try
{
Studio.MainForm.QueueSetStatusText("Reloading Project...");
Studio.MainForm.NewProject = Project.LoadFromXml(_projectXml, Studio.MainForm.AssetManager);
Studio.MainForm.NewProject = Project.LoadFromXml(projectXml, Studio.MainForm.AssetManager);
Studio.MainForm.QueueSetStatusText(string.Empty);
}
catch (Exception ex)
{
Studio.MainForm.QueueClearExplorer();
Studio.MainForm.QueueSetStatusText(ex.Message);
}

_projectXml = null;
}

if (!string.IsNullOrEmpty(_objectXml))
var objectXml = string.Empty;
while (_objectXmls.Count > 0)
{
_objectXmls.TryDequeue(out objectXml);
}
if (!string.IsNullOrEmpty(objectXml))
{
if (Studio.MainForm.Project != null)
{
try
{
Studio.MainForm.QueueSetStatusText("Reloading Object...");
Studio.MainForm.NewObject = Studio.MainForm.Project.LoadObjectFromXml(_objectXml, Studio.MainForm.AssetManager);
Studio.MainForm.NewObject = Studio.MainForm.Project.LoadObjectFromXml(objectXml, Studio.MainForm.AssetManager);
Studio.MainForm.QueueSetStatusText(string.Empty);
}
catch (Exception ex)
Expand All @@ -76,8 +86,6 @@ private void RefreshProc(object state)
Studio.MainForm.QueueSetStatusText(ex.Message);
}
}

_objectXml = null;
}
}

Expand Down

0 comments on commit 1bb37ee

Please sign in to comment.