core: add a hexcode preview below the cursor (#95)

This commit is contained in:
Shreya Gurram 2024-11-28 21:39:39 +05:30 committed by GitHub
parent 4e8837ddab
commit d26cb2f439
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 71 additions and 19 deletions

View file

@ -388,6 +388,7 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) {
// | | // | |
// | --------- | // | --------- |
// //
// (hex code here)
cairo_restore(PCAIRO); cairo_restore(PCAIRO);
if (!m_bNoZoom) { if (!m_bNoZoom) {
@ -424,10 +425,58 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) {
cairo_clip(PCAIRO); cairo_clip(PCAIRO);
cairo_paint(PCAIRO); cairo_paint(PCAIRO);
cairo_surface_flush(PBUFFER->surface); if (!m_bDisableHexPreview) {
const auto currentColor = getColorFromPixel(pSurface, CLICKPOS);
std::string hexBuffer = std::format("#{:02X}{:02X}{:02X}", currentColor.r, currentColor.g, currentColor.b);
cairo_set_source_rgba(PCAIRO, 0.0, 0.0, 0.0, 0.5);
double x, y, width = 85, height = 28, radius = 6;
if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50) && CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
x = CLICKPOS.x - 80;
y = CLICKPOS.y - 40;
} else if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50)) {
x = CLICKPOS.x;
y = CLICKPOS.y - 40;
} else if (CLICKPOS.x > (PBUFFER->pixelSize.x - 100)) {
x = CLICKPOS.x - 80;
y = CLICKPOS.y + 20;
} else {
x = CLICKPOS.x;
y = CLICKPOS.y + 20;
}
cairo_move_to(PCAIRO, x + radius, y);
cairo_arc(PCAIRO, x + width - radius, y + radius, radius, -M_PI_2, 0);
cairo_arc(PCAIRO, x + width - radius, y + height - radius, radius, 0, M_PI_2);
cairo_arc(PCAIRO, x + radius, y + height - radius, radius, M_PI_2, M_PI);
cairo_arc(PCAIRO, x + radius, y + radius, radius, M_PI, -M_PI_2);
cairo_close_path(PCAIRO);
cairo_fill(PCAIRO);
cairo_set_source_rgba(PCAIRO, 1.0, 1.0, 1.0, 1.0);
cairo_select_font_face(PCAIRO, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(PCAIRO, 18);
double padding = 5.0;
double textX = x + padding;
if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50) && CLICKPOS.x > (PBUFFER->pixelSize.x - 100))
cairo_move_to(PCAIRO, textX, CLICKPOS.y - 20);
else if (CLICKPOS.y > (PBUFFER->pixelSize.y - 50))
cairo_move_to(PCAIRO, textX, CLICKPOS.y - 20);
else if (CLICKPOS.x > (PBUFFER->pixelSize.x - 100))
cairo_move_to(PCAIRO, textX, CLICKPOS.y + 40);
else
cairo_move_to(PCAIRO, textX, CLICKPOS.y + 40);
cairo_show_text(PCAIRO, hexBuffer.c_str());
cairo_surface_flush(PBUFFER->surface);
}
cairo_restore(PCAIRO); cairo_restore(PCAIRO);
cairo_pattern_destroy(PATTERN); cairo_pattern_destroy(PATTERN);
} }
} else if (!m_bRenderInactive) { } else if (!m_bRenderInactive) {
@ -447,7 +496,6 @@ void CHyprpicker::renderSurface(CLayerSurface* pSurface, bool forceInactive) {
cairo_paint(PCAIRO); cairo_paint(PCAIRO);
cairo_surface_flush(PBUFFER->surface); cairo_surface_flush(PBUFFER->surface);
cairo_pattern_destroy(PATTERNPRE); cairo_pattern_destroy(PATTERNPRE);
} }

View file

@ -40,10 +40,11 @@ class CHyprpicker {
bool m_bFancyOutput = true; bool m_bFancyOutput = true;
bool m_bAutoCopy = false; bool m_bAutoCopy = false;
bool m_bRenderInactive = false; bool m_bRenderInactive = false;
bool m_bNoZoom = false; bool m_bNoZoom = false;
bool m_bNoFractional = false; bool m_bNoFractional = false;
bool m_bDisableHexPreview = false;
bool m_bRunning = true; bool m_bRunning = true;

View file

@ -6,16 +6,17 @@
static void help(void) { static void help(void) {
std::cout << "Hyprpicker usage: hyprpicker [arg [...]].\n\nArguments:\n" std::cout << "Hyprpicker usage: hyprpicker [arg [...]].\n\nArguments:\n"
<< " -a | --autocopy | Automatically copies the output to the clipboard (requires wl-clipboard)\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" << " -f | --format=fmt | Specifies the output format (cmyk, hex, rgb, hsl, hsv)\n"
<< " -n | --no-fancy | Disables the \"fancy\" (aka. colored) outputting\n" << " -n | --no-fancy | Disables the \"fancy\" (aka. colored) outputting\n"
<< " -h | --help | Show this help message\n" << " -h | --help | Show this help message\n"
<< " -r | --render-inactive | Render (freeze) inactive displays\n" << " -r | --render-inactive | Render (freeze) inactive displays\n"
<< " -z | --no-zoom | Disable the zoom lens\n" << " -z | --no-zoom | Disable the zoom lens\n"
<< " -q | --quiet | Disable most logs (leaves errors)\n" << " -q | --quiet | Disable most logs (leaves errors)\n"
<< " -v | --verbose | Enable more logs\n" << " -v | --verbose | Enable more logs\n"
<< " -t | --no-fractional | Disable fractional scaling support\n" << " -t | --no-fractional | Disable fractional scaling support\n"
<< " -V | --version | Print version info\n"; << " -d | --disable-hex-preview | Disable live preview of Hex code\n"
<< " -V | --version | Print version info\n";
} }
int main(int argc, char** argv, char** envp) { int main(int argc, char** argv, char** envp) {
@ -32,10 +33,11 @@ int main(int argc, char** argv, char** envp) {
{"no-fractional", no_argument, NULL, 't'}, {"no-fractional", no_argument, NULL, 't'},
{"quiet", no_argument, NULL, 'q'}, {"quiet", no_argument, NULL, 'q'},
{"verbose", no_argument, NULL, 'v'}, {"verbose", no_argument, NULL, 'v'},
{"disable-hex-preview", no_argument, NULL, 'd'},
{"version", no_argument, NULL, 'V'}, {"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}}; {NULL, 0, NULL, 0}};
int c = getopt_long(argc, argv, ":f:hnarzqvtV", long_options, &option_index); int c = getopt_long(argc, argv, ":f:hnarzqvtdV", long_options, &option_index);
if (c == -1) if (c == -1)
break; break;
@ -64,6 +66,7 @@ int main(int argc, char** argv, char** envp) {
case 't': g_pHyprpicker->m_bNoFractional = true; break; case 't': g_pHyprpicker->m_bNoFractional = true; break;
case 'q': Debug::quiet = true; break; case 'q': Debug::quiet = true; break;
case 'v': Debug::verbose = true; break; case 'v': Debug::verbose = true; break;
case 'd': g_pHyprpicker->m_bDisableHexPreview = true; break;
case 'V': { case 'V': {
std::cout << "hyprpicker v" << HYPRPICKER_VERSION << "\n"; std::cout << "hyprpicker v" << HYPRPICKER_VERSION << "\n";
exit(0); exit(0);