NSStatusItem中的NSProgressIndicator [英] NSProgressIndicator in NSStatusItem
问题描述
我有NSStatusItem与自定义NSView显示图像。
无论菜单是否打开,它都会显示不同的图像:
I have NSStatusItem with a custom NSView which shows images. Whether the menu is opened or not it show different images just like that:
isMenuVisible = NO;
- (void)awakeFromNib {
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[statusItem retain];
dragStatusView = [[DragStatusView alloc] init];
[dragStatusView retain];
dragStatusView.statusItem = statusItem;
[dragStatusView setMenu:statusMenu];
[dragStatusView setToolTip:NSLocalizedString(@"Menubar Countdown",
@"Status Item Tooltip")];
[statusItem setView:dragStatusView];
[dragStatusView setTitle:@"11"];
}
- (void)drawImage:(NSImage *)aImage centeredInRect:(NSRect)aRect{
NSRect imageRect = NSMakeRect((CGFloat)round(aRect.size.width*0.5f-aImage.size.width*0.5f),
(CGFloat)round(aRect.size.height*0.5f-aImage.size.height*0.5f),
aImage.size.width,
aImage.size.height);
[aImage drawInRect:imageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
}
- (void)drawRect:(NSRect)rect {
// Draw status bar background, highlighted if menu is showing
[statusItem drawStatusBarBackgroundInRect:[self bounds]
withHighlight:isMenuVisible];
if(isMenuVisible) {
[self drawImage:image2 centeredInRect:rect];
}else {
[self drawImage:image1 centeredInRect:rect];
}}
(当然这不是一切,所有相关代码来了解我的问题)
(Of course this is not everything, but i hope all the relevant code to understand my problem)
现在我想在这个NSView(在这NSStatusItem中)显示一个NSProgressIndicator如果上传正在进行,意味着
1.上传开始时设置NSProgressIndicator
2.接收某事? ==>隐藏NSProgressIndicator再次显示图片。
Now i want to show a NSProgressIndicator in this NSView (in this NSStatusItem) if an upload is proceeding which means 1. Set the NSProgressIndicator when upload starts 2.Received something ? ==> Hide NSProgressIndicator show the image again.
我如何解决这个问题?
请帮助我。感谢
推荐答案
以下是基于视图状态项的解决方案。
Here is the solution for View based status item.
NSStatusBar *bar = [NSStatusBar systemStatusBar];
self.theItem = [bar statusItemWithLength:NSVariableStatusItemLength];
self.statusView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 40, 20)];
NSProgressIndicator* progress = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(0, 0, 40, 20)];
[progress setIndeterminate:NO];
[self.statusView addSubview:progress];
self.theItem.view = self.statusView;
您还可以在基于菜单的状态项中绘制进度条
You can also draw a progress bar your self in Menu based status item
on progress change you can simply change the image of your status item.
- (void) updateIconWithProgress:(float)aProgress
{
NSImage* image = [NSImage imageNamed:@"small_icon_16_16.png"];
NSImage* img = [image copy];
[img lockFocus];
NSRect rect = NSMakeRect(2, 4, 12.0*aProgress, 8.0);
[[NSColor blueColor] set];
[NSBezierPath fillRect:rect];
[img unlockFocus];
[self.theItem setImage:img];
}
p.s。代码是为ARC(自动引用计数)编写的,因此没有保留或释放。
p.s. The code was written for ARC (Automatic Reference Counting) so there is no retain or release.
这篇关于NSStatusItem中的NSProgressIndicator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!