[英]How do I get the monitor of an active window in GNOME?
I am very new to GNOME extension development, and I am having a hard time working with it, due to a profound lack of documentation (or maybe my Internet is clandestinely censored) of the API.我对 GNOME 扩展开发非常陌生,由于 API 的文档严重缺乏(或者我的互联网可能被秘密审查),我很难使用它。 I started by modifying an existing extension, so that it is easier to make my way around it.
我首先修改了一个现有的扩展,以便更容易地绕过它。
The issue is, I can obtain the active window using global.display.focus_window
, and also a list of monitors connected to the computer using Main.layoutManager.monitors
.问题是,我可以使用
global.display.focus_window
获取活动窗口,以及使用Main.layoutManager.monitors
连接到计算机的监视器列表。 Now, what I would like to do, is find out which monitor the obtained window is sitting on (so I can move the top panel to that monitor, as it probably means I am working on that monitor at the moment).现在,我想做的是找出获得的窗口所在的显示器(这样我就可以将顶部面板移到该显示器上,因为这可能意味着我目前正在该显示器上工作)。 I tried various things, like .screen, .monitor etc., but with no success.
我尝试了各种东西,比如 .screen、.monitor 等,但没有成功。 I have no IntelliSense completion on this, and I am trying to guess what the members could be, as I cannot seem to find any docs on it.
我在这方面没有 IntelliSense 完成,我试图猜测成员可能是什么,因为我似乎找不到任何关于它的文档。
I appreciate the fact that GNOME is way more customizable than what I used before (Unity, which provided no customization at all), but I don't know how to work with it and resources are scarce.我很欣赏 GNOME 比我以前使用的更可定制(Unity,它根本不提供定制),但我不知道如何使用它,而且资源稀缺。 I tried looking into the source code, but I am not familiar with how it is organized and I could not find the relevant portion of code where the members I need, if they exist, are declared.
我尝试查看源代码,但我不熟悉它的组织方式,并且找不到声明我需要的成员(如果存在)的相关代码部分。
I am coding the .js files, so I need some JavaScript code, I guess.我正在编写 .js 文件,所以我想我需要一些 JavaScript 代码。
Thank you very much.非常感谢。
While most of the user-visible parts of Gnome Shell are written in JavaScript, these are often just bindings for the underlying C libraries.虽然 Gnome Shell 的大多数用户可见部分都是用 JavaScript 编写的,但这些通常只是底层 C 库的绑定。 If you're working with Windows, Monitors and Screens then you're going to want to reference the Mutter documentation and probably the Shell documentation as well:
如果您正在使用 Windows、监视器和屏幕,那么您将需要参考 Mutter 文档以及可能的 Shell 文档:
There is a property on the global
object called screen
(so global.screen
) which is no doubt a MetaScreen
which has a function get_n_monitors()
, as well as get_primary_monitor()
, get_current_monitor()
and others. global
对象上有一个名为screen
(所以global.screen
)的属性,它无疑是一个MetaScreen
,它具有函数get_n_monitors()
,以及get_primary_monitor()
、 get_current_monitor()
等。 MetaWindow
, on the other hand, contains a function called get_monitor()
which returns an integer. MetaWindow
,在另一方面,包含一个函数调用get_monitor()
返回一个整数。 I gather that monitors are referred to by integer number, which is passed to various functions of MetaScreen
and MetaWindow
, since there doesn't seem to be an object for that in the Mutter documentation.我认为监视器是由整数引用的,整数被传递给
MetaScreen
和MetaWindow
各种函数,因为 Mutter 文档中似乎没有一个对象。
Most of the related JavaScript for what you want to do seems to be in layout.js , which probably has better examples of how Mutter is used in Gnome Shell than I can give you.大多数与您想要做的事情相关的 JavaScript 似乎都在layout.js 中,它可能比我给您的 Gnome Shell 中如何使用 Mutter 提供了更好的示例。 It also includes a
Monitor
class, which seems to just be a JS wrapper around the monitor index.它还包括一个
Monitor
类,它似乎只是一个围绕监视器索引的 JS 包装器。 This class is used in the LayoutManager
class (which is the definition of the instance Main.layoutManager
).这个类用于
LayoutManager
类(它是实例Main.layoutManager
的定义)。
A note about documentation关于文档的说明
Originally, the rationale for not having "proper" gnome-shell documentation was that the (internal JavaScript) API was pretty unstable.最初,没有“正确”的 gnome-shell 文档的理由是(内部 JavaScript)API 非常不稳定。 The deal was, you don't get a stable API but you get to read the source in the same language you're going to write it in. In some ways this makes sense, given that you can modify the
prototype
of live objects and monkey-patch at whim.交易是,您没有获得稳定的 API,但您可以使用将要编写它的相同语言读取源代码。在某些方面,这是有道理的,因为您可以修改活动对象的
prototype
并一时兴起的猴子补丁。
The API has settled down a lot, but no one has really stepped up to write a script to auto-document it, yet. API 已经稳定了很多,但还没有人真正加紧编写脚本来自动记录它。 My best advice would be to bookmark the Mutter, Shell and St documentation and use Github or GitLab's search to make things easier.
我最好的建议是为 Mutter、Shell 和 St 文档添加书签,并使用 Github 或 GitLab 的搜索使事情变得更容易。
There is however documentation for the Gnome API as well some of the built-in modules that are worth a skim:然而,有 Gnome API 的文档以及一些值得浏览的内置模块:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.