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

Create save_dir if it does not exist, and allow configuration of starting painting mode #123

Merged
merged 3 commits into from
Apr 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ show_panel=false
line_size=5
text_size=20
text_font=sans-serif
paint_mode=brush
```

- `save_dir` is where swappshots will be saved, can contain env variables and must exist in your filesystem
- `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
- `save_filename_format`: is the filename template, if it contains a date format, this will be parsed into a timestamp. Format is detailed in [strftime(3)](https://man.archlinux.org/man/strftime.3). If this date format is missing, filename will have no timestamp
- `show_panel` is used to toggle the paint panel on or off upon startup
- `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
- `paint_mode` is the mode activated at application start (must be one of: brush|text|rectangle|ellipse|arrow|blur, matching is case-insensitive)

## 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 @@ -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
35 changes: 34 additions & 1 deletion 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 All @@ -97,7 +99,11 @@ static void load_config_from_file(struct swappy_config *config,
save_dir_expanded = g_strdup(p.we_wordv[0]);
wordfree(&p);
if (!save_dir_expanded || !folder_exists(save_dir_expanded)) {
g_warning("save_dir: %s is not a valid directory", save_dir_expanded);
g_info("save_dir: attempting to create non-existent directory '%s'",
save_dir_expanded);
if (g_mkdir_with_parents(save_dir_expanded, 0755)) {
g_warning("save_dir: failed to create '%s'", save_dir_expanded);
}
}

g_free(save_dir);
Expand Down Expand Up @@ -176,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 @@ -190,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
4 changes: 3 additions & 1 deletion swappy.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ The following lines can be used as swappy's default:
line_size=5
text_size=20
text_font=sans-serif
paint_mode=brush
```

- *save_dir* is where swappshots will be saved, can contain env variables and must exist in your filesystem
- *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
- *save_filename_format* is the filename template, if it contains a date format, this will be parsed into a timestamp. Format is detailed in strftime(3). If this date format is missing, filename will have no timestamp
- *show_panel* is used to toggle the paint panel on or off upon startup
- *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
- *paint_mode* is the mode activated at application start (must be one of: brush|text|rectangle|ellipse|arrow|blur, matching is case-insensitive)

# KEY BINDINGS

Expand Down