0

I am having some trouble loading additional icons into a IconTheme. At the moment my code is adding the path which is showing correctly with search_path() however it always returns false when I check for an image with has_icon(). Are there any examples of how to do this in Rust anyone knows about?

I have found a bunch of documentation on this in C but I'm struggling to translate it over. I have looked at this, this and this so far but if there are better examples that would help a lot.

This is how I am trying to add and search for the images at the moment which compiles but can't find the images.

use gtk::prelude::*;
use gtk::gdk::Display;
use adw::{Application, ApplicationWindow};
use std::path::PathBuf;

const APP_ID: &str = "org.test.app";

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_startup(|_| load_images());
    app.connect_activate(build_ui);
    app.run();
}

fn load_images() {
    let mut icon_path = PathBuf::new();
    icon_path.push("/home/user/Projects/test_app/src/Icons");

    let icons = gtk::IconTheme::for_display(&Display::default().expect("Could not connect to a display."));
    icons.add_search_path(icon_path);

    println!("IconTheme.name = {:?}", icons.theme_name());
    println!("IconTheme.search_path() = {:?}", icons.search_path());                          // The search path is showing the dir containing the icon.
    println!("IconTheme.has_icon(\"test_icon.png\") = {}", icons.has_icon("test_icon.png"));  // Returns false.
}

pub fn build_ui(app: &Application) {    
    let window = ApplicationWindow::builder()
        .application(app)
        .title("test_app")
        .default_width(360)
        .default_height(720)
        .build();

    window.present();
}
4
  • I don't think you are supposed to use the filename, rather the icon name. That first link you posted says "The main reason for using a name rather than simply providing a filename is to allow different icons to be used depending on what “icon theme” is selected by the user". Commented Feb 10, 2023 at 23:10
  • Maybe try calling icon_names to see what the actual names of icons in the theme are. Commented Feb 10, 2023 at 23:14
  • So when I printed all of these icons out there were only the default icons. None of the icons from add_search_path() were added. Commented Feb 11, 2023 at 0:02
  • Does the directory contain an index.theme file as requested? Commented Feb 15, 2023 at 6:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.