Skip to content

Commit

Permalink
fix(ui): add stroke size increase/decrease/reset
Browse files Browse the repository at this point in the history
  • Loading branch information
jtheoof committed Dec 27, 2019
1 parent 425f455 commit 5930c99
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 8 deletions.
8 changes: 7 additions & 1 deletion include/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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);
6 changes: 6 additions & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -90,6 +94,8 @@ struct swappy_state_ui {

GtkRadioButton *red;
GtkColorButton *custom;

GtkButton *stroke_size;
};

struct swappy_state {
Expand Down
82 changes: 81 additions & 1 deletion res/swappy.ui
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
<property name="can_focus">False</property>
<property name="icon_name">edit-undo</property>
</object>
<object class="GtkImage" id="zoom-in">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">zoom-in</property>
</object>
<object class="GtkImage" id="zoom-out">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">zoom-out</property>
</object>
<object class="GtkApplicationWindow" id="paint-window">
<property name="visible">True</property>
<property name="can_focus">False</property>
Expand Down Expand Up @@ -56,8 +66,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="y" signal="clicked" modifiers="GDK_CONTROL_MASK"/>
<accelerator key="z" signal="clicked" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
<accelerator key="y" signal="clicked" modifiers="GDK_CONTROL_MASK"/>
</object>
<packing>
<property name="expand">True</property>
Expand Down Expand Up @@ -331,6 +341,7 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">center</property>
<property name="margin_bottom">15</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
Expand Down Expand Up @@ -482,6 +493,75 @@
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">2</property>
<property name="homogeneous">True</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Size</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="minus-button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="image">zoom-out</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="stroke_size_decrease_handler" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="stroke-size-button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="stroke_size_reset_handler" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="plus-button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="image">zoom-in</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="stroke_size_increase_handler" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
<packing>
<property name="resize">False</property>
Expand Down
67 changes: 61 additions & 6 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5930c99

Please sign in to comment.