Skip to content

Commit

Permalink
gallery: Add help support
Browse files Browse the repository at this point in the history
Prints list of key binds by F1 key press.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Aug 10, 2024
1 parent ac6a47a commit 4d60af8
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 404 deletions.
1 change: 1 addition & 0 deletions extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ Alt+ScrollDown = next_frame
# Gallery mode key binding configuration: key = action [parameters]
################################################################################
[keys.gallery]
F1 = help
Home = first_file
End = last_file
Left = step_left
Expand Down
1 change: 0 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ sources = [
'src/pixmap.c',
'src/str.c',
'src/sway.c',
'src/text.c',
'src/ui.c',
'src/viewer.c',
'src/formats/bmp.c',
Expand Down
5 changes: 3 additions & 2 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "loader.h"
#include "str.h"
#include "sway.h"
#include "text.h"
#include "ui.h"
#include "viewer.h"

Expand Down Expand Up @@ -305,7 +304,6 @@ void app_create(void)
image_list_create();
info_create();
keybind_create();
text_create();
viewer_create();
gallery_create();

Expand Down Expand Up @@ -503,6 +501,9 @@ void app_switch_mode(size_t index)
if (info_enabled()) {
info_switch(info_mode);
}
if (info_help_active()) {
info_switch_help();
}

app_redraw();
}
Expand Down
43 changes: 36 additions & 7 deletions src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
#define POINT_FACTOR 64.0 // default points per pixel for 26.6 format
#define SPACE_WH_REL 2.0

// Defaults
#define DEFALT_FONT "monospace"
#define DEFALT_SIZE 14

/** Font context. */
struct font {
FT_Library lib; ///< Font lib instance
FT_Face face; ///< Font face instance
char* name; ///< Font face name
size_t size; ///< Font size (pt)
argb_t color; ///< Font color
argb_t shadow; ///< Font shadow color
};

/** Global font context instance. */
static struct font ctx;

/**
Expand Down Expand Up @@ -127,6 +127,17 @@ static enum config_status load_config(const char* key, const char* value)
ctx.size = num;
status = cfgst_ok;
}
} else if (strcmp(key, "color") == 0) {
if (config_to_color(value, &ctx.color)) {
status = cfgst_ok;
}
} else if (strcmp(key, "shadow") == 0) {
if (strcmp(value, "none") == 0) {
ctx.shadow = 0;
status = cfgst_ok;
} else if (config_to_color(value, &ctx.shadow)) {
status = cfgst_ok;
}
} else {
status = cfgst_invalid_key;
}
Expand All @@ -137,11 +148,13 @@ static enum config_status load_config(const char* key, const char* value)
void font_create(void)
{
// set defaults
str_dup(DEFALT_FONT, &ctx.name);
ctx.size = DEFALT_SIZE;
str_dup("monospace", &ctx.name);
ctx.size = 14;
ctx.color = ARGB(0xff, 0xcc, 0xcc, 0xcc);
ctx.shadow = ARGB(0x80, 0, 0, 0);

// register configuration loader
config_add_loader(FONT_CONFIG_SECTION, load_config);
config_add_loader("font", load_config);
}

void font_init(void)
Expand Down Expand Up @@ -230,3 +243,19 @@ bool font_render(const char* text, struct text_surface* surface)

return true;
}

void font_print(struct pixmap* wnd, ssize_t x, ssize_t y,
const struct text_surface* text)
{
if (ARGB_GET_A(ctx.shadow)) {
ssize_t shadow_offset = text->height / 16;
if (shadow_offset < 1) {
shadow_offset = 1;
}
pixmap_apply_mask(wnd, x + shadow_offset, y + shadow_offset, text->data,
text->width, text->height, ctx.shadow);
}

pixmap_apply_mask(wnd, x, y, text->data, text->width, text->height,
ctx.color);
}
16 changes: 10 additions & 6 deletions src/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

#pragma once

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

// Configuration section
#define FONT_CONFIG_SECTION "font"
#include "pixmap.h"

/** Text surface: array of alpha pixels. */
struct text_surface {
Expand Down Expand Up @@ -40,3 +35,12 @@ void font_destroy(void);
* @return true if operation completed successfully
*/
bool font_render(const char* text, struct text_surface* surface);

/**
* Print surface line on the window.
* @param wnd destination window
* @param x,y text position
* @param text text surface to draw
*/
void font_print(struct pixmap* wnd, ssize_t x, ssize_t y,
const struct text_surface* text);
13 changes: 11 additions & 2 deletions src/gallery.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "info.h"
#include "loader.h"
#include "str.h"
#include "text.h"
#include "ui.h"

#include <stdlib.h>
Expand Down Expand Up @@ -440,6 +439,10 @@ static void on_keyboard(xkb_keysym_t key, uint8_t mods)
for (size_t i = 0; i < kb->num_actions; ++i) {
const struct action* action = &kb->actions[i];
switch (action->type) {
case action_help:
info_switch_help();
app_redraw();
break;
case action_fullscreen:
ui_toggle_fullscreen();
break;
Expand Down Expand Up @@ -481,7 +484,13 @@ static void on_keyboard(xkb_keysym_t key, uint8_t mods)
app_redraw();
break;
case action_exit:
app_exit(0);
if (info_help_active()) {
info_switch_help(); // remove help overlay
app_redraw();
} else {
app_exit(0);
return;
}
break;
default:
break;
Expand Down
Loading

0 comments on commit 4d60af8

Please sign in to comment.