-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Network selection from portfolio header
- Loading branch information
Showing
14 changed files
with
338 additions
and
11 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/Reown.AppKit.Unity/Resources/Reown/AppKit/Components/NetworkButton.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
src/Reown.AppKit.Unity/Resources/Reown/AppKit/Components/NetworkButton/NetworkButton.uss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
:root { | ||
border-radius: var(--ro-border-radius-s); | ||
flex-direction: row; | ||
align-items: center; | ||
align-self: flex-start; | ||
justify-content: center; | ||
flex-grow: 0; | ||
padding: 8 14 8 14; | ||
overflow: hidden; | ||
transition-property: background-color; | ||
transition-duration: 100ms; | ||
} | ||
|
||
:root:hover { | ||
background-color: var(--ro-gray-glass-005); | ||
} | ||
|
||
.border { | ||
padding: 6 14 6 14; | ||
background-color: var(--ro-gray-glass-002); | ||
border-width: 1px; | ||
border-color: var(--ro-gray-glass-002); | ||
} | ||
|
||
#network-button__name { | ||
margin-left: 8px; | ||
flex-grow: 1; | ||
-unity-text-align: middle-left; | ||
color: var(--ro-color-fg-100); | ||
} | ||
|
||
#network-button__icon { | ||
width: 24px; | ||
height: 24px; | ||
border-radius: var(--ro-border-radius-full); | ||
overflow: hidden; | ||
} | ||
|
||
#network-button__chevron{ | ||
width: 12px; | ||
height: 12px; | ||
margin-left: 8px; | ||
--unity-image-tint-color: var(--ro-color-fg-200); | ||
--unity-image: resource('Reown/AppKit/Icons/icon_bold_chevronbottom'); | ||
} |
11 changes: 11 additions & 0 deletions
11
...Reown.AppKit.Unity/Resources/Reown/AppKit/Components/NetworkButton/NetworkButton.uss.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
src/Reown.AppKit.Unity/Resources/Reown/AppKit/Components/NetworkButton/NetworkButton.uxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False"> | ||
<Style src="NetworkButton.uss"/> | ||
<ui:Image name="network-button__icon"/> | ||
<ui:Label name="network-button__name"/> | ||
<ui:Image name="network-button__chevron"/> | ||
</ui:UXML> |
10 changes: 10 additions & 0 deletions
10
...eown.AppKit.Unity/Resources/Reown/AppKit/Components/NetworkButton/NetworkButton.uxml.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
src/Reown.AppKit.Unity/Runtime/Components/NetworkButton.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
using System; | ||
using Reown.AppKit.Unity.Utils; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
namespace Reown.AppKit.Unity.Components | ||
{ | ||
public class NetworkButton : VisualElement, IDisposable | ||
{ | ||
public const string Name = "network-button"; | ||
public static readonly string NameIcon = $"{Name}__icon"; | ||
public static readonly string NameText = $"{Name}__name"; | ||
public static readonly string NameChevron = $"{Name}__chevron"; | ||
|
||
private bool _showName = true; | ||
private bool _showChevron = true; | ||
private bool _showBorder = true; | ||
private bool _disposed; | ||
|
||
private RemoteSprite<Image> _networkIcon; | ||
private Clickable _clickable; | ||
|
||
public bool ShowName | ||
{ | ||
get => _showName; | ||
set | ||
{ | ||
_showName = value; | ||
NetworkName.style.display = value ? DisplayStyle.Flex : DisplayStyle.None; | ||
} | ||
} | ||
|
||
public bool ShowChevron | ||
{ | ||
get => _showChevron; | ||
set | ||
{ | ||
_showChevron = value; | ||
NetworkChevron.style.display = value ? DisplayStyle.Flex : DisplayStyle.None; | ||
} | ||
} | ||
|
||
public bool ShowBorder | ||
{ | ||
get => _showBorder; | ||
set | ||
{ | ||
_showBorder = value; | ||
EnableInClassList("border", value); | ||
} | ||
} | ||
|
||
public Clickable Clickable | ||
{ | ||
get => _clickable; | ||
set | ||
{ | ||
_clickable = value; | ||
this.AddManipulator(value); | ||
} | ||
} | ||
|
||
public readonly Image NetworkIcon; | ||
public readonly Label NetworkName; | ||
public readonly Image NetworkChevron; | ||
|
||
public new class UxmlFactory : UxmlFactory<NetworkButton, UxmlTraits> | ||
{ | ||
} | ||
|
||
public new class UxmlTraits : VisualElement.UxmlTraits | ||
{ | ||
private readonly UxmlBoolAttributeDescription _showName = new() | ||
{ | ||
name = "show-name", | ||
defaultValue = true | ||
}; | ||
|
||
private readonly UxmlBoolAttributeDescription _showChevron = new() | ||
{ | ||
name = "show-chevron", | ||
defaultValue = true | ||
}; | ||
|
||
private readonly UxmlBoolAttributeDescription _showBorder = new() | ||
{ | ||
name = "show-border", | ||
defaultValue = true | ||
}; | ||
|
||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) | ||
{ | ||
base.Init(ve, bag, cc); | ||
var networkButton = (NetworkButton)ve; | ||
|
||
networkButton.ShowName = _showName.GetValueFromBag(bag, cc); | ||
networkButton.ShowChevron = _showChevron.GetValueFromBag(bag, cc); | ||
networkButton.ShowBorder = _showBorder.GetValueFromBag(bag, cc); | ||
} | ||
} | ||
|
||
public NetworkButton() | ||
{ | ||
var asset = Resources.Load<VisualTreeAsset>("Reown/AppKit/Components/NetworkButton/NetworkButton"); | ||
asset.CloneTree(this); | ||
|
||
name = Name; | ||
AddToClassList(Name); | ||
|
||
NetworkIcon = this.Q<Image>(NameIcon); | ||
NetworkName = this.Q<Label>(NameText); | ||
NetworkChevron = this.Q<Image>(NameChevron); | ||
|
||
ShowBorder = true; | ||
focusable = true; | ||
|
||
AppKit.NetworkController.ChainChanged += ChainChangedHandler; | ||
|
||
// Update network data when button becomes visible | ||
// ReSharper disable once HeapView.CanAvoidClosure | ||
RegisterCallbackOnce<GeometryChangedEvent>(_ => UpdateNetworkButton(AppKit.NetworkController.ActiveChain)); | ||
|
||
Clickable = new Clickable(() => AppKit.OpenModal(ViewType.NetworkSearch)); | ||
} | ||
|
||
private void ChainChangedHandler(object sender, NetworkController.ChainChangedEventArgs e) | ||
{ | ||
UpdateNetworkButton(e.NewChain); | ||
} | ||
|
||
private void UpdateNetworkButton(Chain chain) | ||
{ | ||
if (chain == null) | ||
{ | ||
NetworkName.text = "Network"; | ||
NetworkIcon.style.display = DisplayStyle.None; | ||
return; | ||
} | ||
|
||
NetworkName.text = chain.Name; | ||
|
||
var newNetworkIcon = RemoteSpriteFactory.GetRemoteSprite<Image>(chain.ImageUrl); | ||
_networkIcon?.UnsubscribeImage(NetworkIcon); | ||
_networkIcon = newNetworkIcon; | ||
_networkIcon.SubscribeImage(NetworkIcon); | ||
NetworkIcon.style.display = DisplayStyle.Flex; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
AppKit.NetworkController.ChainChanged -= ChainChangedHandler; | ||
_networkIcon?.UnsubscribeImage(NetworkIcon); | ||
|
||
_disposed = true; | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Reown.AppKit.Unity/Runtime/Components/NetworkButton.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
src/Reown.AppKit.Unity/Runtime/Components/NetworkButtonPresenter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Reown.AppKit.Unity.Utils; | ||
using UnityEngine.UIElements; | ||
|
||
namespace Reown.AppKit.Unity.Components | ||
{ | ||
public class NetworkButtonPresenter | ||
{ | ||
private readonly NetworkButton _networkButton; | ||
private RemoteSprite<Image> _networkIcon; | ||
private bool _disposed; | ||
|
||
public NetworkButtonPresenter(NetworkButton networkButton) | ||
{ | ||
_networkButton = networkButton; | ||
|
||
AppKit.NetworkController.ChainChanged += ChainChangedHandler; | ||
UpdateNetworkButton(AppKit.NetworkController.ActiveChain); | ||
} | ||
|
||
private void ChainChangedHandler(object sender, NetworkController.ChainChangedEventArgs e) | ||
{ | ||
UpdateNetworkButton(e.NewChain); | ||
} | ||
|
||
private void UpdateNetworkButton(Chain chain) | ||
{ | ||
if (chain == null) | ||
{ | ||
_networkButton.NetworkName.text = "Network"; | ||
_networkButton.NetworkIcon.style.display = DisplayStyle.None; | ||
return; | ||
} | ||
|
||
_networkButton.NetworkName.text = chain.Name; | ||
|
||
var newNetworkIcon = RemoteSpriteFactory.GetRemoteSprite<Image>(chain.ImageUrl); | ||
_networkIcon?.UnsubscribeImage(_networkButton.NetworkIcon); | ||
_networkIcon = newNetworkIcon; | ||
_networkIcon.SubscribeImage(_networkButton.NetworkIcon); | ||
_networkButton.NetworkIcon.style.display = DisplayStyle.Flex; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
AppKit.NetworkController.ChainChanged -= ChainChangedHandler; | ||
_networkIcon?.UnsubscribeImage(_networkButton.NetworkIcon); | ||
|
||
_disposed = true; | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Reown.AppKit.Unity/Runtime/Components/NetworkButtonPresenter.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.