relm/
widget.rs

1/*
2 * Copyright (c) 2017-2018 Boucher, Antoni <bouanto@zoho.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5 * this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to
7 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 * the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22use glib::{IsA, Object};
23
24use super::{Relm, run};
25use crate::state::Update;
26
27/// Trait to implement to manage widget's events.
28pub trait Widget
29    where Self: Update,
30          Self::Root: Clone + IsA<Object> + IsA<gtk::Widget>,
31{
32    /// The type of the root widget.
33    type Root;
34
35    /// Update the view after it is initially created.
36    /// This method is only useful when using the `#[widget]` attribute, because when not using it,
37    /// you can use the [`view()`](trait.Widget.html#tymethod.view) method instead.
38    fn init_view(&mut self) {
39    }
40
41    /// Method called when the widget is added to its parent.
42    /// This is currently only used to set the child properties of a widget as relm widget could
43    /// have child properties and we don't know its parent when it is defined. Thus, we call
44    /// on_add() when it is added to its parent to set the child properties.
45    fn on_add<W: IsA<gtk::Widget> + IsA<Object>>(&self, _parent: W) {
46    }
47
48    /// Get the parent ID.
49    /// This is useful for custom Container implementation: when you implement the
50    /// [`Container::add_widget()`](trait.Container.html#tymethod.add_widget), you might want to
51    /// insert widgets elsewhere depending of this id.
52    fn parent_id() -> Option<&'static str> {
53        None
54    }
55
56    // TODO: ajouter une méthode param() pour déterminer des paramètres qui seront pris en compte à
57    // l’ajout du widget.
58
59    /// Get the root widget of the view.
60    fn root(&self) -> Self::Root;
61
62    /// Create the window from this widget and start the main loop.
63    fn run(model_param: Self::ModelParam) -> Result<(), glib::BoolError>
64        where Self: 'static,
65    {
66        run::<Self>(model_param)
67    }
68
69    /// Create the initial view.
70    fn view(relm: &Relm<Self>, model: Self::Model) -> Self;
71}
72
73/// Trait implemented by the generator to ease the creation of tests of relm widgets using the
74/// view! macro.
75pub trait WidgetTest : Widget {
76    /// Represents the structure holding all the `StreamHandle`s. Useful for tests.
77    type Streams;
78    /// Represents the structure holding all the widgets. Useful for tests.
79    type Widgets;
80
81    /// Get the structure containing all the `StreamHandle`s. Useful for tests.
82    fn get_streams(&self) -> Self::Streams;
83
84    /// Get the structure containing all the widgets. Useful for tests.
85    fn get_widgets(&self) -> Self::Widgets;
86}