Skip to content

Commit

Permalink
Scales launched and xdg icons to tile size (#102)
Browse files Browse the repository at this point in the history
* Adds initial hack of a method to scale the icons.

* Cleans up error handling of scaled image loader.

* Makes the tile content size configurable and uses it in launcher.

* Uses the configurable content size also for the clip image.

* Makes the app icon (wlmclock) scale with tile size.
  • Loading branch information
phkaeser authored Aug 28, 2024
1 parent cb2f4b4 commit 1bfd8c3
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 22 deletions.
50 changes: 36 additions & 14 deletions apps/wlmclock.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ bool icon_callback(
{
bs_gfxbuf_clear(gfxbuf_ptr, 0);

if (gfxbuf_ptr->width != gfxbuf_ptr->height) {
bs_log(BS_ERROR, "Requiring a square buffer, width %u != height %u",
gfxbuf_ptr->width, gfxbuf_ptr->height);
return false;
}

unsigned width = gfxbuf_ptr->width;
unsigned outer = 4 * gfxbuf_ptr->width / 64;
unsigned inner = 5 * gfxbuf_ptr->width / 64;

cairo_t *cairo_ptr = cairo_create_from_bs_gfxbuf(gfxbuf_ptr);
if (NULL == cairo_ptr) {
bs_log(BS_ERROR, "Failed cairo_create_from_bs_gfxbuf(%p)", gfxbuf_ptr);
Expand All @@ -69,10 +79,14 @@ bool icon_callback(
BS_ASSERT(NULL != pattern_ptr);
cairo_set_source(cairo_ptr, pattern_ptr);
cairo_pattern_destroy(pattern_ptr);
cairo_rectangle(cairo_ptr, 5, 46, 54, 14);
cairo_rectangle(
cairo_ptr,
outer + 1, width - 18, width - 2 * outer - 2, 14);
cairo_fill(cairo_ptr);

wlm_primitives_draw_bezel_at(cairo_ptr, 4, 45, 56, 15, 1.0, false);
wlm_primitives_draw_bezel_at(
cairo_ptr,
outer, width - 19, width - 2 * outer, 15, 1.0, false);

struct timeval tv;
if (0 != gettimeofday(&tv, NULL)) {
Expand All @@ -87,32 +101,40 @@ bool icon_callback(
wlm_cairo_7segment_display_digit(
cairo_ptr,
&wlm_cairo_7segment_param_8x12,
6 + i * 8 + (i / 2) * 2, 58,
width / 2 - 26 + i * 8 + (i / 2) * 2,
width - 6,
color_led,
color_off,
time_buf[i] - '0');
}

cairo_set_source_argb8888(cairo_ptr, color_led);
cairo_rectangle(cairo_ptr, 22, 50, 1, 1.25);
cairo_rectangle(cairo_ptr, 22, 54, 1, 1.25);
cairo_rectangle(cairo_ptr, 40, 50, 1, 1.25);
cairo_rectangle(cairo_ptr, 40, 54, 1, 1.25);
cairo_rectangle(cairo_ptr, width / 2 - 10, width - 14, 1, 1.25);
cairo_rectangle(cairo_ptr, width / 2 - 10, width - 10, 1, 1.25);
cairo_rectangle(cairo_ptr, width / 2 + 8, width - 14, 1, 1.25);
cairo_rectangle(cairo_ptr, width / 2 + 8, width - 10, 1, 1.25);
cairo_fill(cairo_ptr);

wlm_primitives_draw_bezel_at(cairo_ptr, 4, 4, 56, 41, 1.0, false);

// Draws a clock face, with small ticks every hour.
double center_x = 31.5 * width / 64.0;
double center_y = 24.5 * width / 64.0;
double radius = 19 * width / 64.0;

wlm_primitives_draw_bezel_at(
cairo_ptr,
outer, outer,
width - 2 * outer, 41.0 * width / 64.0,
inner - outer, false);
cairo_set_source_argb8888(cairo_ptr, color_background);
cairo_rectangle(cairo_ptr, 5, 5, 54, 39);
cairo_rectangle(
cairo_ptr,
inner, inner,
width - 2 * inner, 39.0 * width / 64.0);
cairo_fill(cairo_ptr);

// Draws a clock face, with small ticks every hour.
double center_x = 31.5;
double center_y = 24.5;
double radius = 19;
cairo_set_source_argb8888(cairo_ptr, color_led);
for (int i = 0; i < 12; ++i) {

// ... and larer ticks every 3 hours.
double ratio = 0.9;
if (i % 3 == 0) {
Expand Down
1 change: 1 addition & 0 deletions etc/style-debian.plist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
};
Tile = {
Size = 128;
ContentSize = 96;
BezelWidth = 4;
Fill = {
From = "argb32:ffa6a6b6";
Expand Down
1 change: 1 addition & 0 deletions etc/style-default.plist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
};
Tile = {
Size = 64;
ContentSize = 48;
BezelWidth = 2;
Fill = {
From = "argb32:ffa6a6b6";
Expand Down
6 changes: 5 additions & 1 deletion src/clip.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ wlmaker_clip_t *wlmaker_clip_create(
wlmaker_clip_destroy(clip_ptr);
return NULL;
}
clip_ptr->image_ptr = wlmtk_image_create(path_ptr, server_ptr->env_ptr);
clip_ptr->image_ptr = wlmtk_image_create_scaled(
path_ptr,
clip_ptr->super_tile.style.content_size,
clip_ptr->super_tile.style.content_size,
server_ptr->env_ptr);
if (NULL == clip_ptr->image_ptr) {
wlmaker_clip_destroy(clip_ptr);
return NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ static const wlmcfg_desc_t _wlmaker_config_style_color_gradient_desc[] = {
static const wlmcfg_desc_t _wlmaker_config_tile_style_desc[] = {
WLMCFG_DESC_UINT64(
"Size", true, wlmtk_tile_style_t, size, 64),
WLMCFG_DESC_UINT64(
"ContentSize", true, wlmtk_tile_style_t, content_size, 48),
WLMCFG_DESC_UINT64(
"BezelWidth", true, wlmtk_tile_style_t, bezel_width, 2),
WLMCFG_DESC_CUSTOM(
Expand Down
3 changes: 2 additions & 1 deletion src/icon_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ void handle_surface_commit(
toplevel_icon_ptr->icon_manager_ptr->wl_display_ptr);
zwlmaker_toplevel_icon_v1_send_configure(
toplevel_icon_ptr->wl_resource_ptr,
64, 64,
toplevel_icon_ptr->super_tile.style.size,
toplevel_icon_ptr->super_tile.style.size,
toplevel_icon_ptr->pending_serial);
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/launcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ wlmaker_launcher_t *wlmaker_launcher_create_from_plist(
wlmaker_launcher_destroy(launcher_ptr);
return NULL;
}
launcher_ptr->image_ptr = wlmtk_image_create(path_ptr, env_ptr);
launcher_ptr->image_ptr = wlmtk_image_create_scaled(
path_ptr,
launcher_ptr->super_tile.style.content_size,
launcher_ptr->super_tile.style.content_size,
env_ptr);
if (NULL == launcher_ptr->image_ptr) {
wlmaker_launcher_destroy(launcher_ptr);
return NULL;
Expand Down
40 changes: 35 additions & 5 deletions src/toolkit/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ struct _wlmtk_image_t {
};

struct wlr_buffer *_wlmtk_image_create_wlr_buffer_from_image(
const char *path_ptr);
const char *path_ptr,
int width,
int height);

static void _wlmtk_image_element_destroy(wlmtk_element_t *element_ptr);

Expand All @@ -35,6 +37,16 @@ static const wlmtk_element_vmt_t _wlmtk_image_element_vmt = {
wlmtk_image_t *wlmtk_image_create(
const char *image_path_ptr,
wlmtk_env_t *env_ptr)
{
return wlmtk_image_create_scaled(image_path_ptr, 0, 0, env_ptr);
}

/* ------------------------------------------------------------------------- */
wlmtk_image_t *wlmtk_image_create_scaled(
const char *image_path_ptr,
int width,
int height,
wlmtk_env_t *env_ptr)
{
wlmtk_image_t *image_ptr = logged_calloc(1, sizeof(wlmtk_image_t));
if (NULL == image_ptr) return NULL;
Expand All @@ -48,7 +60,8 @@ wlmtk_image_t *wlmtk_image_create(
&_wlmtk_image_element_vmt);

struct wlr_buffer *wlr_buffer_ptr =
_wlmtk_image_create_wlr_buffer_from_image(image_path_ptr);
_wlmtk_image_create_wlr_buffer_from_image(
image_path_ptr, width, height);
if (NULL == wlr_buffer_ptr) {
wlmtk_image_destroy(image_ptr);
return NULL;
Expand Down Expand Up @@ -80,11 +93,17 @@ wlmtk_element_t *wlmtk_image_element(wlmtk_image_t *image_ptr)
* size.
*
* @param path_ptr
* @param width Desired width of the image. 0 0r negative to use
* the image's native width.
* @param height Desired height of the image. 0 0r negative to use
* the image's native height.
*
* @return the wlr_buffer or NULL on error.
*/
struct wlr_buffer *_wlmtk_image_create_wlr_buffer_from_image(
const char *path_ptr)
const char *path_ptr,
int width,
int height)
{
cairo_surface_t *icon_surface_ptr = cairo_image_surface_create_from_png(
path_ptr);
Expand All @@ -102,8 +121,14 @@ struct wlr_buffer *_wlmtk_image_create_wlr_buffer_from_image(
return NULL;
}

int w = cairo_image_surface_get_width(icon_surface_ptr);
int h = cairo_image_surface_get_height(icon_surface_ptr);
int w = width;
if (0 >= w) {
w = cairo_image_surface_get_width(icon_surface_ptr);
}
int h = height;
if (0 >= h) {
h = cairo_image_surface_get_height(icon_surface_ptr);
}

struct wlr_buffer *wlr_buffer_ptr = bs_gfxbuf_create_wlr_buffer(w, h);
if (NULL == wlr_buffer_ptr) {
Expand All @@ -117,6 +142,11 @@ struct wlr_buffer *_wlmtk_image_create_wlr_buffer_from_image(
return NULL;
}

cairo_surface_set_device_scale(
icon_surface_ptr,
(double)cairo_image_surface_get_width(icon_surface_ptr) / w,
(double)cairo_image_surface_get_height(icon_surface_ptr) / h);

cairo_set_source_surface(cairo_ptr, icon_surface_ptr, 0, 0);
cairo_rectangle(cairo_ptr, 0, 0, w, h);
cairo_fill(cairo_ptr);
Expand Down
16 changes: 16 additions & 0 deletions src/toolkit/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ wlmtk_image_t *wlmtk_image_create(
const char *image_path_ptr,
wlmtk_env_t *env_ptr);

/**
* Creates a toolkit image, scaled while preserving aspect ratio.
*
* @param image_path_ptr
* @param width
* @param height
* @param env_ptr
*
* @return Pointer to the toolkit image or NULL on error.
*/
wlmtk_image_t *wlmtk_image_create_scaled(
const char *image_path_ptr,
int width,
int height,
wlmtk_env_t *env_ptr);

/**
* Destroys the toolkit image.
*
Expand Down
2 changes: 2 additions & 0 deletions src/toolkit/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ typedef struct {
wlmtk_style_fill_t fill;
/** Size of the tile, in pixels. Tiles are of quadratic shape. */
uint64_t size;
/** Content size of what's in the tile, in pixels. */
uint64_t content_size;
/** Width of the bezel. */
uint64_t bezel_width;
} wlmtk_tile_style_t;
Expand Down

0 comments on commit 1bfd8c3

Please sign in to comment.