Skip to content

Commit

Permalink
feat(ui): add shortcuts for undo/redo
Browse files Browse the repository at this point in the history
  • Loading branch information
jtheoof committed Dec 26, 2019
1 parent e8d2f12 commit d7e7f2b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 71 deletions.
4 changes: 2 additions & 2 deletions include/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ int application_run(struct swappy_state *state);
void application_finish(struct swappy_state *state);

/* Glade signals */
void keypress_handler(GtkWidget *widget, GdkEventKey *event,
struct swappy_state *state);
void window_keypress_handler(GtkWidget *widget, GdkEventKey *event,
struct swappy_state *state);

void undo_clicked_handler(GtkWidget *widget, struct swappy_state *state);
void redo_clicked_handler(GtkWidget *widget, struct swappy_state *state);
Expand Down
12 changes: 2 additions & 10 deletions res/swappy.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkAccelGroup" id="accel-group"/>
<object class="GtkImage" id="edit-redo">
<property name="visible">True</property>
<property name="can_focus">False</property>
Expand All @@ -19,10 +18,7 @@
<property name="resizable">False</property>
<property name="window_position">center</property>
<property name="show_menubar">False</property>
<accel-groups>
<group name="accel-group"/>
</accel-groups>
<signal name="key-press-event" handler="keypress_handler" swapped="no"/>
<signal name="key-press-event" handler="window_keypress_handler" swapped="no"/>
<child type="titlebar">
<object class="GtkHeaderBar">
<property name="visible">True</property>
Expand All @@ -42,7 +38,6 @@
<property name="image">edit-undo</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="undo_clicked_handler" swapped="no"/>
<accelerator key="z" signal="clicked" modifiers="GDK_CONTROL_MASK"/>
</object>
<packing>
<property name="expand">True</property>
Expand All @@ -61,8 +56,8 @@
<property name="image">edit-redo</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="redo_clicked_handler" swapped="no"/>
<accelerator key="z" signal="clicked" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
<accelerator key="y" signal="clicked" modifiers="GDK_CONTROL_MASK"/>
<accelerator key="z" signal="clicked" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
</object>
<packing>
<property name="expand">True</property>
Expand All @@ -71,9 +66,6 @@
<property name="non_homogeneous">True</property>
</packing>
</child>
<style>
<class name="right"/>
</style>
</object>
</child>
<child>
Expand Down
119 changes: 60 additions & 59 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ static void action_undo(struct swappy_state *state) {
if (first) {
state->paints = g_slist_remove_link(state->paints, first);
state->redo_paints = g_slist_prepend(state->redo_paints, first->data);

render_state(state);
update_ui(state);
}
}

Expand All @@ -36,6 +39,9 @@ static void action_redo(struct swappy_state *state) {
if (first) {
state->redo_paints = g_slist_remove_link(state->redo_paints, first);
state->paints = g_slist_prepend(state->paints, first->data);

render_state(state);
update_ui(state);
}
}

Expand Down Expand Up @@ -150,77 +156,72 @@ void copy_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
clipboard_copy_drawing_area_to_selection(state);
}

void keypress_handler(GtkWidget *widget, GdkEventKey *event,
struct swappy_state *state) {
void window_keypress_handler(GtkWidget *widget, GdkEventKey *event,
struct swappy_state *state) {
g_debug("keypress_handler key pressed: keyval: %d - state: %d\n",
event->keyval, event->state);

switch (event->state) {
case 0:
switch (event->keyval) {
case GDK_KEY_Escape:
g_debug("keypress_handler: escape key pressed, ciao bye\n");
gtk_main_quit();
break;
case GDK_KEY_b:
switch_mode_to_brush(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->brush),
true);
break;
case GDK_KEY_t:
switch_mode_to_text(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->text),
true);
break;
case GDK_KEY_r:
switch_mode_to_rectangle(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->rectangle),
true);
break;
case GDK_KEY_o:
switch_mode_to_ellipse(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->ellipse),
true);
break;
case GDK_KEY_a:
switch_mode_to_arrow(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->arrow),
true);
break;
default:
break;
}
break;
case GDK_CONTROL_MASK:
switch (event->keyval) {
case GDK_KEY_c:
clipboard_copy_drawing_area_to_selection(state);
break;
case GDK_KEY_s:
action_save_area_to_file(state);
break;
case GDK_KEY_b:
action_toggle_painting_pane(state);
break;
default:
break;
}
break;
default:
break;
if (event->state == 0) {
switch (event->keyval) {
case GDK_KEY_Escape:
g_debug("keypress_handler: escape key pressed, ciao bye\n");
gtk_main_quit();
break;
case GDK_KEY_b:
switch_mode_to_brush(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->brush), true);
break;
case GDK_KEY_t:
switch_mode_to_text(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->text), true);
break;
case GDK_KEY_r:
switch_mode_to_rectangle(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->rectangle),
true);
break;
case GDK_KEY_o:
switch_mode_to_ellipse(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->ellipse),
true);
break;
case GDK_KEY_a:
switch_mode_to_arrow(state);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->arrow), true);
break;
default:
break;
}
} else if (event->state & GDK_CONTROL_MASK) {
switch (event->keyval) {
case GDK_KEY_c:
clipboard_copy_drawing_area_to_selection(state);
break;
case GDK_KEY_s:
action_save_area_to_file(state);
break;
case GDK_KEY_b:
action_toggle_painting_pane(state);
break;
case GDK_KEY_z:
action_undo(state);
break;
case GDK_KEY_Z:
case GDK_KEY_y:
action_redo(state);
break;
default:
break;
}
}
}

void undo_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
action_undo(state);
render_state(state);
update_ui(state);
}

void redo_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
action_redo(state);
render_state(state);
update_ui(state);
}

gboolean draw_area_handler(GtkWidget *widget, cairo_t *cr,
Expand Down

0 comments on commit d7e7f2b

Please sign in to comment.