Skip to content

Commit

Permalink
feat: support windows stock icons
Browse files Browse the repository at this point in the history
  • Loading branch information
benthillerkus committed Apr 10, 2022
1 parent aadab96 commit 9e0da85
Show file tree
Hide file tree
Showing 10 changed files with 1,161 additions and 17 deletions.
40 changes: 23 additions & 17 deletions example/widgets_api/lib/main.dart
@@ -1,14 +1,13 @@
import 'package:betrayal/betrayal.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

void main() {
// Configure plugin log levels
BetrayalLogConfig.allowIndividualLevels();
BetrayalLogConfig.level = kDebugMode ? "FINER" : "INFO";



// This is only necessary if icons seem to persist after hot restarting.
// That happens when after restarting the app is not immediately interacting
// with [TrayIcon]s again.
Expand All @@ -18,8 +17,6 @@ void main() {
// Now the app will immediately need a new [TrayIcon]
// and the old one can be removed.



runApp(const MyApp());
}

Expand Down Expand Up @@ -93,25 +90,34 @@ class _MyHomePageState extends State<MyHomePage> {
duration: const Duration(milliseconds: 200),
child: Padding(
padding: const EdgeInsets.only(bottom: 32),
child: Builder(
// Get a fresh [BuildContext] that can access the [TrayIcon]
builder: (BuildContext context) => Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
for (var value in WinIcon.values)
IconButton(
tooltip: "Set the ${value.name} icon",
child: SizedBox(
height: 75,
child: ScrollConfiguration(
behavior: ScrollConfiguration.of(context)
.copyWith(dragDevices: {
PointerDeviceKind.touch,
PointerDeviceKind.mouse
}, scrollbars: true),
child: ListView.builder(
itemExtent: 75,
scrollDirection: Axis.horizontal,
itemCount: StockIcon.values.length,
itemBuilder:
(BuildContext context, int index) {
final icon = StockIcon.values[index];
return IconButton(
tooltip: "Set the ${icon.name} icon",
onPressed: shown
? () => TrayIcon.of(context)
.setImage(winIcon: value)
.setImage(stockIcon: icon)
: null,
icon: Text(value.emoji)),
],
icon: Text(icon.emoji));
},
),
),
),
),
)
),
],
),
)),
Expand Down
1 change: 1 addition & 0 deletions lib/betrayal.dart
@@ -1,4 +1,5 @@
export 'src/win_icon.dart';
export 'src/stock_icon.dart';
export 'src/win_event.dart';
export 'src/image.dart';
export 'src/plugin.dart';
Expand Down
6 changes: 6 additions & 0 deletions lib/src/image.dart
Expand Up @@ -5,6 +5,7 @@ import 'dart:typed_data';

import 'package:betrayal/src/plugin.dart';
import 'package:betrayal/src/win_icon.dart';
import 'package:betrayal/src/stock_icon.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
Expand Down Expand Up @@ -52,6 +53,11 @@ Continuing under the assumption that only the file extension is wrong""");
setIcon = (id, plugin) => plugin.setImageAsWinIcon(id, winIcon.code);
}

/// A [TrayIconImageDelegate] that uses a Windows system icon.
TrayIconImageDelegate.fromStockIcon(StockIcon stockIcon) {
setIcon = (id, plugin) => plugin.setImageAsStockIcon(id, stockIcon.code);
}

/// A [TrayIconImageDelegate] that uses an RGBA image buffer.
///
/// The buffer is expected to be in the format of a 32x32 RGBA image.
Expand Down
3 changes: 3 additions & 0 deletions lib/src/imperative.dart
Expand Up @@ -184,6 +184,7 @@ class TrayIcon {
Uri? path,
ByteBuffer? pixels,
String? asset,
StockIcon? stockIcon,
WinIcon? winIcon,
}) async {
_ensureIsActive();
Expand All @@ -196,6 +197,8 @@ class TrayIcon {
delegate = TrayIconImageDelegate.fromAsset(asset);
} else if (path != null) {
delegate = TrayIconImageDelegate.fromPath(uri: path);
} else if (stockIcon != null) {
delegate = TrayIconImageDelegate.fromStockIcon(stockIcon);
} else if (winIcon != null) {
delegate = TrayIconImageDelegate.fromWinIcon(winIcon);
} else {
Expand Down
10 changes: 10 additions & 0 deletions lib/src/plugin.dart
Expand Up @@ -12,6 +12,7 @@ import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';

import 'image.dart';
import 'stock_icon.dart';
import 'win_icon.dart';
import 'win_event.dart';

Expand Down Expand Up @@ -175,6 +176,15 @@ class BetrayalPlugin {
'setImageAsWinIcon', {'id': id, 'resourceId': resourceId});
}

/// Sets the icon's image, by loading a stock icon.
///
/// Check out the [StockIcon] enum for a list of available icons.
@protected
Future<void> setImageAsStockIcon(Id id, int stockIconId) async {
await _channel.invokeMethod(
'setImageAsStockIcon', {'id': id, 'stockIconId': stockIconId});
}

/// Sets the icon's image, by loading a byte buffer.
///
/// The buffer has to be in the format of a 32-bit RGBA image.
Expand Down

0 comments on commit 9e0da85

Please sign in to comment.