From 5930c99b9e0208148d6bc8cf0fc3aa8f69dbd36d Mon Sep 17 00:00:00 2001 From: Jeremy Attali Date: Fri, 27 Dec 2019 12:27:30 -0500 Subject: [PATCH] fix(ui): add stroke size increase/decrease/reset --- include/application.h | 8 ++++- include/swappy.h | 6 ++++ res/swappy.ui | 82 ++++++++++++++++++++++++++++++++++++++++++- src/application.c | 67 +++++++++++++++++++++++++++++++---- 4 files changed, 155 insertions(+), 8 deletions(-) diff --git a/include/application.h b/include/application.h index 89eb4b0..4116390 100644 --- a/include/application.h +++ b/include/application.h @@ -42,4 +42,10 @@ void color_blue_clicked_handler(GtkWidget *widget, struct swappy_state *state); void color_custom_clicked_handler(GtkWidget *widget, struct swappy_state *state); void color_custom_color_set_handler(GtkWidget *widget, - struct swappy_state *state); \ No newline at end of file + struct swappy_state *state); + +void stroke_size_decrease_handler(GtkWidget *widget, + struct swappy_state *state); +void stroke_size_reset_handler(GtkWidget *widget, struct swappy_state *state); +void stroke_size_increase_handler(GtkWidget *widget, + struct swappy_state *state); diff --git a/include/swappy.h b/include/swappy.h index 8771702..2516696 100644 --- a/include/swappy.h +++ b/include/swappy.h @@ -15,6 +15,10 @@ #define GEOMETRY_PATTERN "xx,yy wwxhh" +#define SWAPPY_STROKE_SIZE_MIN 1 +#define SWAPPY_STROKE_SIZE_DEFAULT 5 +#define SWAPPY_STROKE_SIZE_MAX 50 + enum swappy_paint_type { SWAPPY_PAINT_MODE_BRUSH = 0, /* Brush mode to draw arbitrary shapes */ SWAPPY_PAINT_MODE_TEXT, /* Mode to draw texts */ @@ -90,6 +94,8 @@ struct swappy_state_ui { GtkRadioButton *red; GtkColorButton *custom; + + GtkButton *stroke_size; }; struct swappy_state { diff --git a/res/swappy.ui b/res/swappy.ui index 6e918dd..4cf0d9a 100644 --- a/res/swappy.ui +++ b/res/swappy.ui @@ -12,6 +12,16 @@ False edit-undo + + True + False + zoom-in + + + True + False + zoom-out + True False @@ -56,8 +66,8 @@ edit-redo True - + True @@ -331,6 +341,7 @@ True False center + 15 True @@ -482,6 +493,75 @@ 2 + + + True + False + 2 + True + + + True + False + Size + + + False + True + 0 + + + + + True + True + True + zoom-out + True + + + + False + False + 1 + + + + + True + True + True + True + + + + False + True + 2 + + + + + True + True + True + zoom-in + True + + + + False + False + 3 + + + + + False + True + 3 + + False diff --git a/src/application.c b/src/application.c index 26d5934..073e4af 100644 --- a/src/application.c +++ b/src/application.c @@ -12,7 +12,7 @@ #include "swappy.h" #include "wayland.h" -static void update_ui(struct swappy_state *state) { +static void update_ui_undo_redo(struct swappy_state *state) { GtkWidget *undo = GTK_WIDGET(state->ui->undo); GtkWidget *redo = GTK_WIDGET(state->ui->redo); gboolean undo_sensitive = g_slist_length(state->paints) > 0; @@ -21,6 +21,13 @@ static void update_ui(struct swappy_state *state) { gtk_widget_set_sensitive(redo, redo_sensitive); } +static void update_ui_stroke_size_widget(struct swappy_state *state) { + GtkButton *button = GTK_BUTTON(state->ui->stroke_size); + char label[255]; + snprintf(label, 255, "%.0lf", state->painting.w); + gtk_button_set_label(button, label); +} + static void action_undo(struct swappy_state *state) { GSList *first = state->paints; @@ -29,7 +36,7 @@ static void action_undo(struct swappy_state *state) { state->redo_paints = g_slist_prepend(state->redo_paints, first->data); render_state(state); - update_ui(state); + update_ui_undo_redo(state); } } @@ -41,14 +48,14 @@ static void action_redo(struct swappy_state *state) { state->paints = g_slist_prepend(state->paints, first->data); render_state(state); - update_ui(state); + update_ui_undo_redo(state); } } static void action_clear(struct swappy_state *state) { paint_free_all(state); render_state(state); - update_ui(state); + update_ui_undo_redo(state); } static void action_toggle_painting_pane(struct swappy_state *state) { @@ -99,6 +106,35 @@ static void action_update_color_state(struct swappy_state *state, double r, state->painting.b = b; } +static void action_stroke_size_decrease(struct swappy_state *state) { + guint step = state->painting.w <= 10 ? 1 : 5; + + state->painting.w -= step; + + if (state->painting.w < SWAPPY_STROKE_SIZE_MIN) { + state->painting.w = SWAPPY_STROKE_SIZE_MIN; + } + + update_ui_stroke_size_widget(state); +} + +static void action_stroke_size_reset(struct swappy_state *state) { + state->painting.w = SWAPPY_STROKE_SIZE_DEFAULT; + + update_ui_stroke_size_widget(state); +} + +static void action_stroke_size_increase(struct swappy_state *state) { + guint step = state->painting.w >= 10 ? 5 : 1; + state->painting.w += step; + + if (state->painting.w > SWAPPY_STROKE_SIZE_MAX) { + state->painting.w = SWAPPY_STROKE_SIZE_MAX; + } + + update_ui_stroke_size_widget(state); +} + void brush_clicked_handler(GtkWidget *widget, struct swappy_state *state) { switch_mode_to_brush(state); } @@ -329,7 +365,7 @@ void draw_area_button_release_handler(GtkWidget *widget, GdkEventButton *event, paint_commit_temporary(state); paint_free_list(&state->redo_paints); render_state(state); - update_ui(state); + update_ui_undo_redo(state); break; default: return; @@ -369,6 +405,19 @@ void color_custom_color_set_handler(GtkWidget *widget, action_set_color_from_custom(state); } +void stroke_size_decrease_handler(GtkWidget *widget, + struct swappy_state *state) { + action_stroke_size_decrease(state); +} + +void stroke_size_reset_handler(GtkWidget *widget, struct swappy_state *state) { + action_stroke_size_reset(state); +} +void stroke_size_increase_handler(GtkWidget *widget, + struct swappy_state *state) { + action_stroke_size_increase(state); +} + static void apply_css(GtkWidget *widget, GtkStyleProvider *provider) { gtk_style_context_add_provider(gtk_widget_get_style_context(widget), provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); @@ -427,6 +476,9 @@ static bool load_layout(struct swappy_state *state) { state->ui->custom = GTK_COLOR_BUTTON(gtk_builder_get_object(builder, "custom-color-button")); + state->ui->stroke_size = + GTK_BUTTON(gtk_builder_get_object(builder, "stroke-size-button")); + // gtk_popover_set_relative_to(ui, area); gtk_widget_set_size_request(area, geometry->width, geometry->height); @@ -448,6 +500,9 @@ static void init_gtk_window(struct swappy_state *state) { load_layout(state); load_css(state); + + update_ui_stroke_size_widget(state); + update_ui_undo_redo(state); } static gboolean is_geometry_valid(struct swappy_state *state) { @@ -522,7 +577,7 @@ bool application_init(struct swappy_state *state) { state->painting.g = 0; state->painting.b = 0; state->painting.a = 1; - state->painting.w = 2; + state->painting.w = SWAPPY_STROKE_SIZE_DEFAULT; return true; }