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();
}
John554
  • 145
  • 1
  • 7
  • 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". – harmic Feb 10 '23 at 23:10
  • Maybe try calling [icon_names](https://docs.rs/gtk4/latest/gtk4/struct.IconTheme.html#method.icon_names) to see what the actual names of icons in the theme are. – harmic Feb 10 '23 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. – John554 Feb 11 '23 at 00:02
  • Does the directory contain an `index.theme` file [as requested](https://gtk-rs.org/gtk4-rs/git/docs/gtk4/struct.IconTheme.html#search-path)? – frankenapps Feb 15 '23 at 06:59

0 Answers0