GTK+ framework

GTK+ (hereafter gtk) is a cross-platform GUI framework that was created in C. Being cross-platform, applications that are developed using gtk can run on all major platforms, such as Windows, Linux, or MacOS. The gtk project was originally created to develop GIMP, image manipulation software for Linux, and was later open sourced. gtk is also used by many other software projects, such as the Gnome desktop environment on many Linux distributions, which uses it for building its utility software. Architecture-wise, gtk is composed of several libraries that work together to handle various details that are needed to render and facilitate interaction with windows and widgets by the user in the application. Some of these components are as follows:

  • GLib: This is the basic core library and provides several data structures, wrappers for portability, and interfaces for runtime functionality such as the event loop, thread support, dynamic loading, and an object system. Glib itself is composed of components such as GObject, which provides an object model, and GIO, which provides high-level abstractions for I/O.
  • Pango: Pango is a library that provides text rendering and internationalization capabilities.
  • Cairo: This is a 2D graphics library that takes care of drawing things on-screen and, tries to be as consistent across multiple devices, and handles details such as hardware acceleration.
  • ATK: ATK is the accessibility toolkit library that takes care of providing accessibility to input devices such as screen readers, magnifiers, and alternative input devices.

gtk also has an interface builder called Glade, which generates a gtk source code skeleton for rapid application development.

gtk uses the object-oriented model for representing widgets and windows. It leverages the GObject library to provide this abstraction. To use the gtk framework from Rust, we have the gtk-rs project, which contains many crates that follow the same naming convention as libraries that exist in gtk and provides native C bindings to these libraries. Among all the crates that the gtk-rs project contains, we'll be using the gtk crate to build our app.

The gtk crate provides the window and widget system for building GUIs and tries to model the same API as the native C library, though there are differences as Rust does not have an object-oriented type system. Widgets in the gtk crate are smart pointer types. To allow for flexibility when using the API, you can have many mutable references, similar to the one provided by interior mutability in Rust. Any non-trivial widget in gtk inherits from some base widget type. Rust supports this inheritance in widgets via the IsA<T> trait. For instance, the gtk::Label widget has an impl of impl IsA<Widget> for Label. Also, most widgets in gtk share functionality with each other—the gtk crate implements this with the idea of extension traits, such as the WidgetExt trait, for all widget types. Most widgets such as gtk::Button and gtk::ScrollableWindow implement the WidgetExt trait. A widget can also be downcast or upcast to other widgets in its hierarchy using the Cast trait. With that brief introduction aside, let's get into writing a desktop app in Rust.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset