From 60da5491e243c9edd85f6225326a68ae5e3edfd5 Mon Sep 17 00:00:00 2001 From: Seppe Soete Date: Sun, 31 Jul 2022 12:04:54 +0200 Subject: [PATCH] feat(config): add early_exit option --- README.md | 2 ++ include/config.h | 1 + include/swappy.h | 1 + src/application.c | 49 ++++++++++++++++++++++++++--------------------- src/clipboard.c | 4 ++++ src/config.c | 13 +++++++++++++ swappy.1.scd | 3 +++ 7 files changed, 51 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 5cfab42..57d7344 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ line_size=5 text_size=20 text_font=sans-serif paint_mode=brush +early_exit=false ``` - `save_dir` is where swappshots will be saved, can contain env variables, when it does not exist, swappy attempts to create it first, but does not abort if directory creation fails @@ -58,6 +59,7 @@ paint_mode=brush - `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 - `paint_mode` is the mode activated at application start (must be one of: brush|text|rectangle|ellipse|arrow|blur, matching is case-insensitive) +- `early_exit` is used to make the application exit after saving the picture or copying it to the clipboard ## Keyboard Shortcuts diff --git a/include/config.h b/include/config.h index ef59e10..cbc8e65 100644 --- a/include/config.h +++ b/include/config.h @@ -6,6 +6,7 @@ #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 +#define CONFIG_EARLY_EXIT_DEFAULT false void config_load(struct swappy_state *state); void config_free(struct swappy_state *state); diff --git a/include/swappy.h b/include/swappy.h index 5e7f49f..3aea3a0 100644 --- a/include/swappy.h +++ b/include/swappy.h @@ -148,6 +148,7 @@ struct swappy_config { guint32 line_size; guint32 text_size; char *text_font; + gboolean early_exit; }; struct swappy_state { diff --git a/src/application.c b/src/application.c index 883d3b9..8a6c2f7 100644 --- a/src/application.c +++ b/src/application.c @@ -45,6 +45,29 @@ static void update_ui_panel_toggle_button(struct swappy_state *state) { gtk_widget_set_visible(painting_box, toggled); } +void application_finish(struct swappy_state *state) { + g_debug("application finishing, cleaning up"); + paint_free_all(state); + pixbuf_free(state); + cairo_surface_destroy(state->rendering_surface); + cairo_surface_destroy(state->original_image_surface); + if (state->temp_file_str) { + g_info("deleting temporary file: %s", state->temp_file_str); + if (g_unlink(state->temp_file_str) != 0) { + g_warning("unable to delete temporary file: %s", state->temp_file_str); + } + g_free(state->temp_file_str); + } + g_free(state->file_str); + g_free(state->geometry); + g_free(state->window); + g_free(state->ui); + + g_object_unref(state->app); + + config_free(state); +} + static void action_undo(struct swappy_state *state) { GList *first = state->paints; @@ -191,6 +214,10 @@ static void save_state_to_file_or_folder(struct swappy_state *state, } g_object_unref(pixbuf); + + if (state->config->early_exit) { + gtk_main_quit(); + } } static void maybe_save_output_file(struct swappy_state *state) { @@ -254,28 +281,6 @@ void blur_clicked_handler(GtkWidget *widget, struct swappy_state *state) { switch_mode_to_blur(state); } -void application_finish(struct swappy_state *state) { - paint_free_all(state); - pixbuf_free(state); - cairo_surface_destroy(state->rendering_surface); - cairo_surface_destroy(state->original_image_surface); - if (state->temp_file_str) { - g_info("deleting temporary file: %s", state->temp_file_str); - if (g_unlink(state->temp_file_str) != 0) { - g_warning("unable to delete temporary file: %s", state->temp_file_str); - } - g_free(state->temp_file_str); - } - g_free(state->file_str); - g_free(state->geometry); - g_free(state->window); - g_free(state->ui); - - g_object_unref(state->app); - - config_free(state); -} - void save_clicked_handler(GtkWidget *widget, struct swappy_state *state) { // Commit a potential paint (e.g. text being written) commit_state(state); diff --git a/src/clipboard.c b/src/clipboard.c index 188c33c..dd03dd9 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -82,5 +82,9 @@ bool clipboard_copy_drawing_area_to_selection(struct swappy_state *state) { g_object_unref(pixbuf); + if (state->config->early_exit) { + gtk_main_quit(); + } + return true; } diff --git a/src/config.c b/src/config.c index 4ed7e9a..537c0d8 100644 --- a/src/config.c +++ b/src/config.c @@ -20,6 +20,7 @@ static void print_config(struct swappy_config *config) { g_info("text_font: %s", config->text_font); g_info("text_size: %d", config->text_size); g_info("paint_mode: %d", config->paint_mode); + g_info("early_exit: %d", config->early_exit); } static char *get_default_save_dir() { @@ -77,6 +78,7 @@ static void load_config_from_file(struct swappy_config *config, guint64 line_size, text_size; gchar *text_font = NULL; gchar *paint_mode = NULL; + gboolean early_exit; GError *error = NULL; if (file == NULL) { @@ -182,6 +184,16 @@ static void load_config_from_file(struct swappy_config *config, error = NULL; } + early_exit = g_key_file_get_boolean(gkf, group, "early_exit", &error); + + if (error == NULL) { + config->early_exit = early_exit; + } else { + g_info("early_exit is missing in %s (%s)", file, error->message); + g_error_free(error); + error = NULL; + } + paint_mode = g_key_file_get_string(gkf, group, "paint_mode", &error); if (error == NULL) { @@ -223,6 +235,7 @@ static void load_default_config(struct swappy_config *config) { config->text_size = CONFIG_TEXT_SIZE_DEFAULT; config->show_panel = CONFIG_SHOW_PANEL_DEFAULT; config->paint_mode = CONFIG_PAINT_MODE_DEFAULT; + config->early_exit = CONFIG_EARLY_EXIT_DEFAULT; } void config_load(struct swappy_state *state) { diff --git a/swappy.1.scd b/swappy.1.scd index 0f51b25..e1825e9 100644 --- a/swappy.1.scd +++ b/swappy.1.scd @@ -64,6 +64,7 @@ The following lines can be used as swappy's default: text_size=20 text_font=sans-serif paint_mode=brush + early_exit=false ``` - *save_dir* is where swappshots will be saved, can contain env variables, when it does not exist, swappy attempts to create it first, but does not abort if directory creation fails @@ -73,6 +74,8 @@ The following lines can be used as swappy's default: - *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 - *paint_mode* is the mode activated at application start (must be one of: brush|text|rectangle|ellipse|arrow|blur, matching is case-insensitive) +- *early_exit* is used to make the application exit after saving the picture or copying it to the clipboard + # KEY BINDINGS