View Single Post
Old 12-02-2024, 07:31 AM   #1885
thiago.eec
Wizard
thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.thiago.eec ought to be getting tired of karma fortunes by now.
 
Posts: 1,218
Karma: 1419583
Join Date: Dec 2016
Location: Goiânia - Brazil
Device: iPad, Kindle Paperwhite, Kindle Oasis
Quote:
Originally Posted by dunhill View Post
Light Mode/Dark Mode Plugin Icons
I have a humble plugin to which I would like to be able to add this function.
I know that there has to be a folder inside the image plugin with two files indicating icon_dark and icon_light. But I don't know what code I should add and where for this purpose. If it's not too much trouble, could someone guide me? I've reviewed some plugins and I feel like I'm not finding something.
Did you tried what I suggested via PM? Here is with a detailed explanation (Python 3):

First, put your suffixed icons in the 'images' folder inside your plugin zip file. E.g.: 'images/icon_dark.png', 'images/icon_light.png'.

Place this at the start of your main.py or similar:
Code:
# Check for dark theme
def is_dark_theme():
    return QApplication.instance().is_dark_theme
Then, the get_icon() function:
Code:
def get_icon(icon_name):
    icon_name = 'images/' + icon_name

    # Choose the suffix
    tc = 'dark' if is_dark_theme() else 'light'
    sq, ext = os.path.splitext(icon_name)
    sq = f'{sq}_{tc}{ext}'

    return get_icons(sq)
Then, in your code, use the get_icon() function to retrieve the icon like this:

Code:
icon = get_icon(icon_name.png)
Note that you use the icon name without the suffix (dark/light).

This will allow the plugin to change from light/dark theme when calibre starts. If you want it to change without restarting, than you need to rebuild the menus on the fly. Check out the rebuild_menus() method present in many plugins (ui.py).
thiago.eec is offline   Reply With Quote