~midori/midori/gtk3WebKit2only

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
/*
 Copyright (C) 2012-2013 Christian Dywan <christian@twotoasts.de>

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

namespace Midori {
    public class Suggestion : GLib.Object {
        public string? uri { get; set; }
        public string? markup { get; set; }
        public bool use_markup { get; set; }
        public string? background { get; set; }
        public GLib.Icon? icon {  get; set; }
        public bool action { get; set; default = false; }
        public int priority {get; set; default = 0; }

        public Suggestion (string? uri, string? markup, bool use_markup=false,
            string? background=null, GLib.Icon? icon=null, int? priority=0) {

            GLib.Object (uri: uri, markup: markup, use_markup: use_markup,
                         background: background, icon: icon, priority: priority);
        }
    }

    public abstract class Completion : GLib.Object {
        public string? description { get; set; }
        public int max_items { get; internal set; default = 25; }
        internal int position { get; set; }

        public abstract void prepare (GLib.Object app);
        public abstract bool can_complete (string prefix);
        public abstract bool can_action (string action);
        public abstract async List<Suggestion>? complete (string text, string? action, Cancellable cancellable);
    }

    public class Autocompleter : GLib.Object {
        private GLib.Object app;
        private List<Completion> completions;
        private int next_position;
        public Gtk.ListStore model { get; private set; }
        private bool need_to_clear = false;
        private uint current_count = 0;
        private Cancellable? cancellable = null;

        public enum Columns {
            ICON,
            URI,
            MARKUP,
            BACKGROUND,
            YALIGN,
            SIZE,
            PRIORITY,
            N
        }

        public Autocompleter (GLib.Object app) {
            this.app = app;
            completions = new List<Completion> ();
            next_position = 0;
            model = new Gtk.ListStore (Columns.N,
                typeof (GLib.Icon), typeof (string), typeof (string),
                typeof (string), typeof (float), typeof (uint), typeof (int));
        }

        public void add (Completion completion) {
            completion.prepare (app);
            completion.position = next_position;
            next_position += completion.max_items;
            completions.append (completion);
        }

        public bool can_complete (string text) {
            foreach (var completion in completions)
                if (completion.can_complete (text))
                    return true;
            return false;
        }

        private GLib.Icon? scale_if_needed (GLib.Icon? icon) {
            if (icon is Gdk.Pixbuf) {
                var pixbuf = icon as Gdk.Pixbuf;
                int icon_width = 16, icon_height = 16;
                Gtk.icon_size_lookup (Gtk.IconSize.MENU, out icon_width, out icon_height);
                if (pixbuf.width > icon_width || pixbuf.height > icon_height)
                    return pixbuf.scale_simple (icon_width, icon_height, Gdk.InterpType.BILINEAR);
            }
            return icon;
        }

        private void fill_model (Midori.Completion completion, List<Midori.Suggestion>? suggestions) {
            if (need_to_clear) {
                model.clear ();
                need_to_clear = false;
                current_count = 0;
            }
            int start = 0;
            // find the first index with priority greater than the current one
            Gtk.TreeModelForeachFunc find_index = (model, path, iter) => {
                GLib.Value priority;

                model.get_value (iter,Columns.PRIORITY, out priority);
                if ((int)priority < completion.position) {
                    start++;
                    return false;
                }
                return true;
            };
            model.foreach(find_index);
            int count = 0;
            if (completion.description != null) {
                model.insert_with_values (null, start,
                    Columns.URI, "about:completion-description",
                    Columns.MARKUP, "<b>%s</b>\n".printf (Markup.escape_text (completion.description)),
                    Columns.ICON, null,
                    Columns.SIZE, Gtk.IconSize.MENU,
                    Columns.BACKGROUND, null,
                    Columns.YALIGN, 0.25,
                    Columns.PRIORITY, completion.position);
                count++;
            }

            foreach (var suggestion in suggestions) {
                if (suggestion.uri == null) {
                    warning ("suggestion.uri != null");
                    continue;
                }
                if (suggestion.markup == null) {
                    warning ("suggestion.markup != null");
                    continue;
                }
                model.insert_with_values (null, start + count,
                    Columns.URI, suggestion.uri,
                    Columns.MARKUP, suggestion.use_markup
                    ? suggestion.markup : Markup.escape_text (suggestion.markup),
                    Columns.ICON, scale_if_needed (suggestion.icon),
                    Columns.SIZE, Gtk.IconSize.MENU,
                    Columns.BACKGROUND, suggestion.background,
                    Columns.YALIGN, 0.25,
                    Columns.PRIORITY, completion.position);

                count++;
                if (count > completion.max_items)
                    break;
            }

            current_count += count;
            populated (current_count);
        }

        public signal void populated (uint count);

        private async void complete_wrapped (Completion completion, string text, string? action, Cancellable cancellable) {
            List<Midori.Suggestion>? suggestions = yield completion.complete (text, action, cancellable);
            if (!cancellable.is_cancelled () && suggestions != null)
                fill_model (completion, suggestions);
        }

        public async void complete (string text) {
            if (cancellable != null)
                cancellable.cancel ();
            cancellable = new Cancellable ();
            need_to_clear = true;

            foreach (var completion in completions) {
                if (completion.can_complete (text))
                    complete_wrapped.begin (completion, text, null, cancellable);
            }
        }

        public bool can_action (string action) {
            if (action == "about:completion-description")
                return true;
            foreach (var completion in completions)
                if (completion.can_action (action))
                    return true;
            return false;
        }

        public async void action (string action, string text) {
            if (action == "about:completion-description")
                return;

            if (cancellable != null)
                cancellable.cancel ();
            cancellable = new Cancellable ();
            need_to_clear = true;

            foreach (var completion in completions) {
                if (completion.can_action (action))
                    complete_wrapped.begin (completion, text, action, cancellable);
            }
        }
    }
}