January 2018
Beginner to intermediate
454 pages
10h 8m
English
In object-oriented languages, it is very easy to create new widgets and use them like built-in widgets. In this paradigm, you only need to create a new class that inherits from a widget and that's it.
In Chapter 5, Creating a Music Player, we created custom widgets, such as Playlist and MusicToolbar, but we needed to create a function to get the real GTK+ widget:
pub fn view(&self) -> &TreeView { &self.treeview }
An alternative would have been to implement the Deref trait:
use std::ops::Deref; impl Deref for Playlist { type Target = TreeView; fn deref(&self) -> &TreeView { &self.treeview } }
That implementation would allow us to add the widget to its parent like this:
parent.add(&*playlist);
(Note the leading * in ...