From b0158f4246125cd44d2eb212c1c1d29fdcc2b54c Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Thu, 18 Mar 2021 15:45:24 +0200 Subject: Draw soft popup menu border shadows --- src/ui/paint.c | 27 +++++++++++++++++++++++++++ src/ui/paint.h | 1 + src/ui/util.c | 6 +++++- src/ui/widget.c | 8 +------- src/ui/window.c | 22 ++++++++++++++-------- src/ui/window.h | 1 + 6 files changed, 49 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/ui/paint.c b/src/ui/paint.c index 1ac12a66..36e3f703 100644 --- a/src/ui/paint.c +++ b/src/ui/paint.c @@ -108,6 +108,33 @@ void fillRect_Paint(const iPaint *d, iRect rect, int color) { SDL_RenderFillRect(renderer_Paint_(d), (SDL_Rect *) &rect); } +void drawSoftShadow_Paint(const iPaint *d, iRect inner, int thickness, int color, int alpha) { + SDL_Renderer *render = renderer_Paint_(d); + SDL_Texture *shadow = get_Window()->borderShadow; + const iInt2 size = size_SDLTexture(shadow); + const iRect outer = expanded_Rect(inner, init1_I2(thickness)); + const iColor clr = get_Color(color); + SDL_SetTextureColorMod(shadow, clr.r, clr.g, clr.b); + SDL_SetTextureAlphaMod(shadow, alpha); + /* Classic stretched segmented border. */ + SDL_RenderCopy(render, shadow, &(SDL_Rect){ 0, 0, size.x / 2, size.y / 2}, + &(SDL_Rect){ outer.pos.x, outer.pos.y, thickness, thickness }); + SDL_RenderCopy(render, shadow, &(SDL_Rect){ size.x / 2, 0, 1, size.y / 2}, + &(SDL_Rect){ inner.pos.x, outer.pos.y, inner.size.x, thickness }); + SDL_RenderCopy(render, shadow, &(SDL_Rect){ size.x / 2, 0, size.x / 2, size.y / 2}, + &(SDL_Rect){ right_Rect(outer) - thickness, outer.pos.y, thickness, thickness }); + SDL_RenderCopy(render, shadow, &(SDL_Rect){ size.x / 2, size.y / 2, size.x / 2, 1 }, + &(SDL_Rect){ right_Rect(inner), inner.pos.y, thickness, inner.size.y }); + SDL_RenderCopy(render, shadow, &(SDL_Rect){ size.x / 2, size.y / 2, size.x / 2, size.y / 2}, + &(SDL_Rect){ right_Rect(inner), bottom_Rect(inner), thickness, thickness }); + SDL_RenderCopy(render, shadow, &(SDL_Rect){ size.x / 2, size.y / 2, 1, size.y / 2}, + &(SDL_Rect){ inner.pos.x, bottom_Rect(inner), inner.size.x, thickness }); + SDL_RenderCopy(render, shadow, &(SDL_Rect){ 0, size.y / 2, size.x / 2, size.y / 2}, + &(SDL_Rect){ outer.pos.x, bottom_Rect(inner), thickness, thickness }); + SDL_RenderCopy(render, shadow, &(SDL_Rect){ 0, size.y / 2, size.x / 2, 1 }, + &(SDL_Rect){ outer.pos.x, inner.pos.y, thickness, inner.size.y }); +} + void drawLines_Paint(const iPaint *d, const iInt2 *points, size_t count, int color) { setColor_Paint_(d, color); SDL_RenderDrawLines(renderer_Paint_(d), (const SDL_Point *) points, count); diff --git a/src/ui/paint.h b/src/ui/paint.h index 89a01da6..90cc2aef 100644 --- a/src/ui/paint.h +++ b/src/ui/paint.h @@ -47,6 +47,7 @@ void unsetClip_Paint (iPaint *); void drawRect_Paint (const iPaint *, iRect rect, int color); void drawRectThickness_Paint (const iPaint *, iRect rect, int thickness, int color); void fillRect_Paint (const iPaint *, iRect rect, int color); +void drawSoftShadow_Paint (const iPaint *, iRect rect, int thickness, int color, int alpha); void drawLines_Paint (const iPaint *, const iInt2 *points, size_t count, int color); diff --git a/src/ui/util.c b/src/ui/util.c index 13d1bf78..8fbe5d41 100644 --- a/src/ui/util.c +++ b/src/ui/util.c @@ -395,6 +395,7 @@ static iBool isCommandIgnoredByMenus_(const char *cmd) { startsWith_CStr(cmd, "feeds.update.") || equal_Command(cmd, "bookmarks.request.started") || equal_Command(cmd, "bookmarks.request.finished") || + equal_Command(cmd, "bookmarks.changed") || equal_Command(cmd, "document.autoreload") || equal_Command(cmd, "document.reload") || equal_Command(cmd, "document.request.started") || @@ -459,10 +460,13 @@ static iWidget *makeMenuSeparator_(void) { iWidget *makeMenu_Widget(iWidget *parent, const iMenuItem *items, size_t n) { iWidget *menu = new_Widget(); - setBackgroundColor_Widget(menu, uiBackground_ColorId); + setBackgroundColor_Widget(menu, uiBackgroundMenu_ColorId); if (deviceType_App() != desktop_AppDeviceType) { setPadding1_Widget(menu, 2 * gap_UI); } + else { + setPadding1_Widget(menu, gap_UI / 2); + } const iBool isPortraitPhone = (deviceType_App() == phone_AppDeviceType && isPortrait_App()); int64_t itemFlags = (deviceType_App() != desktop_AppDeviceType ? 0 : 0) | (isPortraitPhone ? extraPadding_WidgetFlag : 0); diff --git a/src/ui/widget.c b/src/ui/widget.c index fa0a20c9..81853c2a 100644 --- a/src/ui/widget.c +++ b/src/ui/widget.c @@ -858,13 +858,7 @@ void drawBackground_Widget(const iWidget *d) { if (shadowBorder) { iPaint p; init_Paint(&p); - const iBool isLight = isLight_ColorTheme(colorTheme_App()); - p.alpha = isLight ? 0xc : 0x20; - SDL_SetRenderDrawBlendMode(renderer_Window(get_Window()), SDL_BLENDMODE_BLEND); - iRect shadowRect = expanded_Rect(bounds_Widget(d), mulf_I2(gap2_UI, 8)); -// shadowRect.pos.y += gap_UI * 4; - fillRect_Paint(&p, shadowRect, /*isLight ? white_ColorId :*/ black_ColorId); - SDL_SetRenderDrawBlendMode(renderer_Window(get_Window()), SDL_BLENDMODE_NONE); + drawSoftShadow_Paint(&p, bounds_Widget(d), 12 * gap_UI, black_ColorId, 30); } if (fadeBackground) { iPaint p; diff --git a/src/ui/window.c b/src/ui/window.c index e7945c62..d67d3f5b 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -1328,13 +1328,12 @@ iBool create_Window_(iWindow *d, iRect rect, uint32_t flags) { } #if defined (iPlatformLinux) || defined (LAGRANGE_CUSTOM_FRAME) -static SDL_Surface *loadAppIconSurface_(int resized) { - const iBlock *icon = &imageLagrange64_Embedded; - int w, h, num; - stbi_uc * pixels = stbi_load_from_memory( - constData_Block(icon), size_Block(icon), &w, &h, &num, STBI_rgb_alpha); +static SDL_Surface *loadImage_(const iBlock *data, int resized) { + int w = 0, h = 0, num = 4; + stbi_uc *pixels = stbi_load_from_memory( + constData_Block(data), size_Block(data), &w, &h, &num, STBI_rgb_alpha); if (resized) { - stbi_uc * rsPixels = malloc(num * resized * resized); + stbi_uc *rsPixels = malloc(num * resized * resized); stbir_resize_uint8(pixels, w, h, 0, rsPixels, resized, resized, 0, num); free(pixels); pixels = rsPixels; @@ -1414,7 +1413,7 @@ void init_Window(iWindow *d, iRect rect) { #if defined (iPlatformLinux) SDL_SetWindowMinimumSize(d->win, minSize.x * d->pixelRatio, minSize.y * d->pixelRatio); /* Load the window icon. */ { - SDL_Surface *surf = loadAppIconSurface_(0); + SDL_Surface *surf = loadImage_(&imageLagrange64_Embedded, 0); SDL_SetWindowIcon(d->win, surf); free(surf->pixels); SDL_FreeSurface(surf); @@ -1433,11 +1432,18 @@ void init_Window(iWindow *d, iRect rect) { setupUserInterface_Window(d); postCommand_App("bindings.changed"); /* update from bindings */ updateRootSize_Window_(d, iFalse); + /* Load the border shadow texture. */ { + SDL_Surface *surf = loadImage_(&imageShadow_Embedded, 0); + d->borderShadow = SDL_CreateTextureFromSurface(d->render, surf); + SDL_SetTextureBlendMode(d->borderShadow, SDL_BLENDMODE_BLEND); + free(surf->pixels); + SDL_FreeSurface(surf); + } d->appIcon = NULL; #if defined (LAGRANGE_CUSTOM_FRAME) /* Load the app icon for drawing in the title bar. */ if (prefs_App()->customFrame) { - SDL_Surface *surf = loadAppIconSurface_(appIconSize_()); + SDL_Surface *surf = loadImage_(&imageLagrange64_Embedded, appIconSize_()); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); d->appIcon = SDL_CreateTextureFromSurface(d->render, surf); free(surf->pixels); diff --git a/src/ui/window.h b/src/ui/window.h index 9ac3bc00..cd2e3814 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -72,6 +72,7 @@ struct Impl_Window { uint32_t frameTime; double presentTime; SDL_Texture * appIcon; + SDL_Texture * borderShadow; SDL_Cursor * cursors[SDL_NUM_SYSTEM_CURSORS]; SDL_Cursor * pendingCursor; int loadAnimTimer; -- cgit v1.2.3