Skip to content

Commit 8d9ac5f

Browse files
imp: add key hint popup
1 parent f0af526 commit 8d9ac5f

File tree

7 files changed

+153
-7
lines changed

7 files changed

+153
-7
lines changed

src/global.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,12 @@
141141
border-bottom:0px;
142142
border-radius:0px;
143143
}
144+
145+
.imp-key-hint-slider {
146+
background-color: @theme_base_color;
147+
border-color: @borders;
148+
border-width: 1px 1px 0px 1px;
149+
border-style: solid;
150+
border-radius: 3px 3px 0px 0px;
151+
padding-top: 3px;
152+
}

src/imp/imp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,8 @@ bool ImpBase::handle_click(const GdkEventButton *button_event)
12521252
if (button_event->button != 2)
12531253
set_search_mode(false);
12541254

1255+
main_window->key_hint_set_visible(false);
1256+
12551257
bool need_menu = false;
12561258
if (core->tool_is_active() && button_event->button != 2 && !(button_event->state & Gdk::SHIFT_MASK)
12571259
&& button_event->type != GDK_2BUTTON_PRESS && button_event->type != GDK_3BUTTON_PRESS) {

src/imp/imp_action.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ bool ImpBase::trigger_action(const ActionToolID &action)
211211
if (core->tool_is_active() && !(action_catalog.at(action).flags & ActionCatalogItem::FLAGS_IN_TOOL)) {
212212
return false;
213213
}
214+
main_window->key_hint_set_visible(false);
214215
if (keys_current.size()) {
215216
keys_current.clear();
216217
main_window->tool_hint_label->set_text(">");

src/imp/imp_key.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,49 @@
33
#include "logger/logger.hpp"
44
#include "in_tool_action_catalog.hpp"
55
#include "actions.hpp"
6+
#include "util/str_util.hpp"
67

78
namespace horizon {
89

10+
11+
class KeyLabel : public Gtk::Box {
12+
public:
13+
KeyLabel(Glib::RefPtr<Gtk::SizeGroup> sg, const std::string &key_markup, ActionToolID act)
14+
: Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 8), action(act)
15+
{
16+
{
17+
auto la = Gtk::manage(new Gtk::Label);
18+
la->set_xalign(0);
19+
la->set_markup(key_markup);
20+
la->show();
21+
pack_start(*la, false, false, 0);
22+
sg->add_widget(*la);
23+
}
24+
{
25+
auto la = Gtk::manage(new Gtk::Label);
26+
la->set_xalign(0);
27+
la->set_text(action_catalog.at(action).name);
28+
la->show();
29+
pack_start(*la, true, true, 0);
30+
}
31+
property_margin() = 2;
32+
}
33+
34+
const ActionToolID action;
35+
};
36+
937
void ImpBase::init_key()
1038
{
1139
canvas->signal_key_press_event().connect(sigc::mem_fun(*this, &ImpBase::handle_key_press));
1240
key_sequence_dialog = std::make_unique<KeySequenceDialog>(this->main_window);
1341
connect_action(ActionID::HELP, [this](const auto &a) { key_sequence_dialog->show(); });
42+
main_window->key_hint_box->signal_row_activated().connect([this](auto row) {
43+
if (auto la = dynamic_cast<KeyLabel *>(row->get_child())) {
44+
trigger_action(la->action);
45+
keys_current.clear();
46+
main_window->key_hint_set_visible(false);
47+
}
48+
});
1449
}
1550

1651
bool ImpBase::handle_key_press(const GdkEventKey *key_event)
@@ -96,6 +131,7 @@ bool ImpBase::handle_action_key(const GdkEventKey *ev)
96131
return false;
97132
}
98133
else {
134+
main_window->key_hint_set_visible(false);
99135
keys_current.clear();
100136
return true;
101137
}
@@ -168,6 +204,7 @@ bool ImpBase::handle_action_key(const GdkEventKey *ev)
168204
else if (connections_matched.size() == 1) {
169205
main_window->tool_hint_label->set_text(key_sequence_to_string(keys_current));
170206
keys_current.clear();
207+
main_window->key_hint_set_visible(false);
171208
auto conn = connections_matched.begin()->first;
172209
if (!trigger_action({conn->action_id, conn->tool_id})) {
173210
main_window->tool_hint_label->set_text(">");
@@ -216,12 +253,36 @@ bool ImpBase::handle_action_key(const GdkEventKey *ev)
216253
return false;
217254
}
218255

256+
for (auto ch : main_window->key_hint_box->get_children()) {
257+
delete ch;
258+
}
259+
260+
for (const auto &[conn, it] : connections_matched) {
261+
const auto &[res, seq] = it;
262+
std::string seq_label;
263+
for (size_t i = 0; i < seq.size(); i++) {
264+
seq_label += Glib::Markup::escape_text(key_sequence_item_to_string(seq.at(i))) + " ";
265+
if (i + 1 == keys_current.size()) {
266+
seq_label += "<b>";
267+
}
268+
}
269+
rtrim(seq_label);
270+
seq_label += "</b>";
271+
272+
auto la = Gtk::manage(
273+
new KeyLabel(main_window->key_hint_size_group, seq_label, {conn->action_id, conn->tool_id}));
274+
main_window->key_hint_box->append(*la);
275+
la->show();
276+
}
277+
main_window->key_hint_set_visible(true);
278+
219279
main_window->tool_hint_label->set_text(key_sequence_to_string(keys_current) + "?");
220280
return true;
221281
}
222282
else if (connections_matched.size() == 0 || in_tool_actions_matched.size() == 0) {
223283
main_window->tool_hint_label->set_text("Unknown key sequence");
224284
keys_current.clear();
285+
main_window->key_hint_set_visible(false);
225286
return false;
226287
}
227288
else {

src/imp/main_window.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ MainWindow::MainWindow(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder>
6161
GET_WIDGET(version_info_bar);
6262
GET_WIDGET(version_label);
6363

64+
GET_WIDGET(key_hint_box);
65+
GET_WIDGET(key_hint_revealer);
66+
key_hint_size_group = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL);
67+
6468
set_version_info("");
6569

6670
grid_options_button->signal_clicked().connect([this] {
@@ -103,13 +107,11 @@ MainWindow::MainWindow(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder>
103107
pango_attr_list_insert(attributes_list, attribute_font_features);
104108
}
105109

106-
{
107-
Gtk::EventBox *gl_container = nullptr;
108-
GET_WIDGET(gl_container);
109-
canvas = Gtk::manage(new CanvasGL());
110-
gl_container->add(*canvas);
111-
canvas->show();
112-
}
110+
GET_WIDGET(gl_container);
111+
canvas = Gtk::manage(new CanvasGL());
112+
gl_container->add(*canvas);
113+
canvas->show();
114+
113115
tool_bar_set_visible(false);
114116
hud->set_reveal_child(false);
115117
set_use_action_bar(false);
@@ -266,6 +268,33 @@ void MainWindow::set_version_info(const std::string &s)
266268
}
267269
}
268270

271+
void MainWindow::update_key_hint_position()
272+
{
273+
int dest_x, dest_y;
274+
if (tool_hint_label->translate_coordinates(*gl_container, 0, 0, dest_x, dest_y)) {
275+
key_hint_revealer->set_margin_start(dest_x - 5); // compensate for various borders and margins
276+
}
277+
}
278+
279+
void MainWindow::key_hint_set_visible(bool show)
280+
{
281+
key_hint_connection.disconnect();
282+
if (show) {
283+
if (!key_hint_revealer->get_reveal_child()) {
284+
update_key_hint_position();
285+
key_hint_connection = Glib::signal_timeout().connect(
286+
[this] {
287+
key_hint_revealer->set_reveal_child(true);
288+
return false;
289+
},
290+
500);
291+
}
292+
}
293+
else {
294+
key_hint_revealer->set_reveal_child(false);
295+
}
296+
}
297+
269298
MainWindow *MainWindow::create()
270299
{
271300
MainWindow *w;

src/imp/main_window.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class MainWindow : public Gtk::ApplicationWindow {
4646

4747
Gtk::Button *grid_window_button = nullptr;
4848

49+
Gtk::ListBox *key_hint_box = nullptr;
50+
Glib::RefPtr<Gtk::SizeGroup> key_hint_size_group;
51+
void key_hint_set_visible(bool v);
52+
4953
Glib::SignalProxy<bool, const Glib::ustring &> signal_activate_hud_link()
5054
{
5155
return hud_label->signal_activate_link();
@@ -74,6 +78,8 @@ class MainWindow : public Gtk::ApplicationWindow {
7478

7579
// virtual ~MainWindow();
7680
private:
81+
Gtk::EventBox *gl_container = nullptr;
82+
7783
Gtk::Revealer *tool_bar = nullptr;
7884
Gtk::Label *tool_bar_name_label = nullptr;
7985
Gtk::Label *tool_bar_tip_label = nullptr;
@@ -108,5 +114,9 @@ class MainWindow : public Gtk::ApplicationWindow {
108114
Gtk::Label *version_label = nullptr;
109115

110116
bool tool_bar_use_actions = false;
117+
118+
Gtk::Revealer *key_hint_revealer = nullptr;
119+
sigc::connection key_hint_connection;
120+
void update_key_hint_position();
111121
};
112122
} // namespace horizon

src/imp/window.ui

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,40 @@
11191119
<property name="index">4</property>
11201120
</packing>
11211121
</child>
1122+
<child type="overlay">
1123+
<object class="GtkRevealer" id="key_hint_revealer">
1124+
<property name="visible">True</property>
1125+
<property name="can-focus">False</property>
1126+
<property name="halign">start</property>
1127+
<property name="valign">end</property>
1128+
<property name="transition-type">slide-up</property>
1129+
<property name="transition-duration">100</property>
1130+
<child>
1131+
<object class="GtkFrame">
1132+
<property name="visible">True</property>
1133+
<property name="can-focus">False</property>
1134+
<property name="label-xalign">0</property>
1135+
<property name="shadow-type">none</property>
1136+
<child>
1137+
<object class="GtkListBox" id="key_hint_box">
1138+
<property name="visible">True</property>
1139+
<property name="can-focus">False</property>
1140+
<property name="selection-mode">none</property>
1141+
</object>
1142+
</child>
1143+
<child type="label_item">
1144+
<placeholder/>
1145+
</child>
1146+
<style>
1147+
<class name="imp-key-hint-slider"/>
1148+
</style>
1149+
</object>
1150+
</child>
1151+
</object>
1152+
<packing>
1153+
<property name="index">5</property>
1154+
</packing>
1155+
</child>
11221156
</object>
11231157
<packing>
11241158
<property name="expand">True</property>

0 commit comments

Comments
 (0)