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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use gtk::glib;
use gtk::prelude::{BoxExt, GridExt, TreeViewExt};

use crate::factory::{positions::*, FactoryListView, FactoryView};

impl<Widget> FactoryView<Widget> for gtk::Box
where
    Widget: glib::IsA<gtk::Widget>,
{
    type Position = ();
    type Root = Widget;
    fn add(&self, widget: &Widget, _position: &()) -> Widget {
        self.append(widget);
        widget.clone()
    }

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

impl<Widget> FactoryListView<Widget> for gtk::Box
where
    Self: FactoryView<Widget, Root = Widget>,
    Widget: glib::IsA<gtk::Widget>,
{
    fn insert_after(&self, widget: &Widget, other: &Widget) -> Widget {
        self.insert_child_after(widget, Some(other));
        widget.clone()
    }

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

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

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

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

impl<Widget> FactoryListView<Widget> for gtk::ListBox
where
    Self: FactoryView<Widget, Root = Widget>,
    Widget: gtk::prelude::ListBoxRowExt + glib::IsA<gtk::Widget> + Clone,
{
    fn insert_after(&self, widget: &Widget, other: &Widget) -> Widget {
        self.insert(widget, other.index());
        widget.clone()
    }

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

impl FactoryView<gtk::TreeViewColumn> for gtk::TreeView {
    type Position = ();
    type Root = gtk::TreeViewColumn;

    fn add(&self, widget: &gtk::TreeViewColumn, _position: &()) -> gtk::TreeViewColumn {
        self.insert_column(widget, -1);
        widget.clone()
    }

    fn remove(&self, widget: &gtk::TreeViewColumn) {
        self.remove_column(widget);
    }
}

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

    fn add(&self, widget: &Widget, _position: &()) -> Widget {
        self.insert(widget, -1);
        widget.clone()
    }

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

impl<Widget> FactoryListView<Widget> for gtk::FlowBox
where
    Self: FactoryView<Widget, Root = Widget>,
    Widget: gtk::prelude::FlowBoxChildExt + glib::IsA<gtk::Widget> + Clone,
{
    fn insert_after(&self, widget: &Widget, other: &Widget) -> Widget {
        self.insert(widget, other.index());
        widget.clone()
    }

    fn push_front(&self, widget: &Widget) -> Widget {
        self.insert(widget, 0);
        widget.clone()
    }
}

impl<Widget> FactoryView<Widget> for gtk::Stack
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 gtk::Fixed
where
    Widget: glib::IsA<gtk::Widget>,
{
    type Position = FixedPosition;
    type Root = Widget;

    fn add(&self, widget: &Widget, position: &FixedPosition) -> Widget {
        gtk::prelude::FixedExt::put(self, widget, position.x, position.y);
        widget.clone()
    }

    fn remove(&self, widget: &Widget) {
        gtk::prelude::FixedExt::remove(self, widget);
    }
}

impl<Widget> FactoryView<Widget> for gtk::Grid
where
    Widget: glib::IsA<gtk::Widget>,
{
    type Position = GridPosition;
    type Root = Widget;

    fn add(&self, widget: &Widget, position: &GridPosition) -> Widget {
        self.attach(
            widget,
            position.column,
            position.row,
            position.width,
            position.height,
        );
        widget.clone()
    }

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