Skip to content

Commit fd0b0c8

Browse files
committed
[macos] Implement setProgressBar method #40
1 parent 11ecf53 commit fd0b0c8

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

example/lib/pages/home.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class _HomePageState extends State<HomePage> with WindowListener {
3838
bool _isClosable = true;
3939
bool _isAlwaysOnTop = false;
4040
bool _isSkipTaskbar = false;
41+
double _progress = 0;
4142
bool _hasShadow = true;
4243
double _opacity = 1;
4344

@@ -433,6 +434,19 @@ class _HomePageState extends State<HomePage> with WindowListener {
433434
windowManager.show();
434435
},
435436
),
437+
PreferenceListItem(
438+
title: Text('setProgressBar'),
439+
onTap: () async {
440+
for (var i = 0; i <= 100; i++) {
441+
setState(() {
442+
_progress = i / 100;
443+
});
444+
print(_progress);
445+
await windowManager.setProgressBar(_progress);
446+
await Future.delayed(Duration(milliseconds: 100));
447+
}
448+
},
449+
),
436450
PreferenceListSwitchItem(
437451
title: Text('hasShadow / setHasShadow'),
438452
onTap: () async {

lib/src/window_manager.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ class WindowManager {
336336
await _channel.invokeMethod('setSkipTaskbar', arguments);
337337
}
338338

339+
/// Sets progress value in progress bar. Valid range is [0, 1.0].
340+
Future<void> setProgressBar(double progress) async {
341+
final Map<String, dynamic> arguments = {
342+
'progress': progress,
343+
};
344+
await _channel.invokeMethod('setProgressBar', arguments);
345+
}
346+
339347
/// Returns bool - Whether the window has a shadow.
340348
Future<bool> hasShadow() async {
341349
return await _channel.invokeMethod('hasShadow');

macos/Classes/WindowManager.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,43 @@ public class WindowManager: NSObject, NSWindowDelegate {
338338
NSApplication.shared.setActivationPolicy(isSkipTaskbar ? .accessory : .regular)
339339
}
340340

341+
public func setProgressBar(_ args: [String: Any]) {
342+
let progress: CGFloat = CGFloat(truncating: args["progress"] as! NSNumber)
343+
344+
let dockTile: NSDockTile = NSApp.dockTile;
345+
346+
let firstTime = dockTile.contentView == nil || dockTile.contentView?.subviews.count == 0
347+
348+
if (firstTime) {
349+
let imageView: NSImageView = NSImageView.init()
350+
imageView.image = NSApp.applicationIconImage
351+
dockTile.contentView = imageView
352+
353+
let frame: NSRect = NSMakeRect(0.0, 0.0, dockTile.size.width, 15.0)
354+
let progressIndicator: NSProgressIndicator = NSProgressIndicator.init(frame: frame)
355+
progressIndicator.style = .bar
356+
progressIndicator.isIndeterminate = false
357+
progressIndicator.isBezeled = true
358+
progressIndicator.minValue = 0
359+
progressIndicator.maxValue = 1
360+
progressIndicator.isHidden = false
361+
dockTile.contentView?.addSubview(progressIndicator)
362+
}
363+
364+
let progressIndicator: NSProgressIndicator = dockTile.contentView!.subviews.last as! NSProgressIndicator
365+
if (progress < 0) {
366+
progressIndicator.isHidden = true
367+
} else if (progress > 1) {
368+
progressIndicator.isHidden = false
369+
progressIndicator.isIndeterminate = true
370+
progressIndicator.doubleValue = 1
371+
} else {
372+
progressIndicator.isHidden = false
373+
progressIndicator.doubleValue = progress
374+
}
375+
dockTile.display()
376+
}
377+
341378
public func hasShadow() -> Bool {
342379
return mainWindow.hasShadow
343380
}

macos/Classes/WindowManagerPlugin.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ public class WindowManagerPlugin: NSObject, FlutterPlugin {
180180
windowManager.setSkipTaskbar(args)
181181
result(true)
182182
break
183+
case "setProgressBar":
184+
windowManager.setProgressBar(args)
185+
result(true)
186+
break
183187
case "hasShadow":
184188
result(windowManager.hasShadow())
185189
break

0 commit comments

Comments
 (0)