Skip to content

Commit

Permalink
feature|fix: Add enable option for DND indicator & fix for Big Sur
Browse files Browse the repository at this point in the history
As reported in #41 - this is now fixed
  • Loading branch information
cmacrae committed Apr 8, 2021
1 parent 147672c commit 43e526e
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
**Changed**
- Fixed a bug introduced in Big Sur where buffer reads were incorrect
- Improved efficiency of bar's initialisation
- Fixed DoNotDisturb indicator on Big Sur

**Added**
- New `left|center|right` shell sections: display custom text based on shell pipelines
- 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
- Option to turn the DoNotDisturb indicator on or off
- Option to set the padding between the first/last item and the left/right edge of the display
- Default to "Solid" style of Font Awesome 5 Free icon font
- Option to display spaces for all displays, including: optional separator, secondary & tertiary space indicator colours in relation to display
Expand Down
5 changes: 5 additions & 0 deletions doc/spacebar.1
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ Color to use for drawing the clock icon.
Specify a format for the current time, according to the strftime function.
.RE
.sp
\fBdnd\fP [\fI<BOOL_SEL>\fP]
.RS 4
Enable a DoNotDisturb indicator on the right of the bar.
.RE
.sp
\fBdnd_icon\fP [\fI<sym>\fP]
.RS 4
Specify a symbol to represent the current DoNotDisturb status.
Expand Down
3 changes: 3 additions & 0 deletions doc/spacebar.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ Settings
*clock_format* ['<sym>']::
Specify a format for the current time, according to the strftime function.

*dnd* ['<BOOL_SEL>']::
Enable a DoNotDisturb indicator on the right of the bar.

*dnd_icon* ['<sym>']::
Specify a symbol to represent the current DoNotDisturb status.

Expand Down
21 changes: 11 additions & 10 deletions src/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,16 +385,17 @@ void bar_refresh(struct bar *bar)
}
}

NSUserDefaults* defaults = [[NSUserDefaults alloc]initWithSuiteName:@"com.apple.notificationcenterui"];
bool dnd = [defaults boolForKey:@"doNotDisturb"];
if (dnd) {
struct bar_line dnd_icon = g_bar_manager.dnd_icon;
dnd_icon.color = g_bar_manager.dnd_icon_color;
CGPoint di_pos = bar_align_line(bar, dnd_icon, 0, ALIGN_CENTER);
di_pos.x = bar_right_first_item_x - dnd_icon.bounds.size.width;
bar_right_first_item_x = di_pos.x - g_bar_manager.spacing_right;

bar_draw_line(bar, dnd_icon, di_pos.x, di_pos.y);
if (g_bar_manager.dnd) {
bool dnd = getDoNotDisturb();
if (dnd) {
struct bar_line dnd_icon = g_bar_manager.dnd_icon;
dnd_icon.color = g_bar_manager.dnd_icon_color;
CGPoint di_pos = bar_align_line(bar, dnd_icon, 0, ALIGN_CENTER);
di_pos.x = bar_right_first_item_x - dnd_icon.bounds.size.width;
bar_right_first_item_x = di_pos.x - g_bar_manager.spacing_right;

bar_draw_line(bar, dnd_icon, di_pos.x, di_pos.y);
}
}

if(g_bar_manager.right_shell_on) {
Expand Down
19 changes: 13 additions & 6 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,37 +310,43 @@ void bar_manager_set_position(struct bar_manager *bar_manager, char *pos)
void bar_manager_set_title(struct bar_manager *bar_manager, bool value)
{
bar_manager->title = value;
bar_manager_resize(bar_manager);
bar_manager_refresh(bar_manager);
}

void bar_manager_set_dnd(struct bar_manager *bar_manager, bool value)
{
bar_manager->dnd = value;
bar_manager_refresh(bar_manager);
}

void bar_manager_set_spaces(struct bar_manager *bar_manager, bool value)
{
bar_manager->spaces = value;
bar_manager_resize(bar_manager);
bar_manager_refresh(bar_manager);
}

void bar_manager_set_spaces_for_all_displays(struct bar_manager *bar_manager, bool value)
{
bar_manager->spaces_for_all_displays = value;
bar_manager_resize(bar_manager);
bar_manager_refresh(bar_manager);
}

void bar_manager_set_display_separator(struct bar_manager *bar_manager, bool value)
{
bar_manager->display_separator = value;
bar_manager_resize(bar_manager);
bar_manager_refresh(bar_manager);
}

void bar_manager_set_clock(struct bar_manager *bar_manager, bool value)
{
bar_manager->clock = value;
bar_manager_resize(bar_manager);
bar_manager_refresh(bar_manager);
}

void bar_manager_set_power(struct bar_manager *bar_manager, bool value)
{
bar_manager->power = value;
bar_manager_resize(bar_manager);
bar_manager_refresh(bar_manager);
}

void bar_manager_set_height(struct bar_manager *bar_manager, uint32_t height)
Expand Down Expand Up @@ -533,6 +539,7 @@ void bar_manager_init(struct bar_manager *bar_manager)
bar_manager->spaces = true;
bar_manager->clock = true;
bar_manager->power = true;
bar_manager->dnd = true;
bar_manager->padding_left = 20;
bar_manager->padding_right = 20;
bar_manager->spacing_left = 25;
Expand Down
2 changes: 2 additions & 0 deletions src/bar_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct bar_manager
bool display_separator;
bool clock;
bool power;
bool dnd;
bool left_shell_on;
bool right_shell_on;
bool center_shell_on;
Expand Down Expand Up @@ -86,6 +87,7 @@ void bar_manager_set_power_strip(struct bar_manager *bar_manager, char **icon_st
void bar_manager_set_clock_icon(struct bar_manager *bar_manager, char *icon);
void bar_manager_set_clock_format(struct bar_manager *bar_manager, char *format);
void bar_manager_set_space_icon(struct bar_manager *bar_manager, char *icon);
void bar_manager_set_dnd(struct bar_manager *bar_manager, bool value);
void bar_manager_set_dnd_icon(struct bar_manager *bar_manager, char *icon);
void bar_manager_set_left_shell_icon(struct bar_manager *bar_manager, char *icon);
void bar_manager_set_display_separator(struct bar_manager *bar_manager, bool value);
Expand Down
2 changes: 2 additions & 0 deletions src/manifest.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Cocoa/Cocoa.h>
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/ps/IOPSKeys.h>
#include <Foundation/Foundation.h>

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -33,6 +34,7 @@
#undef HASHTABLE_IMPLEMENTATION
#include "misc/socket.h"
#include "misc/socket.c"
#include "misc/dnd.c"

//#include "osax/sa.h"
//#include "osax/sa_loader.c"
Expand Down
12 changes: 12 additions & 0 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern bool g_verbose;
#define COMMAND_CONFIG_BAR_BATTERY_ICON_COLOR "battery_icon_color"
#define COMMAND_CONFIG_BAR_POWER_ICON_COLOR "power_icon_color"
#define COMMAND_CONFIG_BAR_CLOCK_ICON_COLOR "clock_icon_color"
#define COMMAND_CONFIG_BAR_DND "dnd"
#define COMMAND_CONFIG_BAR_DND_ICON_COLOR "dnd_icon_color"
#define COMMAND_CONFIG_BAR_BACKGROUND "background_color"
#define COMMAND_CONFIG_BAR_FOREGROUND "foreground_color"
Expand Down Expand Up @@ -566,6 +567,17 @@ static void handle_domain_config(FILE *rsp, struct token domain, char *message)
} else {
daemon_fail(rsp, "value for '%.*s' must be either 'main' or 'all'.\n", command.length, command.text);
}
} else if (token_equals(command, COMMAND_CONFIG_BAR_DND)) {
struct token value = get_token(&message);
if (!token_is_valid(value)) {
fprintf(rsp, "%s\n", bool_str[g_bar_manager.dnd]);
} else if (token_equals(value, ARGUMENT_COMMON_VAL_OFF)) {
bar_manager_set_dnd(&g_bar_manager, false);
} else if (token_equals(value, ARGUMENT_COMMON_VAL_ON)) {
bar_manager_set_dnd(&g_bar_manager, true);
} else {
daemon_fail(rsp, "unknown value '%.*s' given to command '%.*s' for domain '%.*s'\n", value.length, value.text, command.length, command.text, domain.length, domain.text);
}
}
else {
daemon_fail(rsp, "unknown command '%.*s' for domain '%.*s'\n", command.length, command.text, domain.length, domain.text);
Expand Down
61 changes: 61 additions & 0 deletions src/misc/dnd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
static bool getDoNotDisturb() {
bool doNotDisturb;
NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
bool isBigSur = version.majorVersion == 11 || (version.majorVersion == 10 && version.minorVersion > 15);
if (isBigSur) {
// On big sur we have to read a plist from a plist...
NSData* dndData = [[[NSUserDefaults alloc] initWithSuiteName:@"com.apple.ncprefs"] dataForKey:@"dnd_prefs"];
// If there is no DND data let's assume that we aren't in DND
if (!dndData) return false;

NSDictionary* dndDict = [NSPropertyListSerialization
propertyListWithData:dndData
options:NSPropertyListImmutable
format:nil
error:nil];
// If the dnd data isn't a valid plist, again assume we aren't in DND
if (!dndDict) return false;

NSDictionary* userPrefs = [dndDict valueForKey:@"userPref"];
if (userPrefs) {
NSNumber* dndEnabled = [userPrefs valueForKey:@"enabled"];
// If the user pref has it set to enabled
if ([dndEnabled intValue] == 1) return true;
}

NSDictionary* scheduledPrefs = [dndDict valueForKey:@"scheduledTime"];
if (scheduledPrefs) {
NSNumber* scheduleEnabled = [scheduledPrefs valueForKey:@"enabled"];
NSNumber* start = [scheduledPrefs valueForKey:@"start"];
NSNumber* end = [scheduledPrefs valueForKey:@"end"];
// If the schedule is enabled, we need to manually determine if we fall in the start / end interval
if ([scheduleEnabled intValue] == 1 && start && end) {
NSDate* now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger current = (hour * 60) + minute;

NSInteger startInt = [start intValue];
NSInteger endInt = [end intValue];
// Normal way round, start is before the end
if (startInt < endInt) {
// Start is inclusive, end is exclusive
if (current >= startInt && current < endInt) return true;
} else if (endInt < startInt) {
// The end can also be _after_ the start making the DND interval loop over midnight
if (current >= startInt) return true;
if (current < endInt) return true;
}
}
}

// Not manually enabled, not enabled due to schedule
return false;
} else {
// Older than big sur we can just read the pref directly
doNotDisturb = [[[[NSUserDefaults alloc] initWithSuiteName:@"com.apple.notificationcenterui"] objectForKey:@"doNotDisturb"] boolValue];
}
return doNotDisturb;
}

0 comments on commit 43e526e

Please sign in to comment.