Skip to content

Commit

Permalink
Fixes #57
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Nov 8, 2023
1 parent 3815324 commit 39e4afe
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PackageProjectUrl>https://github.com/rds1983/Myra</PackageProjectUrl>
<RootNamespace>Myra</RootNamespace>
<Description>UI Library for MonoGame, FNA and Stride</Description>
<VersionPrefix>1.5.2</VersionPrefix>
<VersionPrefix>1.5.3</VersionPrefix>
<XNAssetsVersion>0.7.1</XNAssetsVersion>
<FontStashSharpVersion>1.3.3</FontStashSharpVersion>
<LangVersion>8.0</LangVersion>
Expand Down
4 changes: 3 additions & 1 deletion samples/Myra.Samples.AllWidgets/AllWidgets.Generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Generated by MyraPad at 10/31/2023 1:26:35 PM */
/* Generated by MyraPad at 11/8/2023 1:41:26 PM */
using Myra;
using Myra.Graphics2D;
using Myra.Graphics2D.TextureAtlases;
Expand Down Expand Up @@ -116,6 +116,7 @@ private void BuildUI()
_buttonSaveFile = new ImageTextButton();
_buttonSaveFile.Text = "Save File";
_buttonSaveFile.Padding = new Thickness(8, 0);
_buttonSaveFile.Tooltip = "Tooltip 1";
_buttonSaveFile.Id = "_buttonSaveFile";
Grid.SetColumn(_buttonSaveFile, 1);

Expand All @@ -130,6 +131,7 @@ private void BuildUI()
_buttonOpenFile = new ImageTextButton();
_buttonOpenFile.Text = "Open File";
_buttonOpenFile.Padding = new Thickness(8, 0);
_buttonOpenFile.Tooltip = "Tooltip 2";
_buttonOpenFile.Id = "_buttonOpenFile";
Grid.SetColumn(_buttonOpenFile, 1);
Grid.SetRow(_buttonOpenFile, 1);
Expand Down
4 changes: 2 additions & 2 deletions samples/Myra.Samples.AllWidgets/allControls.xmmp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
<Proportion Type="Fill" />
</Grid.ColumnsProportions>
<Label Text="Button:" />
<ImageTextButton Text="Save File" Padding="8, 0" Id="_buttonSaveFile" Grid.Column="1" />
<ImageTextButton Text="Save File" Padding="8, 0" Tooltip="Tooltip 1" Id="_buttonSaveFile" Grid.Column="1" />
<TextBox Id="_textSaveFile" Grid.Column="2" />
<Label Text="Another Button:" Grid.Row="1" />
<ImageTextButton Text="Open File" Padding="8, 0" Id="_buttonOpenFile" Grid.Column="1" Grid.Row="1" />
<ImageTextButton Text="Open File" Padding="8, 0" Tooltip="Tooltip 2" Id="_buttonOpenFile" Grid.Column="1" Grid.Row="1" />
<TextBox Id="_textOpenFile" Grid.Column="2" Grid.Row="1" />
<Label Text="Blue Button:" Grid.Row="2" />
<ImageTextButton Text="Choose Folder" StyleName="blue" Padding="8, 0" Id="_buttonChooseFolder" Grid.Column="1" Grid.Row="2" />
Expand Down
82 changes: 55 additions & 27 deletions src/Myra/Graphics2D/UI/Desktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

namespace Myra.Graphics2D.UI
{
public enum OverWidgetType
{
ContextMenu,
Tooltip
}

public partial class Desktop : ITransformable, IDisposable
{
private Rectangle _bounds;
Expand Down Expand Up @@ -67,7 +73,7 @@ public Widget Root
return;
}

HideContextMenu();
HideOverWidget();
Widgets.Clear();

if (value != null)
Expand Down Expand Up @@ -112,7 +118,9 @@ internal Rectangle InternalBounds

internal Rectangle LayoutBounds => new Rectangle(0, 0, InternalBounds.Width, InternalBounds.Height);

public Widget ContextMenu { get; private set; }
public Widget OverWidget { get; private set; }

public OverWidgetType OverWidgetType { get; private set; }

/// <summary>
/// Widget having keyboard focus
Expand Down Expand Up @@ -335,15 +343,15 @@ public Widget GetChild(int index)

private void InputOnTouchDown()
{
if (ContextMenu == null || ContextMenu.IsTouchInside)
if (OverWidget == null || OverWidget.IsTouchInside)
{
return;
}

var ev = ContextMenuClosing;
if (ev != null)
{
var args = new CancellableEventArgs<Widget>(ContextMenu);
var args = new CancellableEventArgs<Widget>(OverWidget);
ev(null, args);

if (args.Cancel)
Expand All @@ -352,23 +360,24 @@ private void InputOnTouchDown()
}
}

HideContextMenu();
HideOverWidget();
}

public void ShowContextMenu(Widget menu, Point position)
private void ShowOverWidget(Widget menu, OverWidgetType overWidgetType, Point position)
{
HideContextMenu();
HideOverWidget();

ContextMenu = menu;
if (ContextMenu == null)
OverWidget = menu;
if (OverWidget == null)
{
return;
}

ContextMenu.HorizontalAlignment = HorizontalAlignment.Left;
ContextMenu.VerticalAlignment = VerticalAlignment.Top;
OverWidgetType = overWidgetType;
OverWidget.HorizontalAlignment = HorizontalAlignment.Left;
OverWidget.VerticalAlignment = VerticalAlignment.Top;

var measure = ContextMenu.Measure(LayoutBounds.Size());
var measure = OverWidget.Measure(LayoutBounds.Size());

if (position.X + measure.X > LayoutBounds.Right)
{
Expand All @@ -380,32 +389,32 @@ public void ShowContextMenu(Widget menu, Point position)
position.Y = LayoutBounds.Bottom - measure.Y;
}

ContextMenu.Left = position.X;
ContextMenu.Top = position.Y;
OverWidget.Left = position.X;
OverWidget.Top = position.Y;

ContextMenu.Visible = true;
OverWidget.Visible = true;

Widgets.Add(ContextMenu);
Widgets.Add(OverWidget);

if (ContextMenu.AcceptsKeyboardFocus)
if (OverWidget.AcceptsKeyboardFocus)
{
_previousKeyboardFocus = FocusedKeyboardWidget;
FocusedKeyboardWidget = ContextMenu;
FocusedKeyboardWidget = OverWidget;
}
}

public void HideContextMenu()
public void HideOverWidget()
{
if (ContextMenu == null)
if (OverWidget == null)
{
return;
}

Widgets.Remove(ContextMenu);
ContextMenu.Visible = false;
Widgets.Remove(OverWidget);
OverWidget.Visible = false;

ContextMenuClosed.Invoke(ContextMenu);
ContextMenu = null;
ContextMenuClosed.Invoke(OverWidget);
OverWidget = null;

if (_previousKeyboardFocus != null)
{
Expand All @@ -414,6 +423,25 @@ public void HideContextMenu()
}
}

public void ShowContextMenu(Widget menu, Point position) =>
ShowOverWidget(menu, OverWidgetType.ContextMenu, position);


public void ShowTooltip(Widget widget, string text, Point position)
{
var tooltip = new Label(null)
{
Text = text,
Tag = widget
};

tooltip.ApplyLabelStyle(Stylesheet.Current.TooltipStyle);

ShowOverWidget(tooltip, OverWidgetType.Tooltip, position);
}

public void HideContextMenu() => HideOverWidget();

private void WidgetsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
if (args.Action == NotifyCollectionChangedAction.Add)
Expand Down Expand Up @@ -607,7 +635,7 @@ public void UpdateLayout()

internal void ProcessWidgets(Func<Widget, bool> operation)
{
foreach(var w in ChildrenCopy)
foreach (var w in ChildrenCopy)
{
var result = w.ProcessWidgets(operation);
if (!result)
Expand Down Expand Up @@ -781,9 +809,9 @@ public void OnKeyDown(Keys key)
}
}

if (key == Keys.Escape && ContextMenu != null)
if (key == Keys.Escape && OverWidget != null && OverWidgetType == OverWidgetType.ContextMenu)
{
HideContextMenu();
HideOverWidget();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Myra/Graphics2D/UI/Selectors/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public ComboBox(string styleName = Stylesheet.DefaultStyleName) :

internal void HideDropdown()
{
if (Desktop != null && Desktop.ContextMenu == _listBox)
if (Desktop != null && Desktop.OverWidget == _listBox)
{
Desktop.HideContextMenu();
Desktop.HideOverWidget();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Myra/Graphics2D/UI/Selectors/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public void Close()
{
if (Desktop != null)
{
Desktop.HideContextMenu();
Desktop.HideOverWidget();
}
HoverIndex = SelectedIndex = null;
}
Expand Down Expand Up @@ -578,7 +578,7 @@ private void OnHoverIndexChanged(object sender, EventArgs eventArgs)
return;
}

if (Desktop.ContextMenu != this && menuItem.CanOpen && OpenMenuItem != menuItem)
if (Desktop.OverWidget != this && menuItem.CanOpen && OpenMenuItem != menuItem)
{
SelectedIndex = HoverIndex;
}
Expand All @@ -603,9 +603,9 @@ private void OnSelectedIndexChanged(object sender, EventArgs e)
{
_internalSetSelectedIndex = true;

if (Desktop.ContextMenu != this)
if (Desktop.OverWidget != this)
{
Desktop.HideContextMenu();
Desktop.HideOverWidget();
}
}
finally
Expand Down
22 changes: 22 additions & 0 deletions src/Myra/Graphics2D/UI/Styles/Stylesheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static Stylesheet Current
}

private readonly Dictionary<string, LabelStyle> _labelStyles = new Dictionary<string, LabelStyle>();
private readonly Dictionary<string, LabelStyle> _tooltipStyles = new Dictionary<string, LabelStyle>();
private readonly Dictionary<string, TextBoxStyle> _textBoxStyles = new Dictionary<string, TextBoxStyle>();
private readonly Dictionary<string, ButtonStyle> _buttonStyles = new Dictionary<string, ButtonStyle>();
private readonly Dictionary<string, ImageTextButtonStyle> _checkBoxStyles = new Dictionary<string, ImageTextButtonStyle>();
Expand Down Expand Up @@ -97,6 +98,19 @@ public LabelStyle LabelStyle
}
}

[XmlIgnore]
public LabelStyle TooltipStyle
{
get
{
return GetDefaultStyle(_tooltipStyles);
}
set
{
SetDefaultStyle(_tooltipStyles, value);
}
}

[XmlIgnore]
public TextBoxStyle TextBoxStyle
{
Expand Down Expand Up @@ -378,6 +392,14 @@ public Dictionary<string, LabelStyle> LabelStyles
}
}

public Dictionary<string, LabelStyle> TooltipStyles
{
get
{
return _tooltipStyles;
}
}

public Dictionary<string, TextBoxStyle> TextBoxStyles
{
get
Expand Down
10 changes: 10 additions & 0 deletions src/Myra/Graphics2D/UI/Widget.Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ partial class Widget
public const int DoubleClickRadius = 2;

private DateTime? _lastTouchDown;
private DateTime? _lastMouseMovement;
private Point _lastLocalTouchPosition;
private Point? _localMousePosition;
private Point? _localTouchPosition;
Expand Down Expand Up @@ -303,14 +304,23 @@ internal void ProcessInputEvents()
switch (inputEvent)
{
case InputEventType.MouseLeft:
if (Desktop != null && Desktop.OverWidget != null && Desktop.OverWidget.Tag == this)
{
// Tooltip for this widget is shown
Desktop.HideOverWidget();
}

_lastMouseMovement = null;
OnMouseLeft();
MouseLeft.Invoke(this);
break;
case InputEventType.MouseEntered:
_lastMouseMovement = DateTime.Now;
OnMouseEntered();
MouseEntered.Invoke(this);
break;
case InputEventType.MouseMoved:
_lastMouseMovement = DateTime.Now;
OnMouseMoved();
MouseMoved.Invoke(this);
break;
Expand Down
12 changes: 12 additions & 0 deletions src/Myra/Graphics2D/UI/Widget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ public virtual MouseCursorType? MouseCursor
}
}

[Category("Behavior")]
[DefaultValue(null)]
public string Tooltip { get; set; }


[Category("Transform")]
[DefaultValue("1, 1")]
Expand Down Expand Up @@ -852,6 +856,14 @@ public void Render(RenderContext context)
return;
}

if (!string.IsNullOrEmpty(Tooltip) && Desktop != null &&
(Desktop.OverWidget == null || Desktop.OverWidget.Tag != this) &&
_lastMouseMovement != null && (DateTime.Now - _lastMouseMovement.Value).TotalMilliseconds > MyraEnvironment.TooltipDelayInMs)
{
Desktop.ShowTooltip(this, Tooltip, Desktop.MousePosition + MyraEnvironment.TooltipOffset);
}


UpdateArrange();

var oldTransform = context.Transform;
Expand Down
2 changes: 2 additions & 0 deletions src/Myra/MyraEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ public static AssetManager DefaultAssetManager
public static bool DrawMouseHoveredWidgetFrame { get; set; }
public static bool DrawTextGlyphsFrames { get; set; }
public static bool DisableClipping { get; set; }
public static int TooltipDelayInMs { get; set; } = 500;
public static Point TooltipOffset { get; set; } = new Point(0, 20);

Check failure on line 228 in src/Myra/MyraEnvironment.cs

View workflow job for this annotation

GitHub Actions / BuildAndPublish

The type or namespace name 'Point' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 228 in src/Myra/MyraEnvironment.cs

View workflow job for this annotation

GitHub Actions / BuildAndPublish

The type or namespace name 'Point' could not be found (are you missing a using directive or an assembly reference?)

/// <summary>
/// Makes the text rendering more smooth(especially when scaling) for the cost of sacrificing some performance
Expand Down
4 changes: 4 additions & 0 deletions src/Myra/Resources/default_ui_skin.xmms
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
<Stylesheet TextureRegionAtlas="default_ui_skin.xmat">
<Fonts UsedSpace="0, 0, 1024, 160">
<Font Id="default-font" File="Inter-Regular.ttf" Size="20"/>
<Font Id="tooltip-font" File="Inter-Regular.ttf" Size="16"/>
</Fonts>
<LabelStyles>
<LabelStyle Font="default-font" TextColor="white" DisabledTextColor="gray" />
</LabelStyles>
<TooltipStyles>
<LabelStyle Background="default-pane" Padding="5" Font="tooltip-font" TextColor="white" />
</TooltipStyles>
<TextBoxStyles>
<TextBoxStyle Background="textfield" TextColor="white" DisabledTextColor="gray" Font="default-font" Cursor="cursor" Selection="selection" />
</TextBoxStyles>
Expand Down
4 changes: 2 additions & 2 deletions src/MyraPad/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -974,9 +974,9 @@ private void UpdatePositions()

private void HandleAutoComplete()
{
if (_desktop.ContextMenu == _autoCompleteMenu)
if (_desktop.OverWidget == _autoCompleteMenu)
{
_desktop.HideContextMenu();
_desktop.HideOverWidget();
}

if (_currentTagStart == null || _currentTagEnd != null || string.IsNullOrEmpty(_parentTag))
Expand Down

0 comments on commit 39e4afe

Please sign in to comment.