Skip to content

Commit

Permalink
fix: improve code quality for custom ids feature
Browse files Browse the repository at this point in the history
- Check if `id` is in range
- Refactor do-while into simple while loop
  • Loading branch information
benthillerkus committed Apr 18, 2022
1 parent 22625a4 commit 35583f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/src/imperative.dart
Expand Up @@ -27,10 +27,10 @@ class TrayIcon {
/// to identify the icon and address it
/// when it has been clicked.
static Id _newId() {
Id temp = 0x8FFF;
do {
temp = _random.nextInt(0x8FFF - 0x7FFF);
} while (_allIcons.containsKey(temp));
Id temp = 0;
while (_allIcons.containsKey(temp)) {
temp = _random.nextInt(_kMaximumId - _kMinimumId);
}
return temp;
}

Expand Down Expand Up @@ -66,8 +66,12 @@ class TrayIcon {

/// Creates a new [TrayIcon] that controls a single icon in the system tray.
///
/// If provided, [id] has to be between `0x8FFF` and `0x7FFF`.
/// If provided, [id] has to be between `0` and `4096`.
TrayIcon({Id? id}) : _id = id ?? _newId() {
if (_id < 0 || _id >= (_kMaximumId - _kMinimumId)) {
throw ArgumentError.value(id, "id",
"The Id needs to be in between 0 and ${_kMaximumId - _kMinimumId}");
}
_logger = Logger("betrayal.icon.${_id.hex}");
_allIcons[_id] = this;
_isActive = true;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/plugin.dart
Expand Up @@ -27,6 +27,9 @@ part 'widgets.dart';
/// and call the plugin directly with it!
typedef Id = int;

const Id _kMaximumId = 0x8FFF;
const Id _kMinimumId = 0x7FFF;

/// Allows adding members to [Id] typedef.
extension HexRepresentation on Id {
/// Value in hex [String] notation. (e.g. `0x1234`)
Expand Down

0 comments on commit 35583f7

Please sign in to comment.