~lubuntu-software-center-team/lubuntu-software-center/vala-port

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
//
//  Copyright © 2011 Avi Romanoff, Allen Lowe, Maxwell Barvian
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Code from Granite https://launchpad.net/granite
//

using Gtk;
using Gdk;

namespace Lsc.Widgets {

    public class HintedEntry : Gtk.Entry {

        public string hint_string {
            get {
                return placeholder_text;
            }
            set {
                placeholder_text = value;
            }
        }

        public HintedEntry (string hint_string) {
            this.hint_string = hint_string;
        }
    }

    public class SearchBar : HintedEntry {

        private bool is_searching = true;
        private uint timeout_id = 0;

        /**
         * This value handles how much time (in ms) should pass
         * after the user stops typing. By default it is set 
         * to 300 ms.
         **/
        public int pause_delay { get; set; default = 300; }

        /**
         * text_changed () signal is emitted after a short delay,
         * which depends on pause_delay.
         * If you need a synchronous signal without any delay,
         * use changed () method.
         **/
        public signal void text_changed_pause (string text);

        public SearchBar (string hint_string) {
        
            base (hint_string);
            
            primary_icon_name = "edit-find-symbolic";
            
            // Signals and callbacks
            changed.connect (manage_icon);
            changed.connect_after (on_changed);            
            focus_in_event.connect (on_focus_in);
            focus_out_event.connect (on_focus_out);
            icon_press.connect (on_icon_press);
        }

        protected new void hint () {
            is_searching = false;
            set_icon_from_stock (Gtk.EntryIconPosition.SECONDARY, null);
        }
        
        private bool on_focus_in () {
            if (!is_searching) {
                is_searching = false;
            }
            
            return false;
        }

        private bool on_focus_out () {
            if (get_text () == "") {
                is_searching = false;
            }
            
            return false;
        }

        private void manage_icon () {
            if (text != "")
                set_icon_from_gicon (EntryIconPosition.SECONDARY, new ThemedIcon.with_default_fallbacks ("edit-clear-symbolic"));
            else
                set_icon_from_stock (EntryIconPosition.SECONDARY, null);
        }

        private void on_icon_press (EntryIconPosition position) {
            if (position == EntryIconPosition.SECONDARY) {
                is_searching = false;
                text = "";
                set_icon_from_stock (position, null);
                is_searching = true;
            } else {
                if (!is_focus) {
                    is_searching = false;
                }
            }
        }

        private void on_changed () {

            if (timeout_id > 0)
                Source.remove (timeout_id);

            timeout_id = Timeout.add (pause_delay, emit_text_changed);

        }

        private bool emit_text_changed () {

            var terms = get_text ();
            text_changed_pause (terms); // Emit signal

            return Source.remove (timeout_id);

        }

    }

}