1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::factory::{
    positions::{StackPageInfo, TabPageInfo},
    FactoryListView, FactoryView,
};
use gtk::glib;

impl<Widget> FactoryView<Widget> for adw::Carousel
where
    Widget: glib::IsA<gtk::Widget>,
{
    type Position = ();
    type Root = Widget;

    fn add(&self, widget: &Widget, _position: &Self::Position) -> Widget {
        self.append(widget);
        widget.clone()
    }

    fn remove(&self, widget: &Widget) {
        self.remove(widget);
    }
}

impl<Widget> FactoryView<Widget> for adw::TabView
where
    Widget: glib::IsA<gtk::Widget>,
{
    type Position = TabPageInfo;
    type Root = adw::TabPage;

    fn add(&self, widget: &Widget, position: &TabPageInfo) -> adw::TabPage {
        let page = self.append(widget);

        if let Some(title) = &position.title {
            page.set_title(title);
        }

        if let Some(tooltip) = &position.tooltip {
            page.set_tooltip(tooltip);
        }

        page
    }

    fn remove(&self, widget: &adw::TabPage) {
        self.close_page_finish(widget, true);
    }
}

impl<Widget> FactoryListView<Widget> for adw::TabView
where
    Widget: glib::IsA<gtk::Widget>,
{
    fn insert_after(&self, widget: &Widget, other: &adw::TabPage) -> adw::TabPage {
        let position = self.page_position(other) + 1;
        self.insert(widget, position)
    }

    fn push_front(&self, widget: &Widget) -> adw::TabPage {
        self.prepend(widget)
    }
}

impl<Widget> FactoryView<Widget> for adw::ViewStack
where
    Widget: glib::IsA<gtk::Widget>,
{
    type Position = StackPageInfo;
    type Root = Widget;

    fn add(&self, widget: &Widget, position: &StackPageInfo) -> Widget {
        if let Some(title) = &position.title {
            self.add_titled(widget, position.name.as_deref(), title);
        } else {
            self.add_named(widget, position.name.as_deref());
        }
        widget.clone()
    }

    fn remove(&self, widget: &Widget) {
        self.remove(widget);
    }
}

impl<Widget> FactoryView<Widget> for adw::Leaflet
where
    Widget: glib::IsA<gtk::Widget>,
{
    type Position = ();
    type Root = adw::LeafletPage;

    fn add(&self, widget: &Widget, _position: &()) -> adw::LeafletPage {
        self.append(widget)
    }

    fn remove(&self, widget: &adw::LeafletPage) {
        self.remove(&widget.child());
    }
}

impl<Widget> FactoryListView<Widget> for adw::Leaflet
where
    Widget: glib::IsA<gtk::Widget>,
{
    fn insert_after(&self, widget: &Widget, other: &adw::LeafletPage) -> adw::LeafletPage {
        self.insert_child_after(widget, Some(&other.child()))
    }

    fn push_front(&self, widget: &Widget) -> adw::LeafletPage {
        self.prepend(widget)
    }
}