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
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned};

use crate::widgets::{
    ConditionalBranches, ConditionalWidget, Properties, Property, PropertyType, Widget, WidgetAttr,
    WidgetTemplateAttr,
};

use super::util::WidgetFieldsScope;

impl Property {
    fn init_stream(&self, stream: &mut TokenStream2) {
        match &self.ty {
            PropertyType::Widget(widget) => {
                widget.init_stream(stream);
            }
            PropertyType::ConditionalWidget(cond_widget) => {
                cond_widget.init_stream(stream);
            }
            _ => (),
        }
    }
}

impl Properties {
    fn init_stream(&self, stream: &mut TokenStream2) {
        for prop in &self.properties {
            prop.init_stream(stream);
        }
    }
}

impl Widget {
    pub(crate) fn init_stream(&self, stream: &mut TokenStream2) {
        self.self_init_stream(stream);
        self.other_init_stream(stream);
    }

    pub(crate) fn init_root_init_streams(
        &self,
        init_root_stream: &mut TokenStream2,
        init_stream: &mut TokenStream2,
    ) {
        // Init function as return value
        init_root_stream.extend(match self.template_attr {
            WidgetTemplateAttr::None | WidgetTemplateAttr::TemplateChild => {
                self.func.func_token_stream()
            }
            WidgetTemplateAttr::Template => {
                let widget_ty = &self.func.path;
                quote! {
                    <#widget_ty as relm4::WidgetTemplate>::init()
                }
            }
        });

        self.get_template_child_in_scope(init_stream, WidgetFieldsScope::Init);
        self.other_init_stream(init_stream);
    }

    fn self_init_stream(&self, stream: &mut TokenStream2) {
        let mutability = &self.mutable;
        let name = &self.name;

        let ty = self.func.ty.as_ref().map(|ty| quote! {: #ty});
        if self.attr == WidgetAttr::None {
            match self.template_attr {
                WidgetTemplateAttr::None => {
                    let func = self.func.func_token_stream();
                    stream.extend(quote! {
                        let #mutability #name #ty = #func;
                    });
                }
                WidgetTemplateAttr::Template => {
                    let widget_ty = &self.func.path;
                    stream.extend(quote! {
                        let #mutability #name #ty = <#widget_ty as relm4::WidgetTemplate>::init();
                    });
                }
                // Template children are already initialized by their template.
                WidgetTemplateAttr::TemplateChild => (),
            }
        }

        self.get_template_child_in_scope(stream, WidgetFieldsScope::Init);
    }

    fn other_init_stream(&self, stream: &mut TokenStream2) {
        self.properties.init_stream(stream);
    }
}

impl ConditionalWidget {
    fn init_stream(&self, stream: &mut TokenStream2) {
        let name = &self.name;
        let gtk_import = crate::gtk_import();

        stream.extend(quote_spanned! {
            name.span() =>
                let #name = #gtk_import::Stack::default();
        });

        if let Some(transition) = &self.transition {
            stream.extend(quote_spanned! {
                transition.span() =>
                    #name.set_transition_type(#gtk_import::StackTransitionType:: #transition);
            });
        }

        match &self.branches {
            ConditionalBranches::If(if_branches) => {
                for branch in if_branches {
                    branch.widget.init_stream(stream);
                }
            }
            ConditionalBranches::Match((_, _, match_arms)) => {
                for arm in match_arms {
                    arm.widget.init_stream(stream);
                }
            }
        }
    }
}