activate-window-by-title icon indicating copy to clipboard operation
activate-window-by-title copied to clipboard

Add activateById method

Open poinu opened this issue 2 years ago • 5 comments

Hi! Thank you for this extension :)

In my use cases I find it useful to be able to target an specific window instance when I have multiple instances of the same application running. The WM_CLASS methods don't help with this since both strings are the same for all instances.

For example, this method allows me to save the ID for the window that currently has the focus, set the focus to some other window, and then return the focus to the original window.

poinu avatar Apr 05 '24 12:04 poinu

Hm, I’m not convinced by this yet, and I’m not super excited about further diluting the “activate window by title” name :sweat_smile:

I started this extension when Gnome took away the ability to run JavaScript via dbus – if you need to use lg to get the window ID, you’re already in an environment where you can run JS, so what’s the benefit that the extension brings in that case? (Arguably you could say something similar about the WM_CLASS, but that should at least be relatively stable over time, so you only need to use lg to get the WM_CLASS once. I assume the window ID changes with each new session.)

Can you elaborate on your use case a bit more?

lucaswerkmeister avatar Apr 06 '24 18:04 lucaswerkmeister

I’m not super excited about further diluting the “activate window by title” name 😅

I can understand that, I just felt it fitted nicely with the code structure and the 'activate by WM_CLASS' methods.

Can you elaborate on your use case a bit more?

I need to briefly set the focus to an specific window (by title), and then move the focus back to the window where it originally was. I can't do that by title because I might have multiple windows with the same title (and WM_CLASS), and it would just focus one of these arbitrarily (or the first one, as discussed in #4). With the window ID I can focus the exact window I'm interested in.

I'm using the window-calls-extended extension to get the IDs.

The flow is as follows:

  1. Capture the current window ID (using the window-calls-extended extension)
  2. Switch focus to another window (by title)
  3. Switch back to the previous window, using the captured ID

I can share the full script if interested.

poinu avatar Apr 09 '24 11:04 poinu

I’m not super excited about further diluting the “activate window by title” name 😅

I can understand that, I just felt it fitted nicely with the code structure and the 'activate by WM_CLASS' methods.

Yeah, I set myself up for this when I accepted that feature request, didn’t I :laughing:

I'm using the window-calls-extended extension to get the IDs.

That’s neat, I didn’t know about that extension. (More DBUS extensions \o/) But I don’t see a method to get the ID in there? I see a method to get the PID, but I assume that’s not the same thing – a process can probably have multiple windows. (Unfortunately, the Mutter API reference seems to have vanished alongside the rest of Mutter’s website as linked from their README, so I can’t easily look at the docs of get_id() and get_pid() at the moment.)

I can share the full script if interested.

Sure!

lucaswerkmeister avatar Apr 10 '24 19:04 lucaswerkmeister

Oh, nevermind, I guess you’re using the List method which returns JSON that includes both the ID and PID?

lucaswerkmeister avatar Apr 10 '24 19:04 lucaswerkmeister

But I don’t see a method to get the ID in there? I see a method to get the PID, but I assume that’s not the same thing – a process can probably have multiple windows. (Unfortunately, the Mutter API reference seems to have vanished alongside the rest of Mutter’s website as linked from their README, so I can’t easily look at the docs of get_id() and get_pid() at the moment.)

The docs are back up (get_id(), get_pid()), and they’re indeed different.

lucaswerkmeister avatar Apr 24 '24 19:04 lucaswerkmeister

This looks interesting; any progress?

LaptopDev avatar Dec 28 '24 18:12 LaptopDev

I think I was still waiting for a reply from @poinu (or someone else) about how a user would get the window ID in the first place – it’s not exactly a technical blocker, but it would also be useful to mention in the README when documenting the new method (in addition to satisfying my own curiosity :P).

lucaswerkmeister avatar Dec 28 '24 21:12 lucaswerkmeister

I can get window id's with that list window-calls-extended method

LaptopDev avatar Dec 30 '24 02:12 LaptopDev

Alright, I’ve rebased and updated the change, and also added a bit of debug code that I’ll remove again later. I’ll test this the next time I restart my GNOME Shell (which may be a few days, tbh).

lucaswerkmeister avatar Jan 06 '25 20:01 lucaswerkmeister

Okay, testing shows that, as I feared, GNOME Shell always passes the D-Bus UINT64 (t) into our JavaScript code as a number, even if this changes the value due to floating-point imprecision. (I was hoping maybe it would be turned into a numeric string in that case…) This means the extension won’t work for IDs above ca. 2^54, which technically is the vast majority of the ID space (but in practice hopefully the shell chooses lower window IDs). I don’t think there’s anything we can do about that.

lucaswerkmeister avatar Jan 10 '25 19:01 lucaswerkmeister

Tagged and uploaded as version 10 of the extension (now pending review).

lucaswerkmeister avatar Jan 10 '25 19:01 lucaswerkmeister

Now published \o/

lucaswerkmeister avatar Jan 10 '25 20:01 lucaswerkmeister