Skip to content

Commit

Permalink
imp: add key hint popup
Browse files Browse the repository at this point in the history
  • Loading branch information
carrotIndustries committed Jun 30, 2021
1 parent f0af526 commit 8d9ac5f
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/global.css
Expand Up @@ -141,3 +141,12 @@
border-bottom:0px;
border-radius:0px;
}

.imp-key-hint-slider {
background-color: @theme_base_color;
border-color: @borders;
border-width: 1px 1px 0px 1px;
border-style: solid;
border-radius: 3px 3px 0px 0px;
padding-top: 3px;
}
2 changes: 2 additions & 0 deletions src/imp/imp.cpp
Expand Up @@ -1252,6 +1252,8 @@ bool ImpBase::handle_click(const GdkEventButton *button_event)
if (button_event->button != 2)
set_search_mode(false);

main_window->key_hint_set_visible(false);

bool need_menu = false;
if (core->tool_is_active() && button_event->button != 2 && !(button_event->state & Gdk::SHIFT_MASK)
&& button_event->type != GDK_2BUTTON_PRESS && button_event->type != GDK_3BUTTON_PRESS) {
Expand Down
1 change: 1 addition & 0 deletions src/imp/imp_action.cpp
Expand Up @@ -211,6 +211,7 @@ bool ImpBase::trigger_action(const ActionToolID &action)
if (core->tool_is_active() && !(action_catalog.at(action).flags & ActionCatalogItem::FLAGS_IN_TOOL)) {
return false;
}
main_window->key_hint_set_visible(false);
if (keys_current.size()) {
keys_current.clear();
main_window->tool_hint_label->set_text(">");
Expand Down
61 changes: 61 additions & 0 deletions src/imp/imp_key.cpp
Expand Up @@ -3,14 +3,49 @@
#include "logger/logger.hpp"
#include "in_tool_action_catalog.hpp"
#include "actions.hpp"
#include "util/str_util.hpp"

namespace horizon {


class KeyLabel : public Gtk::Box {
public:
KeyLabel(Glib::RefPtr<Gtk::SizeGroup> sg, const std::string &key_markup, ActionToolID act)
: Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 8), action(act)
{
{
auto la = Gtk::manage(new Gtk::Label);
la->set_xalign(0);
la->set_markup(key_markup);
la->show();
pack_start(*la, false, false, 0);
sg->add_widget(*la);
}
{
auto la = Gtk::manage(new Gtk::Label);
la->set_xalign(0);
la->set_text(action_catalog.at(action).name);
la->show();
pack_start(*la, true, true, 0);
}
property_margin() = 2;
}

const ActionToolID action;
};

void ImpBase::init_key()
{
canvas->signal_key_press_event().connect(sigc::mem_fun(*this, &ImpBase::handle_key_press));
key_sequence_dialog = std::make_unique<KeySequenceDialog>(this->main_window);
connect_action(ActionID::HELP, [this](const auto &a) { key_sequence_dialog->show(); });
main_window->key_hint_box->signal_row_activated().connect([this](auto row) {
if (auto la = dynamic_cast<KeyLabel *>(row->get_child())) {
trigger_action(la->action);
keys_current.clear();
main_window->key_hint_set_visible(false);
}
});
}

bool ImpBase::handle_key_press(const GdkEventKey *key_event)
Expand Down Expand Up @@ -96,6 +131,7 @@ bool ImpBase::handle_action_key(const GdkEventKey *ev)
return false;
}
else {
main_window->key_hint_set_visible(false);
keys_current.clear();
return true;
}
Expand Down Expand Up @@ -168,6 +204,7 @@ bool ImpBase::handle_action_key(const GdkEventKey *ev)
else if (connections_matched.size() == 1) {
main_window->tool_hint_label->set_text(key_sequence_to_string(keys_current));
keys_current.clear();
main_window->key_hint_set_visible(false);
auto conn = connections_matched.begin()->first;
if (!trigger_action({conn->action_id, conn->tool_id})) {
main_window->tool_hint_label->set_text(">");
Expand Down Expand Up @@ -216,12 +253,36 @@ bool ImpBase::handle_action_key(const GdkEventKey *ev)
return false;
}

for (auto ch : main_window->key_hint_box->get_children()) {
delete ch;
}

for (const auto &[conn, it] : connections_matched) {
const auto &[res, seq] = it;
std::string seq_label;
for (size_t i = 0; i < seq.size(); i++) {
seq_label += Glib::Markup::escape_text(key_sequence_item_to_string(seq.at(i))) + " ";
if (i + 1 == keys_current.size()) {
seq_label += "<b>";
}
}
rtrim(seq_label);
seq_label += "</b>";

auto la = Gtk::manage(
new KeyLabel(main_window->key_hint_size_group, seq_label, {conn->action_id, conn->tool_id}));
main_window->key_hint_box->append(*la);
la->show();
}
main_window->key_hint_set_visible(true);

main_window->tool_hint_label->set_text(key_sequence_to_string(keys_current) + "?");
return true;
}
else if (connections_matched.size() == 0 || in_tool_actions_matched.size() == 0) {
main_window->tool_hint_label->set_text("Unknown key sequence");
keys_current.clear();
main_window->key_hint_set_visible(false);
return false;
}
else {
Expand Down
43 changes: 36 additions & 7 deletions src/imp/main_window.cpp
Expand Up @@ -61,6 +61,10 @@ MainWindow::MainWindow(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder>
GET_WIDGET(version_info_bar);
GET_WIDGET(version_label);

GET_WIDGET(key_hint_box);
GET_WIDGET(key_hint_revealer);
key_hint_size_group = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL);

set_version_info("");

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

{
Gtk::EventBox *gl_container = nullptr;
GET_WIDGET(gl_container);
canvas = Gtk::manage(new CanvasGL());
gl_container->add(*canvas);
canvas->show();
}
GET_WIDGET(gl_container);
canvas = Gtk::manage(new CanvasGL());
gl_container->add(*canvas);
canvas->show();

tool_bar_set_visible(false);
hud->set_reveal_child(false);
set_use_action_bar(false);
Expand Down Expand Up @@ -266,6 +268,33 @@ void MainWindow::set_version_info(const std::string &s)
}
}

void MainWindow::update_key_hint_position()
{
int dest_x, dest_y;
if (tool_hint_label->translate_coordinates(*gl_container, 0, 0, dest_x, dest_y)) {
key_hint_revealer->set_margin_start(dest_x - 5); // compensate for various borders and margins
}
}

void MainWindow::key_hint_set_visible(bool show)
{
key_hint_connection.disconnect();
if (show) {
if (!key_hint_revealer->get_reveal_child()) {
update_key_hint_position();
key_hint_connection = Glib::signal_timeout().connect(
[this] {
key_hint_revealer->set_reveal_child(true);
return false;
},
500);
}
}
else {
key_hint_revealer->set_reveal_child(false);
}
}

MainWindow *MainWindow::create()
{
MainWindow *w;
Expand Down
10 changes: 10 additions & 0 deletions src/imp/main_window.hpp
Expand Up @@ -46,6 +46,10 @@ class MainWindow : public Gtk::ApplicationWindow {

Gtk::Button *grid_window_button = nullptr;

Gtk::ListBox *key_hint_box = nullptr;
Glib::RefPtr<Gtk::SizeGroup> key_hint_size_group;
void key_hint_set_visible(bool v);

Glib::SignalProxy<bool, const Glib::ustring &> signal_activate_hud_link()
{
return hud_label->signal_activate_link();
Expand Down Expand Up @@ -74,6 +78,8 @@ class MainWindow : public Gtk::ApplicationWindow {

// virtual ~MainWindow();
private:
Gtk::EventBox *gl_container = nullptr;

Gtk::Revealer *tool_bar = nullptr;
Gtk::Label *tool_bar_name_label = nullptr;
Gtk::Label *tool_bar_tip_label = nullptr;
Expand Down Expand Up @@ -108,5 +114,9 @@ class MainWindow : public Gtk::ApplicationWindow {
Gtk::Label *version_label = nullptr;

bool tool_bar_use_actions = false;

Gtk::Revealer *key_hint_revealer = nullptr;
sigc::connection key_hint_connection;
void update_key_hint_position();
};
} // namespace horizon
34 changes: 34 additions & 0 deletions src/imp/window.ui
Expand Up @@ -1119,6 +1119,40 @@
<property name="index">4</property>
</packing>
</child>
<child type="overlay">
<object class="GtkRevealer" id="key_hint_revealer">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="valign">end</property>
<property name="transition-type">slide-up</property>
<property name="transition-duration">100</property>
<child>
<object class="GtkFrame">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label-xalign">0</property>
<property name="shadow-type">none</property>
<child>
<object class="GtkListBox" id="key_hint_box">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="selection-mode">none</property>
</object>
</child>
<child type="label_item">
<placeholder/>
</child>
<style>
<class name="imp-key-hint-slider"/>
</style>
</object>
</child>
</object>
<packing>
<property name="index">5</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
Expand Down

0 comments on commit 8d9ac5f

Please sign in to comment.