WindowServer: Make corner-radius a variable in config file!

This commit is contained in:
Bananymous 2024-09-12 20:07:30 +03:00
parent 6cd246a38e
commit c706829a91
4 changed files with 46 additions and 35 deletions

Binary file not shown.

View File

@ -13,8 +13,9 @@
#include <unistd.h> #include <unistd.h>
WindowServer::WindowServer(Framebuffer& framebuffer) WindowServer::WindowServer(Framebuffer& framebuffer, int32_t corner_radius)
: m_framebuffer(framebuffer) : m_framebuffer(framebuffer)
, m_corner_radius(corner_radius)
, m_cursor({ framebuffer.width / 2, framebuffer.height / 2 }) , m_cursor({ framebuffer.width / 2, framebuffer.height / 2 })
, m_font(MUST(LibFont::Font::load("/usr/share/fonts/lat0-16.psfu"_sv))) , m_font(MUST(LibFont::Font::load("/usr/share/fonts/lat0-16.psfu"_sv)))
{ {
@ -338,45 +339,43 @@ void WindowServer::invalidate(Rectangle area)
{ {
auto& window = *pwindow; auto& window = *pwindow;
const int32_t corner_radius = 5;
const Rectangle fast_areas[] { const Rectangle fast_areas[] {
{ {
window.full_x() + corner_radius, window.full_x() + m_corner_radius,
window.full_y(), window.full_y(),
window.full_width() - 2 * corner_radius, window.full_width() - 2 * m_corner_radius,
corner_radius m_corner_radius
}, },
{ {
window.full_x(), window.full_x(),
window.full_y() + corner_radius, window.full_y() + m_corner_radius,
window.full_width(), window.full_width(),
window.full_height() - 2 * corner_radius window.full_height() - 2 * m_corner_radius
}, },
{ {
window.full_x() + corner_radius, window.full_x() + m_corner_radius,
window.full_y() + window.full_height() - corner_radius, window.full_y() + window.full_height() - m_corner_radius,
window.full_width() - 2 * corner_radius, window.full_width() - 2 * m_corner_radius,
corner_radius m_corner_radius
} }
}; };
const Position corner_centers[] { const Position corner_centers[] {
{ {
window.full_x() + corner_radius, window.full_x() + m_corner_radius,
window.full_y() + corner_radius, window.full_y() + m_corner_radius,
}, },
{ {
window.full_x() + (window.full_width() - 1) - corner_radius, window.full_x() + (window.full_width() - 1) - m_corner_radius,
window.full_y() + corner_radius, window.full_y() + m_corner_radius,
}, },
{ {
window.full_x() + corner_radius, window.full_x() + m_corner_radius,
window.full_y() + (window.full_height() - 1) - corner_radius, window.full_y() + (window.full_height() - 1) - m_corner_radius,
}, },
{ {
window.full_x() + (window.full_width() - 1) - corner_radius, window.full_x() + (window.full_width() - 1) - m_corner_radius,
window.full_y() + (window.full_height() - 1) - corner_radius, window.full_y() + (window.full_height() - 1) - m_corner_radius,
}, },
}; };
@ -384,26 +383,26 @@ void WindowServer::invalidate(Rectangle area)
{ {
window.full_x(), window.full_x(),
window.full_y(), window.full_y(),
corner_radius, m_corner_radius,
corner_radius m_corner_radius
}, },
{ {
window.full_x() + window.full_width() - corner_radius, window.full_x() + window.full_width() - m_corner_radius,
window.full_y(), window.full_y(),
corner_radius, m_corner_radius,
corner_radius m_corner_radius
}, },
{ {
window.full_x(), window.full_x(),
window.full_y() + window.full_height() - corner_radius, window.full_y() + window.full_height() - m_corner_radius,
corner_radius, m_corner_radius,
corner_radius m_corner_radius
}, },
{ {
window.full_x() + window.full_width() - corner_radius, window.full_x() + window.full_width() - m_corner_radius,
window.full_y() + window.full_height() - corner_radius, window.full_y() + window.full_height() - m_corner_radius,
corner_radius, m_corner_radius,
corner_radius m_corner_radius
} }
}; };
@ -418,7 +417,7 @@ void WindowServer::invalidate(Rectangle area)
const int32_t dy = pos.y - corner_centers[i].y; const int32_t dy = pos.y - corner_centers[i].y;
if (2 * (dy > 0) + (dx > 0) != i) if (2 * (dy > 0) + (dx > 0) != i)
continue; continue;
if (dx * dx + dy * dy >= corner_radius * corner_radius) if (dx * dx + dy * dy >= m_corner_radius * m_corner_radius)
return true; return true;
} }
return false; return false;

View File

@ -19,7 +19,7 @@
class WindowServer class WindowServer
{ {
public: public:
WindowServer(Framebuffer& framebuffer); WindowServer(Framebuffer& framebuffer, int32_t corner_radius);
BAN::ErrorOr<void> set_background_image(BAN::UniqPtr<LibImage::Image>); BAN::ErrorOr<void> set_background_image(BAN::UniqPtr<LibImage::Image>);
@ -48,6 +48,8 @@ private:
BAN::Vector<BAN::RefPtr<Window>> m_client_windows; BAN::Vector<BAN::RefPtr<Window>> m_client_windows;
BAN::Vector<int> m_client_fds; BAN::Vector<int> m_client_fds;
const int32_t m_corner_radius;
BAN::Vector<uint8_t> m_pages_to_sync_bitmap; BAN::Vector<uint8_t> m_pages_to_sync_bitmap;
BAN::UniqPtr<LibImage::Image> m_background_image; BAN::UniqPtr<LibImage::Image> m_background_image;

View File

@ -19,6 +19,7 @@
struct Config struct Config
{ {
BAN::UniqPtr<LibImage::Image> background_image; BAN::UniqPtr<LibImage::Image> background_image;
int32_t corner_radius = 0;
}; };
BAN::Optional<BAN::String> file_read_line(FILE* file) BAN::Optional<BAN::String> file_read_line(FILE* file)
@ -88,6 +89,15 @@ Config parse_config()
else else
config.background_image = image.release_value(); config.background_image = image.release_value();
} }
else if (variable == "corner-radius"_sv)
{
char* endptr = nullptr;
long corner_radius = strtol(value.data(), &endptr, 0);
if (corner_radius < 0 || corner_radius == LONG_MAX || corner_radius >= INT32_MAX)
dwarnln("invalid corner-radius: '{}'", value);
else
config.corner_radius = corner_radius;
}
else else
{ {
dwarnln("Unknown config variable: {}", variable); dwarnln("Unknown config variable: {}", variable);
@ -176,7 +186,7 @@ int main()
auto config = parse_config(); auto config = parse_config();
WindowServer window_server(framebuffer); WindowServer window_server(framebuffer, config.corner_radius);
if (config.background_image) if (config.background_image)
if (auto ret = window_server.set_background_image(BAN::move(config.background_image)); ret.is_error()) if (auto ret = window_server.set_background_image(BAN::move(config.background_image)); ret.is_error())
dwarnln("Could not set background image: {}", ret.error()); dwarnln("Could not set background image: {}", ret.error());