Skip to content

Commit

Permalink
[NUI] Refactoring ControlState to use bitmask
Browse files Browse the repository at this point in the history
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
everLEEst committed Dec 27, 2024
1 parent 27baefc commit 8f6e128
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 126 deletions.
72 changes: 72 additions & 0 deletions src/Tizen.NUI/src/internal/Common/ControlStateUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 = 32;
private static readonly Dictionary<string, long> registeredStates = new Dictionary<string, long>();
private static int nextBitPosition = 0;

/// <summary>
/// </summary>
public static long FullMask => (1L << MaxBitWidth) - 1L;

/// <summary>
/// </summary>
public static IEnumerable<(string, long)> RegisteredStates()
{
foreach (var (key, value) in registeredStates)
{
yield return (key, value);
}
}

public static long 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().ToLowerInvariant();

if (!registeredStates.TryGetValue(trimmed, out long 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 = 1L << nextBitPosition;
registeredStates.Add(trimmed, bitMask);

nextBitPosition++;
}

return bitMask;
}
}
}
Loading

0 comments on commit 8f6e128

Please sign in to comment.