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
//! Reusable and easily configurable save dialog component.
//!
//! **[Example implementation](https://github.com/Relm4/relm4/blob/main/relm4-examples/examples/save_dialog.rs)**

use gtk::prelude::{FileChooserExt, FileExt, NativeDialogExt};
use relm4::{gtk, send, ComponentUpdate, Model, Sender};

use std::marker::PhantomData;
use std::path::PathBuf;

use crate::ParentWindow;

#[derive(Clone, Debug)]
/// Configuration for the save dialog component
pub struct SaveDialogSettings {
    /// Label for cancel button
    pub cancel_label: &'static str,
    /// Label for accept button
    pub accept_label: &'static str,
    /// Allow or disallow creating folders
    pub create_folders: bool,
    /// Modal dialogs freeze other windows as long they are visible
    pub is_modal: bool,
    /// Filter for MINE types or other patterns
    pub filters: Vec<gtk::FileFilter>,
}

///Interface for building the configuration for SaveDialog
pub trait SaveDialogConfig {
    /// Model from which configuration should be built
    type Model: Model;
    /// Configure the save dialog
    fn dialog_config(model: &Self::Model) -> SaveDialogSettings;
}

#[tracker::track]
#[derive(Debug)]
/// Model of the save dialog component
pub struct SaveDialogModel<Conf: SaveDialogConfig> {
    #[do_not_track]
    settings: SaveDialogSettings,
    suggestion: Option<String>,
    is_active: bool,
    name: String,
    #[do_not_track]
    _config_provider: PhantomData<*const Conf>, //we don't own Conf, there is no instance of Conf
}

#[derive(Debug)]
/// Messages that can be sent to the save dialog component
pub enum SaveDialogMsg {
    /// Opens the dialog
    Save,
    /// Opens the dialog with a suggested file name
    SaveAs(String),
    #[doc(hidden)]
    Accept(PathBuf),
    #[doc(hidden)]
    InvalidInput,
    #[doc(hidden)]
    Cancel,
}

impl<Conf: SaveDialogConfig> Model for SaveDialogModel<Conf> {
    type Msg = SaveDialogMsg;
    type Widgets = SaveDialogWidgets;
    type Components = ();
}

/// Interface for the parent model of the save dialog
pub trait SaveDialogParent: Model
where
    Self::Widgets: ParentWindow,
{
    /// Tell the save dialog how to response if the user wants to save
    fn save_msg(path: PathBuf) -> Self::Msg;
}

impl<ParentModel, Conf> ComponentUpdate<ParentModel> for SaveDialogModel<Conf>
where
    ParentModel: SaveDialogParent,
    <ParentModel as relm4::Model>::Widgets: ParentWindow,
    Conf: SaveDialogConfig<Model = ParentModel>,
{
    fn init_model(parent_model: &ParentModel) -> Self {
        Self {
            settings: Conf::dialog_config(parent_model),
            is_active: false,
            suggestion: None,
            name: String::new(),
            tracker: 0,
            _config_provider: PhantomData,
        }
    }

    fn update(
        &mut self,
        msg: SaveDialogMsg,
        _components: &(),
        _sender: Sender<SaveDialogMsg>,
        parent_sender: Sender<ParentModel::Msg>,
    ) {
        self.reset();

        match msg {
            SaveDialogMsg::Save => {
                self.is_active = true;
            }
            SaveDialogMsg::SaveAs(name) => {
                self.is_active = true;
                self.set_name(name);
            }
            SaveDialogMsg::Cancel => {
                self.is_active = false;
            }
            SaveDialogMsg::Accept(path) => {
                self.is_active = false;
                parent_sender.send(ParentModel::save_msg(path)).unwrap();
            }
            _ => (),
        }
    }
}

#[relm4::widget(pub)]
/// Widgets for the save dialog
impl<ParentModel, Conf> relm4::Widgets<SaveDialogModel<Conf>, ParentModel> for SaveDialogWidgets
where
    ParentModel: Model,
    ParentModel::Widgets: ParentWindow,
    Conf: SaveDialogConfig<Model = ParentModel>,
{
    view! {
        gtk::FileChooserNative {
            set_action: gtk::FileChooserAction::Save,
            set_visible: watch!(model.is_active),
            set_current_name: track!(model.changed(SaveDialogModel::<Conf>::name()), &model.name),
            add_filter: iterate!(&model.settings.filters),
            set_create_folders: model.settings.create_folders,
            set_cancel_label: Some(model.settings.cancel_label),
            set_accept_label: Some(model.settings.accept_label),
            set_modal: model.settings.is_modal,
            set_transient_for: parent!(parent_widgets.parent_window().as_ref()),
            connect_response => move |dialog, res_ty| {
                match res_ty {
                    gtk::ResponseType::Accept => {
                        if let Some(file) = dialog.file() {
                            if let Some(path) = file.path() {
                                send!(sender, SaveDialogMsg::Accept(path));
                                return;
                            }
                        }
                        send!(sender, SaveDialogMsg::InvalidInput);
                    },
                    gtk::ResponseType::Cancel => {
                        send!(sender, SaveDialogMsg::Cancel)
                    },
                    _ => (),
                }
            },
        }
    }
}