feat(bar): 将 tray 作为一个 item 集成到 bar 中
This commit is contained in:
@@ -36,6 +36,7 @@ add_executable(${APP_NAME}
|
||||
${SOURCE_DIR}/bar/binding.c
|
||||
${SOURCE_DIR}/bar/cell.c
|
||||
${SOURCE_DIR}/bar/text.c
|
||||
${SOURCE_DIR}/bar/tray.c
|
||||
${SOURCE_DIR}/bar/windows.c
|
||||
${SOURCE_DIR}/bar/workspaces.c
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "bar/binding.h"
|
||||
#include "bar/cell.h"
|
||||
#include "bar/text.h"
|
||||
#include "bar/tray.h"
|
||||
#include "bar/types.h"
|
||||
#include "bar/windows.h"
|
||||
#include "bar/workspaces.h"
|
||||
@@ -130,9 +131,14 @@ static void bar_output_add_windows(
|
||||
bar_windows_add_listeners(listeners, item->state);
|
||||
}
|
||||
|
||||
static inline void bar_output_add_tray(bar_output_t *bar_output, tray_t *tray) {
|
||||
bar_add_item(bar_output->output_id, &bar_output->right, bar_tray, tray);
|
||||
}
|
||||
|
||||
void bar_init(bar_t *bar, listeners_t *listeners) {
|
||||
auto c = &bar->config;
|
||||
bar->ctx = text_context_create(c->font_family, c->font_size, c->dpi);
|
||||
auto tray = &bar->tray;
|
||||
|
||||
for (size_t i = 0; i < bar->count; ++i) {
|
||||
auto bar_output = &bar->bars[i];
|
||||
@@ -141,6 +147,10 @@ void bar_init(bar_t *bar, listeners_t *listeners) {
|
||||
bar_output_add_workspace(bar_output, &bar->config, listeners);
|
||||
bar_output_add_binding(bar_output, &bar->config, listeners);
|
||||
bar_output_add_windows(bar_output, &bar->config, listeners);
|
||||
|
||||
if (tray->api.host_window(tray->handle) == bar_output->window_id) {
|
||||
bar_output_add_tray(bar_output, &bar->tray);
|
||||
}
|
||||
}
|
||||
|
||||
bar->timerfd = time_create_monotonic_timerfd_by_fps(bar->config.fps);
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "bar/types.h"
|
||||
#include "base/color.h"
|
||||
#include "common/listeners.h"
|
||||
#include "interface/tray.h"
|
||||
|
||||
typedef struct bar_output_t {
|
||||
cairo_t *cr;
|
||||
@@ -37,6 +38,7 @@ typedef struct bar_t {
|
||||
zdwm_bar_config_t config;
|
||||
bar_palette_t palette;
|
||||
text_context_t *ctx;
|
||||
tray_t tray;
|
||||
} bar_t;
|
||||
|
||||
void bar_init(bar_t *bar, listeners_t *listeners);
|
||||
|
||||
78
src/bar/tray.c
Normal file
78
src/bar/tray.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "bar/tray.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <zdwm/bar.h>
|
||||
#include <zdwm/types.h>
|
||||
|
||||
#include "base/memory.h"
|
||||
#include "interface/tray.h"
|
||||
|
||||
typedef struct bar_tray_state_t {
|
||||
tray_t tray;
|
||||
bool dirty;
|
||||
} bar_tray_state_t;
|
||||
|
||||
static void bar_tray_icons_changed(void *user_data) {
|
||||
bar_tray_state_t *state = user_data;
|
||||
state->dirty = true;
|
||||
}
|
||||
|
||||
static void *bar_tray_create_state(zdwm_output_id_t output_id, void *config) {
|
||||
tray_t *tray = config;
|
||||
assert(tray != nullptr);
|
||||
|
||||
auto state = p_new(bar_tray_state_t, 1);
|
||||
state->tray = *tray;
|
||||
tray->api.set_listener(tray->handle, bar_tray_icons_changed, state);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
static void bar_tray_update(
|
||||
zdwm_bar_item_t *item,
|
||||
const zdwm_bar_cell_api_t *cell_api,
|
||||
void *state
|
||||
) {
|
||||
bar_tray_state_t *data = state;
|
||||
if (!data->dirty) return;
|
||||
|
||||
auto tray_handle = data->tray.handle;
|
||||
auto tray_api = &data->tray.api;
|
||||
auto icon_count = tray_api->icon_count(tray_handle);
|
||||
auto cell_count = cell_api->get_cell_count(item);
|
||||
if (cell_count == icon_count) {
|
||||
data->dirty = false;
|
||||
return;
|
||||
}
|
||||
|
||||
cell_api->set_cell_count(item, icon_count);
|
||||
auto cell_width = tray_api->icon_size(tray_handle);
|
||||
for (size_t i = 0; i < icon_count; ++i) {
|
||||
cell_api->cell_set_fixed_width(item, i, cell_width);
|
||||
}
|
||||
|
||||
data->dirty = false;
|
||||
}
|
||||
|
||||
static void bar_tray_after_layout(const zdwm_bar_item_hook_params_t *params) {
|
||||
bar_tray_state_t *state = params->state;
|
||||
auto tray_handle = state->tray.handle;
|
||||
auto tray_api = &state->tray.api;
|
||||
tray_api->place(tray_handle, params->region.start);
|
||||
}
|
||||
|
||||
static void bar_tray_destroy_state(void *state) {
|
||||
if (!state) return;
|
||||
|
||||
bar_tray_state_t *data = state;
|
||||
p_delete(&data);
|
||||
}
|
||||
|
||||
zdwm_bar_item_type_t bar_tray = {
|
||||
.create_state = bar_tray_create_state,
|
||||
.update = bar_tray_update,
|
||||
.after_layout = bar_tray_after_layout,
|
||||
.destroy_state = bar_tray_destroy_state,
|
||||
.update_interval_ms = 0,
|
||||
};
|
||||
5
src/bar/tray.h
Normal file
5
src/bar/tray.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <zdwm/bar.h>
|
||||
|
||||
extern zdwm_bar_item_type_t bar_tray;
|
||||
@@ -122,6 +122,7 @@ static void runtime_init_bar(runtime_t *runtime) {
|
||||
auto color = bar->palette.bg.argb;
|
||||
auto bar_window =
|
||||
backend_create_bar_window(backend, bar_rect, color, false);
|
||||
bar->tray = bar_window.tray;
|
||||
auto bar_output = &bar->bars[i];
|
||||
bar_output->cr = bar_window.cr;
|
||||
bar_output->window_id = bar_window.window_id;
|
||||
|
||||
Reference in New Issue
Block a user