rustgtk3gdk

Rust. GTK3. get_screen() method not found in `gtk::Window`


I want to use css for my window using "add_provider_for_screen" method.

pub fn add_provider_for_screen(screen: &gdk::Screen, provider: &impl IsA<StyleProvider>, priority: u32) 

To do this, I need to convert the window to a GdkScreen using the get_screen() method. However, when I try to do so, I get error: method not found in &gtk::Window . https://docs.gtk.org/gtk3/method.Window.get_screen.html

main.rs code:

use std::process::exit;
use gdk_pixbuf::Pixbuf;
use gtk::{Window, Image, Orientation, WindowType, CssProvider};
use gtk::prelude::{GtkWindowExt, WidgetExt, ContainerExt, ButtonExt, CssProviderExt};

fn main() {
    gtk::init().unwrap();
    init_window();
    gtk::main();
}
fn init_css(win: &gtk::Window) {
    let css = CssProvider::new();
    css.load_from_path("main.css").unwrap();
    let screen = win.gtk_window_get_screen().unwrap();
    gtk::StyleContext::add_provider_for_screen(screen, &css, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION);
}

fn init_window() {
    let window = gtk::Window::new(WindowType::Toplevel);
    init_css(&window);
// ....
}

Also, if i trying create GdkScreen in fn init_window(), I get a similar error: method not found in gtk::Window

let screen = window.gtk_screen().unwrap();

Cargo.toml content:

[dependencies]
gtk="0.15.5"
gdk="0.15.4"
gdk-pixbuf="0.15.11"

Solution

  • From the documentation, from a Widgnet, use method WidgetExt::screen:

    Get the gdk::Screen from the toplevel window associated with this widget. This function can only be called after the widget has been added to a widget hierarchy with a Window at the top.