-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NUI] Refactoring ControlState to use bitmask
ControlState was implemented inefficently on the memory and the performance. This patch purposed to reduce inefficency by using bitflags on the state instead of string list. [https://github.sec.samsung.net/NUI/OneUIComponents/issues/15] long type bitmask will be represent each states, 1 1 1 1 1 O S D P F Normal : 0L Focused : 1L Pressed : 2L Disabled : 4L Selected : 8L Other : 16L and All : 31L This concept is based on VisualState of NUI2, https://github.sec.samsung.net/dotnet/nui2/blob/main/src/Tizen.NUI2.Components/Base/ViewState.cs but we had to modified few states to keep backward compatibility of NUI ControlState.
- Loading branch information
Showing
2 changed files
with
136 additions
and
103 deletions.
There are no files selected for viewing
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,73 @@ | ||
/* | ||
* Copyright(c) 2025 Samsung Electronics Co., Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Tizen.NUI | ||
{ | ||
/// <summary> | ||
/// Manages state name and bit mask. | ||
/// </summary> | ||
internal static class ControlStateUtility | ||
{ | ||
private const int MaxBitWidth = 62; | ||
private static readonly Dictionary<string, ulong> registeredStates = new Dictionary<string, ulong>(); | ||
private static int nextBitPosition = 0; | ||
|
||
/// <summary> | ||
/// </summary> | ||
public static ulong FullMask => (1UL << MaxBitWidth) - 1UL; | ||
|
||
/// <summary> | ||
/// </summary> | ||
public static IEnumerable<(string, ulong)> RegisteredStates() | ||
{ | ||
|
||
foreach (var (key, value) in registeredStates) | ||
{ | ||
yield return (key, value); | ||
} | ||
} | ||
|
||
public static ulong Register(string stateName) | ||
{ | ||
if (stateName == null) | ||
throw new ArgumentNullException($"{nameof(stateName)} cannot be null.", nameof(stateName)); | ||
|
||
if (string.IsNullOrWhiteSpace(stateName)) | ||
throw new ArgumentException($"{nameof(stateName)} cannot be whitespace.", nameof(stateName)); | ||
|
||
string trimmed = stateName.Trim(); | ||
ulong bitMask = 0UL; | ||
|
||
if (trimmed != "Normal" && !registeredStates.TryGetValue(trimmed, out bitMask)) | ||
{ | ||
if (nextBitPosition + 1 > MaxBitWidth) | ||
{ | ||
throw new ArgumentException($"The given state name '{stateName}' is not acceptable since there is no more room to register a new state."); | ||
} | ||
|
||
bitMask = 1UL << nextBitPosition; | ||
registeredStates.Add(trimmed, bitMask); | ||
nextBitPosition++; | ||
} | ||
|
||
return bitMask; | ||
} | ||
} | ||
} |
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