Search code examples
rustgtkgtk-rs

In gtk-rs, how do I get the current screen that a gtk::Window is (mostly) on?


What I'm trying to accomplish is to calculate the dpi (or, in my case, the dpmm) of the screen that my application is mostly on. So far as I know, the modern way to do that is to call gdk::Monitor::get_height_mm and gdk::Monitor::get_width_mm.

So, starting with window: gtk::Window, I'm trying to get the Monitor like so:

let screen = window.get_display().and_then(|display| get_monitor_at_window(&window));

But, gtk::Window does not have an IsA<gdk::Window> implementation:

8 |     window.get_display().and_then(|display| display.get_monitor_at_window(&window));
  |                                                                           ^^^^^^^ the trait `glib::IsA<gdk::auto::window::Window>` is not implemented for `gtk::Window`
  |
  = help: the following implementations were found:
            <gtk::Window as glib::IsA<glib::Object>>
            <gtk::Window as glib::IsA<gtk::Bin>>
            <gtk::Window as glib::IsA<gtk::Buildable>>
            <gtk::Window as glib::IsA<gtk::Container>>
          and 2 others

So, what is the path to get from gtk::Window to the millimeters of width and height for the screen that the window is on?


Solution

  • A gtk::Window is not the same as a gdk::Window. The former is more like the concept of a "window", while the latter maps better to the concept of X windows. In other display protocols, these are also called "surfaces", which is why it got renamed in GTK 4, also to clear up this confusion.

    In any case, to get the underlying surface/window of a widget in GTK, you can call the get_window() method on it (in gtk-rs, this is part of the gtk::WidgetExt trait)