Skip to content

Commit

Permalink
feat(blur): use rect blur instead of brush
Browse files Browse the repository at this point in the history
Use a rectangle to blur instead of brushes.

Does not improve the performance that much, will come in a later patch.

Will fix the UX of the feature though.

Closes #17
  • Loading branch information
jtheoof committed Jun 2, 2020
1 parent befb61e commit 1be7798
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 108 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ The following lines can be used as swappy's default:
```
[Default]
save_dir=$HOME/Desktop
blur_level=80
line_size=5
text_size=20
text_font=sans-serif
```

- `save_dir` is where swappshots will be saved, can contain env variables and must exist in your filesystem
- `blur_level` is the default blur level (must be between 1 and 500)
- `line_size` is the default line size (must be between 1 and 50)
- `text_size` is the default text size (must be between 10 and 50)
- `text_font` is the font used to render text, its format is pango friendly
Expand Down
8 changes: 3 additions & 5 deletions include/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ void draw_area_button_release_handler(GtkWidget *widget, GdkEventButton *event,
void draw_area_motion_notify_handler(GtkWidget *widget, GdkEventMotion *event,
struct swappy_state *state);

void blur_radius_decrease_handler(GtkWidget *widget,
struct swappy_state *state);
void blur_radius_increase_handler(GtkWidget *widget,
struct swappy_state *state);
void blur_radius_reset_handler(GtkWidget *widget, struct swappy_state *state);
void blur_level_decrease_handler(GtkWidget *widget, struct swappy_state *state);
void blur_level_increase_handler(GtkWidget *widget, struct swappy_state *state);
void blur_level_reset_handler(GtkWidget *widget, struct swappy_state *state);

void brush_clicked_handler(GtkWidget *widget, struct swappy_state *state);
void text_clicked_handler(GtkWidget *widget, struct swappy_state *state);
Expand Down
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "swappy.h"

#define CONFIG_BLUR_RADIUS_DEFAULT 15
#define CONFIG_BLUR_LEVEL_DEFAULT 80
#define CONFIG_LINE_SIZE_DEFAULT 5
#define CONFIG_TEXT_FONT_DEFAULT "sans-serif"
#define CONFIG_TEXT_SIZE_DEFAULT 20
Expand Down
16 changes: 9 additions & 7 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#define SWAPPY_LINE_SIZE_MIN 1
#define SWAPPY_LINE_SIZE_MAX 50

#define SWAPPY_BLUR_RADIUS_MIN 1
#define SWAPPY_BLUR_RADIUS_MAX 50
#define SWAPPY_BLUR_LEVEL_MIN 1
#define SWAPPY_BLUR_LEVEL_MAX 500

#define SWAPPY_TEXT_SIZE_MIN 10
#define SWAPPY_TEXT_SIZE_MAX 50
Expand Down Expand Up @@ -79,13 +79,15 @@ struct swappy_paint_brush {
};

struct swappy_paint_blur {
double radius;
GList *points;
double bluriness;
struct swappy_point from;
struct swappy_point to;
};

struct swappy_paint {
enum swappy_paint_type type;
bool can_draw;
bool is_committed;
union {
struct swappy_paint_brush brush;
struct swappy_paint_shape shape;
Expand All @@ -108,7 +110,7 @@ struct swappy_state_settings {
double a;
double w;
double t;
guint32 blur_radius;
guint32 blur_level;
};

struct swappy_state_ui {
Expand All @@ -134,7 +136,7 @@ struct swappy_state_ui {
GtkRadioButton *custom;
GtkColorButton *color;

GtkButton *blur_radius;
GtkButton *blur_level;
GtkButton *line_size;
GtkButton *text_size;
};
Expand Down Expand Up @@ -186,7 +188,7 @@ struct swappy_config {
char *save_dir;
guint32 line_size;
guint32 text_size;
guint32 blur_radius;
guint32 blur_level;
char *text_font;
};

Expand Down
8 changes: 4 additions & 4 deletions res/swappy.ui
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Blur Radius</property>
<property name="label" translatable="yes">Blur Level</property>
</object>
<packing>
<property name="expand">False</property>
Expand All @@ -716,7 +716,7 @@
<property name="receives_default">True</property>
<property name="image">zoom-out2</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="blur_radius_decrease_handler" swapped="no"/>
<signal name="clicked" handler="blur_level_decrease_handler" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
Expand All @@ -730,7 +730,7 @@
<property name="can_focus">False</property>
<property name="receives_default">True</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="blur_radius_reset_handler" swapped="no"/>
<signal name="clicked" handler="blur_level_reset_handler" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
Expand All @@ -745,7 +745,7 @@
<property name="receives_default">True</property>
<property name="image">zoom-in2</property>
<property name="always_show_image">True</property>
<signal name="clicked" handler="blur_radius_increase_handler" swapped="no"/>
<signal name="clicked" handler="blur_level_increase_handler" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
Expand Down
58 changes: 29 additions & 29 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ static void update_ui_undo_redo(struct swappy_state *state) {
gtk_widget_set_sensitive(redo, redo_sensitive);
}

static void update_ui_blur_radius_widget(struct swappy_state *state) {
GtkButton *button = GTK_BUTTON(state->ui->blur_radius);
static void update_ui_blur_level_widget(struct swappy_state *state) {
GtkButton *button = GTK_BUTTON(state->ui->blur_level);
char label[255];
snprintf(label, 255, "%u", state->settings.blur_radius);
snprintf(label, 255, "%u", state->settings.blur_level);
gtk_button_set_label(button, label);
}

Expand Down Expand Up @@ -123,31 +123,31 @@ static void switch_mode_to_blur(struct swappy_state *state) {
state->mode = SWAPPY_PAINT_MODE_BLUR;
}

static void action_blur_radius_decrease(struct swappy_state *state) {
guint step = state->settings.blur_radius <= 10 ? 1 : 5;
static void action_blur_level_decrease(struct swappy_state *state) {
guint step = state->settings.blur_level <= 50 ? 5 : 10;

state->settings.blur_radius -= step;
state->settings.blur_level -= step;

if (state->settings.blur_radius < SWAPPY_BLUR_RADIUS_MIN) {
state->settings.blur_radius = SWAPPY_BLUR_RADIUS_MIN;
if (state->settings.blur_level < SWAPPY_BLUR_LEVEL_MIN) {
state->settings.blur_level = SWAPPY_BLUR_LEVEL_MIN;
}

update_ui_blur_radius_widget(state);
update_ui_blur_level_widget(state);
}
static void action_blur_radius_increase(struct swappy_state *state) {
guint step = state->settings.blur_radius >= 10 ? 5 : 1;
state->settings.blur_radius += step;
static void action_blur_level_increase(struct swappy_state *state) {
guint step = state->settings.blur_level >= 50 ? 10 : 5;
state->settings.blur_level += step;

if (state->settings.blur_radius > SWAPPY_BLUR_RADIUS_MAX) {
state->settings.blur_radius = SWAPPY_BLUR_RADIUS_MAX;
if (state->settings.blur_level > SWAPPY_BLUR_LEVEL_MAX) {
state->settings.blur_level = SWAPPY_BLUR_LEVEL_MAX;
}

update_ui_blur_radius_widget(state);
update_ui_blur_level_widget(state);
}
static void action_blur_radius_reset(struct swappy_state *state) {
state->settings.blur_radius = state->config->blur_radius;
static void action_blur_level_reset(struct swappy_state *state) {
state->settings.blur_level = state->config->blur_level;

update_ui_blur_radius_widget(state);
update_ui_blur_level_widget(state);
}

static void action_stroke_size_decrease(struct swappy_state *state) {
Expand Down Expand Up @@ -496,17 +496,17 @@ void draw_area_button_release_handler(GtkWidget *widget, GdkEventButton *event,
}
}

void blur_radius_decrease_handler(GtkWidget *widget,
struct swappy_state *state) {
action_blur_radius_decrease(state);
void blur_level_decrease_handler(GtkWidget *widget,
struct swappy_state *state) {
action_blur_level_decrease(state);
}

void blur_radius_increase_handler(GtkWidget *widget,
struct swappy_state *state) {
action_blur_radius_increase(state);
void blur_level_increase_handler(GtkWidget *widget,
struct swappy_state *state) {
action_blur_level_increase(state);
}
void blur_radius_reset_handler(GtkWidget *widget, struct swappy_state *state) {
action_blur_radius_reset(state);
void blur_level_reset_handler(GtkWidget *widget, struct swappy_state *state) {
action_blur_level_reset(state);
}

void color_red_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
Expand Down Expand Up @@ -654,7 +654,7 @@ static bool load_layout(struct swappy_state *state) {
state->ui->color =
GTK_COLOR_BUTTON(gtk_builder_get_object(builder, "custom-color-button"));

state->ui->blur_radius =
state->ui->blur_level =
GTK_BUTTON(gtk_builder_get_object(builder, "blur-radius-button"));
state->ui->line_size =
GTK_BUTTON(gtk_builder_get_object(builder, "stroke-size-button"));
Expand Down Expand Up @@ -694,7 +694,7 @@ static bool init_gtk_window(struct swappy_state *state) {
return false;
}

update_ui_blur_radius_widget(state);
update_ui_blur_level_widget(state);
update_ui_stroke_size_widget(state);
update_ui_text_size_widget(state);
update_ui_undo_redo(state);
Expand All @@ -721,7 +721,7 @@ static void init_settings(struct swappy_state *state) {
state->settings.a = 1;
state->settings.w = state->config->line_size;
state->settings.t = state->config->text_size;
state->settings.blur_radius = state->config->blur_radius;
state->settings.blur_level = state->config->blur_level;
}

static gint command_line_handler(GtkApplication *app,
Expand Down
20 changes: 10 additions & 10 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ static void print_config(struct swappy_config *config) {
g_info("printing config:");
g_info("config_dir: %s", config->config_file);
g_info("save_dir: %s", config->save_dir);
g_info("blur_radius: %d", config->blur_radius);
g_info("blur_level: %d", config->blur_level);
g_info("line_size: %d", config->line_size);
g_info("text_font: %s", config->text_font);
g_info("text_size: %d", config->text_size);
Expand Down Expand Up @@ -69,7 +69,7 @@ static void load_config_from_file(struct swappy_config *config,
const gchar *group = "Default";
gchar *save_dir = NULL;
gchar *save_dir_expanded = NULL;
guint64 line_size, text_size, blur_radius;
guint64 line_size, text_size, blur_level;
gchar *text_font = NULL;
GError *error = NULL;

Expand Down Expand Up @@ -140,19 +140,19 @@ static void load_config_from_file(struct swappy_config *config,
error = NULL;
}

blur_radius = g_key_file_get_uint64(gkf, group, "blur_radius", &error);
blur_level = g_key_file_get_uint64(gkf, group, "blur_level", &error);

if (error == NULL) {
if (blur_radius >= SWAPPY_BLUR_RADIUS_MIN &&
blur_radius <= SWAPPY_BLUR_RADIUS_MAX) {
config->blur_radius = blur_radius;
if (blur_level >= SWAPPY_BLUR_LEVEL_MIN &&
blur_level <= SWAPPY_BLUR_LEVEL_MAX) {
config->blur_level = blur_level;
} else {
g_warning(
"blur_radius is not a valid value: %ld - see man page for details",
blur_radius);
"blur_level is not a valid value: %ld - see man page for details",
blur_level);
}
} else {
g_info("blur_radius is missing in %s (%s)", file, error->message);
g_info("blur_level is missing in %s (%s)", file, error->message);
g_error_free(error);
error = NULL;
}
Expand All @@ -177,7 +177,7 @@ static void load_default_config(struct swappy_config *config) {
}

config->save_dir = get_default_save_dir();
config->blur_radius = CONFIG_BLUR_RADIUS_DEFAULT;
config->blur_level = CONFIG_BLUR_LEVEL_DEFAULT;
config->line_size = CONFIG_LINE_SIZE_DEFAULT;
config->text_font = g_strdup(CONFIG_TEXT_FONT_DEFAULT);
config->text_size = CONFIG_TEXT_SIZE_DEFAULT;
Expand Down
23 changes: 9 additions & 14 deletions src/paint.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ void paint_free(gpointer data) {

switch (paint->type) {
case SWAPPY_PAINT_MODE_BLUR:
g_list_free_full(paint->content.blur.points, g_free);
break;
case SWAPPY_PAINT_MODE_BRUSH:
g_list_free_full(paint->content.brush.points, g_free);
Expand Down Expand Up @@ -65,6 +64,7 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,
double t = state->settings.t;

paint->type = type;
paint->is_committed = false;

if (state->temp_paint) {
if (type == SWAPPY_PAINT_MODE_TEXT) {
Expand All @@ -77,14 +77,11 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,

switch (type) {
case SWAPPY_PAINT_MODE_BLUR:
paint->can_draw = true;

paint->content.blur.radius = state->settings.blur_radius;
point = g_new(struct swappy_point, 1);
point->x = x;
point->y = y;
paint->can_draw = false;

paint->content.blur.points = g_list_prepend(NULL, point);
paint->content.blur.bluriness = state->settings.blur_level;
paint->content.blur.from.x = x;
paint->content.blur.from.y = y;
break;
case SWAPPY_PAINT_MODE_BRUSH:
paint->can_draw = true;
Expand Down Expand Up @@ -152,12 +149,9 @@ void paint_update_temporary_shape(struct swappy_state *state, double x,

switch (paint->type) {
case SWAPPY_PAINT_MODE_BLUR:
points = paint->content.blur.points;
point = g_new(struct swappy_point, 1);
point->x = x;
point->y = y;

paint->content.blur.points = g_list_prepend(points, point);
paint->can_draw = true;
paint->content.blur.to.x = x;
paint->content.blur.to.y = y;
break;
case SWAPPY_PAINT_MODE_BRUSH:
points = paint->content.brush.points;
Expand Down Expand Up @@ -267,6 +261,7 @@ void paint_commit_temporary(struct swappy_state *state) {
if (!paint->can_draw) {
paint_free(paint);
} else {
paint->is_committed = true;
state->paints = g_list_prepend(state->paints, paint);
}

Expand Down
Loading

0 comments on commit 1be7798

Please sign in to comment.