From 35583f7fdd4e583d832af32cbcaed27e237a0c51 Mon Sep 17 00:00:00 2001 From: Bent Hillerkus <29630575+benthillerkus@users.noreply.github.com> Date: Mon, 18 Apr 2022 23:38:07 +0200 Subject: [PATCH] fix: improve code quality for custom ids feature - Check if `id` is in range - Refactor do-while into simple while loop --- lib/src/imperative.dart | 14 +++++++++----- lib/src/plugin.dart | 3 +++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/src/imperative.dart b/lib/src/imperative.dart index a2f5a88..53e0b62 100644 --- a/lib/src/imperative.dart +++ b/lib/src/imperative.dart @@ -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; } @@ -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; diff --git a/lib/src/plugin.dart b/lib/src/plugin.dart index 9594019..c00bb61 100644 --- a/lib/src/plugin.dart +++ b/lib/src/plugin.dart @@ -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`)