Skip to content

Commit ecab1de

Browse files
imp: add undo/redo hint
1 parent d8ba3ff commit ecab1de

File tree

13 files changed

+136
-11
lines changed

13 files changed

+136
-11
lines changed

src/global.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,7 @@ box.imp-instance-path-bar > label {
246246
.imp-instance-path-bar:backdrop > label {
247247
opacity:0.8;
248248
}
249+
250+
.imp_undo_redo_hint {
251+
border-radius: 5px;
252+
}

src/imp/3d/3d_view.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ bool View3DWindow::handle_action_key(const GdkEventKey *ev)
512512
void View3DWindow::trigger_action(ActionID action)
513513
{
514514
auto conn = action_connections.at(action);
515-
conn.cb(conn);
515+
conn.cb(conn, ActionSource::UNKNOWN);
516516
}
517517

518518
ActionConnection &View3DWindow::connect_action(ActionID action_id, std::function<void(const ActionConnection &)> cb)
@@ -523,9 +523,10 @@ ActionConnection &View3DWindow::connect_action(ActionID action_id, std::function
523523
if (action_catalog.count(action_id) == 0) {
524524
throw std::runtime_error("invalid action");
525525
}
526+
auto cb_wrapped = [cb](const ActionConnection &conn, ActionSource) { cb(conn); };
526527
auto &act = action_connections
527528
.emplace(std::piecewise_construct, std::forward_as_tuple(action_id),
528-
std::forward_as_tuple(action_id, cb))
529+
std::forward_as_tuple(action_id, cb_wrapped))
529530
.first->second;
530531

531532
return act;

src/imp/action.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ enum class ActionGroup {
6767
VIEW_3D,
6868
};
6969

70+
enum class ActionSource {
71+
UNKNOWN,
72+
KEY,
73+
};
74+
7075
using KeySequenceItem = std::pair<unsigned int, GdkModifierType>;
7176
using KeySequence = std::vector<KeySequenceItem>;
7277

@@ -81,13 +86,13 @@ KeyMatchResult key_sequence_match(const KeySequence &keys_current, const KeySequ
8186

8287
class ActionConnection {
8388
public:
84-
ActionConnection(ActionToolID atid, std::function<void(const ActionConnection &)> c) : id(atid), cb(c)
89+
ActionConnection(ActionToolID atid, std::function<void(const ActionConnection &, ActionSource)> c) : id(atid), cb(c)
8590
{
8691
}
8792

8893
const ActionToolID id;
8994
std::vector<KeySequence> key_sequences;
90-
std::function<void(const ActionConnection &)> cb;
95+
std::function<void(const ActionConnection &, ActionSource)> cb;
9196
};
9297

9398
} // namespace horizon

src/imp/imp.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,16 @@ void ImpBase::run(int argc, char *argv[])
498498
main_window->set_version_info("");
499499
}
500500
});
501-
connect_action(ActionID::UNDO, [this](const auto &a) {
501+
connect_action_with_source(ActionID::UNDO, [this](const auto &a, auto src) {
502+
if (src == ActionSource::KEY && preferences.undo_redo.show_hints)
503+
main_window->set_undo_redo_hint("Undid " + core->get_undo_comment());
502504
core->undo();
503505
this->canvas_update_from_pp();
504506
this->update_property_panels();
505507
});
506-
connect_action(ActionID::REDO, [this](const auto &a) {
508+
connect_action_with_source(ActionID::REDO, [this](const auto &a, auto src) {
509+
if (src == ActionSource::KEY && preferences.undo_redo.show_hints)
510+
main_window->set_undo_redo_hint("Redid " + core->get_redo_comment());
507511
core->redo();
508512
this->canvas_update_from_pp();
509513
this->update_property_panels();

src/imp/imp.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class ImpBase {
9494

9595
ActionConnection &connect_action(ToolID tool_id);
9696
ActionConnection &connect_action(ActionToolID id, std::function<void(const ActionConnection &)> cb);
97+
ActionConnection &connect_action_with_source(ActionToolID id,
98+
std::function<void(const ActionConnection &, ActionSource)> cb);
9799

98100
class RulesWindow *rules_window = nullptr;
99101

@@ -129,7 +131,7 @@ class ImpBase {
129131
bool handle_close(const GdkEventAny *ev);
130132
json send_json(const json &j);
131133

132-
bool trigger_action(ActionToolID action);
134+
bool trigger_action(ActionToolID action, ActionSource source = ActionSource::UNKNOWN);
133135

134136
void connect_go_to_project_manager_action();
135137

src/imp/imp_action.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ Gtk::Button *ImpBase::create_action_button(ActionToolID action)
204204
return button;
205205
}
206206

207-
bool ImpBase::trigger_action(ActionToolID action)
207+
bool ImpBase::trigger_action(ActionToolID action, ActionSource source)
208208
{
209209
if (core->tool_is_active() && !(action_catalog.at(action).flags & ActionCatalogItem::FLAGS_IN_TOOL)) {
210210
return false;
@@ -217,7 +217,7 @@ bool ImpBase::trigger_action(ActionToolID action)
217217
reset_tool_hint_label();
218218
}
219219
auto conn = action_connections.at(action);
220-
conn.cb(conn);
220+
conn.cb(conn, source);
221221
return true;
222222
}
223223

@@ -232,7 +232,8 @@ ActionConnection &ImpBase::connect_action(ToolID tool_id)
232232
return connect_action(tool_id, sigc::mem_fun(*this, &ImpBase::handle_tool_action));
233233
}
234234

235-
ActionConnection &ImpBase::connect_action(ActionToolID id, std::function<void(const ActionConnection &)> cb)
235+
ActionConnection &ImpBase::connect_action_with_source(ActionToolID id,
236+
std::function<void(const ActionConnection &, ActionSource)> cb)
236237
{
237238
if (action_connections.count(id)) {
238239
throw std::runtime_error("duplicate action");
@@ -247,6 +248,11 @@ ActionConnection &ImpBase::connect_action(ActionToolID id, std::function<void(co
247248
return act;
248249
}
249250

251+
ActionConnection &ImpBase::connect_action(ActionToolID id, std::function<void(const ActionConnection &)> cb)
252+
{
253+
return connect_action_with_source(id, [cb](const ActionConnection &conn, ActionSource) { cb(conn); });
254+
}
255+
250256
void ImpBase::handle_pan_action(const ActionConnection &c)
251257
{
252258
Coordf d;

src/imp/imp_key.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ bool ImpBase::handle_action_key(const GdkEventKey *ev)
206206
keys_current.clear();
207207
main_window->key_hint_set_visible(false);
208208
auto conn = connections_matched.begin()->first;
209-
if (!trigger_action(conn->id)) {
209+
if (!trigger_action(conn->id, ActionSource::KEY)) {
210210
reset_tool_hint_label();
211211
return false;
212212
}

src/imp/main_window.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ MainWindow::MainWindow(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder>
7373
GET_WIDGET(hierarchy_stack);
7474
GET_WIDGET(out_of_hierarchy_label);
7575

76+
GET_WIDGET(undo_redo_hint_frame);
77+
GET_WIDGET(undo_redo_hint_label);
78+
undo_redo_hint_frame->set_visible(false);
79+
7680
set_version_info("");
7781

7882
grid_options_button->signal_clicked().connect([this] {
@@ -307,6 +311,32 @@ void MainWindow::key_hint_set_visible(bool show)
307311
}
308312
}
309313

314+
void MainWindow::set_undo_redo_hint(const std::string &s)
315+
{
316+
if (!undo_redo_hint_frame->get_visible()) {
317+
undo_redo_hint_connection.disconnect();
318+
undo_redo_hint_label->set_text(s);
319+
undo_redo_hint_frame->set_visible(true);
320+
undo_redo_hint_connection = Glib::signal_timeout().connect(
321+
[this] {
322+
undo_redo_hint_frame->set_visible(false);
323+
return false;
324+
},
325+
1000);
326+
}
327+
else if (undo_redo_hint_frame->get_visible()) {
328+
undo_redo_hint_connection.disconnect();
329+
undo_redo_hint_frame->set_visible(false);
330+
const std::string s_copy = s;
331+
undo_redo_hint_connection = Glib::signal_timeout().connect(
332+
[this, s_copy] {
333+
set_undo_redo_hint(s_copy);
334+
return false;
335+
},
336+
200);
337+
}
338+
}
339+
310340
MainWindow *MainWindow::create()
311341
{
312342
MainWindow *w;

src/imp/main_window.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class MainWindow : public Gtk::ApplicationWindow {
8484

8585
void set_version_info(const std::string &s);
8686

87+
void set_undo_redo_hint(const std::string &s);
88+
8789
// virtual ~MainWindow();
8890
private:
8991
Gtk::EventBox *gl_container = nullptr;
@@ -126,5 +128,9 @@ class MainWindow : public Gtk::ApplicationWindow {
126128
Gtk::Revealer *key_hint_revealer = nullptr;
127129
sigc::connection key_hint_connection;
128130
void update_key_hint_position();
131+
132+
Gtk::Frame *undo_redo_hint_frame = nullptr;
133+
Gtk::Label *undo_redo_hint_label = nullptr;
134+
sigc::connection undo_redo_hint_connection;
129135
};
130136
} // namespace horizon

src/imp/window.ui

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,38 @@
12961296
<property name="index">5</property>
12971297
</packing>
12981298
</child>
1299+
<child type="overlay">
1300+
<object class="GtkFrame" id="undo_redo_hint_frame">
1301+
<property name="visible">True</property>
1302+
<property name="can-focus">False</property>
1303+
<property name="halign">center</property>
1304+
<property name="valign">end</property>
1305+
<property name="margin-bottom">100</property>
1306+
<property name="label-xalign">0</property>
1307+
<property name="shadow-type">none</property>
1308+
<child>
1309+
<object class="GtkLabel" id="undo_redo_hint_label">
1310+
<property name="visible">True</property>
1311+
<property name="can-focus">False</property>
1312+
<property name="margin-start">10</property>
1313+
<property name="margin-end">10</property>
1314+
<property name="margin-top">10</property>
1315+
<property name="margin-bottom">10</property>
1316+
<property name="label" translatable="yes">Undid Move</property>
1317+
</object>
1318+
</child>
1319+
<child type="label_item">
1320+
<placeholder/>
1321+
</child>
1322+
<style>
1323+
<class name="osd"/>
1324+
<class name="imp_undo_redo_hint"/>
1325+
</style>
1326+
</object>
1327+
<packing>
1328+
<property name="index">6</property>
1329+
</packing>
1330+
</child>
12991331
</object>
13001332
<packing>
13011333
<property name="expand">True</property>

0 commit comments

Comments
 (0)