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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright (c) 2016 Boucher, Antoni <bouanto@zoho.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

use gdk::{RGBA, SELECTION_PRIMARY};
use gtk;
use gtk::{
    BoxExt,
    Clipboard,
    CssProvider,
    CssProviderExt,
    EditableExt,
    EditableSignals,
    EntryExt,
    LabelExt,
    OrientableExt,
    PackType,
    StateFlags,
    StyleContextExt,
    WidgetExt,
    STYLE_PROVIDER_PRIORITY_APPLICATION,
};
use gtk::Orientation::Horizontal;
use pango::EllipsizeMode;
use relm::{Relm, Widget};
use relm_attributes::widget;

use self::Msg::*;
use self::ItemMsg::{Color, Text};

#[derive(Msg)]
pub enum Msg {
    BarVisible(bool),
    Copy,
    Cut,
    DeleteNextChar,
    DeleteNextWord,
    DeletePreviousWord,
    End,
    EntryActivate(Option<String>),
    EntryChanged(Option<String>),
    EntryText(String),
    EntryShown(bool),
    Identifier(String),
    NextChar,
    NextWord,
    Paste,
    PasteSelection,
    PreviousChar,
    PreviousWord,
    ShowIdentifier,
    SmartHome,
}

pub struct Model {
    identifier_label: &'static str,
    identifier_visible: bool,
    relm: Relm<StatusBar>,
    visible: bool,
}

#[widget]
impl Widget for StatusBar {
    fn init_view(&mut self) {
        // Adjust the look of the entry.
        let style_context = self.command_entry.get_style_context();
        // TODO: remove the next line when relm supports css.
        let style = include_bytes!("../../style/command-input.css");
        let provider = CssProvider::new();
        provider.load_from_data(style).unwrap();
        style_context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
    }

    fn model(relm: &Relm<Self>, _: ()) -> Model {
        Model {
            identifier_label: ":",
            identifier_visible: false,
            relm: relm.clone(),
            visible: true,
        }
    }

    /// Set whether the entry is visible or not.
    fn set_entry_shown(&mut self, visible: bool) {
        // TODO: document this use of lock.
        let _lock = self.model.relm.stream().lock();
        self.command_entry.set_text("");
        self.model.identifier_visible = visible;
        self.command_entry.set_visible(visible);

        if visible {
            self.command_entry.grab_focus();
        }
    }

    /// Show the identifier.
    fn show_identifier(&mut self) {
        self.model.identifier_visible = true;
    }

    fn update(&mut self, msg: Msg) {
        match msg {
            BarVisible(visible) => self.model.visible = visible,
            Copy => self.copy(),
            Cut => self.cut(),
            DeleteNextChar => self.delete_next_char(),
            DeleteNextWord => self.delete_next_word(),
            DeletePreviousWord => self.delete_previous_word(),
            End => self.end(),
            EntryActivate(_) | EntryChanged(_) => (), // NOTE: to be listened by the user.
            EntryShown(visible) => self.set_entry_shown(visible),
            EntryText(input) => self.set_input(&input),
            Identifier(identifier) => self.set_identifier(&identifier),
            NextChar => self.next_char(),
            NextWord => self.next_word(),
            Paste => self.paste(),
            PasteSelection => self.paste_selection(),
            PreviousChar => self.previous_char(),
            PreviousWord => self.previous_word(),
            ShowIdentifier => self.show_identifier(),
            SmartHome => self.smart_home(),
        }
    }

    view! {
        #[container]
        gtk::Box {
            property_height_request: 20, // TODO: is this still useful?
            orientation: Horizontal,
            visible: self.model.visible,
            #[name="identifier_label"]
            gtk::Label {
                text: self.model.identifier_label,
                visible: self.model.identifier_visible,
            },
            #[name="command_entry"]
            gtk::Entry {
                activate(entry) => EntryActivate(entry.get_text().map(|text| text.to_string())),
                changed(entry) => EntryChanged(entry.get_text().map(|text| text.to_string())),
                has_frame: false,
                hexpand: true,
                name: "mg-input-command",
            },
        }
    }
}

impl StatusBar {
    /// Copy the selection to the clipboard.
    fn copy(&self) {
        self.command_entry.copy_clipboard();
    }

    /// Cut the selection to the clipboard.
    fn cut(&self) {
        self.command_entry.cut_clipboard();
    }

    /// Delete the character after the cursor.
    fn delete_next_char(&self) {
        if !self.delete_selection() {
            if let Some(text) = self.get_command() {
                if !text.is_empty() {
                    let pos = self.command_entry.get_position();
                    let len = text.chars().count();
                    if pos < len as i32 {
                        // NOTE: Lock to avoid moving the cursor when updating the text entry.
                        let _lock = self.model.relm.stream().lock();
                        self.command_entry.delete_text(pos, pos + 1);
                    }
                }
            }
        }
        self.emit_entry_changed();
    }

    /// Delete the word after the cursor.
    fn delete_next_word(&self) {
        if !self.delete_selection() {
            if let Some(text) = self.get_command() {
                if !text.is_empty() {
                    let pos = self.command_entry.get_position();
                    let end = text.chars().enumerate()
                        .skip(pos as usize)
                        .skip_while(|&(_, c)| !c.is_alphanumeric())
                        .skip_while(|&(_, c)| c.is_alphanumeric())
                        .map(|(index, _)| index)
                        .next()
                        .unwrap_or_else(|| text.chars().count());
                    // NOTE: Lock to avoid moving the cursor when updating the text entry.
                    let _lock = self.model.relm.stream().lock();
                    self.command_entry.delete_text(pos, end as i32);
                }
            }
        }
        self.emit_entry_changed();
    }

    /// Delete the word before the cursor.
    fn delete_previous_word(&self) {
        if !self.delete_selection() {
            if let Some(text) = self.get_command() {
                if !text.is_empty() {
                    let pos = self.command_entry.get_position();
                    let len = text.chars().count();
                    let start = text.chars().rev().enumerate()
                        .skip(len - pos as usize)
                        .skip_while(|&(_, c)| !c.is_alphanumeric())
                        .skip_while(|&(_, c)| c.is_alphanumeric())
                        .map(|(index, _)| len - index)
                        .next()
                        .unwrap_or_default();
                    // NOTE: Lock to avoid moving the cursor when updating the text entry.
                    let _lock = self.model.relm.stream().lock();
                    self.command_entry.delete_text(start as i32, pos);
                }
            }
        }
        self.emit_entry_changed();
    }

    /// Delete the selected text.
    fn delete_selection(&self) -> bool {
        if self.command_entry.get_selection_bounds().is_some() {
            // NOTE: Lock to avoid moving the cursor when updating the text entry.
            let _lock = self.model.relm.stream().lock();
            self.command_entry.delete_selection();
            true
        }
        else {
            false
        }
    }

    /// Emit the EntryChanged event with the current text.
    fn emit_entry_changed(&self) {
        self.model.relm.stream().emit(EntryChanged(self.command_entry.get_text().map(|text| text.to_string())));
    }

    /// Go to the end of the command entry.
    fn end(&self) {
        let text = self.get_command().unwrap_or_default();
        self.command_entry.set_position(text.chars().count() as i32);
    }

    /// Get the text of the command entry.
    fn get_command(&self) -> Option<String> {
        self.command_entry.get_text().map(|text| text.to_string())
    }

    /// Go forward one character in the command entry.
    fn next_char(&self) {
        let pos = self.command_entry.get_position();
        let text = self.get_command().unwrap_or_default();
        if pos < text.chars().count() as i32 {
            self.command_entry.set_position(pos + 1);
        }
    }

    /// Go forward one word in the command entry.
    fn next_word(&self) {
        let pos = self.command_entry.get_position();
        let text = self.get_command().unwrap_or_default();
        let position = text.chars().enumerate()
            .skip(pos as usize)
            .skip_while(|&(_, c)| !c.is_alphanumeric())
            .skip_while(|&(_, c)| c.is_alphanumeric())
            .next()
            .map(|(index, _)| index)
            .unwrap_or_else(|| text.chars().count());
        self.command_entry.set_position(position as i32);
    }

    /// Paste the clipboard to the selection or the cursor position.
    fn paste(&self) {
        self.command_entry.paste_clipboard();
    }

    /// Paste the selection clipboard to the selection or the cursor position.
    fn paste_selection(&self) {
        self.delete_selection();
        let clipboard = Clipboard::get(&SELECTION_PRIMARY);
        if let Some(text) = clipboard.wait_for_text() {
            let mut position = self.command_entry.get_position();
            self.command_entry.insert_text(&text, &mut position);
            self.command_entry.set_position(position);
        }
    }

    /// Go back one character in the command entry.
    fn previous_char(&self) {
        let pos = self.command_entry.get_position();
        if pos > 0 {
            self.command_entry.set_position(pos - 1);
        }
    }

    /// Go back one word in the command entry.
    fn previous_word(&self) {
        let pos = self.command_entry.get_position();
        let text = self.get_command().unwrap_or_default();
        let len = text.chars().count();
        let position = text.chars().rev().enumerate()
            .skip(len - pos as usize)
            .skip_while(|&(_, c)| !c.is_alphanumeric())
            .skip_while(|&(_, c)| c.is_alphanumeric())
            .next()
            .map(|(index, _)| len - index)
            .unwrap_or_default();
        self.command_entry.set_position(position as i32);
    }

    /// Seth the prefix identifier shown at the left of the command entry.
    fn set_identifier(&self, identifier: &str) {
        self.identifier_label.set_text(identifier);
    }

    /// Set the text of the input entry and move the cursor at the end.
    fn set_input(&self, command: &str) {
        // NOTE: Prevent updating the completions when the user selects a completion entry.
        let _lock = self.model.relm.stream().lock();
        self.command_entry.set_text(command);
        self.command_entry.set_position(command.chars().count() as i32);
    }

    /// Go to the beginning of the command entry.
    /// If the cursor is already at the beginning, go after the spaces after the command name.
    fn smart_home(&self) {
        let pos = self.command_entry.get_position();
        if pos == 0 {
            let text = self.get_command().unwrap_or_default();
            let position = text.chars().enumerate()
                .skip_while(|&(_, c)| c.is_whitespace())
                .skip_while(|&(_, c)| !c.is_whitespace())
                .skip_while(|&(_, c)| c.is_whitespace())
                .map(|(index, _)| index)
                .next()
                .unwrap_or_default();
            self.command_entry.set_position(position as i32);
        }
        else {
            self.command_entry.set_position(0);
        }
    }
}

#[derive(Msg)]
pub enum ItemMsg {
    /// Set the color of the status bar item.
    Color(Option<RGBA>),
    /// Set the text of the status bar item.
    Text(String),
}

/// A status bar text item.
#[widget]
impl Widget for StatusBarItem {
    fn model() -> () {
        ()
    }

    fn update(&mut self, msg: ItemMsg) {
        match msg {
            Color(color) => self.label.override_color(StateFlags::NORMAL, color.as_ref()),
            Text(text) => self.label.set_text(&text),
        }
    }

    view! {
        #[parent="status-bar-item"]
        #[name="label"]
        gtk::Label {
            ellipsize: EllipsizeMode::End,
            child: {
                pack_type: PackType::End,
                padding: 3,
            },
        }
    }
}