Skip to content

Commit

Permalink
feat: Add a button to allow clear messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarianZ committed Nov 13, 2024
1 parent 79b4d4d commit ce6d52d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 12 deletions.
9 changes: 8 additions & 1 deletion Editor/EditorMessageUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,20 @@ public static Texture GetCustomDataIcon()
return EditorGUIUtility.IconContent("animation.play").image;
}

public static Texture GetClearIcon()
{
return EditorGUIUtility.IconContent("clear").image;
}


public static Image NewImage(Texture image = null, string tooltip = null, DisplayStyle display = DisplayStyle.Flex)
public static Image NewImage(Texture image = null, string tooltip = null,
DisplayStyle display = DisplayStyle.Flex, PickingMode pickingMode = PickingMode.Ignore)
{
Image imageElement = new Image
{
tooltip = tooltip,
image = image,
pickingMode = pickingMode,
style =
{
display = display,
Expand Down
17 changes: 10 additions & 7 deletions Editor/MessageBanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ public bool ShowMessageTypeCount
}
}

public object Source { get; private set; }
public string SourceName { get; private set; }
public object Source { get; set; }
public string SourceName { get; set; }
public bool AllowClearMessages { get; set; }
public IList<Message> Messages { get; private set; }
public int CurrentMessageIndex { get; private set; }


public MessageBanner(object source, string sourceName, bool showMessageTypeCount = true)
: this(null, source, sourceName, showMessageTypeCount) { }
public MessageBanner(object source, string sourceName,
bool showMessageTypeCount = true, bool allowClearMessages = false)
: this(null, source, sourceName, showMessageTypeCount, allowClearMessages) { }

/// <summary>
/// 消息横幅。
Expand All @@ -48,12 +50,13 @@ public MessageBanner(object source, string sourceName, bool showMessageTypeCount
/// <param name="sourceName">调用源的名字。</param>
/// <param name="showMessageTypeCount">是否显示各类型消息的计数。</param>
public MessageBanner(IList<Message> messages, object source, string sourceName,
bool showMessageTypeCount = true)
bool showMessageTypeCount = true, bool allowClearMessages = false)
{
_showMessageTypeCount = showMessageTypeCount;
Messages = messages;
Source = source;
SourceName = sourceName;
AllowClearMessages = allowClearMessages;

style.flexDirection = FlexDirection.Row;
style.paddingLeft = 4;
Expand Down Expand Up @@ -208,9 +211,9 @@ private void RefreshMessageTypeCountDisplay()

private void OnClick(ClickEvent evt)
{
if (evt.clickCount == 2)
if (evt.clickCount == 2 && Messages != null && Messages.Count > 0)
{
MessageViewer.Open(Messages, Source, SourceName);
MessageViewer.Open(Messages, Source, SourceName, AllowClearMessages);
}
}

Expand Down
32 changes: 29 additions & 3 deletions Editor/MessageViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class MessageViewer : EditorWindow
/// <param name="source">调用源。当调用源变为null时,消息查看器窗口会自动关闭。若传入null,则消息查看器窗口不会自动关闭。</param>
/// <param name="sourceName">调用源的名字。会出现在消息查看器窗口标题中。</param>
/// <returns></returns>
public static MessageViewer Open(IList<Message> messages, object source, string sourceName)
public static MessageViewer Open(IList<Message> messages, object source, string sourceName,
bool allowClearMessages = true)
{
if (source == null)
{
Expand All @@ -32,6 +33,7 @@ public static MessageViewer Open(IList<Message> messages, object source, string
_sourcelessInstance._sourceless = true;
}

_sourcelessInstance._showClearButton = allowClearMessages;
_sourcelessInstance.SetMessages(messages);
_sourcelessInstance.Show();
_sourcelessInstance.Focus();
Expand All @@ -47,15 +49,16 @@ public static MessageViewer Open(IList<Message> messages, object source, string

viewer.titleContent = new GUIContent($"Message Viewer({sourceName ?? source})");
viewer.Source = source;
viewer._showClearButton = allowClearMessages;
viewer.SetMessages(messages);
viewer.Show();
viewer.Focus();
return viewer;
}

public static MessageViewer Open(object source, string sourceName)
public static MessageViewer Open(object source, string sourceName, bool allowClearMessages = true)
{
return Open(null, source, sourceName);
return Open(null, source, sourceName, allowClearMessages);
}


Expand All @@ -69,6 +72,7 @@ public static MessageViewer Open(object source, string sourceName)
private MessageTypeToggle _infoMessageToggle;
private MessageTypeToggle _warningMessageToggle;
private MessageTypeToggle _errorMessageToggle;
private ToolbarButton _clearButton;
private ListView _messageListView;
private MessageDetailsElement _messageDetailsElement;

Expand Down Expand Up @@ -110,6 +114,9 @@ public static MessageViewer Open(object source, string sourceName)
[SerializeField]
[HideInInspector]
private bool _showErrorMessage = true;
[SerializeField]
[HideInInspector]
private bool _showClearButton;

#endregion

Expand Down Expand Up @@ -249,6 +256,19 @@ private void CreateGUI()
_errorMessageToggle.RegisterValueChangedCallback(OnMessageTypeToggleChanged);
typeToggleContainer.Add(_errorMessageToggle);

// Clear Button
_clearButton = new ToolbarImageButton(EditorMessageUtility.GetClearIcon(), ClearMessages)
{
tooltip = "Clear All Messages",
style =
{
width = 22,
flexShrink = 0,
display = _showClearButton ? DisplayStyle.Flex : DisplayStyle.None,
}
};
toolbar.Add(_clearButton);

#endregion


Expand Down Expand Up @@ -487,6 +507,12 @@ private bool TestMessageSearchPattern(Message message)
return message.message.Contains(_searchPattern, StringComparison.OrdinalIgnoreCase);
}

private void ClearMessages()
{
Messages?.Clear();
Refresh();
}

private void TryClose()
{
if (_sourcelessInstance == this)
Expand Down
32 changes: 32 additions & 0 deletions Editor/ToolbarImageButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace GBG.EditorMessages.Editor
{
internal class ToolbarImageButton : ToolbarButton
{
public Texture Image
{
get => _image.image;
set => _image.image = value;
}

private readonly Image _image;


public ToolbarImageButton(Action clickEvent) : base(clickEvent)
{
float iconSize = EditorMessageUtility.GlobalIconSize;

_image = EditorMessageUtility.NewImage();
Insert(0, _image);
}

public ToolbarImageButton(Texture image, Action clickEvent) : this(clickEvent)
{
Image = image;
}
}
}
11 changes: 11 additions & 0 deletions Editor/ToolbarImageButton.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.greenbamboogames.editormessages",
"version": "1.0.2",
"version": "1.0.3",
"displayName": "Editor Messages!",
"description": "Show custom messages in Unity Editor.",
"unity": "2022.3",
Expand Down

0 comments on commit ce6d52d

Please sign in to comment.