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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use crate::edit_view::EditView;
use crate::main_win::MainState;
use crate::pref_storage::*;
use crate::rpc::Core;
use gettextrs::gettext;
use gtk::*;
use log::{debug, error, trace};
use pango::*;
use std::cell::RefCell;
use std::rc::Rc;

pub struct PrefsWin {
    core: Core,
    window: Window,
}

impl PrefsWin {
    pub fn new(
        parent: &ApplicationWindow,
        main_state: &Rc<RefCell<MainState>>,
        core: &Core,
        edit_view: Option<Rc<RefCell<EditView>>>,
    ) -> Rc<RefCell<Self>> {
        const SRC: &str = include_str!("ui/prefs_win.glade");
        let builder = Builder::new_from_string(SRC);

        let window: Window = builder.get_object("prefs_win").unwrap();
        let font_chooser_widget: FontChooserWidget =
            builder.get_object("font_chooser_widget").unwrap();
        let theme_combo_box: ComboBoxText = builder.get_object("theme_combo_box").unwrap();
        let tab_stops_checkbutton: ToggleButton =
            builder.get_object("tab_stops_checkbutton").unwrap();
        let scroll_past_end_checkbutton: ToggleButton =
            builder.get_object("scroll_past_end_checkbutton").unwrap();
        let word_wrap_checkbutton: ToggleButton =
            builder.get_object("word_wrap_checkbutton").unwrap();
        let draw_trailing_spaces_checkbutton: ToggleButton = builder
            .get_object("draw_trailing_spaces_checkbutton")
            .unwrap();
        let margin_checkbutton: ToggleButton = builder.get_object("margin_checkbutton").unwrap();
        let margin_spinbutton: SpinButton = builder.get_object("margin_spinbutton").unwrap();
        let highlight_line_checkbutton: ToggleButton =
            builder.get_object("highlight_line_checkbutton").unwrap();
        let tab_size_spinbutton: SpinButton = builder.get_object("tab_size_spinbutton").unwrap();

        let xi_config = &main_state.borrow().config;

        {
            let mut font_desc = FontDescription::new();
            let font_face = &xi_config.config.font_face;
            font_desc.set_size(xi_config.config.font_size as i32 * pango::SCALE);
            font_desc.set_family(font_face);

            trace!("{}: {}", gettext("Setting font description"), font_face);

            font_chooser_widget.set_font_desc(&font_desc);
        }

        {
            font_chooser_widget.connect_property_font_desc_notify(
                clone!(main_state => move |font_widget| {
                    if let Some(font_desc) = font_widget.get_font_desc() {
                        let mut font_conf = &mut main_state.borrow_mut().config;

                        let font_family = font_desc.get_family().unwrap();
                        let font_size = font_desc.get_size() / pango::SCALE;
                        debug!("{} {}", gettext("Setting font to"), &font_family);
                        debug!("{} {}", gettext("Setting font size to"), &font_size);

                        font_conf.config.font_size = font_size as u32;
                        font_conf.config.font_face = font_family.to_string();
                        font_conf
                            .save()
                            .map_err(|e| error!("{}", e.to_string()))
                            .unwrap();
                    }
                }),
            );
        }

        {
            let main_state = main_state.borrow();
            for (i, theme_name) in main_state.themes.iter().enumerate() {
                theme_combo_box.append_text(theme_name);
                if &main_state.theme_name == theme_name {
                    trace!("{}: {}", gettext("Setting active theme"), i);
                    theme_combo_box.set_active(Some(i as u32));
                }
            }
        }

        theme_combo_box.connect_changed(clone!(core, main_state => move |cb|{
            if let Some(theme_name) = cb.get_active_text() {
                debug!("{} {:?}", gettext("Theme changed to"), &theme_name);
                core.set_theme(&theme_name);

                crate::pref_storage::set_theme_schema(theme_name.to_string());

                let mut main_state = main_state.borrow_mut();
                main_state.theme_name = theme_name.to_string();
            }
        }));

        {
            {
                scroll_past_end_checkbutton
                    .set_active(main_state.borrow().config.config.scroll_past_end);
            }

            scroll_past_end_checkbutton.connect_toggled(clone!(main_state => move |toggle_btn| {
                let value = toggle_btn.get_active();;
                debug!("{}: {}", gettext("Scrolling past end"), value);
                main_state.borrow_mut().config.config.scroll_past_end = value;
                main_state.borrow().config.save()
                    .map_err(|e| error!("{}", e.to_string()))
                    .unwrap();
            }));
        }

        {
            {
                word_wrap_checkbutton.set_active(main_state.borrow().config.config.word_wrap);
            }

            word_wrap_checkbutton.connect_toggled(clone!(main_state => move |toggle_btn| {
                let value = toggle_btn.get_active();
                debug!("{}: {}", gettext("Word wrapping"), value);
                main_state.borrow_mut().config.config.word_wrap = value;
                main_state.borrow().config.save()
                    .map_err(|e| error!("{}", e.to_string()))
                    .unwrap();
            }));
        }

        {
            {
                tab_stops_checkbutton.set_active(main_state.borrow().config.config.use_tab_stops);
            }

            tab_stops_checkbutton.connect_toggled(clone!(main_state => move |toggle_btn| {
                let value = toggle_btn.get_active();
                debug!("{}: {}", gettext("Tab stops"), value);
                main_state.borrow_mut().config.config.use_tab_stops = value;
                main_state.borrow().config.save()
                    .map_err(|e| error!("{}", e.to_string()))
                    .unwrap();
            }));
        }

        {
            draw_trailing_spaces_checkbutton.set_active(get_draw_trailing_spaces_schema());

            draw_trailing_spaces_checkbutton.connect_toggled(move |toggle_btn| {
                let value = toggle_btn.get_active();
                set_draw_trailing_spaces_schema(value);
            });
        }

        {
            margin_checkbutton.set_active(get_draw_right_margin());

            margin_checkbutton.connect_toggled(
                clone!(edit_view, margin_spinbutton => move |toggle_btn| {
                    let value = toggle_btn.get_active();
                    debug!("{}: {}", gettext("Right hand margin"), value);
                    set_draw_right_margin(value);
                    if let Some(ev) = edit_view.clone() {
                        ev.borrow().view_item.edit_area.queue_draw();
                    }
                    margin_spinbutton.set_sensitive(value);
                }),
            );
        }

        {
            margin_spinbutton.set_sensitive(get_draw_right_margin());
            margin_spinbutton.set_value(f64::from(get_column_right_margin()));

            margin_spinbutton.connect_value_changed(clone!(edit_view => move |spin_btn| {
                let value = spin_btn.get_value();
                debug!("{}: {}", gettext("Right hand margin width"), value);
                set_column_right_margin(value as u32);
                if let Some(ev) = edit_view.clone() {
                    ev.borrow().view_item.edit_area.queue_draw();
                }
            }));
        }

        {
            tab_size_spinbutton.set_value(f64::from(main_state.borrow().config.config.tab_size));

            tab_size_spinbutton.connect_value_changed(
                clone!(main_state, edit_view => move |spin_btn| {
                    let value = spin_btn.get_value();
                    debug!("{}: {}", gettext("Setting tab size"), value);
                    main_state.borrow_mut().config.config.tab_size = value as u32;
                    main_state.borrow().config.save()
                    .map_err(|e| error!("{}", e.to_string()))
                    .unwrap();
                    if let Some(ev) = edit_view.clone() {
                        ev.borrow().view_item.edit_area.queue_draw();
                    }
                }),
            );
        }

        {
            highlight_line_checkbutton.set_active(get_highlight_line());

            highlight_line_checkbutton.connect_toggled(clone!(edit_view => move |toggle_btn| {
                let value = toggle_btn.get_active();
                set_highlight_line(value);
                if let Some(ev) = edit_view.clone() {
                    ev.borrow().view_item.edit_area.queue_draw();
                }
            }));
        }

        let prefs_win = Rc::new(RefCell::new(Self {
            core: core.clone(),
            window: window.clone(),
        }));

        window.set_transient_for(Some(parent));
        window.show_all();

        prefs_win
    }
}