feat(notify): add notify support

In this commit I added a new feature, a flag for notifications -n /
--notify. It's using the notify-send command line utility to send
desktop notifications. By default I made it compatible with dunst (you
will need to enable full markup in the dunst config) but it should work
with other notification daemons too.
This commit is contained in:
r3b00thx 2025-06-04 20:53:57 +03:00
parent 500c46185d
commit 5da4693a66
No known key found for this signature in database
GPG key ID: D972C6E6008EA013
6 changed files with 63 additions and 1 deletions

View file

@ -5,6 +5,7 @@
#include "helpers/Monitor.hpp"
#include "helpers/Color.hpp"
#include "clipboard/Clipboard.hpp"
#include "notify/Notify.hpp"
// git stuff
#ifndef GIT_COMMIT_HASH

View file

@ -1,5 +1,8 @@
#include "hyprpicker.hpp"
#include "src/notify/Notify.hpp"
#include <csignal>
#include <cstdio>
#include <format>
void sigHandler(int sig) {
g_pHyprpicker->m_vLayerSurfaces.clear();
@ -666,6 +669,13 @@ void CHyprpicker::initMouse() {
if (m_bAutoCopy)
Clipboard::copy("%g%% %g%% %g%% %g%%", c, m, y, k);
if (m_bNotify) {
char buf[64];
snprintf(buf, sizeof(buf), "cmyk(%f%%, %f%%, %f%%, %f%%)", c, m, y, k);
Notify::send(buf);
}
finish();
break;
}
@ -693,6 +703,13 @@ void CHyprpicker::initMouse() {
if (m_bAutoCopy)
Clipboard::copy("#%s%s%s", toHex(COL.r).c_str(), toHex(COL.g).c_str(), toHex(COL.b).c_str());
if (m_bNotify) {
char buf[64];
snprintf(buf, sizeof(buf), "#%s%s%s", toHex(COL.r).c_str(), toHex(COL.g).c_str(), toHex(COL.b).c_str());
Notify::send(buf);
}
finish();
break;
}
@ -704,6 +721,13 @@ void CHyprpicker::initMouse() {
if (m_bAutoCopy)
Clipboard::copy("%i %i %i", COL.r, COL.g, COL.b);
if (m_bNotify) {
char buf[64];
snprintf(buf, sizeof(buf), "rgb(%i, %i, %i)", COL.r, COL.g, COL.b);
Notify::send(buf);
}
finish();
break;
}
@ -721,6 +745,13 @@ void CHyprpicker::initMouse() {
if (m_bAutoCopy)
Clipboard::copy("%g %g%% %g%%", h, s, l_or_v);
if (m_bNotify) {
char buf[64];
snprintf(buf, sizeof(buf), "hsl(%f, %f%%, %f%%)", h, s, l_or_v);
Notify::send(buf);
}
finish();
break;
}

View file

@ -41,6 +41,7 @@ class CHyprpicker {
bool m_bFancyOutput = true;
bool m_bAutoCopy = false;
bool m_bNotify = false;
bool m_bRenderInactive = false;
bool m_bNoZoom = false;
bool m_bNoFractional = false;

View file

@ -8,6 +8,7 @@ static void help() {
std::cout << "Hyprpicker usage: hyprpicker [arg [...]].\n\nArguments:\n"
<< " -a | --autocopy | Automatically copies the output to the clipboard (requires wl-clipboard)\n"
<< " -f | --format=fmt | Specifies the output format (cmyk, hex, rgb, hsl, hsv)\n"
<< " -j | --notify | Sends a desktop notification when a color is picked (requires notify-send and a notification daemon like dunst)\n"
<< " -n | --no-fancy | Disables the \"fancy\" (aka. colored) outputting\n"
<< " -h | --help | Show this help message\n"
<< " -r | --render-inactive | Render (freeze) inactive displays\n"
@ -29,6 +30,7 @@ int main(int argc, char** argv, char** envp) {
{"format", required_argument, nullptr, 'f'},
{"help", no_argument, nullptr, 'h'},
{"no-fancy", no_argument, nullptr, 'n'},
{"notify", no_argument, nullptr, 'j'},
{"render-inactive", no_argument, nullptr, 'r'},
{"no-zoom", no_argument, nullptr, 'z'},
{"no-fractional", no_argument, nullptr, 't'},
@ -39,7 +41,7 @@ int main(int argc, char** argv, char** envp) {
{"version", no_argument, nullptr, 'V'},
{nullptr, 0, nullptr, 0}};
int c = getopt_long(argc, argv, ":f:hnarzqvtdlV", long_options, &option_index);
int c = getopt_long(argc, argv, ":f:hnjarzqvtdlV", long_options, &option_index);
if (c == -1)
break;
@ -62,6 +64,7 @@ int main(int argc, char** argv, char** envp) {
break;
case 'h': help(); exit(0);
case 'n': g_pHyprpicker->m_bFancyOutput = false; break;
case 'j': g_pHyprpicker->m_bNotify = true; break;
case 'a': g_pHyprpicker->m_bAutoCopy = true; break;
case 'r': g_pHyprpicker->m_bRenderInactive = true; break;
case 'z': g_pHyprpicker->m_bNoZoom = true; break;

18
src/notify/Notify.cpp Normal file
View file

@ -0,0 +1,18 @@
#include "Notify.hpp"
#include "../includes.hpp"
#include <cstdint>
#include <cstdio>
#include <iostream>
void Notify::send(const char* color) {
char bodyBuf[NOTIFYBODYSIZE];
char colorBuf[64];
snprintf(bodyBuf, sizeof(bodyBuf), "<span>You selected the color: <span color='%s'><b>%s</b></span></span>", color, color);
if (fork() == 0)
execlp("notify-send", "notify-send", "-t", "5000", "-i", "color-select-symbolic", "Color Picker", bodyBuf, NULL);
std::cout << colorBuf;
}

8
src/notify/Notify.hpp Normal file
View file

@ -0,0 +1,8 @@
#pragma once
#include <cstdint>
#define NOTIFYBODYSIZE 128
namespace Notify {
void send(const char* color);
}