feature: Introduce on|off switches for spaces/clock/power

This commit is contained in:
Calum MacRae 2021-03-28 00:13:15 +00:00
parent db5f34c6de
commit 10995851cd
No known key found for this signature in database
GPG key ID: 53922D7E54A14F5D
9 changed files with 166 additions and 59 deletions

View file

@ -12,6 +12,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
**Added**
- Option to turn focused window title display on or off (thanks [@Norviah](https://github.com/Norviah)!)
- Option to turn the spaces indicator on or off
- Option to turn the clock on or off
- Option to turn the power indicator on or off
## [1.2.1](https://github.com/cmacrae/spacebar/releases/tag/v1.2.1) - 2020-11-18

View file

@ -72,6 +72,10 @@ Here's a configuration taken from [the examples directory](examples):
spacebar -m config position top
spacebar -m config height 26
spacebar -m config title on
spacebar -m config spaces on
spacebar -m config clock on
spacebar -m config power on
spacebar -m config spacing_left 25
spacebar -m config spacing_right 15
spacebar -m config text_font "Helvetica Neue:Bold:12.0"
@ -105,6 +109,10 @@ If you're using the `services.spacebar` module from [nix-darwin](https://github.
services.spacebar.config = {
position = "top";
height = 26;
title = "on";
spaces = "on";
clock = "on";
power = "on";
spacing_left = 25;
spacing_right = 15;
text_font = ''"Helvetica Neue:Bold:12.0"'';

View file

@ -2,12 +2,12 @@
.\" Title: spacebar
.\" Author: [see the "AUTHOR(S)" section]
.\" Generator: Asciidoctor 2.0.10
.\" Date: 2021-03-27
.\" Date: 2021-03-28
.\" Manual: spacebar manual
.\" Source: spacebar
.\" Language: English
.\"
.TH "SPACEBAR" "1" "2021-03-27" "spacebar" "spacebar manual"
.TH "SPACEBAR" "1" "2021-03-28" "spacebar" "spacebar manual"
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.ss \n[.ss] 0
@ -87,6 +87,21 @@ Height in pixels to draw the bar.
Enable focused window title display at the centre of the bar.
.RE
.sp
\fBspaces\fP [\fI<BOOL_SEL>\fP]
.RS 4
Enable space indicator on the left of the bar.
.RE
.sp
\fBclock\fP [\fI<BOOL_SEL>\fP]
.RS 4
Enable clock on the right of the bar.
.RE
.sp
\fBpower\fP [\fI<BOOL_SEL>\fP]
.RS 4
Enable power indicator on the right of the bar.
.RE
.sp
\fBspacing_left\fP [\fI<NUM>\fP]
.RS 4
Spacing in pixels between the left space indicators.

View file

@ -70,6 +70,15 @@ Settings
*title* ['<BOOL_SEL>']::
Enable focused window title display at the centre of the bar.
*spaces* ['<BOOL_SEL>']::
Enable space indicator on the left of the bar.
*clock* ['<BOOL_SEL>']::
Enable clock on the right of the bar.
*power* ['<BOOL_SEL>']::
Enable power indicator on the right of the bar.
*spacing_left* ['<NUM>']::
Spacing in pixels between the left space indicators.

View file

@ -3,6 +3,9 @@
spacebar -m config position top
spacebar -m config height 26
spacebar -m config title on
spacebar -m config spaces on
spacebar -m config clock on
spacebar -m config power on
spacebar -m config spacing_left 25
spacebar -m config spacing_right 15
spacebar -m config text_font "Helvetica Neue:Bold:12.0"

126
src/bar.c
View file

@ -225,34 +225,36 @@ void bar_refresh(struct bar *bar)
//
int bar_left_final_item_x = 10;
int space_count;
uint64_t *space_list = display_space_list(bar->did, &space_count);
if (space_list) {
uint64_t sid = display_space_id(bar->did);
if (g_bar_manager.spaces) {
int space_count;
uint64_t *space_list = display_space_list(bar->did, &space_count);
if (space_list) {
uint64_t sid = display_space_id(bar->did);
for (int i = 0; i < space_count; ++i) {
CGPoint pos = CGContextGetTextPosition(bar->context);
for (int i = 0; i < space_count; ++i) {
CGPoint pos = CGContextGetTextPosition(bar->context);
int index = mission_control_index(space_list[i]) - 1;
int index = mission_control_index(space_list[i]) - 1;
struct bar_line space_line = index >= buf_len(g_bar_manager.space_icon_strip)
? g_bar_manager.space_icon
: g_bar_manager.space_icon_strip[index];
if (i == 0) {
pos = bar_align_line(bar, space_line, ALIGN_LEFT, ALIGN_CENTER);
} else {
pos.x += g_bar_manager.spacing_left;
}
struct bar_line space_line = index >= buf_len(g_bar_manager.space_icon_strip)
? g_bar_manager.space_icon
: g_bar_manager.space_icon_strip[index];
if (i == 0) {
pos = bar_align_line(bar, space_line, ALIGN_LEFT, ALIGN_CENTER);
} else {
pos.x += g_bar_manager.spacing_left;
}
if (sid == space_list[i]) {
space_line.color = g_bar_manager.space_icon_color;
}
bar_draw_line(bar, space_line, pos.x, pos.y);
if (sid == space_list[i]) {
space_line.color = g_bar_manager.space_icon_color;
}
bar_draw_line(bar, space_line, pos.x, pos.y);
bar_left_final_item_x = pos.x + space_line.bounds.size.width + 10;
}
bar_left_final_item_x = pos.x + space_line.bounds.size.width + 10;
}
free(space_list);
free(space_list);
}
}
//
@ -262,49 +264,59 @@ void bar_refresh(struct bar *bar)
// This is used to calculate overlap for the cetner bar.
// It is updated to represent the X position of the first item, depending on what's displayed.
int bar_right_first_item_x = bar->frame.size.width - 10;
time_t rawtime;
time(&rawtime);
int time_line_pos;
struct tm *timeinfo = localtime(&rawtime);
if (timeinfo) {
char time[255];
strftime(time, sizeof(time), g_bar_manager._clock_format, timeinfo);
struct bar_line time_line = bar_prepare_line(g_bar_manager.t_font, time, g_bar_manager.foreground_color);
CGPoint t_pos = bar_align_line(bar, time_line, ALIGN_RIGHT, ALIGN_CENTER);
bar_draw_line(bar, time_line, t_pos.x, t_pos.y);
if (g_bar_manager.clock) {
time_t rawtime;
time(&rawtime);
struct tm *timeinfo = localtime(&rawtime);
if (timeinfo) {
char time[255];
strftime(time, sizeof(time), g_bar_manager._clock_format, timeinfo);
struct bar_line time_line = bar_prepare_line(g_bar_manager.t_font, time, g_bar_manager.foreground_color);
CGPoint t_pos = bar_align_line(bar, time_line, ALIGN_RIGHT, ALIGN_CENTER);
bar_draw_line(bar, time_line, t_pos.x, t_pos.y);
struct bar_line time_icon = g_bar_manager.clock_icon;
time_icon.color = g_bar_manager.clock_icon_color;
CGPoint ti_pos = bar_align_line(bar, g_bar_manager.clock_icon, 0, ALIGN_CENTER);
ti_pos.x = t_pos.x - g_bar_manager.clock_icon.bounds.size.width - 5;
time_line_pos = ti_pos.x;
bar_right_first_item_x = ti_pos.x;
struct bar_line time_icon = g_bar_manager.clock_icon;
time_icon.color = g_bar_manager.clock_icon_color;
CGPoint ti_pos = bar_align_line(bar, g_bar_manager.clock_icon, 0, ALIGN_CENTER);
ti_pos.x = t_pos.x - g_bar_manager.clock_icon.bounds.size.width - 5;
time_line_pos = ti_pos.x;
bar_right_first_item_x = ti_pos.x;
bar_draw_line(bar, time_icon, ti_pos.x, ti_pos.y);
bar_destroy_line(time_line);
bar_draw_line(bar, time_icon, ti_pos.x, ti_pos.y);
bar_destroy_line(time_line);
}
}
bool has_batt = false;
bool charging = false;
int percent = bar_find_battery_life(&has_batt, &charging);
int batt_line_pos;
if (has_batt) {
char batt[255];
snprintf(batt, sizeof(batt), "%' '3d%%", percent);
struct bar_line batt_line = bar_prepare_line(g_bar_manager.t_font, batt, g_bar_manager.foreground_color);
CGPoint p_pos = bar_align_line(bar, batt_line, ALIGN_RIGHT, ALIGN_CENTER);
p_pos.x = time_line_pos - (batt_line.bounds.size.width + g_bar_manager.spacing_right);
bar_draw_line(bar, batt_line, p_pos.x, p_pos.y);
if (g_bar_manager.power) {
bool has_batt = false;
bool charging = false;
int percent = bar_find_battery_life(&has_batt, &charging);
if (has_batt) {
char batt[255];
snprintf(batt, sizeof(batt), "%' '3d%%", percent);
struct bar_line batt_line = bar_prepare_line(g_bar_manager.t_font, batt, g_bar_manager.foreground_color);
CGPoint p_pos = bar_align_line(bar, batt_line, ALIGN_RIGHT, ALIGN_CENTER);
if (g_bar_manager.clock) {
p_pos.x = time_line_pos - (batt_line.bounds.size.width + g_bar_manager.spacing_right);
}
bar_draw_line(bar, batt_line, p_pos.x, p_pos.y);
struct bar_line batt_icon = charging ? g_bar_manager.power_icon : g_bar_manager.battr_icon;
batt_icon.color = charging ? g_bar_manager.power_icon_color : g_bar_manager.battery_icon_color;
CGPoint pi_pos = bar_align_line(bar, batt_icon, 0, ALIGN_CENTER);
pi_pos.x = p_pos.x - batt_icon.bounds.size.width;
batt_line_pos = pi_pos.x;
bar_right_first_item_x = pi_pos.x;
struct bar_line batt_icon = charging ? g_bar_manager.power_icon : g_bar_manager.battr_icon;
batt_icon.color = charging ? g_bar_manager.power_icon_color : g_bar_manager.battery_icon_color;
CGPoint pi_pos = bar_align_line(bar, batt_icon, 0, ALIGN_CENTER);
if (g_bar_manager.clock) {
pi_pos.x = p_pos.x - batt_icon.bounds.size.width;
} else {
pi_pos.x = p_pos.x - g_bar_manager.power_icon.bounds.size.width - 5;
}
batt_line_pos = pi_pos.x;
bar_right_first_item_x = pi_pos.x;
bar_draw_line(bar, batt_icon, pi_pos.x, pi_pos.y);
bar_destroy_line(batt_line);
bar_draw_line(bar, batt_icon, pi_pos.x, pi_pos.y);
bar_destroy_line(batt_line);
}
}
NSUserDefaults* defaults = [[NSUserDefaults alloc]initWithSuiteName:@"com.apple.notificationcenterui"];

View file

@ -219,6 +219,21 @@ void bar_manager_set_title(struct bar_manager *bar_manager, bool value)
bar_manager->title = value;
}
void bar_manager_set_spaces(struct bar_manager *bar_manager, bool value)
{
bar_manager->spaces = value;
}
void bar_manager_set_clock(struct bar_manager *bar_manager, bool value)
{
bar_manager->clock = value;
}
void bar_manager_set_power(struct bar_manager *bar_manager, bool value)
{
bar_manager->power = value;
}
void bar_manager_set_height(struct bar_manager *bar_manager, uint32_t height)
{
bar_manager->height = height;
@ -264,6 +279,9 @@ void bar_manager_init(struct bar_manager *bar_manager)
bar_manager_set_position(bar_manager, string_copy("top"));
bar_manager_set_height(bar_manager, 26);
bar_manager_set_title(bar_manager, true);
bar_manager_set_spaces(bar_manager, true);
bar_manager_set_clock(bar_manager, true);
bar_manager_set_power(bar_manager, true);
bar_manager_set_spacing_left(bar_manager, 25);
bar_manager_set_spacing_right(bar_manager, 15);
bar_manager_set_text_font(bar_manager, string_copy("Helvetica Neue:Regular:10.0"));

View file

@ -34,6 +34,9 @@ struct bar_manager
struct bar_line power_icon;
struct bar_line dnd_icon;
bool title;
bool spaces;
bool clock;
bool power;
};
void bar_manager_set_foreground_color(struct bar_manager *bar_manager, uint32_t color);
@ -53,6 +56,9 @@ void bar_manager_set_space_icon(struct bar_manager *bar_manager, char *icon);
void bar_manager_set_dnd_icon(struct bar_manager *bar_manager, char *icon);
void bar_manager_set_position(struct bar_manager *bar_manager, char *pos);
void bar_manager_set_title(struct bar_manager *bar_manager, bool value);
void bar_manager_set_spaces(struct bar_manager *bar_manager, bool value);
void bar_manager_set_clock(struct bar_manager *bar_manager, bool value);
void bar_manager_set_power(struct bar_manager *bar_manager, bool value);
void bar_manager_set_height(struct bar_manager *bar_manager, uint32_t height);
void bar_manager_set_spacing_left(struct bar_manager *bar_manager, uint32_t spacing);
void bar_manager_set_spacing_right(struct bar_manager *bar_manager, uint32_t spacing);

View file

@ -32,6 +32,9 @@ extern bool g_verbose;
#define COMMAND_CONFIG_BAR_SPACING_LEFT "spacing_left"
#define COMMAND_CONFIG_BAR_SPACING_RIGHT "spacing_right"
#define COMMAND_CONFIG_BAR_TITLE "title"
#define COMMAND_CONFIG_BAR_SPACES "spaces"
#define COMMAND_CONFIG_BAR_CLOCK "clock"
#define COMMAND_CONFIG_BAR_POWER "power"
/* --------------------------------COMMON ARGUMENTS----------------------------- */
#define ARGUMENT_COMMON_VAL_ON "on"
@ -324,6 +327,36 @@ static void handle_domain_config(FILE *rsp, struct token domain, char *message)
} else {
daemon_fail(rsp, "value for '%.*s' must be either 'on' or 'off'.\n", command.length, command.text);
}
} else if (token_equals(command, COMMAND_CONFIG_BAR_SPACES)) {
char * on = "on";
char * off = "off";
if (strcmp(message,on) == 0) {
bar_manager_set_spaces(&g_bar_manager, true);
} else if (strcmp(message,off) == 0) {
bar_manager_set_spaces(&g_bar_manager, false);
} else {
daemon_fail(rsp, "value for '%.*s' must be either 'on' or 'off'.\n", command.length, command.text);
}
} else if (token_equals(command, COMMAND_CONFIG_BAR_CLOCK)) {
char * on = "on";
char * off = "off";
if (strcmp(message,on) == 0) {
bar_manager_set_clock(&g_bar_manager, true);
} else if (strcmp(message,off) == 0) {
bar_manager_set_clock(&g_bar_manager, false);
} else {
daemon_fail(rsp, "value for '%.*s' must be either 'on' or 'off'.\n", command.length, command.text);
}
} else if (token_equals(command, COMMAND_CONFIG_BAR_POWER)) {
char * on = "on";
char * off = "off";
if (strcmp(message,on) == 0) {
bar_manager_set_power(&g_bar_manager, true);
} else if (strcmp(message,off) == 0) {
bar_manager_set_power(&g_bar_manager, false);
} else {
daemon_fail(rsp, "value for '%.*s' must be either 'on' or 'off'.\n", command.length, command.text);
}
} else {
daemon_fail(rsp, "unknown command '%.*s' for domain '%.*s'\n", command.length, command.text, domain.length, domain.text);
}