Skip to content

Commit 1c6bf9c

Browse files
authored
Merge pull request #3056 from Eideren/ui_services
fix: Replace static FocusedElement and more proactive UI service availability
2 parents 3a1e5e8 + 02f6b77 commit 1c6bf9c

File tree

15 files changed

+114
-53
lines changed

15 files changed

+114
-53
lines changed

sources/engine/Stride.UI.Tests/Layering/EditTextTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public void TestBasicInvalidations()
2929
services.AddService<IGame>(new Game());
3030

3131
var edit = new EditText();
32-
edit.UIElementServices = new UIElementServices { Services = services };
3332

3433
// - test the properties that are supposed to invalidate the object measurement
3534
UIElementLayeringTests.TestMeasureInvalidation(edit, () => edit.Font = new DummyFont());

sources/engine/Stride.UI.Tests/Regression/EditTextTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ protected override async Task LoadContent()
9999
canvas.Children.Add(edit3);
100100
canvas.Children.Add(edit4);
101101

102-
canvas.UIElementServices = new UIElementServices { Services = this.Services };
103-
104102
UIComponent.Page = new Engine.UIPage { RootElement = canvas };
105103
}
106104

sources/engine/Stride.UI.Tests/Regression/MouseOverTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ protected override async Task LoadContent()
8383
canvas.MouseOverStateChanged += (sender, args) => { triggeredCanvas = true;};
8484
stackPanel.MouseOverStateChanged += (sender, args) => { triggeredStackPanel = true;};
8585

86-
canvas.UIElementServices = new UIElementServices { Services = this.Services };
87-
8886
UIComponent.Page = new Engine.UIPage { RootElement = canvas };
8987
}
9088

sources/engine/Stride.UI/Controls/EditText.Android.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
using Android.Text.Method;
1111
using Stride.Core;
1212
using Stride.Games;
13+
using Stride.Input;
1314
using Exception = System.Exception;
15+
using KeyEvent = Android.Views.KeyEvent;
1416

1517
namespace Stride.UI.Controls
1618
{
@@ -228,7 +230,7 @@ private void OnMinLinesChangedImpl()
228230
editText.Post(editTextSetMinLinesAction);
229231
}
230232

231-
private void ActivateEditTextImpl()
233+
private void ActivateEditTextImpl(InputManager inputManager)
232234
{
233235
lock (syncRoot)
234236
{
@@ -250,7 +252,7 @@ private void ActivateEditTextImpl()
250252
}
251253
}
252254

253-
private void DeactivateEditTextImpl()
255+
private void DeactivateEditTextImpl(InputManager inputManager)
254256
{
255257
lock (syncRoot)
256258
{
@@ -265,8 +267,6 @@ private void DeactivateEditTextImpl()
265267
activeEditText = null;
266268

267269
GetGameContext().HideEditTextPopup();
268-
269-
FocusedElement = null;
270270
}
271271
}
272272

sources/engine/Stride.UI/Controls/EditText.Direct.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,14 @@ internal override void OnTextInput(TextEventArgs args)
9090
}
9191
}
9292

93-
private void ActivateEditTextImpl()
93+
private void ActivateEditTextImpl(InputManager inputManager)
9494
{
95-
var input = UIElementServices.Services.GetSafeServiceAs<InputManager>();
96-
input.TextInput?.EnabledTextInput();
95+
inputManager.TextInput?.EnabledTextInput();
9796
}
98-
private void DeactivateEditTextImpl()
99-
{
100-
var input = UIElementServices.Services.GetSafeServiceAs<InputManager>();
101-
input.TextInput?.DisableTextInput();
102-
Composition = "";
10397

104-
FocusedElement = null;
98+
private void DeactivateEditTextImpl(InputManager inputManager)
99+
{
100+
inputManager.TextInput?.DisableTextInput();
105101
}
106102

107103
private void InterpretKey(Keys key, InputManager input)

sources/engine/Stride.UI/Controls/EditText.UWP.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Stride.Core;
99
using Stride.Core.Annotations;
1010
using Stride.Games;
11+
using Stride.Input;
1112

1213
namespace Stride.UI.Controls
1314
{
@@ -42,7 +43,7 @@ private void OnMinLinesChangedImpl()
4243
{
4344
}
4445

45-
private void ActivateEditTextImpl()
46+
private void ActivateEditTextImpl(InputManager inputManager)
4647
{
4748
// try to show the virtual keyboard if no hardward keyboard available
4849
Windows.UI.ViewManagement.InputPane.GetForCurrentView().TryShow();
@@ -103,7 +104,7 @@ private void EditText_TextChanged(object sender, Windows.UI.Xaml.Controls.TextCh
103104
UpdateSelectionFromEditImpl();
104105
}
105106

106-
private void DeactivateEditTextImpl()
107+
private void DeactivateEditTextImpl(InputManager inputManager)
107108
{
108109
if (editText != null)
109110
{
@@ -119,7 +120,6 @@ private void DeactivateEditTextImpl()
119120
editText = null;
120121
activeEditText = null;
121122
}
122-
FocusedElement = null;
123123
}
124124

125125
private void UpdateTextToEditImpl()

sources/engine/Stride.UI/Controls/EditText.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Stride.Engine;
1212
using Stride.Games;
1313
using Stride.Graphics;
14+
using Stride.Input;
1415
using Stride.UI.Events;
1516

1617
namespace Stride.UI.Controls
@@ -148,20 +149,25 @@ public bool IsSelectionActive
148149
if (IsReadOnly && value) // prevent selection when the Edit is read only
149150
return;
150151

151-
isSelectionActive = value;
152+
var uiSystem = UIElementServices.Services?.GetService<UISystem>();
153+
var inputManager = UIElementServices.Services?.GetService<InputManager>();
154+
if (inputManager is null || uiSystem is null)
155+
return;
152156

153-
if (IsSelectionActive)
157+
isSelectionActive = value;
158+
if (isSelectionActive)
154159
{
155-
var previousEditText = FocusedElement as EditText;
156-
if (previousEditText != null)
160+
if (uiSystem.FocusedElement is EditText previousEditText)
157161
previousEditText.IsSelectionActive = false;
158162

159-
FocusedElement = this;
160-
ActivateEditTextImpl();
163+
uiSystem.FocusedElement = this;
164+
ActivateEditTextImpl(inputManager);
161165
}
162166
else
163167
{
164-
DeactivateEditTextImpl();
168+
uiSystem.FocusedElement = null;
169+
DeactivateEditTextImpl(inputManager);
170+
Composition = "";
165171
}
166172
}
167173
}

sources/engine/Stride.UI/Controls/EditText.iOS.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Stride.Core;
1111
using Stride.Core.Annotations;
1212
using Stride.Games;
13+
using Stride.Input;
1314
using Stride.UI.Events;
1415

1516
namespace Stride.UI.Controls
@@ -127,7 +128,6 @@ private static void TextFieldOnEditingDidEnd(object sender, EventArgs eventArgs)
127128
currentActiveEditText.IsSelectionActive = false;
128129
barView.Hidden = true;
129130
overlayView.Hidden = true;
130-
FocusedElement = null;
131131

132132
if (currentActiveEditText != null)
133133
{
@@ -171,7 +171,7 @@ private void OnMinLinesChangedImpl()
171171
{
172172
}
173173

174-
private void ActivateEditTextImpl()
174+
private void ActivateEditTextImpl(InputManager inputManager)
175175
{
176176
EnsureGameContext();
177177

@@ -199,7 +199,7 @@ private bool ShouldChangeCharacters(UITextField theTextField, NSRange range, str
199199
return replacementSize < 0 || theTextField.Text.Length + replacementSize <= MaxLength;
200200
}
201201

202-
private void DeactivateEditTextImpl()
202+
private void DeactivateEditTextImpl(InputManager inputManager)
203203
{
204204
attachedTextField.EditingChanged -= TextFieldOnValueChanged;
205205
attachedTextField.ShouldChangeCharacters -= ShouldChangeCharacters;

sources/engine/Stride.UI/Engine/UIComponent.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Stride.Engine
1616
[DataContract("UIComponent")]
1717
[Display("UI", Expand = ExpandRule.Once)]
1818
[DefaultEntityComponentRenderer(typeof(UIRenderProcessor))]
19+
[DefaultEntityComponentProcessor(typeof(UIProcessor))]
1920
[ComponentOrder(9800)]
2021
[ComponentCategory("UI")]
2122
public sealed class UIComponent : ActivableEntityComponent
@@ -36,7 +37,15 @@ public UIComponent()
3637
/// <userdoc>The UI page.</userdoc>
3738
[DataMember(10)]
3839
[Display("Page")]
39-
public UIPage Page { get; set; }
40+
public UIPage Page
41+
{
42+
get;
43+
set
44+
{
45+
field = value;
46+
field?.Services = Services;
47+
}
48+
}
4049

4150
/// <summary>
4251
/// Specifies the sampling method to be used for this component
@@ -125,5 +134,16 @@ public UIComponent()
125134
/// </summary>
126135
[DataMemberIgnore]
127136
public const float FixedSizeVerticalUnit = 1; // 100% of the vertical resolution
137+
138+
[DataMemberIgnore]
139+
internal IServiceRegistry Services
140+
{
141+
get;
142+
set
143+
{
144+
field = value;
145+
Page?.Services = Services;
146+
}
147+
}
128148
}
129149
}

sources/engine/Stride.UI/Engine/UIPage.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ public sealed class UIPage : ComponentBase
2121
/// </summary>
2222
/// <userdoc>The root element of the page.</userdoc>
2323
[DataMember]
24-
public UIElement RootElement { get; set; }
24+
public UIElement RootElement
25+
{
26+
get;
27+
set
28+
{
29+
field = value;
30+
field?.UIElementServices = new UIElementServices { Services = Services };
31+
}
32+
}
33+
34+
[DataMemberIgnore]
35+
internal IServiceRegistry Services
36+
{
37+
get;
38+
set
39+
{
40+
field = value;
41+
RootElement?.UIElementServices = new UIElementServices { Services = Services };
42+
}
43+
}
2544
}
2645
}

0 commit comments

Comments
 (0)