py-sdl / py-sdl2

Python ctypes wrapper around SDL2
Other
296 stars 49 forks source link

Window icon and title are not set properly #268

Open broughtong opened 1 year ago

broughtong commented 1 year ago

What doesn't work?

Changing a window icon using sdl2.SDL_SetWindowIcon doesn't work on Ubuntu 20.04.

Creating a window in the C++ SDL has no logo set by default (in pysdl2 it has a python logo by default).

Changing it with SDL_SetWindowIcon in C++ correctly sets it, but same code in python does nothing, the logo remains the Python logo. I've tried hiding the window to start with, setting the logo, and then showing it but same issue. Likewise the title showing in the taskbar is always "Python (v3.8)" regardless of what sdl2.SDL_SetWindowTitle is set to.

How To Reproduce Any code that reproduces the bug, e.g.:

import time
import ctypes
import sdl2
import sdl2.ext
import sdl2.sdlimage

def main():
    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)

    window = sdl2.SDL_CreateWindow(b"Test Window", sdl2.SDL_WINDOWPOS_UNDEFINED, sdl2.SDL_WINDOWPOS_UNDEFINED, 400, 400, 0) #sdl2.SDL_WINDOW_HIDDEN)

    icon = sdl2.sdlimage.IMG_Load(b"icon.png")
    sdl2.SDL_SetWindowIcon(window, icon)
    print(sdl2.SDL_GetError())

    sdl2.SDL_ShowWindow(window)

    sdl2.SDL_SetWindowTitle(window, b"New title")

    time.sleep(5)

    sdl2.SDL_DestroyWindow(window)
    sdl2.SDL_Quit()

if __name__ == "__main__":
    main()

Platform (if relevant):

broughtong commented 1 year ago

Similarly on Windows 11, in C++ SDL the taskbar icon is updated when the window icon is changed, but in PySDL the window icon will change, but the taskbar icon remains a default.

a-hurst commented 1 year ago

Hi @broughtong, this seems like a window manager-specific bug: with Fedora 38 and KDE, you test example works perfectly: Screenshot_20230817_140824

Can you try adding an sdl2.SDL_PumpEvents() call after showing your window? That updates various window properties on some window managers, from what I remember.

broughtong commented 1 year ago

Tried the Pump events, no change.

I tried also Kubuntu 20.04, which might clear up what is happening:

Screenshot_20230818_090614

The icon and title used on the window itself are correct.

The icon in the task bar though is just the regular python icon, and the title in this case "Python (v3.10)" with a subheading of the real window title of "some title".

In regular Ubuntu, due to the window manager the only logo visible at all is the Python one (in the taskbar to the left), the title on the window is correct, and the title at the top of the screen is just "Python (v3.x)".

In C++ SDL, in all cases the logo on both the window and taskbar are the one given by SDL_SetWindowIcon, and the title on both the window and taskbar is the one from SDL_SetWindowTitle

a-hurst commented 1 year ago

I can partially reproduce that: with the latest KDE Plasma from Fedora 38, I get the "python" main heading along with the proper window title as the subheading, but the custom-set window icon still shows fine for me in the task bar:

Screenshot_20230818_091006

From what I can tell, the top heading in the KDE taskbar is the name of the application itself, with the subheading being the title of that specific window (if there are multiple windows open for the same app, it lists the app title once and the window name for each separate window):

Screenshot_20230818_091947

As such, since whatever you're running with PySDL2 is run via the Python interpreter, most window managers will report that as the application name instead of the app's current window title. Seems this isn't an issue limited to PySDL2 either, with reports of the same problem applying to people using PyQT or PySide: https://stackoverflow.com/questions/27453436/set-application-name-in-pyside

Since this isn't PySDL2-specific, maybe there's a generic Python library out there that can set/override the current application title within a given Python script?

broughtong commented 1 year ago

Ok so I'm now pretty sure this is actually a bug within SDL itself, not setting the class/classname property correctly in the x server.

In C++ it doesn't matter as there is nothing already set, but when called through Python , it is getting some wrong info about the window.

If I alter the class/classname manually in X for the window, all calls (including previous calls) now take effect correctly in the taskbar.

Will report it over there.

To see the difference, open your window. Run xdotool selectwindow in a terminal and click the window to get the ID.

Then do xdotool set_window --class my_new_title WINDOWID and xdotool set_window --classname my_new_title WINDOWID

All icons and titles should now correctly appear.