Skip to content

Commit

Permalink
feat: add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
benthillerkus committed Apr 9, 2022
1 parent 2d8afd0 commit 89360e9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -45,7 +45,6 @@ Please refer to the [example subdirectory](example) for more [information](examp
- FIXME Find out all possible errors and repackage / handle them
- TODO Find out, communicate and memoize the correct system metrics (icon resolution)
- TODO Support interaction
- TODO Logging

## Style

Expand Down
8 changes: 7 additions & 1 deletion lib/src/image.dart
Expand Up @@ -6,11 +6,14 @@ import 'dart:typed_data';
import 'package:betrayal/src/plugin.dart';
import 'package:betrayal/src/win_icon.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;

/// A helper that sets the image of a [TrayIcon].
@immutable
class TrayIconImageDelegate {
static final _logger = Logger('betrayal.image_delegate');

/// A [TrayIconImageDelegate] that uses a `.ico` file.
///
/// If both [uri] and [path] are given, [uri] takes precedence.
Expand All @@ -28,7 +31,10 @@ class TrayIconImageDelegate {
path = uri.toFilePath(windows: true);
}

// path.endsWith(".ico");
if (!path!.endsWith(".ico")) {
_logger.warning("""'$path' is not an .ico file.
Continuing under the assumption that only the file extension is wrong""");
}

setIcon = (id, plugin) => plugin.setImageFromPath(id, path!);
}
Expand Down
12 changes: 11 additions & 1 deletion lib/src/imperative.dart
Expand Up @@ -10,6 +10,7 @@ import 'package:betrayal/src/win_icon.dart';

import 'package:flutter/widgets.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';

part 'widgets.dart';

Expand All @@ -32,6 +33,8 @@ class TrayIcon {
static final Map<Id, TrayIcon> _allIcons = {};
static final Random _random = Random();

late final Logger _logger;

/// This [Id] is used by the operating system
/// to identify the icon and address it
/// when it has been clicked.
Expand Down Expand Up @@ -59,8 +62,10 @@ class TrayIcon {

/// Creates a new [TrayIcon] that controls a single icon in the system tray.
TrayIcon() : _id = _newId() {
_logger = Logger("betrayal.icon.$_id");
_allIcons[_id] = this;
_isActive = true;
_logger.fine("initialized instance");
// In Debug mode users can hot restart the app.
// [As of right now, there is no way to detect that.](https://github.com/flutter/flutter/issues/10437)
// That means we have to call an init method for cleanup.
Expand All @@ -75,7 +80,8 @@ class TrayIcon {
final TrayIcon? result =
context.dependOnInheritedWidgetOfExactType<_TrayIconHeritage>()?.icon;
assert(result != null, 'No TrayIcon found in context');
return result!;
result!._logger.fine("provided by `_TrayIconHeritage`");
return result;
}

/// Disposes all icons and clears up any residual icons.
Expand All @@ -93,6 +99,7 @@ class TrayIcon {
static void clearAll() {
for (final TrayIcon icon in _allIcons.values) {
icon._isActive = false;
icon._logger.fine("disposed by `clearAll`");
}
_allIcons.clear();
_plugin.reset();
Expand All @@ -106,6 +113,7 @@ class TrayIcon {
///
/// Calling this method twice is a no-op.
Future<void> dispose() async {
_logger.finer("trying to dispose");
if (!_isActive) return;
_isActive = false;
if (_isReal) {
Expand All @@ -114,6 +122,7 @@ class TrayIcon {
_allIcons.remove(_id);

_disposedAt = StackTrace.current;
_logger.fine("disposed", null, _disposedAt);
}

/// Ensures the icon has been constructed in native code.
Expand All @@ -123,6 +132,7 @@ class TrayIcon {
if (!_isReal) {
await _plugin.addTray(_id);
_isReal = true;
_logger.fine("made real (created in native code)");
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/src/plugin.dart
Expand Up @@ -6,6 +6,7 @@ import 'dart:typed_data';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';

/// An identifier.
///
Expand All @@ -28,17 +29,21 @@ class BetrayalPlugin {

static const MethodChannel _channel = MethodChannel('betrayal');

final Logger _logger = Logger('betrayal.plugin');

/// The singleton constructor.
///
/// Once it is invoked, it will try to clear up any icons registered
/// with the plugin.
BetrayalPlugin._internal() {
_logger.finer('initializing plugin...');
// This makes sure the plugin can be invoked
// before `runApp` is called in main
WidgetsFlutterBinding.ensureInitialized();

_channel.setMethodCallHandler(_handleMethod);
reset();
_logger.info('BetrayalPlugin initialized!');
}

Future<dynamic> _handleMethod(MethodCall methodCall) async {
Expand All @@ -62,6 +67,7 @@ class BetrayalPlugin {
@protected
Future<void> reset() async {
await _channel.invokeMethod('reset');
_logger.finer('removed all icons');
}

/// Removes and cleans up a single item.
Expand Down
28 changes: 16 additions & 12 deletions lib/src/widgets.dart
Expand Up @@ -20,7 +20,9 @@ class TrayIconWidget extends StatefulWidget {
/// A hidden [TrayIcon] will not be in the system tray.
/// But its state will be remembered by the plugin,
/// so that it can be shown again later.
final bool visible;
///
/// If `visible == null` the icon will remain as-is.
final bool? visible;

/// The tooltip to show when hovering over the [TrayIcon].
///
Expand All @@ -46,7 +48,7 @@ class TrayIconWidget extends StatefulWidget {
/// Rebuilding this [TrayIconWidget] will different parameters will update the icon.
///
/// Contrary to [TrayIcon.setImage], if [tooltip] or any of the image related fields
/// are not set, the current values are kept. This is to ensure that rebuilds
/// are `null`, the current values are kept. This is to ensure that rebuilds
/// don't interfere with using the [TrayIcon] widget directly through.
/// [TrayIcon.of(BuildContext context)].
TrayIconWidget(
Expand Down Expand Up @@ -94,10 +96,12 @@ class _TrayIconHeritage extends InheritedWidget {

class _TrayIconWidgetState extends State<TrayIconWidget> {
late final TrayIcon _icon = TrayIcon();
late final _logger = Logger("betrayal.widget.${_icon._id}");

@override
void activate() {
_icon.show();
_logger.info("reinserted. icon shown again.");
super.activate();
}

Expand All @@ -109,41 +113,41 @@ class _TrayIconWidgetState extends State<TrayIconWidget> {
@override
void deactivate() {
_icon.hide();
_logger.info("removed. icon hidden.");
super.deactivate();
}

@override
void didUpdateWidget(covariant TrayIconWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (!widget.visible) {
_logger.info("updating icon…");
if (widget.visible != null && !widget.visible!) {
_icon.hide();
return;
}
if (widget.addToContext ||
!oldWidget.visible ||
widget.tooltip != oldWidget.tooltip) {
if (widget.addToContext || widget.tooltip != oldWidget.tooltip) {
if (widget.tooltip != null) _icon.setTooltip(widget.tooltip!);
}
if (widget.addToContext ||
!oldWidget.visible ||
widget._delegate != oldWidget._delegate) {
if (widget.addToContext || widget._delegate != oldWidget._delegate) {
if (widget._delegate != null) _icon.setImage(delegate: widget._delegate);
}
_icon.show();
if (widget.visible != null) {
_icon.show();
}
}

@override
void dispose() {
_logger.info("disposing…");
_icon.dispose();
super.dispose();
}

@override
void initState() {
super.initState();
if (!widget.visible) return;
if (widget._delegate != null) _icon.setImage(delegate: widget._delegate);
if (widget.tooltip != null) _icon.setTooltip(widget.tooltip!);
_icon.show();
if (widget.visible ?? false) _icon.show();
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Expand Up @@ -10,6 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
logging: ^1.0.2

dev_dependencies:
flutter_lints: ^1.0.0
Expand Down

0 comments on commit 89360e9

Please sign in to comment.