Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
command-palette.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5/* Authors:
6 * Abhay Raj Singh <abhayonlyone@gmail.com>
7 *
8 * Copyright (C) 2020 Authors
9 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
10 */
11
12#ifndef INKSCAPE_DIALOG_COMMAND_PALETTE_H
13#define INKSCAPE_DIALOG_COMMAND_PALETTE_H
14
15#include <gtkmm/enums.h> // Gtk::DirectionType
16
17#include "xml/document.h"
18
19namespace Gdk {
20enum class ModifierType;
21}
22
23namespace Gio {
24class Action;
25} // namespace Gio
26
27namespace Gtk {
28class Box;
29class Builder;
30class Label;
31class ListBox;
32class ListBoxRow;
33class ScrolledWindow;
34class SearchEntry2;
35class Widget;
36} // namespace Gtk
37
38namespace Inkscape::UI::Dialog {
39
40// Enables using switch case
41enum class TypeOfVariant
42{
43 NONE,
44 UNKNOWN,
45 BOOL,
46 INT,
47 DOUBLE,
48 STRING,
50};
51
52enum class CPMode
53{
54 SEARCH,
55 INPUT,
56 SHELL,
58};
59
60enum class HistoryType
61{
62 LPE,
63 ACTION,
66};
67
68struct History
69{
71 std::string data;
72
73 History(HistoryType ht, std::string &&data)
74 : history_type(ht)
75 , data(data)
76 {}
77};
78
80{
81public:
82 // constructors, asssignment, destructor
85
86 // Handy wrappers for code clearity
87 void add_action(const std::string &full_action_name);
88
89 void add_import(const std::string &uri);
90 void add_open(const std::string &uri);
91
93 void add_action_parameter(const std::string &full_action_name, const std::string &param);
94
95 std::optional<History> get_last_operation();
96
98 std::vector<History> get_operation_history() const;
100 std::vector<std::string> get_action_parameter_history(const std::string &full_action_name) const;
101
102private:
103 void save() const;
104
105 void add_operation(const HistoryType history_type, const std::string &data);
106
107 static std::optional<HistoryType> _get_operation_type(Inkscape::XML::Node *operation);
108
109 const std::string _file_path;
110
112 // handy for xml doc child
115};
116
118{
119public:
121 ~CommandPalette() = default;
122
123 CommandPalette(CommandPalette const &) = delete; // no copy
124 CommandPalette &operator=(CommandPalette const &) = delete; // no assignment
125
126 void open();
127 void close();
128 void toggle();
129
130 Gtk::Box &get_base_widget() { return _CPBase; }
131
132private:
133 using ActionPtr = Glib::RefPtr<Gio::Action>;
134 using ActionPtrName = std::pair<ActionPtr, Glib::ustring>;
135
136 // Insert actions in _CPSuggestions
137 void load_app_actions();
139
140 void append_recent_file_operation(const Glib::ustring &path, bool is_suggestion, bool is_import = true);
141 bool generate_action_operation(const ActionPtrName &action_ptr_name, const bool is_suggestion);
142
143 void on_search();
144
145 int on_filter_general(Gtk::ListBoxRow *child);
146 bool on_filter_full_action_name(Gtk::ListBoxRow *child);
147 bool on_filter_recent_file(Gtk::ListBoxRow *child, bool const is_import);
148
149 bool on_key_pressed(unsigned keyval, unsigned keycode, Gdk::ModifierType state);
150 void on_window_focus(Gtk::Widget const *focus);
151
153 bool on_entry_keypress(unsigned keyval, unsigned, Gdk::ModifierType);
154
155 // when search bar is empty
156 void hide_suggestions();
157 // when search bar isn't empty
158 void show_suggestions();
159
160 void on_row_activated(Gtk::ListBoxRow *activated_row);
161 void on_history_selection_changed(Gtk::ListBoxRow *lb);
162
163 bool operate_recent_file(Glib::ustring const &uri, bool const import);
164
165 void on_action_fullname_clicked(const Glib::ustring &action_fullname);
166
168 static bool fuzzy_search(const Glib::ustring &subject, const Glib::ustring &search);
169 static bool normal_search(const Glib::ustring &subject, const Glib::ustring &search);
170 static bool fuzzy_tolerance_search(const Glib::ustring &subject, const Glib::ustring &search);
171 static int fuzzy_points(const Glib::ustring &subject, const Glib::ustring &search);
172 static int fuzzy_tolerance_points(const Glib::ustring &subject, const Glib::ustring &search);
173 static int fuzzy_points_compare(int fuzzy_points_count_1, int fuzzy_points_count_2, int text_len_1, int text_len_2);
174
175 static void test_sort();
176 int on_sort(Gtk::ListBoxRow *row1, Gtk::ListBoxRow *row2);
177 void set_mode(CPMode mode);
178
179 // Color addition in searched character
180 void add_color(Gtk::Label *label, const Glib::ustring &search, const Glib::ustring &subject, bool tooltip=false);
181 void remove_color(Gtk::Label *label, const Glib::ustring &subject, bool tooltip=false);
182 static void add_color_description(Gtk::Label *label, const Glib::ustring &search);
183
184 // Executes Action
185 bool ask_action_parameter(const ActionPtrName &action);
186 static ActionPtrName get_action_ptr_name(Glib::ustring full_action_name);
187 bool execute_action(const ActionPtrName &action, const Glib::ustring &value);
188
189 static TypeOfVariant get_action_variant_type(const ActionPtr &action_ptr);
190
191 static std::pair<Gtk::Label *, Gtk::Label *> get_name_desc(Gtk::ListBoxRow *child);
192 Gtk::Label *get_full_action_name(Gtk::ListBoxRow *child);
193
194 // Widgets
195 Glib::RefPtr<Gtk::Builder> _builder;
196 Gtk::Box &_CPBase;
197 Gtk::Box &_CPListBase;
198 Gtk::SearchEntry2 &_CPFilter;
199 Gtk::ListBox &_CPSuggestions;
200 Gtk::ListBox &_CPHistory;
201 Gtk::ScrolledWindow &_CPSuggestionsScroll;
202 Gtk::ScrolledWindow &_CPHistoryScroll;
203
204 // Data
205 const int _max_height_requestable = 360;
206 Glib::ustring _search_text;
207
208 // States
209 bool _is_open = false;
211
219 // Default value other than SEARCH required
220 // set_mode() switches between mode hence checks if it already in the target mode.
221 // Constructed value is sometimes SEARCH being the first Item for now
222 // set_mode() never attaches the on search listener then
223 // This initialising value can be any thing other than the initial required mode
224 // Example currently it's open in search mode
225
228
230 std::optional<ActionPtrName> _ask_action_ptr_name;
231};
232
233} // namespace Inkscape::UI::Dialog
234
235#endif // INKSCAPE_DIALOG_COMMAND_PALETTE_H
236
237/*
238 Local Variables:
239 mode:c++
240 c-file-style:"stroustrup"
241 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
242 indent-tabs-mode:nil
243 fill-column:99
244 End:
245*/
246// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Inkscape::XML::Document * _xml_doc
void add_import(const std::string &uri)
void add_open(const std::string &uri)
void add_action(const std::string &full_action_name)
std::vector< History > get_operation_history() const
To construct _CPHistory.
std::vector< std::string > get_action_parameter_history(const std::string &full_action_name) const
To get parameter history when an action is selected, LIFO stack like so more recent first.
void add_action_parameter(const std::string &full_action_name, const std::string &param)
Remember parameter for action.
std::optional< History > get_last_operation()
static std::optional< HistoryType > _get_operation_type(Inkscape::XML::Node *operation)
void add_operation(const HistoryType history_type, const std::string &data)
void on_row_activated(Gtk::ListBoxRow *activated_row)
CPMode _mode
Remember the mode we are in helps in unnecessary signal disconnection and reconnection Used by set_mo...
static bool fuzzy_tolerance_search(const Glib::ustring &subject, const Glib::ustring &search)
The Searching algorithm consists of fuzzy search and fuzzy points.
bool operate_recent_file(Glib::ustring const &uri, bool const import)
bool on_filter_full_action_name(Gtk::ListBoxRow *child)
static bool fuzzy_search(const Glib::ustring &subject, const Glib::ustring &search)
Implements text matching logic.
void add_color(Gtk::Label *label, const Glib::ustring &search, const Glib::ustring &subject, bool tooltip=false)
bool generate_action_operation(const ActionPtrName &action_ptr_name, const bool is_suggestion)
static TypeOfVariant get_action_variant_type(const ActionPtr &action_ptr)
int on_filter_general(Gtk::ListBoxRow *child)
static ActionPtrName get_action_ptr_name(Glib::ustring full_action_name)
Calls actions with parameters.
bool on_filter_recent_file(Gtk::ListBoxRow *child, bool const is_import)
Gtk::ScrolledWindow & _CPSuggestionsScroll
static int fuzzy_points(const Glib::ustring &subject, const Glib::ustring &search)
Calculates the fuzzy_point.
Glib::RefPtr< Gio::Action > ActionPtr
void on_action_fullname_clicked(const Glib::ustring &action_fullname)
static std::pair< Gtk::Label *, Gtk::Label * > get_name_desc(Gtk::ListBoxRow *child)
sigc::connection _cpfilter_search_connection
Stores the search connection to deactivate when not needed.
void append_recent_file_operation(const Glib::ustring &path, bool is_suggestion, bool is_import=true)
bool ask_action_parameter(const ActionPtrName &action)
Maybe replaced by: Temporary arrangement may be replaced by snippets This can help us provide paramet...
void remove_color(Gtk::Label *label, const Glib::ustring &subject, bool tooltip=false)
Color removal.
static int fuzzy_tolerance_points(const Glib::ustring &subject, const Glib::ustring &search)
bool on_key_pressed(unsigned keyval, unsigned keycode, Gdk::ModifierType state)
Gtk::Label * get_full_action_name(Gtk::ListBoxRow *child)
static bool normal_search(const Glib::ustring &subject, const Glib::ustring &search)
Searching the full search_text in the subject string used for CPDescription text.
std::pair< ActionPtr, Glib::ustring > ActionPtrName
Glib::RefPtr< Gtk::Builder > _builder
std::optional< ActionPtrName > _ask_action_ptr_name
Stores the most recent ask_action_name for when Entry::activate fires & we are in INPUT mode.
static void add_color_description(Gtk::Label *label, const Glib::ustring &search)
Color addition for description text Coloring complete consecutive search text in the description text...
int on_sort(Gtk::ListBoxRow *row1, Gtk::ListBoxRow *row2)
compare different rows for order of display priority of comparison 1) CPName->get_text() 2) CPName->g...
static int fuzzy_points_compare(int fuzzy_points_count_1, int fuzzy_points_count_2, int text_len_1, int text_len_2)
void on_window_focus(Gtk::Widget const *focus)
void on_history_selection_changed(Gtk::ListBoxRow *lb)
CommandPalette & operator=(CommandPalette const &)=delete
CommandPalette(CommandPalette const &)=delete
bool on_entry_keypress(unsigned keyval, unsigned, Gdk::ModifierType)
bool execute_action(const ActionPtrName &action, const Glib::ustring &value)
Interface for refcounted XML nodes.
Definition node.h:80
Glib::ustring label
Definition desktop.h:50
Dialog code.
Definition desktop.h:117
int mode
Ocnode * child[8]
Definition quantize.cpp:33
static const Point data[]
History(HistoryType ht, std::string &&data)
Interface for XML documents.
Definition document.h:43
Interface for XML documents.