问题
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. 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
. 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. 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.
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. 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.
Thank you very much.
回答1:
While most of the user-visible parts of Gnome Shell are written in JavaScript, these are often just bindings for the underlying C libraries. 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:
- Mutter: https://developer.gnome.org/meta/stable/
- Shell: https://developer.gnome.org/shell/stable/
- St (Shell Toolkit): https://developer.gnome.org/st/stable/index.html
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. MetaWindow
, on the other hand, contains a function called get_monitor() which returns an integer. 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.
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. It also includes a Monitor class, which seems to just be a JS wrapper around the monitor index. This class is used here in the LayoutManager
class (which is the definition of the instance 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. 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.
The API has settled down a lot, but no one has really stepped up to write a script to auto-document it, yet. My best advice would be to bookmark the Mutter, Shell and St documentation and use Github or GitLab's search to make things easier.
There is however documentation for the Gnome API as well some of the built-in modules that are worth a skim:
- Gnome API: https://devdocs.baznga.org/
- Modules: https://gitlab.gnome.org/GNOME/gjs/wikis/Modules
- General GObject usage in GJS: https://gitlab.gnome.org/GNOME/gjs/wikis/Mapping
来源:https://stackoverflow.com/questions/50080430/how-do-i-get-the-monitor-of-an-active-window-in-gnome