Skip to content

Commit

Permalink
feat(config): allow paint_mode to be configured through config file
Browse files Browse the repository at this point in the history
Signed-off-by: Gaoyang Zhang <[email protected]>
  • Loading branch information
blurgyy committed Dec 26, 2021
1 parent 0b1c6e9 commit b6c2c60
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define CONFIG_TEXT_SIZE_DEFAULT 20
#define CONFIG_SHOW_PANEL_DEFAULT false
#define CONFIG_SAVE_FILENAME_FORMAT_DEFAULT "swappy-%Y%m%d_%H%M%S.png"
#define CONFIG_PAINT_MODE_DEFAULT SWAPPY_PAINT_MODE_BRUSH

void config_load(struct swappy_state *state);
void config_free(struct swappy_state *state);
1 change: 1 addition & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ struct swappy_config {
char *config_file;
char *save_dir;
char *save_filename_format;
gint8 paint_mode;
gboolean show_panel;
guint32 line_size;
guint32 text_size;
Expand Down
29 changes: 29 additions & 0 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,32 @@ static bool load_layout(struct swappy_state *state) {
return true;
}

static void set_paint_mode(struct swappy_state *state) {
switch (state->mode) {
case SWAPPY_PAINT_MODE_BRUSH:
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->brush), true);
break;
case SWAPPY_PAINT_MODE_TEXT:
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->text), true);
break;
case SWAPPY_PAINT_MODE_RECTANGLE:
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->rectangle),
true);
break;
case SWAPPY_PAINT_MODE_ELLIPSE:
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->ellipse), true);
break;
case SWAPPY_PAINT_MODE_ARROW:
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->arrow), true);
break;
case SWAPPY_PAINT_MODE_BLUR:
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->blur), true);
break;
default:
break;
}
}

static bool init_gtk_window(struct swappy_state *state) {
if (!state->original_image) {
g_critical("original image not loaded");
Expand All @@ -776,6 +802,8 @@ static bool init_gtk_window(struct swappy_state *state) {
return false;
}

set_paint_mode(state);

update_ui_stroke_size_widget(state);
update_ui_text_size_widget(state);
update_ui_undo_redo(state);
Expand All @@ -799,6 +827,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->mode = state->config->paint_mode;
}

static gint command_line_handler(GtkApplication *app,
Expand Down
29 changes: 29 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static void print_config(struct swappy_config *config) {
g_info("line_size: %d", config->line_size);
g_info("text_font: %s", config->text_font);
g_info("text_size: %d", config->text_size);
g_info("paint_mode: %d", config->paint_mode);
}

static char *get_default_save_dir() {
Expand Down Expand Up @@ -75,6 +76,7 @@ static void load_config_from_file(struct swappy_config *config,
gchar *save_dir_expanded = NULL;
guint64 line_size, text_size;
gchar *text_font = NULL;
gchar *paint_mode = NULL;
GError *error = NULL;

if (file == NULL) {
Expand Down Expand Up @@ -180,6 +182,32 @@ static void load_config_from_file(struct swappy_config *config,
error = NULL;
}

paint_mode = g_key_file_get_string(gkf, group, "paint_mode", &error);

if (error == NULL) {
if (g_ascii_strcasecmp(paint_mode, "brush") == 0) {
config->paint_mode = SWAPPY_PAINT_MODE_BRUSH;
} else if (g_ascii_strcasecmp(paint_mode, "text") == 0) {
config->paint_mode = SWAPPY_PAINT_MODE_TEXT;
} else if (g_ascii_strcasecmp(paint_mode, "rectangle") == 0) {
config->paint_mode = SWAPPY_PAINT_MODE_RECTANGLE;
} else if (g_ascii_strcasecmp(paint_mode, "ellipse") == 0) {
config->paint_mode = SWAPPY_PAINT_MODE_ELLIPSE;
} else if (g_ascii_strcasecmp(paint_mode, "arrow") == 0) {
config->paint_mode = SWAPPY_PAINT_MODE_ARROW;
} else if (g_ascii_strcasecmp(paint_mode, "blur") == 0) {
config->paint_mode = SWAPPY_PAINT_MODE_BLUR;
} else {
g_warning(
"paint_mode is not a valid value: %s - see man page for details",
paint_mode);
}
} else {
g_info("paint_mode is missing in %s (%s)", file, error->message);
g_error_free(error);
error = NULL;
}

g_key_file_free(gkf);
}

Expand All @@ -194,6 +222,7 @@ static void load_default_config(struct swappy_config *config) {
config->text_font = g_strdup(CONFIG_TEXT_FONT_DEFAULT);
config->text_size = CONFIG_TEXT_SIZE_DEFAULT;
config->show_panel = CONFIG_SHOW_PANEL_DEFAULT;
config->paint_mode = CONFIG_PAINT_MODE_DEFAULT;
}

void config_load(struct swappy_state *state) {
Expand Down

0 comments on commit b6c2c60

Please sign in to comment.