Skip to content

Commit 2f35f02

Browse files
blurgyyjtheoof
authored andcommitted
feat(config): allow paint_mode to be configured through config file
Signed-off-by: Gaoyang Zhang <[email protected]>
1 parent 4fb291a commit 2f35f02

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

include/config.h

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define CONFIG_TEXT_SIZE_DEFAULT 20
66
#define CONFIG_SHOW_PANEL_DEFAULT false
77
#define CONFIG_SAVE_FILENAME_FORMAT_DEFAULT "swappy-%Y%m%d_%H%M%S.png"
8+
#define CONFIG_PAINT_MODE_DEFAULT SWAPPY_PAINT_MODE_BRUSH
89

910
void config_load(struct swappy_state *state);
1011
void config_free(struct swappy_state *state);

include/swappy.h

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ struct swappy_config {
143143
char *config_file;
144144
char *save_dir;
145145
char *save_filename_format;
146+
gint8 paint_mode;
146147
gboolean show_panel;
147148
guint32 line_size;
148149
guint32 text_size;

src/application.c

+29
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,32 @@ static bool load_layout(struct swappy_state *state) {
762762
return true;
763763
}
764764

765+
static void set_paint_mode(struct swappy_state *state) {
766+
switch (state->mode) {
767+
case SWAPPY_PAINT_MODE_BRUSH:
768+
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->brush), true);
769+
break;
770+
case SWAPPY_PAINT_MODE_TEXT:
771+
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->text), true);
772+
break;
773+
case SWAPPY_PAINT_MODE_RECTANGLE:
774+
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->rectangle),
775+
true);
776+
break;
777+
case SWAPPY_PAINT_MODE_ELLIPSE:
778+
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->ellipse), true);
779+
break;
780+
case SWAPPY_PAINT_MODE_ARROW:
781+
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->arrow), true);
782+
break;
783+
case SWAPPY_PAINT_MODE_BLUR:
784+
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(state->ui->blur), true);
785+
break;
786+
default:
787+
break;
788+
}
789+
}
790+
765791
static bool init_gtk_window(struct swappy_state *state) {
766792
if (!state->original_image) {
767793
g_critical("original image not loaded");
@@ -776,6 +802,8 @@ static bool init_gtk_window(struct swappy_state *state) {
776802
return false;
777803
}
778804

805+
set_paint_mode(state);
806+
779807
update_ui_stroke_size_widget(state);
780808
update_ui_text_size_widget(state);
781809
update_ui_undo_redo(state);
@@ -799,6 +827,7 @@ static void init_settings(struct swappy_state *state) {
799827
state->settings.a = 1;
800828
state->settings.w = state->config->line_size;
801829
state->settings.t = state->config->text_size;
830+
state->mode = state->config->paint_mode;
802831
}
803832

804833
static gint command_line_handler(GtkApplication *app,

src/config.c

+29
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static void print_config(struct swappy_config *config) {
1919
g_info("line_size: %d", config->line_size);
2020
g_info("text_font: %s", config->text_font);
2121
g_info("text_size: %d", config->text_size);
22+
g_info("paint_mode: %d", config->paint_mode);
2223
}
2324

2425
static char *get_default_save_dir() {
@@ -75,6 +76,7 @@ static void load_config_from_file(struct swappy_config *config,
7576
gchar *save_dir_expanded = NULL;
7677
guint64 line_size, text_size;
7778
gchar *text_font = NULL;
79+
gchar *paint_mode = NULL;
7880
GError *error = NULL;
7981

8082
if (file == NULL) {
@@ -180,6 +182,32 @@ static void load_config_from_file(struct swappy_config *config,
180182
error = NULL;
181183
}
182184

185+
paint_mode = g_key_file_get_string(gkf, group, "paint_mode", &error);
186+
187+
if (error == NULL) {
188+
if (g_ascii_strcasecmp(paint_mode, "brush") == 0) {
189+
config->paint_mode = SWAPPY_PAINT_MODE_BRUSH;
190+
} else if (g_ascii_strcasecmp(paint_mode, "text") == 0) {
191+
config->paint_mode = SWAPPY_PAINT_MODE_TEXT;
192+
} else if (g_ascii_strcasecmp(paint_mode, "rectangle") == 0) {
193+
config->paint_mode = SWAPPY_PAINT_MODE_RECTANGLE;
194+
} else if (g_ascii_strcasecmp(paint_mode, "ellipse") == 0) {
195+
config->paint_mode = SWAPPY_PAINT_MODE_ELLIPSE;
196+
} else if (g_ascii_strcasecmp(paint_mode, "arrow") == 0) {
197+
config->paint_mode = SWAPPY_PAINT_MODE_ARROW;
198+
} else if (g_ascii_strcasecmp(paint_mode, "blur") == 0) {
199+
config->paint_mode = SWAPPY_PAINT_MODE_BLUR;
200+
} else {
201+
g_warning(
202+
"paint_mode is not a valid value: %s - see man page for details",
203+
paint_mode);
204+
}
205+
} else {
206+
g_info("paint_mode is missing in %s (%s)", file, error->message);
207+
g_error_free(error);
208+
error = NULL;
209+
}
210+
183211
g_key_file_free(gkf);
184212
}
185213

@@ -194,6 +222,7 @@ static void load_default_config(struct swappy_config *config) {
194222
config->text_font = g_strdup(CONFIG_TEXT_FONT_DEFAULT);
195223
config->text_size = CONFIG_TEXT_SIZE_DEFAULT;
196224
config->show_panel = CONFIG_SHOW_PANEL_DEFAULT;
225+
config->paint_mode = CONFIG_PAINT_MODE_DEFAULT;
197226
}
198227

199228
void config_load(struct swappy_state *state) {

0 commit comments

Comments
 (0)