Skip to content

Commit

Permalink
Merge pull request #433 from malisipi/high-contrast-management
Browse files Browse the repository at this point in the history
High Contrast Management
  • Loading branch information
hassandraga authored Jun 25, 2024
2 parents f40d35a + 900e6f8 commit 6b539c3
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
19 changes: 19 additions & 0 deletions include/webui.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,25 @@ WEBUI_EXPORT bool webui_show_wv(size_t window, const char* content);
*/
WEBUI_EXPORT void webui_set_kiosk(size_t window, bool status);

/**
* @brief Set the window high contrast support. It would be useful when default high contrast support of browser is not working fine or you want to build better high contrast theme by CSS.
*
* @param window The window number
* @param status True or False
*
* @example webui_set_high_contrast_support(myWindow, true);
*/
WEBUI_EXPORT void webui_set_high_contrast_support(size_t window, bool status);

/**
* @brief Get user high contrast preference.
*
* @return Returns True if user is using high contrast theme
*
* @example webui_user_prefers_high_contrast();
*/
bool webui_user_prefers_high_contrast();

/**
* @brief Wait until all opened windows get closed.
*
Expand Down
81 changes: 78 additions & 3 deletions src/webui.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ typedef struct _webui_window_t {
bool has_events;
char* server_root_path;
bool kiosk_mode;
bool disable_high_contrast_support;
bool hide;
int width;
int height;
Expand Down Expand Up @@ -899,6 +900,67 @@ void webui_set_kiosk(size_t window, bool status) {
win->kiosk_mode = status;
}

void webui_set_high_contrast_support(size_t window, bool status) {

#ifdef WEBUI_LOG
printf("[User] webui_set_high_contrast_support([%zu])\n", window);
#endif

// Initialization
_webui_init();

// Dereference
if (_webui_mutex_is_exit_now(WEBUI_MUTEX_NONE) || _webui_core.wins[window] == NULL)
return;
_webui_window_t * win = _webui_core.wins[window];

win->disable_high_contrast_support = !status;
}

bool webui_user_prefers_high_contrast() {

#ifdef WEBUI_LOG
printf("[User] webui_set_high_contrast_support()\n");
#endif

// Initialization
_webui_init();

bool is_enabled = false;

#ifdef _WIN32
char high_contrast_flags_as_char[WEBUI_MAX_PATH];

_webui_get_windows_reg_value(
HKEY_CURRENT_USER,
L"Control Panel\\Accessibility\\"
L"HighContrast",
L"Flags", high_contrast_flags_as_char
);

int high_contrast_flags = atoi(high_contrast_flags_as_char);

is_enabled = (high_contrast_flags & 0x01) == 1;

#elif __linux__
FILE* process_output;
char buf[100];

process_output = popen("gsettings get org.gnome.desktop.a11y.interface high-contrast", "r");
if (process_output != NULL && fgets(buf, sizeof(buf), process_output) != NULL) { // Running the command is not failed and fgets works correctly
is_enabled = strstr(buf, "true") != NULL;
}

pclose(process_output);
#endif

#ifdef WEBUI_LOG
printf("[User] webui_set_high_contrast_support() -> %d", is_enabled);
#endif

return is_enabled;
}

void webui_close(size_t window) {

#ifdef WEBUI_LOG
Expand Down Expand Up @@ -4288,7 +4350,10 @@ static bool _webui_browser_create_new_profile(_webui_window_t * win, size_t brow
_webui_free_mem((void * ) win->profile_path);
win->profile_path = (char*)_webui_malloc(WEBUI_MAX_PATH);
win->profile_name = (char*)_webui_malloc(WEBUI_MAX_PATH);
WEBUI_SCOPY_DYN(win->profile_name, WEBUI_MAX_PATH, WEBUI_PROFILE_NAME);
if(!win->disable_high_contrast_support)
WEBUI_SCOPY_DYN(win->profile_name, WEBUI_MAX_PATH, WEBUI_PROFILE_NAME);
else
WEBUI_SCOPY_DYN(win->profile_name, WEBUI_MAX_PATH, WEBUI_PROFILE_NAME "-NoHC");
}

#ifdef WEBUI_LOG
Expand Down Expand Up @@ -4363,8 +4428,12 @@ static bool _webui_browser_create_new_profile(_webui_window_t * win, size_t brow
// macOS
const char* path_ini = "~/Library/Application Support/Firefox/profiles.ini";
#endif
if (!win->custom_profile)
WEBUI_SPF_DYN(win->profile_path, WEBUI_MAX_PATH, "%s%s.WebUI%sWebUIFirefoxProfile", temp, webui_sep, webui_sep);
if (!win->custom_profile){
if(!win->disable_high_contrast_support)
WEBUI_SPF_DYN(win->profile_path, WEBUI_MAX_PATH, "%s%s.WebUI%sWebUIFirefoxProfile", temp, webui_sep, webui_sep);
else
WEBUI_SPF_DYN(win->profile_path, WEBUI_MAX_PATH, "%s%s.WebUI%sWebUIFirefoxNoHighContrastProfile", temp, webui_sep, webui_sep);
}

if (!_webui_folder_exist(win->profile_path) ||
!_webui_is_firefox_ini_profile_exist(path_ini, win->profile_name)) {
Expand Down Expand Up @@ -4415,6 +4484,9 @@ static bool _webui_browser_create_new_profile(_webui_window_t * win, size_t brow
fputs("user_pref(\"browser.shell.checkDefaultBrowser\", false); ", file);
fputs("user_pref(\"browser.tabs.warnOnClose\", false); ", file);
fputs("user_pref(\"browser.tabs.inTitlebar\", 0); ", file);
if(win->disable_high_contrast_support){
fputs("user_pref(\"browser.display.document_color_use\", 1); ", file);
}
fclose(file);

// userChrome.css
Expand Down Expand Up @@ -5497,6 +5569,9 @@ static int _webui_get_browser_args(_webui_window_t * win, size_t browser, char*
// Kiosk Mode
if (win->kiosk_mode)
c += WEBUI_SPF_DYN(buffer + c, len, " %s", "--chrome-frame --kiosk");
// High Contrast Support
if (win->disable_high_contrast_support)
c += WEBUI_SPF_DYN(buffer + c, len, " %s", "--disable-features=ForcedColors");
// Hide Mode
if (win->hide)
c += WEBUI_SPF_DYN(buffer + c, len, " %s", "--headless --headless=new");
Expand Down

0 comments on commit 6b539c3

Please sign in to comment.