简体   繁体   English

如何创建一个不仅仅包含菜单项列表的MenuBar应用程序(使用Swift)?

[英]How can I make a MenuBar app (using Swift) that has more than just a list of menu items?

I'm new to OSX development (I've done a little iOS stuff, but this is my first OSX app, and my first Swift app). 我是OSX开发的新手(我做了一些iOS的东西,但这是我的第一个OSX应用程序,也是我的第一个Swift应用程序)。

I've found several tutorials for how to make a MenuBar app with just a simple list of menu items, like the following: 我找到了几个关于如何使用简单的菜单项列表制作MenuBar应用程序的教程,如下所示:

简单的菜单

But I'm looking for something more like Evernote's, or Google Drive's, or Mint's MenuBar window: 但我正在寻找更像Evernote,或Google Drive,或Mint的MenuBar窗口的东西:

窗口

I believe that to do something like the latter, I need a regular NSWindow that is just toggled by clicking the status item (right?), but how do I go about positioning the window the way Evernote & Google do? 我相信要做类似后者的事情,我需要一个常规的NSWindow,只需通过点击状态项(右边?)来切换,但我如何按照Evernote和Google的方式来定位窗口?

I've found this question that gives a couple of ways to get the status item position, if using an Objective C app, but I'm not sure how to translate that to a Swift app. 我发现这个问题提供了几种获取状态项位置的方法,如果使用Objective C应用程序,但我不知道如何将其转换为Swift应用程序。 For example, one answer suggests using the following to get the frame of the status item after clicking on it: 例如,一个答案建议使用以下内容在单击后获取状态项的框架:

[[[NSApp currentEvent] window] frame]

but, in Swift, NSApp.currentEvent does not have a member named "window"... 但是,在Swift中,NSApp.currentEvent没有名为“window”的成员...

Does anyone know of any example MenuBar apps, written in Swift, that have more than just a simple list of menu items? 有没有人知道任何用Swift编写的MenuBar应用程序,它不仅仅包含一个简单的菜单项列表? Is such a thing simply not possible with Swift, yet? 这样的事情对Swift来说根本不可能吗?


UPDATE: 更新:
Since the examples and starter apps that do this kind of thing in Objective C are abundant, and there's little or nothing on how to do it in Swift, I ended up abandoning the Swift route and built an Objective C app, instead. 由于在Objective C中执行此类操作的示例和入门应用程序非常丰富,而且在Swift中如何做到这一点很少甚至没有,我最终放弃了Swift路径并构建了一个Objective C应用程序。 I used this one as a starter: https://github.com/shpakovski/Popup . 我用这个作为首发: https//github.com/shpakovski/Popup It was very quick/easy to get started and ended up being a lot less work than a Swift app would have been. 它非常快速/容易上手,最终比Swift应用程序的工作少得多。

Maybe one day when Swift is more mature, I'll try another one in that language. 也许有一天当Swift更成熟时,我会用那种语言尝试另一种。

实际上, NSApplication.sharedApplication().currentEvent?.window似乎很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM