Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add quit_on_copy config #134

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ struct swappy_config {
guint32 line_size;
guint32 text_size;
char *text_font;
gboolean early_exit;
};

struct swappy_state {
Expand Down
49 changes: 27 additions & 22 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

jtheoof marked this conversation as resolved.
Show resolved Hide resolved
void save_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
// Commit a potential paint (e.g. text being written)
commit_state(state);
Expand Down
4 changes: 4 additions & 0 deletions src/clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 13 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions swappy.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down