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** **Added**
- Option to turn focused window title display on or off (thanks [@Norviah](https://github.com/Norviah)!) - 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 ## [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 position top
spacebar -m config height 26 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_left 25
spacebar -m config spacing_right 15 spacebar -m config spacing_right 15
spacebar -m config text_font "Helvetica Neue:Bold:12.0" 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 = { services.spacebar.config = {
position = "top"; position = "top";
height = 26; height = 26;
title = "on";
spaces = "on";
clock = "on";
power = "on";
spacing_left = 25; spacing_left = 25;
spacing_right = 15; spacing_right = 15;
text_font = ''"Helvetica Neue:Bold:12.0"''; text_font = ''"Helvetica Neue:Bold:12.0"'';

View file

@ -2,12 +2,12 @@
.\" Title: spacebar .\" Title: spacebar
.\" Author: [see the "AUTHOR(S)" section] .\" Author: [see the "AUTHOR(S)" section]
.\" Generator: Asciidoctor 2.0.10 .\" Generator: Asciidoctor 2.0.10
.\" Date: 2021-03-27 .\" Date: 2021-03-28
.\" Manual: spacebar manual .\" Manual: spacebar manual
.\" Source: spacebar .\" Source: spacebar
.\" Language: English .\" 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 .ie \n(.g .ds Aq \(aq
.el .ds Aq ' .el .ds Aq '
.ss \n[.ss] 0 .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. Enable focused window title display at the centre of the bar.
.RE .RE
.sp .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] \fBspacing_left\fP [\fI<NUM>\fP]
.RS 4 .RS 4
Spacing in pixels between the left space indicators. Spacing in pixels between the left space indicators.

View file

@ -70,6 +70,15 @@ Settings
*title* ['<BOOL_SEL>']:: *title* ['<BOOL_SEL>']::
Enable focused window title display at the centre of the bar. 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_left* ['<NUM>']::
Spacing in pixels between the left space indicators. Spacing in pixels between the left space indicators.

View file

@ -3,6 +3,9 @@
spacebar -m config position top spacebar -m config position top
spacebar -m config height 26 spacebar -m config height 26
spacebar -m config title on 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_left 25
spacebar -m config spacing_right 15 spacebar -m config spacing_right 15
spacebar -m config text_font "Helvetica Neue:Bold:12.0" spacebar -m config text_font "Helvetica Neue:Bold:12.0"

View file

@ -225,6 +225,7 @@ void bar_refresh(struct bar *bar)
// //
int bar_left_final_item_x = 10; int bar_left_final_item_x = 10;
if (g_bar_manager.spaces) {
int space_count; int space_count;
uint64_t *space_list = display_space_list(bar->did, &space_count); uint64_t *space_list = display_space_list(bar->did, &space_count);
if (space_list) { if (space_list) {
@ -254,6 +255,7 @@ void bar_refresh(struct bar *bar)
free(space_list); free(space_list);
} }
}
// //
// BAR RIGHT // BAR RIGHT
@ -262,9 +264,10 @@ void bar_refresh(struct bar *bar)
// This is used to calculate overlap for the cetner 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. // 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; int bar_right_first_item_x = bar->frame.size.width - 10;
int time_line_pos;
if (g_bar_manager.clock) {
time_t rawtime; time_t rawtime;
time(&rawtime); time(&rawtime);
int time_line_pos;
struct tm *timeinfo = localtime(&rawtime); struct tm *timeinfo = localtime(&rawtime);
if (timeinfo) { if (timeinfo) {
char time[255]; char time[255];
@ -283,29 +286,38 @@ void bar_refresh(struct bar *bar)
bar_draw_line(bar, time_icon, ti_pos.x, ti_pos.y); bar_draw_line(bar, time_icon, ti_pos.x, ti_pos.y);
bar_destroy_line(time_line); bar_destroy_line(time_line);
} }
}
int batt_line_pos;
if (g_bar_manager.power) {
bool has_batt = false; bool has_batt = false;
bool charging = false; bool charging = false;
int percent = bar_find_battery_life(&has_batt, &charging); int percent = bar_find_battery_life(&has_batt, &charging);
int batt_line_pos;
if (has_batt) { if (has_batt) {
char batt[255]; char batt[255];
snprintf(batt, sizeof(batt), "%' '3d%%", percent); 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); 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); 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); 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); 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; 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; 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); 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; 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; batt_line_pos = pi_pos.x;
bar_right_first_item_x = pi_pos.x; bar_right_first_item_x = pi_pos.x;
bar_draw_line(bar, batt_icon, pi_pos.x, pi_pos.y); bar_draw_line(bar, batt_icon, pi_pos.x, pi_pos.y);
bar_destroy_line(batt_line); bar_destroy_line(batt_line);
} }
}
NSUserDefaults* defaults = [[NSUserDefaults alloc]initWithSuiteName:@"com.apple.notificationcenterui"]; NSUserDefaults* defaults = [[NSUserDefaults alloc]initWithSuiteName:@"com.apple.notificationcenterui"];
bool dnd = [defaults boolForKey:@"doNotDisturb"]; bool dnd = [defaults boolForKey:@"doNotDisturb"];

View file

@ -219,6 +219,21 @@ void bar_manager_set_title(struct bar_manager *bar_manager, bool value)
bar_manager->title = 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) void bar_manager_set_height(struct bar_manager *bar_manager, uint32_t height)
{ {
bar_manager->height = 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_position(bar_manager, string_copy("top"));
bar_manager_set_height(bar_manager, 26); bar_manager_set_height(bar_manager, 26);
bar_manager_set_title(bar_manager, true); 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_left(bar_manager, 25);
bar_manager_set_spacing_right(bar_manager, 15); bar_manager_set_spacing_right(bar_manager, 15);
bar_manager_set_text_font(bar_manager, string_copy("Helvetica Neue:Regular:10.0")); 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 power_icon;
struct bar_line dnd_icon; struct bar_line dnd_icon;
bool title; bool title;
bool spaces;
bool clock;
bool power;
}; };
void bar_manager_set_foreground_color(struct bar_manager *bar_manager, uint32_t color); 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_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_position(struct bar_manager *bar_manager, char *pos);
void bar_manager_set_title(struct bar_manager *bar_manager, bool value); 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_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_left(struct bar_manager *bar_manager, uint32_t spacing);
void bar_manager_set_spacing_right(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_LEFT "spacing_left"
#define COMMAND_CONFIG_BAR_SPACING_RIGHT "spacing_right" #define COMMAND_CONFIG_BAR_SPACING_RIGHT "spacing_right"
#define COMMAND_CONFIG_BAR_TITLE "title" #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----------------------------- */ /* --------------------------------COMMON ARGUMENTS----------------------------- */
#define ARGUMENT_COMMON_VAL_ON "on" #define ARGUMENT_COMMON_VAL_ON "on"
@ -324,6 +327,36 @@ static void handle_domain_config(FILE *rsp, struct token domain, char *message)
} else { } else {
daemon_fail(rsp, "value for '%.*s' must be either 'on' or 'off'.\n", command.length, command.text); 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 { } else {
daemon_fail(rsp, "unknown command '%.*s' for domain '%.*s'\n", command.length, command.text, domain.length, domain.text); daemon_fail(rsp, "unknown command '%.*s' for domain '%.*s'\n", command.length, command.text, domain.length, domain.text);
} }